3

I would like to be able to reset global object of my V8 JS context. Let's say everything works fine, code executes etc. But now I would like to return to my starting state (remove all objects, functions etc.)

I tried this way:

First I leave my current context:

v8::Local<v8::Context> context(m_isolate->GetCurrentContext());
context->Exit();

m_context.Reset(); //v8::Global<v8::Context>

And I create new context with new global object:

v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(m_isolate);
global->SetInternalFieldCount(1);
install_global_functions(global);

v8::Local<v8::Context> context = v8::Context::New(m_isolate, nullptr, global);
m_context.Reset(m_isolate, context); //making context persistent

context->Enter();

v8::Local<v8::Object> g_obj = context->Global();
g_obj->SetAlignedPointerInInternalField(0, this);

And... I still have access to variables, functions, classes I defined in previous context. WHY? How is this possible. When I create a new context with new global object template shouldn't it be just a 'clean' instance? What is wrong?

What I want to do is just clear everything (stuff that was created in JS and stuff that was added to global object from C++ both)? Is there any way to do this (other then destroying an isolate and reinitializing everything)?

EDIT:

It seems like isolate->GetCurrentContext() isn't returning correct context. I'm using this method frequently to find 'current' context. Tried two different methods isolate->GetEnteredContext() and m_context.Get(m_isolate) and this two seem to work correctly. I still don't understand WHY? Why isolate->GetCurrentContext() is returning old context while it's name suggests otherwise??? What does isolate->GetCurrentContext() actually return? I'm lost...

1 Answers1

0

isolate->GetCurrentContext() returns the last Enter()-ed context...

user64204
  • 167
  • 1
  • 7