Saturday, September 19, 2009

The OSX experience

Dimdim was the first time when I encountered OSX. The first time I saw this machine.
Everything there was new including using it, the file system layout, conventions, file extensions etc etc. Then I came to know that it is actually a Unix based system. I actually had a terminal at my disposal where I could do all my stuff starting from cp/mv to ps/top. That was an awesome news for me. I was really spellbound the styling and effects and the systematic organization of files/apps etc on mac. I liked it.
But then the keyboard, the shortcuts everything was new to me.
Then there was XCode, the dev environment on OSX. It took some time to familiarize XCode but then I soon found that this piece of software was really good and usable.
The more I saw MAC the more I was impressed by it.
Enter Objective-C. Aaaaaaaaaaaaaa!!!!! I couldn't help but shout. Coming from a background of C/C++ starting my IIIT days, this was one language which was hell different. The most significant difference to me was the function declaration and invocation.

Declaration
C/C++ style:
char * func(int i, char * c);
Objective-C style:
-(char *) func : (int) i charlabel (char *) c;

In C, we have a type and name for each argument, but in obj-c we have a label, type and name for each argument, except for first which has no label.
function name above is "func:charlabel" :D

Invocation:
C/C++ style:
char * r = func(i,c);
Objective-C style:
char * r = [func : i charlabel : c];

It actually took me a while to digest the fact that I was reading functions in the code and I was actually invoking a funtion when I wrote the above lines :D
But then I realize that this was one language which had been developed well before C++ had evolved and it had the object-orientedness embedded in it.
You feel like OSX development is a world in itself, totally untouched and uneffected by external world!
Then started the actual coding. I realized after some time that you can invoke methods on "nil" (equivalent of our NULL) objects and not crash ur code. It just doesn't get executed! functions calls are also like messages which are delivered on a best effort basis!
below is an example:

static MyClass * c = nil;

void doOpOnMyClassVar()
{
//some code
int i = [c getVar1Val];
//some more code
}


This code was failing functionally because the "i" was not getting assigned correctly! It took me a while to trace that I forgot allocation of "c". Once I allocated, things were working awesome! But note, no crash! just ignored function call!
I had to refer to apple documentation for class references and etc. I find that though the documentation is extensive, it is not really so appealing as one finds...say java documentation or MSDN for that matter. To find the help for a funtion you may have to refer multiple places and you find many links which refer each other (page 1 says refer page 2 and page 2 says refer page 1).
After all the struggle and fight, I could finally complete the task in hand and was instantly named as the future expert of OSX code and the guy responsible for the OSX codebase :)
After this first stint, I have worked a lot on OSX and at one stage I found it strange to come back to the all familiar visual studio. I would actually instinctively use the shortcuts of OSX on windows!
I cannot help but smile when I come across these situations.
Before joining Dimdim, karthik was known as the Linux guy everywhere. But after coming here, I have worked only on Windows and OSX. Windows is OK, I worked a bit there too...but never did I dream even in my wildest dreams that I shall actually work on Objective-C and actually even respect it and get mildly addicted to it ;)

NPAPI

I always start blogging but then stop it after a while!
This time I plan to continue blogging by including a genre which I never included before, Tech.
I had to develop an NPRuntime based plugin for windows to work across all non-IE browsers. This is my first stint at such tasks and needless to say I faced a real tough time.
But then, there are always life lines lying on the internet to help the dummies like me and one such lifeline I found was The Mozilla site . Without this, I could may be never have completed this task.
NPRuntime library is provided by Gecko. Based on my requirements I had to use Gecko 1.8 version only and nothing higher than that.
The incompatibility between the samples provided at Mozilla site and the Gecko version also proved to be smaller nightmare.
But finally I overcome all that and the plugin loads. But when I invoke a string returning function, browser hangs! Works fine for int returning functions, but hangs for string returning functions! Irony, this is the plugin sample code provided by Mozilla :(
I fought for 2 days on this single issue and finally I found the reason.
You should not allocate the "string to return" using our normal strdup() or malloc(). The correct way is here:


std::stringstream sm;
sm << "foo return val";
std::string wstr = sm.str();
char * val = (char*)NPN_MemAlloc(1 + wstr.length());
strcpy(val, sm.str().c_str());
STRINGZ_TO_NPVARIANT(val, *result);


Man! I dunno the difference between our malloc() and NPN_MemAlloc() but this being the reason for failure was something I did not expect.
So, I say, my happiest techie times are when I am submerged in a pure c/c++ code.