SpiderApe is a C++ library built on top of SpiderMonkey,
the Mozilla Project's Javascript/ECMAScript engine. It allows C++ developers to embed
SpiderMonkey simply and easily into their client applications. Like
its forefather, SpiderApe falls under the Mozilla Public License
(MPL).
SpiderApe started development in August, 2005. Originally part of a
larger client program, the generic bits of embedding SpiderMonkey were
factored out into something intended to empower SpiderMonkey users a
bit more... to beef it up. Thus the name SpiderApe - the bigger,
stronger monkey.
Related Work
Flusspferd (the German word for hippopotamus) is a project
similar in SpiderApe, but newer and more actively maintained. They also provide class-binding
features and add-ons for popular third-party libs like cURL and sqlite3.
A few days ago i stumbled across the book
Programmer's Guide to nCurses
by Dan Gookin. It is an invaluable reference for anyone wanting to work with ncurses,
whether you're interested in the C API or the
ncurses plugin for SpiderApe.
30 April 2007:
SQLite3 plugin: added sqlite3_query_e4x() function, which returns
the result of a query as an E4X XML tree. This can greatly simplify the
iteration of data from an sqlite3 database.e.g.: var xml = sqlite3_query_e4x(mydb, "select * from mytable");
31 Jan 2007:
The new sqlite3 API, which mimics the native C API pretty closely,
is essentially complete, with over 70(!) functions, including the
ability to define custom SQL functions in JS code (but not yet to
define custom collating sequences in JS). The "original" sqlite3
wrapper has been removed, as it was basically an ugly hack. The newer
OO API (SQ3Connection class and friends) has been expanded to allow it
to work in conjunction with the non-OO API.
The plugins are being pulled out of the build tree. Starting
with the next release, the core tree won't come with any plugins.
The plugins now each have their own build tree, but their build
process is extremely simple because the SpiderApe-config script
(installed during 'make install') can give the plugins most
of the information they need to know for compiling and linking.
The documentation has undergone a split: the plugins docs were
moved into their own document. The plugins docs accounted for just
over half of the manual's content, so this made the manual a good deal
smaller. Part of the reason for this split was to help support the new
split of the plugins and core trees, so that those docs can be updated
individually of the main tree's docs. The plugin manual weighs in at
29 pages at the moment, and the main manual at 23 pages.
29 Jan 2007:
After taking a several-month break due to an annoying cancer, i am back to
hacking, at least part-time.
Yesterday's work includes yet another sqlite3 API, this one wrapping
the sqlite3 C API. This will be included in the next release.
19 August 2006:
Happy first birthday, SpiderApe! It was approximately one year ago that work started
on this library. Though it was intended to be a "short hack", it has far surpassed the
original plans i had for it.
The SQLite3 plugin provides a new, more powerful sqlite3 API wrapper, based off
of the C++ sqlite3 wrapper available from
http://s11n.net/download/#sqlite3x. It will be
available in today's release and is documented in the library manual.
Recent additions to the core Ape API have made it much simpler to pass exceptions from native
code back into script code, and chances are that much of the existing plugin code will be revisited
to take take advantage of this capability.
The library manual is now directly hosted on this web site.
At the moment is just over 40 pages long.
Features
Ape currently provides the following features:
A simplified OO wrapper for "high-level" embedded use: MonkeyWrapper.
Clients simply wishing to run JS code from C++, without adding their own
JS functions or classes, need to write only a few lines of code do to so.
Many JS functions (free or class member functions) can be forwarded to
native equivalents with one line of code, thanks to templates.
Ape's Class<> mechanism allows relatively easy creation of new
JS-side classes.
Several built-in JS functions which the core SM lib lacks, like print(),
var-checking functions like is_object() and is_defined(). Binding in your
own functions via MonkeyWrapper is trivial, and implementing native functions
is simplified over "raw SpiderMonkey" by the template-based type conversion
framework.
A type-safe JS-to/from-Native binding system, allowing binding of
more than one native object to a JS object (in fact, any number,
provided each is of a different native type).
JS-to/from-Native conversions can be easily acheived in many cases
using template-based tricks. This includes pointer/reference conversions
for most types.
A really simple plugins API, allowing the inclusion of new native
class/function bindings at runtime.
A few useful JS classes, like InFile and OutFile (SpiderMonkey does not
build with a File class by default), CFile (a different
API for working with files), and Signal/Slot classes.
Two different JS binding for ncurses. One is an OO windows
toolkit, a subset of Thomas
Dickey's ncurses++ 5.4. The other wraps the lower-level C API.
Complete, ncurses applications can be written in JS, prototyped and
deployed in record time compared to writing the equivalent curses code
in C/C++. The event handling/dispatching capabilities of JS are a
great match for ncurses.
Has an API to allow plugins to add new JS functions and classes when a
plugin is opened. For example, we can load the InFile and OutFIle types and
functions like rmdir(), chdir(), and mkdir() via JS code:
open_dll('iofile','system_funcs')
Comes with an sqlite3 plugin providing two
independent sqlite3 wrapper APIs, one object-oriented and one matching the
sqlite3 C API very closely. (As far as i am aware, this is the world's only
freely-available sqlite3 binding for Javascript, though i have heard rumor of
a commercial product with similar support.)
The scriptable_base<T> type allows native C++ code
to bind objects to native- and JS-space simultaneously, to add
functions using either native or script code, and to easily call
script-side member functions from native code.
Comes with a colorful manual, providing an overview of using the
API and documenting the various plugins and script-side functions.
Requirements
SpiderMonkey.
Ape comes complete with its own copy of SpiderMonkey, but this is only
to ease distribution: it should work with any compatible copy of
SpiderMonkey (ours is not hacked at all, only copied). Be aware that
building the original SpiderMonkey sources is not trivial, because it
is intended to be built during the Mozilla browser's build process.
For some good docs on how to get and build the standalone libjs, visit:
http://developer.mozilla.org/en/docs/Introduction_to_the_JavaScript_shell.
While the majority of the code is platform-independent (or portable),
the project is very Unix-centric, and thus not all of the support might
work under non-Unix platforms. It is developed on Linux machines, and
may not build out of the box on other Unices. In particular, the plugins
support does not completely work on Win32 platforms due to the "odd" linking
requirements of DLLs under Windows.
A recent C++ compiler with good support for class templates, including
partial and full template specializations. gcc 3.2+ are believed to be
suitable.
Various plugins are enabled if the configure script determines
that the underlying features are available. These include bindings
to 3rd party APIs such as
ncurses,
readline_cpp, and
sqlite3.
JavaScript Resources
The Mozilla Development Center has the only
set of halfway-complete online API docs for the SpiderMonkey API. If you use the SpiderMonkey API, save yourself
lots of grief and visit this site.
libnspr, a library which can be used
to compile SpiderMonkey with multi-thread support.
Jaap de Wolff's contributions include Windows fixes, threading code,
and he developed the Class<> type creation/binding mechanism (which is used extensively by various
plugins).
If you're interested in assisting in development, please get in touch.
i don't have much intention of turning this into a full-time project,
and would be happy if someone else would get involved.
Getting started...
You can find several getting-started examples on
the demos page.