On Wed, Jan 19, 2011 at 3:58 PM, Jak Sprats <[email protected]> wrote:
> Is there a similarly simple mechanism to register JS functions to call
> defined C functions.
>
> This is the part I am not yet fully grasping on how to code it :)
>
That's the easy part. Here's an (untested/skeleton) example:
using namespace v8;
Handle<Value> my_func( Arguments const & argv )
{
if( argv.Length() < 1 ) {
return ThrowException("Requires 1 argument!");
}
unsigned int sleepTime = static_cast<unsigned int>(argv[0]->Int32Value);
unsigned int rc;
{
v8::Unlocker unlock; // allows other threads to work while
sleep()ing
rc = ::sleep(sleepTime);
}
return Integer::New( static_cast<int32_t>(rc) );
}
(remember to do more range checking on the arguments, and remember that JS
has no unsigned integer type, so a user passing a negative number would
sleep very, very long in the above code.)
my_func() simply implements the v8::InvocationCallback interface (the
standard callback type), and can be bound directly to v8.
That glue is basically what v8-juice/vu8 provide for you, but if you just
want to wrap a small handful of functions then such helpers are probably
overkill.
--
----- stephan beal
http://wanderinghorse.net/home/stephan/
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users