Chaining global objects in multiple contextes

38 views
Skip to first unread message

Alberto Navatta

unread,
May 6, 2015, 9:17:56 AM5/6/15
to v8-u...@googlegroups.com
We are evaluating the possibility to change the Javascript engine in our application, switching from Mozilla Spidermonkey to Google V8.

We have some doubt about how to achieve on V8 one of the functionalities required by our application using the JS Engine.

In our javascript domain we have several sessions each with multiple javascript variable/object declared in nested scopes and we need to move between session and scopes during application processing (so basically under some circumstances we must evaluate variables at one level, under other at a difference scope level in a specific session).

Scopes are nested, that means that in case one variable is not found in the current scope the search continues in containing scope, if any.

To simplify let's say we have just a root scope and a child scope and that we have a console.log function that just prints information.

// code executed in root scope
var rootVariable = 'this is root';
var simpleVar ='I am in root scope';

// code executed in child scope
var childVariable = 'this is child';
var simpleVar ='I am in child scope';


The expected behavior would be the following:
// code executed in root scope
console
.log('Root = ' + rootVariable); // OUTPUT --> "Root = this is root"
console
.log('Simple = ' + simpleVar); // OUTPUT --> "Simple = I am in root scope"
console
.log('CHILD = ' + childVariable); // OUTPUT --> triggers an exception, childVariable is not defined in root scope

// code executed in child scope
console
.log('ROOT = ' + rootVariable); // OUTPUT --> "ROOT = this is root"
console
.log('Simple = ' + simpleVar); // OUTPUT --> "Simple = I am in child scope"
console
.log('CHILD = ' + childVariable); // OUTPUT --> "CHILD = this is child"

We were unsuccessful to find an implementation model using V8 basic functionalities (such as contextes and handle scopes) to satisfy our needs.

The subsequent idea we've worked on was to create some kind of relationship among the two global objects of the two contextes corresponding to our scopes (root and child). In theory what would be needed is:
1) to find a way to handle locally in the "child context global object" any property/variable lookup
2) if the property lookup fails, to proxy the request to the parent (root context global object)

Anyway we have a very little knowledge of V8 environment, any suggestion or feedback about possible solutions would be very appreciated.

Alberto

Toon Verwaest

unread,
May 6, 2015, 9:25:05 AM5/6/15
to v8-u...@googlegroups.com
I'd say either install the parent global as prototype of the child global, or install a non-masking interceptor.

In the first case you probably want to swap Object.prototype in the global's chain with the parent, but that means that the methods will come from the parent context.

If you don't want that, you'll have to install a non-masking (PropertyHandlerFlags::kNonMasking) interceptor ("Property Handler") on your child global which accesses the "missing" local variable on the parent global.

Regards,
Toon

--
--
v8-users mailing list
v8-u...@googlegroups.com
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 v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alberto Navatta

unread,
May 20, 2015, 7:06:01 AM5/20/15
to v8-u...@googlegroups.com
Hi Toon,

the second solution worked perfectly, thanks!

While with the first in the few quick tests I've made it looks that the nested scope can see only a clone of the parent global and any subsequent change happened to the parent global was not seed by the child.

Alberto
Reply all
Reply to author
Forward
0 new messages