Re: Netscape 'Spidermonkey' JavaScript bindings
Peder Holt <[email protected]>
| Newsgroups | gmane.comp.lib.boost.langbinding |
|---|---|
| Message-ID | <[email protected]> |
Warrick Buchanan <warrick <at> argonaut.com> writes:
>
> [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();
>
> ...
>
Hi.
I have started to look into a binder for Netscape's Spidermonkey myself,
building on my experience from the clipp project.
I encountered the problem of uniquely mapping a jsNative function to a c++
function compile time.
I had to adapt to the syntax of boost.python:
class_<T>(...)
.function("f1",f1)
.function("f2",f2);
and for global functions:
module<T>(...)
.function("f1",f1)
.function("f2",f2);
where T is some arbitrary unique type.
The trick here is to let "function" return a specialized indexing class that
points to the next free function pointer for the type T.
template<typename T,unsigned IndexI>
struct callback_function {
JSBool callback(...); //map using typeid(T) or JSObject* and IndexI
};
template<typename DerivedT,unsigned IndexI>
struct indexed_define {
indexed_define(DerivedT*)
template<typename Fn>
indexed_define<DerivedT,IndexI+1> function(const char* name,Fn fn) {
jsNative callback
= callback_function<DerivedT::value_type,IndexI>::callback;
}
};
template<typename T>
struct module : public indexed_define<module,0>
{
typedef T value_type;
};
My knowledge of SpiderMonkey is rudimentary at best, and there might be some
other smart way of avoiding the above problem.
Would you be interested in combining efforts on this problem?
Peder Holt
-------------------------------------------------------
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