OnSwipe redirect code

Tuesday, August 26, 2008

Building and embedding spiderMonkey

If you already do not know, SpiderMonkey is the JavaScript engine of the Mozilla web browser. The interesting part about this JS engine is that you can use it in your application and execute JS from your application. Well I can talk a lot about the uses and the advantages of such an embeddable JS engine but thats for another post. Here I will tell you how to build SpiderMonkey and how to embed it in your application and finally how to get your application running with the JS engine.

Actually the first two steps are very well explained in these MDC pages:

http://developer.mozilla.org/En/Building_only_SpiderMonkey
http://developer.mozilla.org/en/JavaScript_C_Engine_Embedder%27s_Guide

  • The first one tells you how to get SpiderMonkey and build it. It involves downloading the source and running "make -f Makefile.ref" and thats the end of the build story. You will have the engine binary, both in the form of a reusable library - dynamic and static and also an ready to use JS-execution shell. The reusable library is named libjs.so(dynamic) or libjs.a(static). The interactive JS-Shell will be an executable named js. More about using the Shell here.

  • The second one tells you how to write a simple application using this reusable JS engine library. It also explains you the basic types used in the engine and the general essential terminologies. There is a boilerplate program also which you can use to test your first embedding attempt.
With all this great documentation, I was stuck at the point where I have to compile my application with the JS library. After the build I do not get any directory named "sdk" or something similar. It just gives me a build directory with a static library, a dynamic library and an executable. There are also a bunch of object files (.o files) but they are not really of much use. Only two .h files are copied to the object directory and that does not involve the main "jsapi.h" file.

As a result you will end up with a hell lot of compilation errors if you just try to build your application. So there are a couple of steps, probably very evident ones, that you need to do before you build your app.

  1. Put all the .h files, header files, from the source (src) directory in the include path when building your app. The best way for this would be to create a spidermonkey folder under the includes directory or your app and providing that directory as the include path to the compiler at build time.
  2. Copy the libjs.so to the lib directory of your application and pass it as a linker option (-ljs).
  3. The various JS script types map to some system types, probably, based on the OS being used and hence they are present under some #ifdef. If you do not define any OS then you will not get defintions for several types and you end up with compilation errors. To avoid this manually define the OS before you include the first file from spiderMonkey (which is typically jsapi.h). Defining is in the usual way: #define XP_UNIX // -- For linux systems.
With this you should be able to build your application and run it. So you now have a program of yours for which input can be JavaScript and it will compile and run that JS and give you the output.

Happy embedding. :-)

No comments:

Post a Comment