Aaron, your code seems reasonable enough.
v8 splits global object into two parts: so named global proxy (one which you obtain with the Global()) and 'real' global---object on which properties can be set, etc. The reason is browser security. yours, anton. On Mon, Jan 24, 2011 at 4:44 PM, Aaron C <[email protected]> wrote: > Hi, I'm interested in learning how to get the window object map to the > "global" object in particular correctly. As I said, I can't edit the > JavaScript file being loaded (as it's not mine and I didn't mention > that it's also in source control and this code would be part of a > build/verification tool, and I'm sure that it would be a slippery > slope of one tiny fix after another). And although I could pre-load a > JavaScript file with 'fixes" as I also had mentioned, I'd like to > learn how to do this in C++ first, before I resort to a JavaScript > solution. > > I've continued to explore and believe I've gotten closer to a working > solution. I'm not sure if it's correct, but the JavaScript code now > loads. :) > > Handle<Object> v8RealGlobal = Handle<Object>::Cast(context->Global()- >>GetPrototype()); > > Context::Scope context_scope(context); > > Local<ObjectTemplate> docTmpl = ObjectTemplate::New(); > docTmpl->SetNamedPropertyHandler(MapGet, MapSet); > Local<Object> docObj = docTmpl->NewInstance(); > > v8RealGlobal->Set(String::New("document"), docObj); > > // this appears to work -- grab the prototype from the context global > and assign it to the global variable named "window" > v8RealGlobal->Set(String::New("window"), v8RealGlobal); > > The code then loads the scripts. > > And then very simple MapGet/Set functions: > > Handle<Value> MapGet(Local<String> name, const AccessorInfo& info) { > return Object::New(); > } > > Handle<Value> MapSet(Local<String> name, > Local<Value> value_obj, > const AccessorInfo& info) > { > return value_obj; > } > > Thanks! > > --Aaron > http://www.wiredprairie.us > > > > On Jan 23, 1:11 pm, Stephan Beal <[email protected]> wrote: >> On Sun, Jan 23, 2011 at 7:55 PM, Aaron C <[email protected]> wrote: >> > this.navigator = { >> > userAgent : "MSIE" >> > }; >> > this.document = { >> > documentElement : {} >> > }; >> >> Maybe something like... >> >> a) Create a new Object: >> >> Local<Object> obj( Object::New() ); >> >> b) Add that object to the global object: >> >> Local<Object> global( v8::Context::GetCurrent()->Global() ); >> >> global->Set( String::New("document"), obj ); >> >> now your global object has a plain object called 'document'. You would >> repeat this for the navigator item, and insert the userAgent and >> documentElements as children of the navigator/document. >> >> Or, even easier: >> >> Since you're using the sample shell, you have the load() function: simply >> write the above JS code into a file, and load() that file before proceeding >> with the other scripts. >> >> >> >> > This, for example, throws an exception: >> >> > global->Set(String::New("window"), global); >> >> That should work just fine - i do that same thing in some code to give my >> global object a name. >> >> > if (!window) this.window = this; >> >> You can do this to avoid that error: >> >> if( 'undefined' === typeof this.window ) this.window = this; >> >> -- >> ----- stephan bealhttp://wanderinghorse.net/home/stephan/ > > -- > 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
