The code below throws an exception because the saved property "arraytest" 
is doesn't seem to be an array on the second execution of script.
I presume the build-ins prototypes like "Array" must be different after 
Reattaching the same global to the 2nd context.
But why?
The failure happens in the final test at the end of the code.

------------------------------------------------
static inline v8::Local<v8::Value> CompileRun(const char* source) {
  return v8::Script::Compile(v8::String::New(source))->Run();
}

  HandleScope scope;
  Persistent<Context> ctxRequest = Context::New();

  Local<Value> foo = String::New("foo");
  ctxRequest->SetSecurityToken(foo);
  ctxRequest->Enter();  //Start execution of 1st request.

  //Create an object we can use to store properties between requests.
  Persistent<Object> sessionObject = Persistent<Object>::New(Object::New());
  Local<Object> requestGlobal = ctxRequest->Global();

  //Create a reference to session object on request global.
  requestGlobal->Set(String::New("Session"), sessionObject);
  TryCatch trycatch;

  //Add property to the session object and save it. Add an empty array too.
  CompileRun("Session.saveme = 42; if(Session.arraytest === undefined) 
{Session.arraytest = [];}");

  //Makes sure we have an array and return value in saveme
  Local<Value> result = Script::Compile(String::New("if(!(Session.arraytest 
instanceof Array)) {throw new Error('Failed instanceof Array test.');} 
Session.saveme"))->Run();
  if(result.IsEmpty())
  {
     Handle<Value> exception = trycatch.Exception();
     String::AsciiValue exception_str(exception);
     printf("Exception: %s\n", *exception_str);
  }
  else
  {
     CHECK(!result->IsUndefined()); 
     CHECK(result->IsInt32());
     CHECK(42 == result->Int32Value());
  }

  //Save the the global and reattach to 2nd request later
  Persistent<Object> requestSavedGlobal = 
Persistent<Object>::Persistent(ctxRequest->Global());
  ctxRequest->DetachGlobal();
  ctxRequest->Exit();

  Persistent<Context> ctxRequest2 = Context::New();
  ctxRequest2->ReattachGlobal(requestSavedGlobal);
  ctxRequest2->SetSecurityToken(foo);
  ctxRequest2->Enter();

  requestSavedGlobal->Set(String::New("Session"), sessionObject);
    
  //Check that we can get value of saved property in the session.
  result = Script::Compile(String::New("if(!(Session.arraytest instanceof 
Array)) {throw new Error('Failed instanceof Array test.');}"))->Run();
  if(result.IsEmpty())
  {
    Handle<Value> exception = trycatch.Exception();
    String::AsciiValue exception_str(exception);
    printf("Exception: %s\n", *exception_str);
  }

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to