Skip to content

Commit

Permalink
Ha! All green. The content of iframe elements (and likely frames
Browse files Browse the repository at this point in the history
within frame sets, although that is untested) now have their own
private JS global object and execution environment, though code in
one window can access the DOM in others using the standard window
properties correctly.  As a side effect, a document object can now
be correctly reloaded with new content, the document.parentWindow
property will work in all contexts, and there are new Rhino-provided
global functions to manipulate the scope chain of JS Function objects.
  • Loading branch information
gleneivey committed Jul 22, 2009
1 parent a0c864c commit 3701d37
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 50 deletions.
36 changes: 21 additions & 15 deletions dist/env.js
Expand Up @@ -3549,7 +3549,7 @@ __extend__(DOMImplementation.prototype,{
createDocument : function(nsuri, qname, doctype){
//TODO - this currently returns an empty doc
//but needs to handle the args
return new HTMLDocument($implementation);
return new HTMLDocument($implementation, null);
},
translateErrCode : function(code) {
//convert DOMException Code to human readable error message;
Expand Down Expand Up @@ -3795,6 +3795,8 @@ function __parseLoop__(impl, doc, p) {
// change scope of window object creation functions, so that
// functions/code they create will be scoped to new window object
// *FunctionObjectsScope() from EnvjsRhinoSupraGlobal.java
var oldMalnwScope = getFunctionObjectsScope(
mkAndLoadNewWindow);
var oldMkWinScope = getFunctionObjectsScope(localCopy_mkWinFn);
var oldLoadScope = getFunctionObjectsScope(load);
var oldLoadScriptScope = getFunctionObjectsScope(
Expand All @@ -3808,6 +3810,7 @@ function __parseLoop__(impl, doc, p) {
mkAndLoadNewWindow();

// now restore the scope
setFunctionObjectsScope(mkAndLoadNewWindow, oldMalnwScope);
setFunctionObjectsScope(localCopy_mkWinFn, oldMkWinScope);
setFunctionObjectsScope(load, oldLoadScope);
setFunctionObjectsScope($env.loadLocalScript, oldLoadScriptScope);
Expand Down Expand Up @@ -4153,15 +4156,15 @@ $implementation.errorChecking = false;$debug("Defining Document");
* @author Jon van Noort (jon@webarcana.com.au)
* @param implementation : DOMImplementation - the creator Implementation
*/
var DOMDocument = function(implementation) {
var DOMDocument = function(implementation, docParentWindow) {
//$log("\tcreating dom document");
this.DOMNode = DOMNode;
this.DOMNode(this);

this.doctype = null; // The Document Type Declaration (see DocumentType) associated with this document
this.implementation = implementation; // The DOMImplementation object that handles this document.
this._documentElement = null; // "private" variable providing the read-only document.documentElement property
this._parentWindow = null; // "private" variable providing the read-only document.parentWindow property
this._parentWindow = docParentWindow; // "private" variable providing the read-only document.parentWindow property

this.nodeName = "#document";
this._id = 0;
Expand Down Expand Up @@ -4227,7 +4230,6 @@ __extend__(DOMDocument.prototype, {
},
load: function(url){
$debug("Loading url into DOM Document: "+ url + " - (Asynch? "+$w.document.async+")");
this._parentWindow = $w;
var scripts, _this = this;
var xhr = new XMLHttpRequest();
xhr.open("GET", url, $w.document.async);
Expand Down Expand Up @@ -4887,9 +4889,9 @@ $debug("Defining HTMLDocument");
*
* @extends DOMDocument
*/
var HTMLDocument = function(implementation) {
var HTMLDocument = function(implementation, docParentWindow) {
this.DOMDocument = DOMDocument;
this.DOMDocument(implementation);
this.DOMDocument(implementation, docParentWindow);

this._refferer = "";
this._domain;
Expand Down Expand Up @@ -5972,15 +5974,15 @@ __extend__(HTMLFrameElement.prototype, {
// global/window and won't be able to get at them....
var localCopy_mkWinFn = _$envjs$makeObjectIntoWindow$_;
var localCopy_$env = $env;
var localCopy_parent = window.parent;
var localCopy_top = window.top;
var localCopy_window = window;

// a local function gives us something whose scope
// is easy to change
var mkAndLoadNewWindow = function(){
if (makingNewWinFlag){
localCopy_mkWinFn(frameWindow, localCopy_$env,
localCopy_parent, localCopy_top);
localCopy_window,
localCopy_window.top);
}

frameWindow.location = value;
Expand All @@ -5991,24 +5993,28 @@ __extend__(HTMLFrameElement.prototype, {
// functions, so that functions/code they create
// will be scoped to new window object
// *FunctionObjectsScope() from EnvjsRhinoSupraGlobal.java
var oldMalnwScope = getFunctionObjectsScope(
mkAndLoadNewWindow);
var oldMkWinScope = getFunctionObjectsScope(
localCopy_mkWinFn);
var oldLoadScope = getFunctionObjectsScope(load);
var oldLoadScriptScope = getFunctionObjectsScope(
$env.loadLocalScript);
var oldMkWinScope = getFunctionObjectsScope(
localCopy_mkWinFn);
$env.loadLocalScript);

setFunctionObjectsScope(localCopy_mkWinFn, frameWindow);
setFunctionObjectsScope(mkAndLoadNewWindow, frameWindow);
setFunctionObjectsScope(localCopy_mkWinFn, frameWindow);
setFunctionObjectsScope(load, frameWindow);
setFunctionObjectsScope($env.loadLocalScript, frameWindow);

mkAndLoadNewWindow();
this._content = frameWindow;

// now restore the scope
setFunctionObjectsScope(mkAndLoadNewWindow, oldMalnwScope);
setFunctionObjectsScope(localCopy_mkWinFn, oldMkWinScope);
setFunctionObjectsScope(load, oldLoadScope);
setFunctionObjectsScope($env.loadLocalScript,
oldLoadScriptScope);
oldLoadScriptScope);
} catch(e){
$error("failed to load frame content: from " + value, e);
}
Expand Down Expand Up @@ -9882,7 +9888,7 @@ __extend__(HTMLDocument.prototype, {



var $document = new HTMLDocument($implementation);
var $document = new HTMLDocument($implementation, $w);
$w.__defineGetter__("document", function(){
return $document;
});
Expand Down
44 changes: 26 additions & 18 deletions dist/env.rhino.js
Expand Up @@ -231,20 +231,22 @@ var Envjs = function(){
if (timer.at <= now){
f = timer.fn;
f();
timer.at = Date.now() + timer.interval;
timer.at += timer.interval;
}
}
empty = true;
sleep = null;
now = Date.now();
for (i in timers){
empty = false;
empty = false;
timer = timers[i];
after = timer.at - now
sleep = (sleep === null || after < sleep) ? after : sleep;
}
sleep = sleep < 0 ? 0 : sleep;
if (empty || ( wait !== 0 ) && ( ( sleep > 0 && !wait ) || ( Date.now() + sleep > wait ) ) ) {
if (empty ||
( wait !== 0 ) &&
( ( sleep > 0 && !wait ) || ( Date.now() + sleep > wait ) ) ) {
break;
}
if (sleep) {
Expand Down Expand Up @@ -3986,7 +3988,7 @@ __extend__(DOMImplementation.prototype,{
createDocument : function(nsuri, qname, doctype){
//TODO - this currently returns an empty doc
//but needs to handle the args
return new HTMLDocument($implementation);
return new HTMLDocument($implementation, null);
},
translateErrCode : function(code) {
//convert DOMException Code to human readable error message;
Expand Down Expand Up @@ -4232,6 +4234,8 @@ function __parseLoop__(impl, doc, p) {
// change scope of window object creation functions, so that
// functions/code they create will be scoped to new window object
// *FunctionObjectsScope() from EnvjsRhinoSupraGlobal.java
var oldMalnwScope = getFunctionObjectsScope(
mkAndLoadNewWindow);
var oldMkWinScope = getFunctionObjectsScope(localCopy_mkWinFn);
var oldLoadScope = getFunctionObjectsScope(load);
var oldLoadScriptScope = getFunctionObjectsScope(
Expand All @@ -4245,6 +4249,7 @@ function __parseLoop__(impl, doc, p) {
mkAndLoadNewWindow();

// now restore the scope
setFunctionObjectsScope(mkAndLoadNewWindow, oldMalnwScope);
setFunctionObjectsScope(localCopy_mkWinFn, oldMkWinScope);
setFunctionObjectsScope(load, oldLoadScope);
setFunctionObjectsScope($env.loadLocalScript, oldLoadScriptScope);
Expand Down Expand Up @@ -4590,15 +4595,15 @@ $implementation.errorChecking = false;$debug("Defining Document");
* @author Jon van Noort (jon@webarcana.com.au)
* @param implementation : DOMImplementation - the creator Implementation
*/
var DOMDocument = function(implementation) {
var DOMDocument = function(implementation, docParentWindow) {
//$log("\tcreating dom document");
this.DOMNode = DOMNode;
this.DOMNode(this);

this.doctype = null; // The Document Type Declaration (see DocumentType) associated with this document
this.implementation = implementation; // The DOMImplementation object that handles this document.
this._documentElement = null; // "private" variable providing the read-only document.documentElement property
this._parentWindow = null; // "private" variable providing the read-only document.parentWindow property
this._parentWindow = docParentWindow; // "private" variable providing the read-only document.parentWindow property

this.nodeName = "#document";
this._id = 0;
Expand Down Expand Up @@ -4664,7 +4669,6 @@ __extend__(DOMDocument.prototype, {
},
load: function(url){
$debug("Loading url into DOM Document: "+ url + " - (Asynch? "+$w.document.async+")");
this._parentWindow = $w;
var scripts, _this = this;
var xhr = new XMLHttpRequest();
xhr.open("GET", url, $w.document.async);
Expand Down Expand Up @@ -5324,9 +5328,9 @@ $debug("Defining HTMLDocument");
*
* @extends DOMDocument
*/
var HTMLDocument = function(implementation) {
var HTMLDocument = function(implementation, docParentWindow) {
this.DOMDocument = DOMDocument;
this.DOMDocument(implementation);
this.DOMDocument(implementation, docParentWindow);

this._refferer = "";
this._domain;
Expand Down Expand Up @@ -6409,15 +6413,15 @@ __extend__(HTMLFrameElement.prototype, {
// global/window and won't be able to get at them....
var localCopy_mkWinFn = _$envjs$makeObjectIntoWindow$_;
var localCopy_$env = $env;
var localCopy_parent = window.parent;
var localCopy_top = window.top;
var localCopy_window = window;

// a local function gives us something whose scope
// is easy to change
var mkAndLoadNewWindow = function(){
if (makingNewWinFlag){
localCopy_mkWinFn(frameWindow, localCopy_$env,
localCopy_parent, localCopy_top);
localCopy_window,
localCopy_window.top);
}

frameWindow.location = value;
Expand All @@ -6428,24 +6432,28 @@ __extend__(HTMLFrameElement.prototype, {
// functions, so that functions/code they create
// will be scoped to new window object
// *FunctionObjectsScope() from EnvjsRhinoSupraGlobal.java
var oldMalnwScope = getFunctionObjectsScope(
mkAndLoadNewWindow);
var oldMkWinScope = getFunctionObjectsScope(
localCopy_mkWinFn);
var oldLoadScope = getFunctionObjectsScope(load);
var oldLoadScriptScope = getFunctionObjectsScope(
$env.loadLocalScript);
var oldMkWinScope = getFunctionObjectsScope(
localCopy_mkWinFn);
$env.loadLocalScript);

setFunctionObjectsScope(localCopy_mkWinFn, frameWindow);
setFunctionObjectsScope(mkAndLoadNewWindow, frameWindow);
setFunctionObjectsScope(localCopy_mkWinFn, frameWindow);
setFunctionObjectsScope(load, frameWindow);
setFunctionObjectsScope($env.loadLocalScript, frameWindow);

mkAndLoadNewWindow();
this._content = frameWindow;

// now restore the scope
setFunctionObjectsScope(mkAndLoadNewWindow, oldMalnwScope);
setFunctionObjectsScope(localCopy_mkWinFn, oldMkWinScope);
setFunctionObjectsScope(load, oldLoadScope);
setFunctionObjectsScope($env.loadLocalScript,
oldLoadScriptScope);
oldLoadScriptScope);
} catch(e){
$error("failed to load frame content: from " + value, e);
}
Expand Down Expand Up @@ -10319,7 +10327,7 @@ __extend__(HTMLDocument.prototype, {



var $document = new HTMLDocument($implementation);
var $document = new HTMLDocument($implementation, $w);
$w.__defineGetter__("document", function(){
return $document;
});
Expand Down
Binary file modified rhino/mainForEnvjs.jar
Binary file not shown.
Expand Up @@ -127,8 +127,13 @@ public static void whereAmI(Context cx, Scriptable thisObj, Object[] args,
Function funObj)
{
System.out.println("whereAmI : " + Context.toString(args[0]));
// System.out.println(thisObj.getClass().getName() +
// " (" + thisObj.hashCode() + ")");
////////////
System.out.println(" this: " + thisObj.getClass().getName() +
" (" + thisObj.hashCode() + ")");
if (args[1] != null)
System.out.println(" fn: " + args[1].getClass().getName() +
" (" + args[1].hashCode() + ")");
////////////
System.out.println(" scope:");
Scriptable temp = thisObj;
while (temp != null){
Expand Down
5 changes: 2 additions & 3 deletions src/dom/document.js
Expand Up @@ -7,15 +7,15 @@ $debug("Defining Document");
* @author Jon van Noort (jon@webarcana.com.au)
* @param implementation : DOMImplementation - the creator Implementation
*/
var DOMDocument = function(implementation) {
var DOMDocument = function(implementation, docParentWindow) {
//$log("\tcreating dom document");
this.DOMNode = DOMNode;
this.DOMNode(this);

this.doctype = null; // The Document Type Declaration (see DocumentType) associated with this document
this.implementation = implementation; // The DOMImplementation object that handles this document.
this._documentElement = null; // "private" variable providing the read-only document.documentElement property
this._parentWindow = null; // "private" variable providing the read-only document.parentWindow property
this._parentWindow = docParentWindow; // "private" variable providing the read-only document.parentWindow property

this.nodeName = "#document";
this._id = 0;
Expand Down Expand Up @@ -81,7 +81,6 @@ __extend__(DOMDocument.prototype, {
},
load: function(url){
$debug("Loading url into DOM Document: "+ url + " - (Asynch? "+$w.document.async+")");
this._parentWindow = $w;
var scripts, _this = this;
var xhr = new XMLHttpRequest();
xhr.open("GET", url, $w.document.async);
Expand Down
5 changes: 4 additions & 1 deletion src/dom/implementation.js
Expand Up @@ -33,7 +33,7 @@ __extend__(DOMImplementation.prototype,{
createDocument : function(nsuri, qname, doctype){
//TODO - this currently returns an empty doc
//but needs to handle the args
return new HTMLDocument($implementation);
return new HTMLDocument($implementation, null);
},
translateErrCode : function(code) {
//convert DOMException Code to human readable error message;
Expand Down Expand Up @@ -279,6 +279,8 @@ function __parseLoop__(impl, doc, p) {
// change scope of window object creation functions, so that
// functions/code they create will be scoped to new window object
// *FunctionObjectsScope() from EnvjsRhinoSupraGlobal.java
var oldMalnwScope = getFunctionObjectsScope(
mkAndLoadNewWindow);
var oldMkWinScope = getFunctionObjectsScope(localCopy_mkWinFn);
var oldLoadScope = getFunctionObjectsScope(load);
var oldLoadScriptScope = getFunctionObjectsScope(
Expand All @@ -292,6 +294,7 @@ function __parseLoop__(impl, doc, p) {
mkAndLoadNewWindow();

// now restore the scope
setFunctionObjectsScope(mkAndLoadNewWindow, oldMalnwScope);
setFunctionObjectsScope(localCopy_mkWinFn, oldMkWinScope);
setFunctionObjectsScope(load, oldLoadScope);
setFunctionObjectsScope($env.loadLocalScript, oldLoadScriptScope);
Expand Down
4 changes: 2 additions & 2 deletions src/html/document.js
Expand Up @@ -8,9 +8,9 @@ $debug("Defining HTMLDocument");
*
* @extends DOMDocument
*/
var HTMLDocument = function(implementation) {
var HTMLDocument = function(implementation, docParentWindow) {
this.DOMDocument = DOMDocument;
this.DOMDocument(implementation);
this.DOMDocument(implementation, docParentWindow);

this._refferer = "";
this._domain;
Expand Down

0 comments on commit 3701d37

Please sign in to comment.