SpiderMonkey

Article:
SpiderMonkey

Updated: September 22, 2008

What's this about?

SpiderMonkey is the Mozilla implementation of JavaScript written in C. I'm rather interested in this for a number of reasons.

So I decided to pull it down from mozilla and build it. In particular, I want JavaScript to have support for the File object which provides access to the host filesystem. I found some issues and bugs in the implementation. And I wanted to be able to add new object types to SpiderMonkey in C++ (and Cocoa). These notes are mostly to remind me how I got this to work. You may find them helpful if you wish to travel this road.

Artwork credit:
http://www.fotosearch.com/PHC006/73104402

Article

1 Pull down and install the code

Pull down the SpiderMonkey source from Mozilla. You can use CVS, or I simply got the tars from:
ftp://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz and http://ftp.mozilla.org/pub/mozilla.org/nspr/releases/v4.7/src/nspr-4.7.tar.gz.
I dropped them into ~/mozilla/tars (location doesn't matter).
4 /Users/rmills/mozilla/tars> ls -lt
total 4848
-rw-r--r--  1 rmills  staff  1165607 Mar  7 10:32 js-1.7.0.tar.gz
-rw-------  1 rmills  staff  1304632 Feb  6 21:28 nspr-4.7.tar.gz
5 /Users/rmills/mozilla/tars>
Open the tars and install them into a directory. Again, I used ~/mozilla, however it's your choice. You will however have to create a symbolic link from dist -> to the nspr dist directory (I discovered with some pain - I haven't found this documented anywhere.)
6 /P/Users/rmills/mozilla> ls -lt
total 16
drwxr-xr-x  5 rmills  staff  264 Mar  7 13:28 tars
lrwxr-xr-x@ 1 rmills  staff   29 Mar  7 13:26 dist -> nspr-4.7/mozilla/nsprpub/dist
drwxr-xr-x  3 rmills  staff  264 Feb  6 13:28 nspr-4.7
drwxr-xr-x  5 rmills  staff  264 Oct 19 11:58 js

2 Build and install the nspr libraries

Build and install nspr (some support libraries needed for the File object and Thread Safety in SpiderMonkey).
7 /Users/rmills/mozilla/nspr-4.7/mozilla/nsprpub> cd /Users/rmills/mozilla/nspr-4.7/mozilla/nsprpub
8 /Users/rmills/mozilla/nspr-4.7/mozilla/nsprpub> configure
loading cache ./config.cache
checking host system type... i386-apple-darwin9.2.0
... shake rattle and roll ...
9 /Users/rmills/mozilla/nspr-4.7/mozilla/nsprpub> make
.... more rattle and roll ...
10 /Users/rmills/mozilla/nspr-4.7/mozilla/nsprpub> sudo make install
... more rolling ...

3 Build SpiderMonkey libraries and js shell

Now build the SpiderMonkey js shell (a command-line prompt for JavaScript).
11 /Users/rmills/mozilla/nspr-4.7/mozilla/nsprpub> cd ../../../js/src
12 /Users/rmills/mozilla/js/src> make -f Makefile.ref JS_HAS_FILE_OBJECT=1 JS_THREADSAFE=1
... do dah do dah ...

4 Test js shell

13 /Users/rmills/mozilla/js/src> Darwin_DBG.OBJ/js
js> dir = new File(".") ; "OK"
OK
js> for ( i in dir ) print(i) ;
length
parent
path
name
isDirectory
isFile
exists
canRead
canWrite
canAppend
canReplace
isOpen
type
mode
creationTime
lastModified
size
hasRandomAccess
hasAutoFlush
position
isNative
js> files = dir.list() ;  "OK"
OK
js> for ( i in files ) print(files[i].name)
.cvsignore
... bla bla bla ...
Y.js
js> quit() ;
14 /Users/rmills/mozilla/js/src> 
Pretty cool, right! And it has E4X (EcmaScript extensions for XML). The File object however seems to have a lot of bugs. I'll discuss them later.

Changing the installation directory to /opt/local/

I received the following very helpful email from David in Canada. He wanted to build js and install it into /opt/local/bin/js. And he wanted to store libnspr.dylib in /opt/local/lib/libnspr.dylib. Here are his patch files:
osx-configure.in.patch and osx-configure.patch.

And this is how to use them:


untar nspr-4.7.1

from untarred folder apply patches:

patch -p0 <  osx-configure.patch
patch -p0 <  osx-configure.in.patch

./configure --prefix=/opt/local --disable-debug
make
make install

Spidermonkey install to /opt/local

Download js-1.7.0.tar.gz and untar
cd to js/src

Run make

Spidermonkey's default is to look for DIST at $(DEPTH)/../../dist
(see config.mk line 43). It expects nspr distribution at this location -
we are passing it in as argument instead. We also pass LIBDIR in for linking.

make -f Makefile.ref DIST="/opt/local" LIBDIR="/opt/local/lib"
SO_SUFFIX=dylib JS_HAS_FILE_OBJECT=1 JS_THREADSAFE=1

Install files

Once compilation is complete, we have a Darwin_DBG.OBJ folder containing
binaries that we need to place in appropriate folders.

Darwin_DBG.OBJ/js -> /opt/local/bin
Darwin_DBG.OBJ/libjs.a -> /opt/local/lib
Darwin_DBG.OBJ/libjs.dylib -> /opt/local/lib

Copy or move all *.h files from Darwin_DBG.OBJ -> /opt/local/include/js
Copy or move all *.h files and all *.tbl files from js/src -> /opt/local/include/js

The complied binary needs the nspr4.dylib to run. We include it in the
DYLD_LIBRARY_PATH:

setenv DYLD_LIBRARY_PATH /opt/local/lib    (csh  syntax)
export DYLD_LIBRARY_PATH=/opt/local/lib    (bash syntax)

shell-prompt% js
js>  ... congratulations .....

Thank you, David for sharing this information

5 Still to be written

I'm going to continue this article to discuss the following additional subjects:

  1. Bugs in the File object
  2. Building and debugging with Xcode on Mac OS X
  3. Adding new Object Classes to SpiderMonkey
  4. Embedding SpiderMonkey in Applications
  5. Building a Universal version
However for the moment, I'm busy with something else and I'll return to this subject eventually.
Comments?

I'm very happy to accept comments, feedback and suggestions for any of my articles. I'm always happy to hear you - especially if you have constructive suggestions. And I'm particularily pleased if you can let me know about corrections.


Home ......... About

Page design © 1996-2008 Robin Mills / webmaster@clanmills.com

Updated Monday September 22, 2008