Hi, all!
i thought i'd share this:
i've created yet another JS class generator which makes it really easy
to do the actual generation (you've still got to implement your
functions!), and wanted to share it for the benefit of other just-
starter-outers who don't want to experiment for many hours to find the
exact order of operations needed to make class construction work (e.g.
don't call constructorTemplate->GetFunction() before setting up your
Prototype object, or your class won't work).
Here's the C++ ctor signature for demonstrated JSClassCreator, so the
code below will make a bit more sense:
JSClassCreator( char const * className, // JS class name
Handle<Object> target, // where to install the new JS
class
v8::InvocationCallback ctor, // callback for the
constructor
int internalFieldCount = 0 );
The following creates a new JS class and adds members to it (this is
real code):
(assume that the "whio" object referenced here is some arbitrary
object - it's just the target object the class ctors get installed to)
////////////////////////////////////////////////////////////
// IOBase class:
v8::juice::JSClassCreator bindAbs( strings::IOBase, whio,
abstract_ctor );
{
Local<Function> noop = FunctionTemplate::New
(abstract_reimplement)->GetFunction();
bindAbs
.Set(strings::read, noop )
.Set(strings::write ,noop)
.Set(strings::isGood,base_isgood)
.Set(strings::flush,base_flush)
.Set(strings::close,base_close)
.Set(strings::toString, devT_tostring<IOBase,strings::IOBase> )
.Set(strings::SEEK_SET_,Integer::New(SEEK_SET) )
.Set(strings::SEEK_END_, Integer::New(SEEK_END))
.Set(strings::SEEK_CUR_,Integer::New(SEEK_CUR) )
.Seal(); // must be the final call. Closes the class creation
process.
}
To create a new type which subclasses another type:
////////////////////////////////////////////////////////////
// IODevice class:
{
v8::juice::JSClassCreator
bindIOD( strings::IODevice, whio, combined_ctor< whio_dev,
&dev_construct >, 1 );
bindIOD
.Inherit( bindAbs )
.SetInternalFieldCount(1)
...
.Seal();
}
The code is part of the v8-juice lib but the implementation is
standalone and should work as-is in arbitrary v8 client code. It's
available from:
http://code.google.com/p/v8-juice/source/browse/trunk/src/include/v8/juice/JSClassCreator.h
http://code.google.com/p/v8-juice/source/browse/trunk/src/lib/juice/JSClassCreator.cc
The docs are all in the header file.
There's tons more than can be done with this style of class creation
by via templates magic (e.g. automatically binding the native object
to JS, automatic forwarding to member functions, etc.) but i'm still
in the just-learning phase.
Happy hacking!
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---