Skip to content

Commit aefd8de

Browse files
committed
scope handling fix (wip)
1 parent adebc68 commit aefd8de

10 files changed

Lines changed: 37 additions & 54 deletions

File tree

src/main/java/org/htmlunit/WebWindow.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import java.io.Serializable;
1818

19-
import org.htmlunit.corejs.javascript.TopLevel;
2019
import org.htmlunit.css.ComputedCssStyleDeclaration;
2120
import org.htmlunit.html.DomElement;
2221
import org.htmlunit.javascript.HtmlUnitScriptable;
@@ -77,9 +76,6 @@ public interface WebWindow extends Serializable {
7776
*/
7877
WebWindow getTopWindow();
7978

80-
TopLevel getTopLevelScope();
81-
void setTopLevelScope(TopLevel topLevelScope);
82-
8379
/**
8480
* Returns the web client that "owns" this window.
8581
*

src/main/java/org/htmlunit/WebWindowImpl.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,6 @@ public <T> T getScriptableObject() {
186186
return (T) scriptObject_;
187187
}
188188

189-
/**
190-
* {@inheritDoc}
191-
*/
192-
@Override
193-
public void setTopLevelScope(final TopLevel topLevelScope) {
194-
topLevelScope_ = topLevelScope;
195-
}
196-
197-
/**
198-
* {@inheritDoc}
199-
*/
200-
@Override
201-
public TopLevel getTopLevelScope() {
202-
return topLevelScope_;
203-
}
204-
205189
/**
206190
* {@inheritDoc}
207191
*/

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.htmlunit.corejs.javascript.Function;
6565
import org.htmlunit.corejs.javascript.Script;
6666
import org.htmlunit.corejs.javascript.Scriptable;
67+
import org.htmlunit.corejs.javascript.ScriptableObject;
6768
import org.htmlunit.corejs.javascript.VarScope;
6869
import org.htmlunit.css.ComputedCssStyleDeclaration;
6970
import org.htmlunit.css.CssStyleSheet;
@@ -950,8 +951,11 @@ public ScriptResult executeJavaScript(String sourceCode, final String sourceName
950951
}
951952
}
952953

954+
final Window window = getEnclosingWindow().getScriptableObject();
955+
final VarScope scope = ScriptableObject.getTopLevelScope(window.getParentScope());
956+
953957
final Object result = getWebClient().getJavaScriptEngine()
954-
.execute(this, getEnclosingWindow().getTopLevelScope(), sourceCode, sourceName, startLine);
958+
.execute(this, scope, sourceCode, sourceName, startLine);
955959
return new ScriptResult(result);
956960
}
957961

@@ -1029,9 +1033,12 @@ JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final
10291033
return JavaScriptLoadResult.COMPILATION_ERROR;
10301034
}
10311035

1036+
final Window window = getEnclosingWindow().getScriptableObject();
1037+
final VarScope scope = ScriptableObject.getTopLevelScope(window.getParentScope());
1038+
10321039
@SuppressWarnings("unchecked")
10331040
final AbstractJavaScriptEngine<Object> engine = (AbstractJavaScriptEngine<Object>) client.getJavaScriptEngine();
1034-
engine.execute(this, getEnclosingWindow().getTopLevelScope(), script);
1041+
engine.execute(this, scope, script);
10351042
return JavaScriptLoadResult.SUCCESS;
10361043
}
10371044

@@ -1119,7 +1126,10 @@ else if (!MimeType.isJavascriptMimeType(contentType)) {
11191126
final String scriptCode = response.getContentAsString(scriptEncoding);
11201127
if (null != scriptCode) {
11211128
final AbstractJavaScriptEngine<?> javaScriptEngine = client.getJavaScriptEngine();
1122-
final VarScope scope = getEnclosingWindow().getTopLevelScope();
1129+
1130+
final Window window = getEnclosingWindow().getScriptableObject();
1131+
final VarScope scope = ScriptableObject.getTopLevelScope(window.getParentScope());
1132+
11231133
final Object script = javaScriptEngine.compile(this, scope, scriptCode, url.toExternalForm(), 1);
11241134
if (script != null && cache.cacheIfPossible(request, response, script)) {
11251135
// no cleanup if the response is stored inside the cache

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,21 @@ public Object callFunction(
832832
final Object[] args,
833833
final DomNode node) {
834834

835-
final VarScope scope = getScope(page, node);
835+
VarScope scope = ScriptableObject.getTopLevelScope(thisObject.getParentScope());
836+
837+
if (node != null && node instanceof HtmlElement htmlElement) {
838+
final HTMLElement elem = htmlElement.getScriptableObject();
839+
scope = new WithScope(scope, elem.getOwnerDocument());
840+
841+
if (htmlElement instanceof SubmittableElement) {
842+
final HtmlForm enclosingForm = htmlElement.getEnclosingForm();
843+
if (enclosingForm != null) {
844+
scope = new WithScope(scope, enclosingForm.getScriptableObject());
845+
}
846+
}
847+
848+
scope = new WithScope(scope, node.getScriptableObject());
849+
}
836850

837851
return callFunction(page, javaScriptFunction, scope, thisObject, args);
838852
}
@@ -869,25 +883,6 @@ protected String getSourceCode(final Context cx) {
869883
return getContextFactory().callSecured(action, page);
870884
}
871885

872-
private static VarScope getScope(final HtmlPage page, final DomNode node) {
873-
final TopLevel topLevel = page.getEnclosingWindow().getTopLevelScope();
874-
if (node != null && node instanceof HtmlElement htmlElement) {
875-
final HTMLElement elem = htmlElement.getScriptableObject();
876-
WithScope scope = new WithScope(topLevel, elem.getOwnerDocument());
877-
878-
if (htmlElement instanceof SubmittableElement) {
879-
final HtmlForm enclosingForm = htmlElement.getEnclosingForm();
880-
if (enclosingForm != null) {
881-
scope = new WithScope(scope, enclosingForm.getScriptableObject());
882-
}
883-
}
884-
885-
return new WithScope(scope, node.getScriptableObject());
886-
}
887-
888-
return topLevel;
889-
}
890-
891886
/**
892887
* Indicates if JavaScript is running in current thread.
893888
* <p>This allows code to know if their own evaluation has been triggered by some JS code.

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,10 @@ public void postMessage(final Object message) {
149149
for (final BroadcastChannel channel : broadcastChannels) {
150150
if (channel != this && name_.equals(channel.name_)) {
151151
final Window channelWindow = channel.getWindow();
152-
final WebWindow channelWebWindow = channelWindow.getWebWindow();
153-
final Page channelPage = channelWebWindow.getEnclosedPage();
152+
final Page channelPage = channelWindow.getWebWindow().getEnclosedPage();
154153

155154
if (UrlUtils.isSameOrigin(currentURL, channelPage.getUrl())) {
156-
final TopLevel scope = channelWebWindow.getTopLevelScope();
155+
final TopLevel scope = getTopLevelScope(channelWindow.getParentScope());
157156
final Scriptable ports = JavaScriptEngine.newArray(scope, 0);
158157

159158
final MessageEvent event = new MessageEvent();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void setDomNode(final DomNode domNode) {
125125
super.setDomNode(domNode);
126126

127127
final Window window = getWindow();
128-
setParentScope(new WithScope(window.getWebWindow().getTopLevelScope(), window.getDocument()));
128+
setParentScope(new WithScope(getTopLevelScope(getParentScope()), window.getDocument()));
129129
// CSSStyleDeclaration uses the parent scope
130130
style_ = new CSSStyleDeclaration(this, new ElementCssStyleDeclaration(getDomNodeOrDie()));
131131

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Storage(final Window window, final Map<String, String> store) {
7575
super();
7676
store_ = store;
7777
storeSize_ = 0L;
78-
setParentScope(window.getWebWindow().getTopLevelScope());
78+
setParentScope(getTopLevelScope(window.getParentScope()));
7979
setPrototype(window.getPrototype(Storage.class));
8080
}
8181

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,6 @@ public External getExternal() {
759759
public void initialize(final TopLevel scope, final WebWindow webWindow, final Page pageToEnclose) {
760760
webWindow_ = webWindow;
761761
webWindow_.setScriptableObject(this);
762-
webWindow.setTopLevelScope(scope);
763762

764763
defineProperty(scope, "length", null, GETTER_LENGTH, SETTER_LENGTH, ScriptableObject.READONLY);
765764
defineProperty(scope, "self", null, GETTER_SELF, SETTER_SELF, ScriptableObject.READONLY);
@@ -1423,7 +1422,7 @@ public void triggerOnError(final ScriptException e) {
14231422
final int line = e.getFailingLineNumber();
14241423
final int column = e.getFailingColumnNumber();
14251424

1426-
final TopLevel scope = getWebWindow().getTopLevelScope();
1425+
final TopLevel scope = getTopLevelScope(getParentScope());
14271426

14281427
Object jsError = e.getMessage();
14291428
if (e.getCause() instanceof JavaScriptException) {
@@ -1769,7 +1768,7 @@ public Selection getSelection() {
17691768
public Selection getSelectionImpl() {
17701769
if (selection_ == null) {
17711770
selection_ = new Selection();
1772-
selection_.setParentScope(getWebWindow().getTopLevelScope());
1771+
selection_.setParentScope(getParentScope());
17731772
selection_.setPrototype(getPrototype(selection_.getClass()));
17741773
}
17751774
return selection_;
@@ -2079,7 +2078,7 @@ public static int getPort(final URL url) {
20792078
public Scriptable getPerformance() {
20802079
if (performance_ == null) {
20812080
final Performance performance = new Performance();
2082-
performance.setParentScope(getWebWindow().getTopLevelScope());
2081+
performance.setParentScope(getParentScope());
20832082
performance.setPrototype(getPrototype(performance.getClass()));
20842083
performance_ = performance;
20852084
}

src/main/java/org/htmlunit/javascript/host/crypto/Crypto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void jsConstructor() {
6060
*/
6161
public Crypto(final Window window) {
6262
this();
63-
setParentScope(window.getWebWindow().getTopLevelScope());
63+
setParentScope(getTopLevelScope(window.getParentScope()));
6464
setPrototype(window.getPrototype(Crypto.class));
6565
}
6666

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public StyleSheetList(final Document document) {
8787
super();
8888

8989
final WebWindow webWindow = document.getWindow().getWebWindow();
90-
setParentScope(new WithScope(webWindow.getTopLevelScope(), document));
90+
setParentScope(new WithScope(getTopLevelScope(document.getParentScope()), document));
9191
setPrototype(getPrototype(getClass()));
9292

9393
final WebClient webClient = webWindow.getWebClient();

0 commit comments

Comments
 (0)