Consider a v8::String variable containing some js code.
For instance:
v8::Local<v8::String> source = v8::String::New("this.foo();");

I want to evaluate js code with my explicitly defined 'this' keyword.
First of all I found v8::Function::Call() method. Where I can pass
'this' object with recv argument (sic).

My idea was to create v8::Function:
v8::Local<v8::FunctionTemplate> templ =
v8::FunctionTemplate::New(eval, source);
v8::Local<v8::Function> function = templ->GetFunction();

where eval is:
v8::Handle<v8::Value> eval(const v8::Arguments &args) {
  v8::Handle<v8::String> source =
v8::Handle<v8::String>::Cast(args.Data());
  // I'm not talking about performance here. It's not a real life ;)
  v8::Local<v8::Script> script = v8::Script::Compile(source);
  script->Run();
  return v8::Undefined();
}

and call this function like this:
function->Call(myObj, 0, NULL);

But what I found is that inside eval, args.This() points to my object
(and that is good)... unfortunately inside script->Run() in expression
"this.foo()", 'this' means Global object of a current context. And
that is probably what expected by v8 developers... but definitely not
good for me :)

My dirty solution is to build "function() { " + source + "}" string,
Compile, Run and Cast the result to v8::Function.

Is there any clever solution?

Thanks.

-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users

Reply via email to