script->Run in custom context?

56 views
Skip to first unread message

ondras

unread,
Feb 2, 2009, 1:11:25 AM2/2/09
to v8-users
Hi,

is it actually possible for script to run in a given (custom) context?
It would be really helpful to be able to run script in its own
"global" context which is different from the real global one - and
script's context will be returned as a JS object, available to global
context.

Thanks a lot for response!


Sincerely,
Ondrej Zara

Søren Gjesse

unread,
Feb 2, 2009, 2:43:47 AM2/2/09
to v8-u...@googlegroups.com
When a script is compiled and run it is always in the context which is currently active. Each context has a global object which is a normal JavaScript object, which can be assigned to a property is another context. You will also need to set the security token for the two contexts to the same value (or set up security callbacks).

Regards,
Søren

Ondrej Zara

unread,
Feb 2, 2009, 9:32:39 AM2/2/09
to v8-u...@googlegroups.com
Hi Soren,

thanks a lot for this positive info. However, I am really confused by
context switching. If I want to just create secondary context, enter
it, execute code and leave, what exactly must I do? Do I use
context::Enter and context::Exit? Do I need a context scope?

I tried several experiments with this, but I ended mostly with
segfault. I would appreciate either some small sample code, or an
explanation about how scope contexts work...


Thanks a lot,
Ondrej



2009/2/2 Søren Gjesse <sgj...@chromium.org>:

Ondrej Zara

unread,
Feb 2, 2009, 9:32:55 AM2/2/09
to v8-u...@googlegroups.com
Hi Soren,

thanks a lot for this positive info. However, I am really confused by
context switching. If I want to just create secondary context, enter
it, execute code and leave, what exactly must I do? Do I use
context::Enter and context::Exit? Do I need a context scope?

I tried several experiments with this, but I ended mostly with
segfault. I would appreciate either some small sample code, or an
explanation about how scope contexts work...


Thanks a lot,
Ondrej



2009/2/2 Ondrej Zara <ondre...@gmail.com>:

Søren Gjesse

unread,
Feb 2, 2009, 10:38:29 AM2/2/09
to v8-u...@googlegroups.com
To enter a context you just call Enter on it this will make it the current global context and calling Exit will return to the previous active context. The class Context::Scope simply calls Enter in its constructor and Exit in its destructor, so the two blocks below does the same (provided there are no way to skip calling context->Exit() in the last block).

Handle<Context> context;
...
{
  Context::Scope(context)
  <some code>
}

{
  context->Enter()
  <some code>
  context->Exit()
}

You can take a look at TEST(SecurityHandler) in test-api.cc to see how one global object becomes a property of another.

Regards,
Søren

Ondrej Zara

unread,
Feb 2, 2009, 11:31:53 AM2/2/09
to v8-u...@googlegroups.com
Hi Soren,

thanks again for you perfectly prompt and educative response. My
confusion was caused by severe mixing of Context scopes and/or
Enter+Exit calls.

I am now really able to enter other context and execute code there. I
am also able to access one context from another. Hower, there is one
task left which is not working yet:

v8::Handle<v8::Value> token = v8::String::New("x");
v8::Context::GetCurrent()->SetSecurityToken(token);
v8::Handle<v8::ObjectTemplate> local = v8::ObjectTemplate::New();
v8::Handle<v8::Context> context = v8::Context::New(NULL, local);
context->SetSecurityToken(token);

context->Enter();
v8::Handle<v8::String> source = v8::String::New("var x = 4");
v8::Handle<v8::Script> script = v8::Script::Compile(source);
script->Run();
context->Exit();

return context->Global();


As you can see, I would like to access the local context's global
object (populated by executing code inside it) and "see" its contents
from outside (my global context). However, the shown code always
returns null.

Ondrej Zara

unread,
Feb 3, 2009, 2:57:26 AM2/3/09
to v8-u...@googlegroups.com
Fixed! Turned out that I was trying to print the result using my
JS-based recursive serialization funcion, which performs a series of
"typeof" and "instanceof" checks - and this failed, because the result
does not satisfy "result instanceof Object" condition (it is instance
of Object defined in other context).


Ondrej
Reply all
Reply to author
Forward
0 new messages