Skip to content

Commit 8b3f485

Browse files
committed
working on a more complete context setup for WebWorker (#816)
1 parent 7e25a9d commit 8b3f485

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

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

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -228,42 +228,25 @@ private void init(final WebWindow webWindow, final Page page, final Context cont
228228
context.initSafeStandardObjects(window);
229229
configureRhino(webClient, browserVersion, window);
230230

231+
final Map<Class<? extends Scriptable>, Scriptable> prototypes = new HashMap<>();
232+
final Map<String, Scriptable> prototypesPerJSName = new HashMap<>();
233+
231234
final ClassConfiguration windowConfig = jsConfig_.getClassConfiguration("Window");
232-
if (windowConfig.getJsConstructor() != null) {
233-
final FunctionObject functionObject =
234-
new RecursiveFunctionObject("Window", windowConfig.getJsConstructor().getValue(), window, browserVersion);
235-
ScriptableObject.defineProperty(window, "constructor", functionObject,
236-
ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY);
237-
}
238-
else {
239-
defineConstructor(window, window, new Window());
240-
}
235+
final FunctionObject functionObject =
236+
new RecursiveFunctionObject("Window", windowConfig.getJsConstructor().getValue(), window, browserVersion);
237+
ScriptableObject.defineProperty(window, "constructor", functionObject,
238+
ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY);
241239

242-
URLSearchParams.NativeParamsIterator.init(window, "URLSearchParams Iterator");
243-
FormData.FormDataIterator.init(window, "FormData Iterator");
240+
configureConstantsPropertiesAndFunctions(windowConfig, window);
244241

245-
// strange but this is the reality for browsers
246-
// because there will be still some sites using this for browser detection the property is
247-
// set to null
248-
// https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browsers
249-
// https://bugzilla.mozilla.org/show_bug.cgi?id=1442035
250-
if (browserVersion.hasFeature(JS_WINDOW_INSTALL_TRIGGER_NULL)) {
251-
window.put("InstallTrigger", window, null);
252-
}
253-
254-
final Map<Class<? extends Scriptable>, Scriptable> prototypes = new HashMap<>();
255-
final Map<String, Scriptable> prototypesPerJSName = new HashMap<>();
242+
final HtmlUnitScriptable windowPrototype = configureClass(windowConfig, window, browserVersion);
243+
window.setPrototype(windowPrototype);
244+
prototypes.put(windowConfig.getHostClass(), windowPrototype);
245+
prototypesPerJSName.put(windowConfig.getClassName(), windowPrototype);
256246

257247
// setup the prototypes
258248
for (final ClassConfiguration config : jsConfig_.getAll()) {
259-
final boolean isWindow = windowConfig == config;
260-
if (isWindow) {
261-
configureConstantsPropertiesAndFunctions(config, window);
262-
263-
final HtmlUnitScriptable prototype = configureClass(config, window, browserVersion);
264-
prototypesPerJSName.put(config.getClassName(), prototype);
265-
}
266-
else {
249+
if (windowConfig != config) {
267250
final HtmlUnitScriptable prototype = configureClass(config, window, browserVersion);
268251
if (config.isJsObject()) {
269252
// Place object with prototype property in Window scope
@@ -305,7 +288,7 @@ private void init(final WebWindow webWindow, final Page page, final Context cont
305288
if (prototype != null && config.isJsObject()) {
306289
if (jsConstructor == null) {
307290
final ScriptableObject constructor;
308-
if ("Window".equals(jsClassName)) {
291+
if (windowConfig == config) {
309292
constructor = (ScriptableObject) ScriptableObject.getProperty(window, "constructor");
310293
}
311294
else {
@@ -317,7 +300,7 @@ private void init(final WebWindow webWindow, final Page page, final Context cont
317300
}
318301
else {
319302
final BaseFunction function;
320-
if ("Window".equals(jsClassName)) {
303+
if (windowConfig == config) {
321304
function = (BaseFunction) ScriptableObject.getProperty(window, "constructor");
322305
}
323306
else {
@@ -348,7 +331,18 @@ private void init(final WebWindow webWindow, final Page page, final Context cont
348331
}
349332
}
350333
}
351-
window.setPrototype(prototypesPerJSName.get(Window.class.getSimpleName()));
334+
335+
URLSearchParams.NativeParamsIterator.init(window, "URLSearchParams Iterator");
336+
FormData.FormDataIterator.init(window, "FormData Iterator");
337+
338+
// strange but this is the reality for browsers
339+
// because there will be still some sites using this for browser detection the property is
340+
// set to null
341+
// https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browsers
342+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1442035
343+
if (browserVersion.hasFeature(JS_WINDOW_INSTALL_TRIGGER_NULL)) {
344+
window.put("InstallTrigger", window, null);
345+
}
352346

353347
// special handling for image/option
354348
final Method imageCtor = HTMLImageElement.class.getDeclaredMethod("jsConstructorImage");

0 commit comments

Comments
 (0)