On Thu, Oct 3, 2013 at 5:11 PM, ChaRles Lew <[email protected]> wrote:
> Hi, i'm a newbie on using v8. I'm trying to embedded v8 into a c++
> application and provide javascript as an extension language.
> Now i'm seeking a way to invoke javascript code (as a string) from c++
> environment, with a specific object as 'this'.
>
> We may have some c++ strings containing javascript expressions like
>
> "this.propA + globalObjectX.propB"
>
> which accesses properties of a specific object as 'this'. I'm able to obtain
> the Handle<Object> of that specific object and the Context which contains
> globalObjectX.
>
> I can think of some ways like direct eval call which involves some
> javascript code that seems to be able to get this working. But I'm wondering
> what is the proper and reasonably efficient way to execute this code from
> C++ environment. Thanks a lot.
The `this` object is customizable only when JS code is invoked as a
function. In node.js, we have some bootstrap code that's invoked with
v8::Script::Run(). It returns a JS function, which is then invoked in
the context of another object. IOW:
// Caveat emptor: error handling elided for the sake of brevity
const char source[] = "(function() { /* Your code here. */ })";
Local<String> script_source = String::New(source, sizeof(source) - 1);
Local<Script> script = Script::New(source);
Local<Value> rval = script->Run();
Local<Function> fn = rval.As<Function>();
Local<Object> this_object = /* ... */;
fn->Call(this_object, 0, NULL);
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.