Calling javascript methods from C++ ?

2,490 views
Skip to first unread message

goo...@dataworx.com.au

unread,
Sep 4, 2008, 6:04:05 PM9/4/08
to v8-users
I have been using SpiderMonkey, however as a C++ veteran I am far more
comfortable with embedded programming in v8.

I have a need to call from C++ a method on a javascript object handle
by name, as yet I have been unable to determine a way to do this by
inspecting the v8 code.

Does anybody know if there is a way I can do this ?

goo...@dataworx.com.au

unread,
Sep 4, 2008, 6:07:29 PM9/4/08
to v8-users
Just to clarify, I have the v8 object handle, I need to call the
javascript method by name

Feng Qian

unread,
Sep 4, 2008, 11:57:37 PM9/4/08
to v8-u...@googlegroups.com
Do you mean 'call JavaScript method from C++'?

If you, you can do

Handle<Value> func = Handle<Function>::Cast(obj->Get(func_name));
Handle<Value> result = func->Call(obj, ...);

Function is just a JSObject, when calling the function, you pass obj
as the receiver
to Function::Call.

Cheers,
Feng

goo...@dataworx.com.au

unread,
Sep 5, 2008, 8:10:47 PM9/5/08
to v8-users
Thanks Fenq, that's exactly what I needed.

Cheers, Paul.

rajika

unread,
Sep 15, 2008, 12:54:36 AM9/15/08
to v8-users
hi,
Can you share the code that made this possible, I want to do the same
thing, i.e. to call a Javascript method (with an argument) within C++
and get the result. Thanks in advance.

goo...@dataworx.com.au

unread,
Sep 15, 2008, 8:30:48 AM9/15/08
to v8-users
It's really quite easy,

As a starting point you can look at the code in the process.cc example

Lines 178-192 locate the callback function by name and stores it a
persistent handle named 'process_' - the script must have been
compiled before searching for the function by name.

// The script compiled and ran correctly. Now we fetch out the
// Process function from the global object.
Handle<String> process_name = String::New("Process");
Handle<Value> process_val = context->Global()->Get(process_name);

// If there is no Process function, or if it is not a function,
// bail out
if (!process_val->IsFunction()) return false;

// It is a function; cast it to a Function
Handle<Function> process_fun = Handle<Function>::Cast(process_val);

// Store the function in a Persistent handle, since we also want
// that to remain after this call returns
process_ = Persistent<Function>::New(process_fun);

Then lines 259 - 263 set up some args and call the function

// Invoke the process function, giving the global object as 'this'
// and one argument, the request.
const int argc = 1;
Handle<Value> argv[argc] = { request_obj };
Handle<Value> result = process_->Call(context_->Global(), argc,
argv);

All of this assumes you are calling the callback function on the same
thread that created the context ( and that the context handle doesn't
go out of scope or is stored in a persist handle as in lines 161 - 164
of process.cc

// Store the context in the processor object in a persistent handle,
// since we want the reference to remain after we return from this
// method.
context_ = Persistent<Context>::New(context);

Cheers, Paul.

On Sep 15, 2:54 pm, rajika <rajik...@gmail.com> wrote:
> hi,
> Can you share the code that made this possible, I want to do the same
> thing, i.e. to call aJavascriptmethod (with an argument) withinC++
> and get the result. Thanks in advance.
>
> On Sep 6, 5:10 am, goo...@dataworx.com.au wrote:
>
> > Thanks Fenq, that's exactly what I needed.
>
> > Cheers, Paul.
>
> > On Sep 5, 1:57 pm, Feng Qian <f...@chromium.org> wrote:
>
> > > Do you mean 'callJavaScriptmethod fromC++'?
>
> > > If you, you can do
>
> > > Handle<Value> func = Handle<Function>::Cast(obj->Get(func_name));
> > > Handle<Value> result = func->Call(obj, ...);
>
> > > Function is just a JSObject, whencallingthe function, you pass obj
> > > as the receiver
> > > to Function::Call.
>
> > > Cheers,
> > > Feng
>
> > > On Thu, Sep 4, 2008 at 3:07 PM, <goo...@dataworx.com.au> wrote:
>
> > > > Just to clarify, I have the v8 object handle, I need to call the
> > > >javascriptmethod by name
>
> > > > On Sep 5, 8:04 am, goo...@dataworx.com.au wrote:
> > > >> I have been using SpiderMonkey, however as aC++veteran I am far more
> > > >> comfortable with embedded programming in v8.
>
> > > >> I have a need to call fromC++a method on ajavascriptobject handle

rajika

unread,
Sep 15, 2008, 3:16:43 PM9/15/08
to v8-users
hey thanks a lot!
Reply all
Reply to author
Forward
0 new messages