* Be more careful about setting parent scope on functions (which may be nested).

Fixes bug 531 reported by juerg lehni on helma-dev.
  http://helma.org/bugs/show_bug.cgi?id=531
This commit is contained in:
hns 2007-07-18 22:54:36 +00:00
parent fb3029ed25
commit de0c08ce5a
2 changed files with 20 additions and 4 deletions

View file

@ -696,9 +696,15 @@ public class HopObject extends ScriptableObject implements Wrapper, PropertyReco
if (value instanceof Function) { if (value instanceof Function) {
// reset function's parent scope, needed because of the way we compile // reset function's parent scope, needed because of the way we compile
// prototype code, using the prototype objects as scope // prototype code, using the prototype objects as scope
Function f = (Function) value; Scriptable scriptable = (Scriptable) value;
if (f.getParentScope() == this) while (scriptable != null) {
f.setParentScope(core.global); Scriptable scope = scriptable.getParentScope();
if (scope == this) {
scriptable.setParentScope(core.global);
break;
}
scriptable = scope;
}
} }
} }
super.put(name, start, value); super.put(name, start, value);

View file

@ -30,6 +30,7 @@ public class HopObjectCtor extends FunctionObject {
// static constructor property access // static constructor property access
boolean initialized; boolean initialized;
RhinoCore core; RhinoCore core;
Scriptable protoProperty;
static Method hopObjCtor; static Method hopObjCtor;
@ -52,6 +53,7 @@ public class HopObjectCtor extends FunctionObject {
public HopObjectCtor(String protoName, RhinoCore core, Scriptable prototype) { public HopObjectCtor(String protoName, RhinoCore core, Scriptable prototype) {
super(protoName, hopObjCtor, core.global); super(protoName, hopObjCtor, core.global);
this.core = core; this.core = core;
this.protoProperty = prototype;
// Scriptable ps = prototype.getParentScope(); // Scriptable ps = prototype.getParentScope();
addAsConstructor(core.global, prototype); addAsConstructor(core.global, prototype);
// prototype.setParentScope(ps); // prototype.setParentScope(ps);
@ -122,7 +124,15 @@ public class HopObjectCtor extends FunctionObject {
if (value instanceof Function) { if (value instanceof Function) {
// reset static function's parent scope, needed because of the way we compile // reset static function's parent scope, needed because of the way we compile
// prototype code, using the prototype objects as scope // prototype code, using the prototype objects as scope
((Function) value).setParentScope(core.global); Scriptable scriptable = (Scriptable) value;
while (scriptable != null) {
Scriptable scope = scriptable.getParentScope();
if (scope == protoProperty) {
scriptable.setParentScope(core.global);
break;
}
scriptable = scope;
}
} }
super.put(name, start, value); super.put(name, start, value);
} }