Hey Mike: Two quick things. 1) You cant have a Global object that doesn't have Object, Array, or String. Those are built-in to the language as Native objects and part of ECMA. 2) Don't set the Prototypes of each of the session Global objects on entering the Context, instead set them before they become Global Context objects by creating FunctionTemplates for them, link the FunctionTemplate's prototype to the Global Session's by using FunctionTemplate::Inherit, and then grab the ObjectTemplate from that FunctionTemplate and provide it to the Context Constructor.
On Wed, Jun 13, 2012 at 10:51 PM, MikeM <[email protected]> wrote: > Here's some sample code to illustrate what I'm trying to do. > I know this isn't 100% correct. > In fact the small script-let below where I try to persistent the property > "saveme" fails: > > *Fatal error in ..\..\src\objects.cc, line 4265 > CHECK(proto->IsJSGlobalObject()) failed* > > This started happening after I added the call to SetPrototype(). > Also I'm not sure how to make it so the "request" global has no built-ins > (String, Array, Object etc). > I want these to come via prototype chain from the "session" global. In > order to preserve "InstanceOf" working across several requests. > Any ideas? > ------------------------ > #include <v8.h> > using namespace v8; > > // Helper function that compiles and runs the source. > static inline v8::Local<v8::Value> CompileRun(const char* source) { > return v8::Script::Compile(v8::String::New(source))->Run(); > } > > >> int main(int argc, char* argv[]) >> { >> HandleScope scope; >> Persistent<Context> ctxRequest = Context::New(); >> Persistent<Context> ctxSession = Context::New(); >> >> //Set same security token for request and session. >> Local<Value> foo = String::New("foo"); >> ctxRequest->SetSecurityToken(foo); >> ctxSession->SetSecurityToken(foo); >> >> //Hold on to global from ctxSession and detach global We want to use >> this later >> Persistent<Object> globalSession = >> Persistent<Object>::Persistent(ctxSession->Global()); >> ctxSession->DetachGlobal(); >> ctxSession.Dispose(); >> >> ctxRequest->Enter(); //Start execution of 1st request. >> >> //Chain request global's prototype to session global so built in >> functions and objects are always found in session. >> ctxRequest->Global()->SetPrototype(globalSession); >> >> //Create a reference to session global on request global. >> //Allows clients to directly set properties that will persistent with >> the session >> ctxRequest->Global()->Set(String::New("Session"), globalSession); >> >> //Add property to the session global and save it. >> CompileRun("Session.saveme = 42;"); //THIS LINE ASSERTS. >> ctxRequest->Exit(); //1st request is done. >> >> Persistent<Context> ctxRequest2 = Context::New(); >> ctxRequest2->SetSecurityToken(foo); >> ctxRequest2->Enter(); >> >> //Chain request global's prototype to session global so built in >> functions and objects are always found in session. >> ctxRequest2->Global()->SetPrototype(globalSession); >> >> //Allows clients to directly set properties that will persistent with >> the session >> ctxRequest2->Global()->Set(String::New("Session"), globalSession); >> >> //Check that we can get value of saved property in the session. >> Local<Value> result = CompileRun("Session.saveme"); >> CHECK(!result->IsUndefined()); >> CHECK(result->IsInt32()); >> CHECK(42 == result->Int32Value()); >> >> ctxRequest2->Exit(); >> ctxRequest2.Dispose(); > > > >> >> -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users > -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
