This project has moved. For the latest updates, please go here.

Override global object

Mar 5, 2013 at 7:06 AM
Over the past few days I’ve been attempting to glue Jurassic to a DOM tree. While there's still a massive amount of work to do, I’m attempting to solve some of the basics first. I hit a small snag with 'Window' that I’m not really happy with.

As you might know, the Window object is the global object in HTML. I’ve tried various ways of making this work with Jurassic:
  • Added properties to the global object as you describe elsewhere. While this more or less works, this isn't really a solution for me. To name two reasons: (1) people seem to make assumptions about the (prototype) inheritance tree of Window’s, which is something I’d like to mimic and (2) I'd like to keep my window code completely separate from the javascript engine.
  • I therefore attempted modifying the constructor to create a new global object. In retrospect, this was a bad idea, since the rest of the initialization code uses Global.
  • Eventually I made a small patch that makes it possible to derive from the global object and pass it to Window as reference, e.g. added a protected GlobalObject constructor and

//where: (ScriptEngine.cs)

public void SetGlobal(Func<GlobalObject, GlobalObject> fcn) 
    this.globalObject = fcn(this.globalObject);
This still means I need a helper 'constructor class' that defines part of the prototype chain and an instance class for Window, but does eventually lead to the correct prototype chain.

This basically solves it and gives the correct behavior / prototype chain, but I’d rather prefer a solution that doesn’t involve changing the code of Jurassic... I’ve read up on the other forum posts, but am I correct in the assumption this is currently the only way to do this?
Mar 5, 2013 at 10:09 AM

Can't you just set the prototype of the global object? E.g.

(function() {this.prototype = myWindow;})()

/Alxandr - mobile

Mar 5, 2013 at 11:03 AM
Alxandr wrote:
Can't you just set the prototype of the global object? E.g. (function() {this.prototype = myWindow;})() -- /Alxandr - mobile
Hmm interesting....

Normally you have Window : WindowClass : GlobalObject as prototype chain, what you're suggesting is having GlobalObject : Window : WindowClass as prototype chain. To be honest I'm not entirely sure what the behavior will be in this case, although I'm pretty sure it isn't compliant with the HTML standard. For example, what would happen if you create a new window? Would you return a new instance of GlobalObject instead of Window?

I did thought about entering the Window scope (object scope) before executing any code, thereby effectively setting a new global object without breaking anything... this is the way this behavior is commonly implemented in web browsers afaik. However, I couldn't find a way to do that from c#. What my patch basically does is just that.