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