78
edits
m (→Timeline) |
|||
| Line 179: | Line 179: | ||
As you can see, it looks like C++ syntactically, but don't be fooled. You can't overload functions using IDL, and there are a few other limitations as well. But let me break it down line-by-line. | As you can see, it looks like C++ syntactically, but don't be fooled. You can't overload functions using IDL, and there are a few other limitations as well. But let me break it down line-by-line. | ||
Line 1 : we're including nsISupports, which the interface implements. All objects must implement nsISupports as far as I know. It handles reference counting, and a ton of other garbage. | Line 1 : we're including nsISupports, which the interface implements. All objects must implement nsISupports as far as I know. It handles reference counting, and a ton of other garbage. You'll notice that the '''out'''s a string rather than as a return value. This was a novice mistake. It is easier just to return a string. | ||
Line 3 : I guess scriptable means we can use it from Javascript. If you look at [http://www.xulplanet.com/ xulplanet]'s XPCOM reference, you sometimes see functions with [noscript]. A uuid is a unique identifier. On most *nix machines, you can get it from uuidgen command. | Line 3 : I guess scriptable means we can use it from a scripting language (in this case Javascript). If you look at [http://www.xulplanet.com/ xulplanet]'s XPCOM reference, you sometimes see functions with [noscript]. A uuid is a unique identifier. On most *nix machines, you can get it from uuidgen command. | ||
Line 4 : Here we name the class, and tell it what other interfaces it implements. | Line 4 : Here we name the class, and tell it what other interfaces it implements. | ||
Line 5 : My function. It takes in | Line 5 : My function. It takes in an object from Javascript (but doesn't use it. I heard you cannot use ''out'' like ''inout'') and changes it. The out keyword tells the IDL compiler to change it to a non-const reference. You can see that I didn't return a string, but rather passed in one. Its strange to explain, maybe it would be more clear as we progress. | ||
Now we need to compile using xpidl. This is found in your xulrunner/dist/bin directory. You'll also need to include the directory which contains nsISupports, that is in xulrunner/dist/sdk/include. | Now we need to compile using xpidl. This is found in your xulrunner/dist/bin directory. You'll also need to include the directory which contains nsISupports, that is in xulrunner/dist/sdk/include. | ||
| Line 214: | Line 214: | ||
8 { 0x97, 0x23, 0xb4, 0xf6, 0xb1, 0x47, 0x6a, 0xc9 } } | 8 { 0x97, 0x23, 0xb4, 0xf6, 0xb1, 0x47, 0x6a, 0xc9 } } | ||
Line 6-8 : This is the | Line 6-8 : This is the CID of our class. I don't know why I put it here. I only use it in one place. The CID is a universally unique identifier (UUID). | ||
10 class Boo : public iTest { | 10 class Boo : public iTest { | ||
| Line 234: | Line 234: | ||
20 } | 20 } | ||
Line 19 : | Line 19 : This isn't needed anymore | ||
23 NS_IMETHODIMP Boo::Hello(nsACString & world) { | 23 NS_IMETHODIMP Boo::Hello(nsACString & world) { | ||
| Line 242: | Line 242: | ||
Line 23 : The declaration uses NS_IMETHOD, and the implementation uses NS_IMETHODIMP. This is just a rule, don't ask. | Line 23 : The declaration uses NS_IMETHOD, and the implementation uses NS_IMETHODIMP. This is just a rule, don't ask. | ||
Line 24 : We append "Hello" to the parameter. I am told that we should use .Assign() rather than .Append() because .Append() relies on an existing value (remember its an out parameter). | |||
Line 25 : We need to return whether we were successful or not. | Line 25 : We need to return whether we were successful or not. | ||
| Line 268: | Line 270: | ||
Line 52 : "Boo module" is just a name as far as I can tell. I haven't seen it used. | Line 52 : "Boo module" is just a name as far as I can tell. I haven't seen it used. | ||
Line 53 : BOO_CID is the | Line 53 : BOO_CID is the contract ID. This is the only placed used as far as I know. | ||
Line 54 : "@cesar.org/boo;1" is the CID. This is what we use in Components.classes["@cesar.org/boo;1"] | Line 54 : "@cesar.org/boo;1" is the CID. This is what we use in Components.classes["@cesar.org/boo;1"] | ||
| Line 280: | Line 282: | ||
Now you need to compile this cpp file (lets call it hello.cpp) into a shared library. For windows/mac, your on your own. In linux, your in luck! | Now you need to compile this cpp file (lets call it hello.cpp) into a shared library. For windows/mac, your on your own. In linux, your in luck! | ||
g++ -fPIC -c -g hello.cpp -o hello.o -I path/to/include | g++ -fPIC -c -g hello.cpp -o hello.o -I path/to/include | ||
creates an object file in a format that can be put into a shared library. | creates an object file in a format that can be put into a shared library. | ||
g++ -shared -o libhello.so hello.o | g++ -shared -o libhello.so hello.o [ -z defs -W1 ] | ||
Actually creates the shared library (I heard the library must start with lib). | Actually creates the shared library (I heard the library must start with lib). Its also a good idea to include -z defs and -W1, since they can save you a ton of trouble if you forgot to link something. Although, when I did it, I got a ton of undefined references :) | ||
Ok, we have an xpt and a .so file. Now we just need to have XULRunner find these and it will load them up so we can start using them. I am using the trunk version of XULRunner, your results may vary. | Ok, we have an xpt and a .so file. Now we just need to have XULRunner find these and it will load them up so we can start using them. I am using the trunk version of XULRunner, your results may vary. | ||
| Line 302: | Line 304: | ||
Boo.Hello(x); | Boo.Hello(x); | ||
alert(x.value); | alert(x.value); | ||
Thanks to Christian Biesinger for his comments and input. | |||
=Feedback= | =Feedback= | ||
I appreciate any comments/suggestions/criticisms. But please post anything in the discussion page. | I appreciate any comments/suggestions/criticisms. But please post anything in the discussion page. | ||
edits