Skip to content

Commit a9587ff

Browse files
committed
scope handling (wip)
1 parent 280b6e5 commit a9587ff

19 files changed

+52
-34
lines changed

src/main/java/org/htmlunit/html/ScriptElementSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private static void executeEvent(final DomElement element, final String type) {
306306
final EventTarget eventTarget = element.getScriptableObject();
307307
final Event event = new Event(element, type);
308308

309-
event.setParentScope(eventTarget);
309+
event.setParentScope(eventTarget.getParentScope());
310310
event.setPrototype(eventTarget.getPrototype(event.getClass()));
311311

312312
eventTarget.executeEventLocally(event);

src/main/java/org/htmlunit/javascript/FunctionWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.htmlunit.corejs.javascript.Scriptable;
2222
import org.htmlunit.corejs.javascript.Symbol;
2323
import org.htmlunit.corejs.javascript.SymbolScriptable;
24+
import org.htmlunit.corejs.javascript.VarScope;
2425

2526
/**
2627
* Wrapper for a {@link Function} delegating all calls to the wrapped instance.
@@ -181,7 +182,7 @@ public void setPrototype(final Scriptable prototype) {
181182
* {@inheritDoc}
182183
*/
183184
@Override
184-
public Scriptable getParentScope() {
185+
public VarScope getParentScope() {
185186
return wrapped_.getParentScope();
186187
}
187188

src/main/java/org/htmlunit/javascript/HtmlUnitScriptable.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ public void setParentScope(final Scriptable scope) {
9797
if (scope == this) {
9898
throw new IllegalArgumentException("Object can't be its own parentScope");
9999
}
100-
if (scope instanceof Window) {
101-
throw new IllegalArgumentException("parentScope can't be the window");
102-
}
103100
super.setParentScope(scope);
104101
}
105102

src/main/java/org/htmlunit/javascript/JavaScriptEngine.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import org.htmlunit.corejs.javascript.StackStyle;
5858
import org.htmlunit.corejs.javascript.Symbol;
5959
import org.htmlunit.corejs.javascript.TopLevel;
60+
import org.htmlunit.corejs.javascript.VarScope;
61+
import org.htmlunit.corejs.javascript.WithScope;
6062
import org.htmlunit.html.DomNode;
6163
import org.htmlunit.html.HtmlPage;
6264
import org.htmlunit.javascript.background.BackgroundJavaScriptFactory;
@@ -857,11 +859,12 @@ protected String getSourceCode(final Context cx) {
857859
return getContextFactory().callSecured(action, page);
858860
}
859861

860-
private static Scriptable getScope(final HtmlPage page, final DomNode node) {
862+
private static VarScope getScope(final HtmlPage page, final DomNode node) {
863+
final TopLevel topLevel = page.getEnclosingWindow().getTopLevelScope();
861864
if (node != null) {
862-
return node.getScriptableObject();
865+
return new WithScope(topLevel, node.getScriptableObject());
863866
}
864-
return page.getEnclosingWindow().getScriptableObject();
867+
return topLevel;
865868
}
866869

867870
/**
@@ -1447,7 +1450,7 @@ public static long lengthOfArrayLike(final Context cx, final Scriptable obj) {
14471450
/**
14481451
* @return the top call scope
14491452
*/
1450-
public static Scriptable getTopCallScope() {
1453+
public static TopLevel getTopCallScope() {
14511454
return ScriptRuntime.getTopCallScope(Context.getCurrentContext());
14521455
}
14531456

src/main/java/org/htmlunit/javascript/host/Element.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.htmlunit.corejs.javascript.NativeObject;
4040
import org.htmlunit.corejs.javascript.Scriptable;
4141
import org.htmlunit.corejs.javascript.ScriptableObject;
42+
import org.htmlunit.corejs.javascript.WithScope;
4243
import org.htmlunit.css.ComputedCssStyleDeclaration;
4344
import org.htmlunit.css.ElementCssStyleDeclaration;
4445
import org.htmlunit.cssparser.parser.CSSException;
@@ -122,7 +123,8 @@ public void jsConstructor() {
122123
public void setDomNode(final DomNode domNode) {
123124
super.setDomNode(domNode);
124125

125-
setParentScope(getWindow().getDocument());
126+
final Window window = getWindow();
127+
setParentScope(new WithScope(window.getWebWindow().getTopLevelScope(), window.getDocument()));
126128
// CSSStyleDeclaration uses the parent scope
127129
style_ = new CSSStyleDeclaration(this, new ElementCssStyleDeclaration(getDomNodeOrDie()));
128130

src/main/java/org/htmlunit/javascript/host/Location.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,16 @@ public static Location jsConstructor(final Context cx, final Scriptable scope,
180180
public void initialize(final Scriptable scope, final Window window, final Page page) {
181181
final int attributes = ScriptableObject.PERMANENT | ScriptableObject.READONLY;
182182

183-
FunctionObject functionObject = new FunctionObject(METHOD_ASSIGN.getName(), METHOD_ASSIGN, this);
183+
FunctionObject functionObject = new FunctionObject(METHOD_ASSIGN.getName(), METHOD_ASSIGN, scope);
184184
defineProperty(METHOD_ASSIGN.getName(), functionObject, attributes);
185185

186-
functionObject = new FunctionObject(METHOD_RELOAD.getName(), METHOD_RELOAD, this);
186+
functionObject = new FunctionObject(METHOD_RELOAD.getName(), METHOD_RELOAD, scope);
187187
defineProperty(METHOD_RELOAD.getName(), functionObject, attributes);
188188

189-
functionObject = new FunctionObject(METHOD_REPLACE.getName(), METHOD_REPLACE, this);
189+
functionObject = new FunctionObject(METHOD_REPLACE.getName(), METHOD_REPLACE, scope);
190190
defineProperty(METHOD_REPLACE.getName(), functionObject, attributes);
191191

192-
functionObject = new FunctionObject(METHOD_TO_STRING.getName(), METHOD_TO_STRING, this);
192+
functionObject = new FunctionObject(METHOD_TO_STRING.getName(), METHOD_TO_STRING, scope);
193193
defineProperty("toString", functionObject, attributes);
194194

195195
defineProperty(scope, "hash", null, GETTER_HASH, SETTER_HASH, attributes);

src/main/java/org/htmlunit/javascript/host/NamedNodeMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void jsConstructor() {
6868
*/
6969
public NamedNodeMap(final DomElement element) {
7070
super();
71-
setParentScope(element.getScriptableObject());
71+
setParentScope(element.getScriptableObject().getParentScope());
7272
setPrototype(getPrototype(getClass()));
7373

7474
attributes_ = element.getAttributes();

src/main/java/org/htmlunit/javascript/host/css/CSSRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ protected CSSRule(final CSSStyleSheet stylesheet, final AbstractCSSRuleImpl rule
209209
super();
210210
stylesheet_ = stylesheet;
211211
rule_ = rule;
212-
setParentScope(stylesheet);
212+
setParentScope(stylesheet.getParentScope());
213213
setPrototype(getPrototype(getClass()));
214214
}
215215

src/main/java/org/htmlunit/javascript/host/css/CSSStyleDeclaration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public class CSSStyleDeclaration extends HtmlUnitScriptable {
121121

122122
/** The wrapped CSSStyleDeclaration */
123123
private AbstractCssStyleDeclaration styleDeclaration_;
124+
private CSSStyleSheet parentStyleSheet_;
124125

125126
/**
126127
* Creates an instance.
@@ -165,13 +166,14 @@ public CSSStyleDeclaration(final Element element, final AbstractCssStyleDeclarat
165166
*/
166167
CSSStyleDeclaration(final CSSStyleSheet parentStyleSheet, final WrappedCssStyleDeclaration styleDeclaration) {
167168
super();
168-
setParentScope(parentStyleSheet);
169+
setParentScope(parentStyleSheet.getParentScope());
169170
setPrototype(getPrototype(getClass()));
170171

171172
if (styleDeclaration == null) {
172173
throw new IllegalStateException("styleDeclaration can't be null");
173174
}
174175
styleDeclaration_ = styleDeclaration;
176+
parentStyleSheet_ = parentStyleSheet;
175177
}
176178

177179
protected AbstractCssStyleDeclaration getCssStyleDeclaration() {
@@ -1504,7 +1506,7 @@ public void setPaddingTop(final Object paddingTop) {
15041506
public CSSRule getParentRule() {
15051507
final AbstractCSSRuleImpl parentRule = styleDeclaration_.getParentRule();
15061508
if (parentRule != null) {
1507-
return CSSRule.create((CSSStyleSheet) getParentScope(), parentRule);
1509+
return CSSRule.create(parentStyleSheet_, parentRule);
15081510
}
15091511
return null;
15101512
}

src/main/java/org/htmlunit/javascript/host/css/MediaList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void jsConstructor() {
6060
public MediaList(final CSSStyleSheet parent, final CssMediaList cssMediaList) {
6161
super();
6262
cssMediaList_ = cssMediaList;
63-
setParentScope(parent);
63+
setParentScope(parent.getParentScope());
6464
setPrototype(getPrototype(getClass()));
6565
}
6666

0 commit comments

Comments
 (0)