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
> > > >> 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