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

Reply via email to