Netscape 'Spidermonkey' JavaScript bindings

Warrick Buchanan <[email protected]>
Newsgroups gmane.comp.lib.boost.langbinding
Message-ID <[email protected]>
[Posted through web interface as posting to mailing list doesn't seem to work?]

Hi there,

First off thanks to David for letting me know about the existance of this 
list.  I currently have the beginnings (compiling with MSVC++ 7.1) of bindings 
for Netscape's 'Spidermonkey' JavaScript Engine (1.5 RC6) with which I can do 
stuff like the following:

...

struct Vector3
{
	float x, y, z;

	float LengthSquared()
	{
		return x*x + y*y + z*z;
	}
};


Declare<Vector3>::Property<0>("x", &Vector3::x);
Declare<Vector3>::Property<1>("y", &Vector3::y);
Declare<Vector3>::Property<2>("z", &Vector3::z);
Declare<Vector3>::Function<0>("lengthSquared", &Vector3::LengthSquared);
Declare<Vector3>::Definition("Vector3", jsContext, jsObject, jsPrototype);

...

Then in JavaScript land I can do:

...

var v = new Vector3();

v.x = 3.0;
v.y = 4.0;
v.z = 5.0;

var l = v.lengthSquared();

...

Of course I wouldn't like to reinvent the wheel or any sub-components of it(!), 
and am more than happy to help build this into something usable by others via 
boost (Including changing coding style from mine to the 'boostified' one ;) ).

I'm aware that my current binding classes/functions don't quite match up with 
boost::python at the moment (_class::def() etc ) for example - and I'm not sure 
if this is a good or bad thing yet (as boost::python has python like syntax of 
course!) and whether or not it will have to change due to the features I have 
yet to add.  Also I have this notion of a property/function index that 
originally came from spidermonkeys 'tinyids' due to the way I evolved the code -
 hence the need for <0>, <1> etc - but this has the added side effect of 
allowing me to change the definition of the exposed classes during runtime - 
replace a function property etc.

Internally I use a static table of type safe function pointers for the get/set 
functions for the properties - although I'm looking to change this so I can 
patch it directly into the function pointers spidermonkey keeps for these (to 
get rid of the un-needed extra level of indirection) - I actually do this for 
classes functions already.

For generating the overloaded templates needed to wrap all combinations of 
class functions to expose I started to look at boost::python's way of doing 
things with the boost::preprocessor library (accompanied by a stiff drink).  
But I think my preference would be to have a small C++ app to generate the code 
instead (Maybe the venerable pre-processor should be replaced by PHP or some 
equivalent one day!).

Obviously I still have a fair amount of work to do - parameter/return value 
ownership, constructors etc.

Anyhow I shall have a good read of this lists archives now...

(BTW in case it gets mentioned I am aware of the CliPP library and am following 
its progress with much interest but unfortunately its interpreter is not as 
complete as the Netscape one yet I believe)

Cheers,

Warrick Buchanan
Senior Tools Engineer
Argonaut Games Plc
www.argonaut.com



-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.