V8 has no way to "clean up a global object".

The global object is referenced in two ways: either directly (implicit
references from contexts), or indirectly via the global proxy (references
from JavaScript).

ReattachGlobal didn't do what you would have expected, reattached the
global object of a context to the global proxy used by that context. It did
not allow to attach any random other global object, since it could not
update existing references in contexts (all over the heap).

What Jakob says is one way. You may want to freeze the global object to
ensure nothing accidentally ends up in the global object though, in case
you forget "var" somewhere in your script. Otherwise you could manually
delete properties you introduced.

Alternatively you spin up different native contexts for different
executions.

cheers,
Toon


On Wed, Jul 16, 2014 at 9:56 AM, Jakob Kummerow <[email protected]>
wrote:

> How about wrapping the fragments in anonymous closures? Roughly:
>
> script_to_execute = "(function() {" + fragment + "})();";
>
> That should get you most of what you want, except for undoing
> monkey-patching of Array.prototype and stuff, but maybe that's good enough,
> and it's certainly easy and fast.
>
>
> On Wed, Jul 16, 2014 at 8:35 AM, Oliver Bock <[email protected]> wrote:
>
>> What is the most efficient way to clean up the global object between
>> running fragments of JavaScript?
>>
>> I am running a fragment of JavaScript in a tight loop to make a simple
>> calculation for each of hundreds of thousands of inputs.  I don't want
>> variables set by one execution to bleed into the next.
>>
>> My first thought was to try to swap in a different global object for each
>> v8 iteration, but I can find no mechanism to do that.  (Earlier versions of
>> v8 might have supported it via something like Context.ReattachGlobal()
>> <https://groups.google.com/forum/#!msg/v8-users/FfqgU3jEKf4>, but that
>> function no longer exists.)
>>
>> I can manually iterate over the global object and clean up after myself
>> (as this guy does
>> <https://groups.google.com/forum/#%21topic/v8-users/q2CDsy58Dcc>), but
>> my JavaScript fragments are fairly simple and the cleanup dominates
>> execution time (because v8 is so fast).
>>
>> I think recreating the Context each time will be even slower than this.
>> particularly because I will need to repopulate it with various objects.
>>
>>
>>   Oliver
>>
>> --
>> --
>> 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/d/optout.
>>
>
>  --
> --
> 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/d/optout.
>

-- 
-- 
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/d/optout.

Reply via email to