|
64 | 64 | import org.htmlunit.corejs.javascript.Function; |
65 | 65 | import org.htmlunit.corejs.javascript.Script; |
66 | 66 | import org.htmlunit.corejs.javascript.Scriptable; |
| 67 | +import org.htmlunit.corejs.javascript.ScriptableObject; |
67 | 68 | import org.htmlunit.corejs.javascript.VarScope; |
68 | 69 | import org.htmlunit.css.ComputedCssStyleDeclaration; |
69 | 70 | import org.htmlunit.css.CssStyleSheet; |
@@ -950,8 +951,11 @@ public ScriptResult executeJavaScript(String sourceCode, final String sourceName |
950 | 951 | } |
951 | 952 | } |
952 | 953 |
|
| 954 | + final Window window = getEnclosingWindow().getScriptableObject(); |
| 955 | + final VarScope scope = ScriptableObject.getTopLevelScope(window.getParentScope()); |
| 956 | + |
953 | 957 | final Object result = getWebClient().getJavaScriptEngine() |
954 | | - .execute(this, getEnclosingWindow().getTopLevelScope(), sourceCode, sourceName, startLine); |
| 958 | + .execute(this, scope, sourceCode, sourceName, startLine); |
955 | 959 | return new ScriptResult(result); |
956 | 960 | } |
957 | 961 |
|
@@ -1029,9 +1033,12 @@ JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final |
1029 | 1033 | return JavaScriptLoadResult.COMPILATION_ERROR; |
1030 | 1034 | } |
1031 | 1035 |
|
| 1036 | + final Window window = getEnclosingWindow().getScriptableObject(); |
| 1037 | + final VarScope scope = ScriptableObject.getTopLevelScope(window.getParentScope()); |
| 1038 | + |
1032 | 1039 | @SuppressWarnings("unchecked") |
1033 | 1040 | final AbstractJavaScriptEngine<Object> engine = (AbstractJavaScriptEngine<Object>) client.getJavaScriptEngine(); |
1034 | | - engine.execute(this, getEnclosingWindow().getTopLevelScope(), script); |
| 1041 | + engine.execute(this, scope, script); |
1035 | 1042 | return JavaScriptLoadResult.SUCCESS; |
1036 | 1043 | } |
1037 | 1044 |
|
@@ -1119,7 +1126,10 @@ else if (!MimeType.isJavascriptMimeType(contentType)) { |
1119 | 1126 | final String scriptCode = response.getContentAsString(scriptEncoding); |
1120 | 1127 | if (null != scriptCode) { |
1121 | 1128 | 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 | + |
1123 | 1133 | final Object script = javaScriptEngine.compile(this, scope, scriptCode, url.toExternalForm(), 1); |
1124 | 1134 | if (script != null && cache.cacheIfPossible(request, response, script)) { |
1125 | 1135 | // no cleanup if the response is stored inside the cache |
|
0 commit comments