Skip to content

Commit 2cf40ca

Browse files
committed
scope handling fix (wip)
1 parent 42f5ab3 commit 2cf40ca

17 files changed

Lines changed: 52 additions & 38 deletions

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

Lines changed: 2 additions & 1 deletion
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.VarScope;
6768
import org.htmlunit.css.ComputedCssStyleDeclaration;
6869
import org.htmlunit.css.CssStyleSheet;
6970
import org.htmlunit.html.impl.SimpleRange;
@@ -1116,7 +1117,7 @@ else if (!MimeType.isJavascriptMimeType(contentType)) {
11161117
final String scriptCode = response.getContentAsString(scriptEncoding);
11171118
if (null != scriptCode) {
11181119
final AbstractJavaScriptEngine<?> javaScriptEngine = client.getJavaScriptEngine();
1119-
final Scriptable scope = getEnclosingWindow().getScriptableObject();
1120+
final VarScope scope = getEnclosingWindow().getTopLevelScope();
11201121
final Object script = javaScriptEngine.compile(this, scope, scriptCode, url.toExternalForm(), 1);
11211122
if (script != null && cache.cacheIfPossible(request, response, script)) {
11221123
// no cleanup if the response is stored inside the cache

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

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

1717
import org.htmlunit.Page;
1818
import org.htmlunit.WebWindow;
19-
import org.htmlunit.corejs.javascript.Scriptable;
19+
import org.htmlunit.corejs.javascript.VarScope;
2020
import org.htmlunit.html.HtmlPage;
2121
import org.htmlunit.javascript.configuration.JavaScriptConfiguration;
2222

@@ -120,7 +120,7 @@ public interface AbstractJavaScriptEngine<SCRIPT> {
120120
* @param startLine the line at which the script source starts
121121
* @return the result of executing the specified code
122122
*/
123-
SCRIPT compile(HtmlPage owningPage, Scriptable scope, String sourceCode, String sourceName, int startLine);
123+
SCRIPT compile(HtmlPage owningPage, VarScope scope, String sourceCode, String sourceName, int startLine);
124124

125125
/**
126126
* Executes the specified JavaScript code in the context of a given page.
@@ -130,7 +130,7 @@ public interface AbstractJavaScriptEngine<SCRIPT> {
130130
* @param script the script to execute
131131
* @return the result of executing the specified code
132132
*/
133-
Object execute(HtmlPage page, Scriptable scope, SCRIPT script);
133+
Object execute(HtmlPage page, VarScope scope, SCRIPT script);
134134

135135
/**
136136
* Executes the specified JavaScript code in the context of a given page.
@@ -143,10 +143,10 @@ public interface AbstractJavaScriptEngine<SCRIPT> {
143143
* @return the result of executing the specified code
144144
*/
145145
Object execute(HtmlPage page,
146-
Scriptable scope,
147-
String sourceCode,
148-
String sourceName,
149-
int startLine);
146+
VarScope scope,
147+
String sourceCode,
148+
String sourceName,
149+
int startLine);
150150

151151
/**
152152
* @return this JavaScript engine's {@link HtmlUnitContextFactory}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public FunctionWrapper(final Function wrapped) {
4646
* {@inheritDoc}
4747
*/
4848
@Override
49-
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
49+
public Object call(final Context cx, final VarScope scope, final Scriptable thisObj, final Object[] args) {
5050
return wrapped_.call(cx, scope, thisObj, args);
5151
}
5252

@@ -62,7 +62,7 @@ public String getClassName() {
6262
* {@inheritDoc}
6363
*/
6464
@Override
65-
public Scriptable construct(final Context cx, final Scriptable scope, final Object[] args) {
65+
public Scriptable construct(final Context cx, final VarScope scope, final Object[] args) {
6666
return wrapped_.construct(cx, scope, args);
6767
}
6868

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.htmlunit.corejs.javascript.Function;
3636
import org.htmlunit.corejs.javascript.Script;
3737
import org.htmlunit.corejs.javascript.Scriptable;
38+
import org.htmlunit.corejs.javascript.VarScope;
3839
import org.htmlunit.corejs.javascript.debug.Debugger;
3940
import org.htmlunit.html.HtmlElement;
4041
import org.htmlunit.html.HtmlPage;
@@ -303,7 +304,7 @@ protected Object doTopCall(final Callable callable,
303304
*/
304305
@Override
305306
protected Object doTopCall(final Script script,
306-
final Context cx, final Scriptable scope,
307+
final Context cx, final VarScope scope,
307308
final Scriptable thisObj) {
308309

309310
final TimeoutContext tcx = (TimeoutContext) cx;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.htmlunit.corejs.javascript.Scriptable;
3535
import org.htmlunit.corejs.javascript.ScriptableObject;
3636
import org.htmlunit.corejs.javascript.TopLevel;
37+
import org.htmlunit.corejs.javascript.VarScope;
3738
import org.htmlunit.html.DomNode;
3839
import org.htmlunit.html.HtmlImage;
3940
import org.htmlunit.javascript.host.Window;
@@ -418,23 +419,24 @@ public HtmlUnitScriptable clone() {
418419
}
419420

420421
protected NativePromise setupPromise(final FailableSupplier<Object, IOException> resolver) {
421-
final Scriptable scope = ScriptableObject.getTopLevelScope(this);
422+
final VarScope scope = ScriptableObject.getTopLevelScope(this);
422423
final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
423424

424425
try {
425426
final LambdaFunction resolve = (LambdaFunction) getProperty(ctor, "resolve");
426-
return (NativePromise) resolve.call(Context.getCurrentContext(), this, ctor, new Object[] {resolver.get()});
427+
return (NativePromise) resolve.call(Context.getCurrentContext(), scope,
428+
ctor, new Object[] {resolver.get()});
427429
}
428430
catch (final IOException e) {
429431
final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
430-
return (NativePromise) reject.call(Context.getCurrentContext(), this, ctor, new Object[] {e.getMessage()});
432+
return (NativePromise) reject.call(Context.getCurrentContext(), scope, ctor, new Object[] {e.getMessage()});
431433
}
432434
}
433435

434436
protected NativePromise setupRejectedPromise(final Supplier<Object> resolver) {
435-
final Scriptable scope = ScriptableObject.getTopLevelScope(this);
437+
final VarScope scope = ScriptableObject.getTopLevelScope(this);
436438
final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
437439
final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
438-
return (NativePromise) reject.call(Context.getCurrentContext(), this, ctor, new Object[] {resolver.get()});
440+
return (NativePromise) reject.call(Context.getCurrentContext(), scope, ctor, new Object[] {resolver.get()});
439441
}
440442
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public static void configureRhino(final WebClient webClient, final BrowserVersio
476476
* @throws IOException in case of problems
477477
*/
478478
public static void applyPolyfills(final WebClient webClient, final BrowserVersion browserVersion,
479-
final Context context, final Scriptable scope, final HtmlUnitScriptable scriptable) throws IOException {
479+
final Context context, final VarScope scope, final HtmlUnitScriptable scriptable) throws IOException {
480480

481481
if (webClient.getOptions().isFetchPolyfillEnabled()) {
482482
Polyfill.getFetchPolyfill().apply(context, scope, scriptable);
@@ -738,7 +738,7 @@ public void shutdown() {
738738
* {@inheritDoc}
739739
*/
740740
@Override
741-
public Script compile(final HtmlPage owningPage, final Scriptable scope, final String sourceCode,
741+
public Script compile(final HtmlPage owningPage, final VarScope scope, final String sourceCode,
742742
final String sourceName, final int startLine) {
743743
WebAssert.notNull("sourceCode", sourceCode);
744744

@@ -773,7 +773,7 @@ public final <T> T callSecured(final ContextAction<T> action, final HtmlPage pag
773773
*/
774774
@Override
775775
public Object execute(final HtmlPage page,
776-
final Scriptable scope,
776+
final VarScope scope,
777777
final String sourceCode,
778778
final String sourceName,
779779
final int startLine) {
@@ -789,7 +789,7 @@ public Object execute(final HtmlPage page,
789789
* {@inheritDoc}
790790
*/
791791
@Override
792-
public Object execute(final HtmlPage page, final Scriptable scope, final Script script) {
792+
public Object execute(final HtmlPage page, final VarScope scope, final Script script) {
793793
if (shutdownPending_ || webClient_ == null) {
794794
// shutdown was already called
795795
return null;
@@ -826,7 +826,7 @@ public Object callFunction(
826826
final Object[] args,
827827
final DomNode node) {
828828

829-
final Scriptable scope = getScope(page, node);
829+
final VarScope scope = getScope(page, node);
830830

831831
return callFunction(page, javaScriptFunction, scope, thisObject, args);
832832
}
@@ -841,7 +841,7 @@ public Object callFunction(
841841
* @return the function result
842842
*/
843843
public Object callFunction(final HtmlPage page, final Function function,
844-
final Scriptable scope, final Scriptable thisObject, final Object[] args) {
844+
final VarScope scope, final Scriptable thisObject, final Object[] args) {
845845
if (shutdownPending_ || webClient_ == null) {
846846
// shutdown was already called
847847
return null;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.htmlunit.corejs.javascript.Function;
2424
import org.htmlunit.corejs.javascript.Scriptable;
2525
import org.htmlunit.corejs.javascript.ScriptableObject;
26+
import org.htmlunit.corejs.javascript.VarScope;
2627

2728
/**
2829
* Replacement (in fact a wrapper) for Rhino's native toString function on Function prototype
@@ -70,7 +71,7 @@ static class NativeFunctionToStringFunctionChrome extends FunctionWrapper {
7071
* {@inheritDoc}
7172
*/
7273
@Override
73-
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
74+
public Object call(final Context cx, final VarScope scope, final Scriptable thisObj, final Object[] args) {
7475
final String s = (String) super.call(cx, scope, thisObj, args);
7576

7677
if (thisObj instanceof BaseFunction function && s.contains("[native code")) {
@@ -91,7 +92,7 @@ static class NativeFunctionToStringFunctionFF extends FunctionWrapper {
9192
* {@inheritDoc}
9293
*/
9394
@Override
94-
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
95+
public Object call(final Context cx, final VarScope scope, final Scriptable thisObj, final Object[] args) {
9596
final String s = (String) super.call(cx, scope, thisObj, args);
9697

9798
if (thisObj instanceof BaseFunction function && s.contains("[native code")) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.htmlunit.corejs.javascript.Function;
3030
import org.htmlunit.corejs.javascript.Scriptable;
3131
import org.htmlunit.corejs.javascript.ScriptableObject;
32+
import org.htmlunit.corejs.javascript.VarScope;
3233
import org.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer;
3334
import org.htmlunit.html.HtmlPage;
3435
import org.htmlunit.javascript.AbstractJavaScriptEngine;
@@ -480,7 +481,7 @@ void callFunction(final Function function, final Object[] args) {
480481
if (function == null) {
481482
return;
482483
}
483-
final Scriptable scope = function.getParentScope();
484+
final VarScope scope = function.getParentScope();
484485
final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
485486
engine.callFunction(containingPage_, function, scope, this, args);
486487
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ public int animateAnimationsFrames() {
685685

686686
for (final AnimationFrame animationFrame : animationFrames) {
687687
jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(),
688-
animationFrame.callback_, this, getParentScope(), args);
688+
animationFrame.callback_, getParentScope(), this, args);
689689
}
690690
return animationFrames_.size();
691691
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.htmlunit.corejs.javascript.FunctionObject;
2626
import org.htmlunit.corejs.javascript.Scriptable;
2727
import org.htmlunit.corejs.javascript.ScriptableObject;
28+
import org.htmlunit.corejs.javascript.VarScope;
2829
import org.htmlunit.javascript.HtmlUnitScriptable;
2930
import org.htmlunit.javascript.JavaScriptEngine;
3031
import org.htmlunit.javascript.background.BackgroundJavaScriptFactory;
@@ -171,7 +172,7 @@ public static Object queueMicrotask(final Scriptable thisObj, final Object[] arg
171172
}
172173

173174
final Function callback = (Function) args[0];
174-
final Scriptable scope = ScriptableObject.getTopLevelScope(thisObj);
175+
final VarScope scope = ScriptableObject.getTopLevelScope(thisObj);
175176
final Context cx = Context.getCurrentContext();
176177
cx.enqueueMicrotask(() -> {
177178
try {

0 commit comments

Comments
 (0)