eval in current C++ scope

70 views
Skip to first unread message

Francisco Tolmasky

unread,
Nov 23, 2016, 7:17:48 PM11/23/16
to v8-users
I'd like to replicate the effect of eval in a C++ function. In other words, I'd like to be able to eval and affect the current HandleScope. So if I do "var x = 10", I'd like the HandleScope to get a x added, not for the Context's global to receive the x. Is this possible?

I suppose a parallel question is, can I "push" a scope for Script->Run? Basically, I'd like successive Runs to modify the same scope, not the global, the same way successive evals.

Thanks,

Francisco

Ben Noordhuis

unread,
Nov 25, 2016, 3:22:41 AM11/25/16
to v8-users
I think the closest you can get is
v8::ScriptCompiler::CompileFunctionInContext(). It lets you compile
code in function context with control over its arguments and captured
variables.

Another approach is to intercept all global property accesses with
v8::ObjectTemplate::SetHandler() but that can be very slow.

tbl...@icloud.com

unread,
Nov 25, 2016, 8:16:56 PM11/25/16
to v8-users
A HandleScope does not store any key-value pairs.

It sounds like what you need is to create multiple Contexts. If that's not quite what you want, I'd suggest creating objects using Object::New and using them as the receiver in CompileFunctionInContext.

~Theodore

Francisco Tolmasky

unread,
Dec 30, 2016, 9:26:42 AM12/30/16
to v8-users
CompileFunctionInContext seems interesting. Do var's get added to context_extension? Also, is it possible to make a generator function with the?

Theodore Dubois

unread,
Dec 30, 2016, 4:04:56 PM12/30/16
to v8-u...@googlegroups.com
Doesn’t look like it. As the documentation says, CompileFunctionInContext is equivalent to

with (obj) {
// code
}

You really just need to use multiple contexts. Anything that you want to be shared between contexts can be shared.

~Theodore
> --
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/uotsqjxkMWY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages