Jim Lawless' Blog


Extending SpiderMonkey JavaScript on Windows

Originally published on: Thu, 29 Jul 2010

My goal for this afternoon was to be able to compile Mozilla's SpiderMonkey JavaScript engine on Windows. I had tried this a few weeks ago using an older version of Microsoft Visual C++, but I ran into some errors in the build. Once I got the build working, I wanted to try to add a Windows API function to the engine to determine the difficulty of adding further extensions.

Please note that any code snippets that I supply in this article fall under the following copyright:

Copyright 2010 by James K. Lawless. See MIT/X11 license at http://www.mailsend-online.com/wp/license.php.

I created a batch file that would invoke the compile process using the file js.mak.

compy.bat

A number of lines flew by the screen as each C source file was compiled ending in a link-time error message that began with:

This was the same error that I had seen when trying to perform the build from the IDE.

I found that these missing functions were defined in a file called jsiter.c. I could not find reference to jsiter anywhere in js.mak.

I manually compiled jsiter.c using an altered copy of the command-line used to compile most of the other C files:

Once that was done, I added jsiter.obj by changing the line:

...to...

...in two places in js.mak. I then ran the make again. No errors were reported on this build.

In the Release directory, I found an executable named jsshell.exe. When I invoked the JS shell, I was greeted with a "js>" prompt. I tried a quick, one-liner JS snippet at the REPL prompt.

It looked like all was working well.

At the prompt, I entered a call for the help() function to see what options were present in SpiderMonkey.

With the functions documented in the list above, I could use the jsshell program to write filter programs and such using the readline() function to process input and could use print() to process output. I would have to redirect console input into the jsshell process in order to process the input and would likewise have to redirect the output into a file if I wanted to capture the data.

Rhino JavaScript has some very nice interoperability with the Java environment. One has access to most all of the Java API. SpiderMonkey, however, is meant to compile and run on a number of operating-systems, so no ties are provided for the Windows API.

I then tried to add a simple WINAPI function call to the list of built-in functions. I added a function named msgbox() which accepts a single string parameter and displays the string in a popup window via the MessageBoxA API function.

Under the body of the Print() function in js.c, I added the following:

In the table of functions, I changed the line:

...to...

I recompiled with no errors using the makefile. I then tested from the REPL using the new version of jsshell.exe:

After invoking the msgbox() function, I was greeted with the following popup window:

In later experiments, I hope to add a few functions that will allow one to invoke arbitrary WINAPI functions. I'd also like to see if I can embed a script directly into the EXE so that stand-alone programs can be created.

Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Rhino JavaScript to EXE with launch4j
Next post:Auto Save Clipboard Images Redux


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

A Simple Media Control Interface Script Processor

Scott Ballantyne: Blazin' Into Forth

A GUI for urlx

COM Scripting in C by way of JavaScript

An Interview with Tom Zimmer: Forth System Developer

Choose your own Adventure with Sinatra

Structuring my Thinking

Obfuscated C

Directory Traversal in Rhino JavaScript

Obfuscated Ruby


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service