Skip to content

Commit e8059a8

Browse files
committed
refatore to use only one loop
1 parent 5eb94bf commit e8059a8

1 file changed

Lines changed: 41 additions & 42 deletions

File tree

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

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -285,48 +285,47 @@ public static void configureScope(final HtmlUnitScriptable jsScope,
285285

286286
final Scriptable objectPrototype = ScriptableObject.getObjectPrototype(jsScope);
287287

288-
// setup the prototypes
288+
final Map<String, Function> ctorPrototypesPerJSName = new HashMap<>();
289289
for (final ClassConfiguration config : jsConfig.getAll()) {
290+
final String jsClassName = config.getClassName();
291+
292+
// setup the prototypes
290293
if (config == scopeConfig) {
291-
final Scriptable prototype = prototypesPerJSName.get(config.getClassName());
292-
if (!StringUtils.isEmpty(config.getExtendedClassName())) {
293-
final Scriptable parentPrototype = prototypesPerJSName.get(config.getExtendedClassName());
294-
prototype.setPrototype(parentPrototype);
294+
final Scriptable prototype = prototypesPerJSName.get(jsClassName);
295+
if (StringUtils.isEmpty(config.getExtendedClassName())) {
296+
prototype.setPrototype(objectPrototype);
295297
}
296298
else {
297-
prototype.setPrototype(objectPrototype);
299+
final Scriptable parentPrototype = prototypesPerJSName.get(config.getExtendedClassName());
300+
prototype.setPrototype(parentPrototype);
298301
}
299302
}
300303
else {
301-
final HtmlUnitScriptable prototype = configureClass(config, jsScope);
304+
final HtmlUnitScriptable classPrototype = configureClass(config, jsScope);
302305
if (config.isJsObject()) {
303306
// Place object with prototype property in Window scope
304307
final HtmlUnitScriptable obj = config.getHostClass().getDeclaredConstructor().newInstance();
305-
prototype.defineProperty("__proto__", prototype, ScriptableObject.DONTENUM);
306-
obj.defineProperty("prototype", prototype, ScriptableObject.DONTENUM); // but not setPrototype!
308+
classPrototype.defineProperty("__proto__", classPrototype, ScriptableObject.DONTENUM);
309+
obj.defineProperty("prototype", classPrototype, ScriptableObject.DONTENUM); // but not setPrototype!
307310
obj.setParentScope(jsScope);
308-
obj.setClassName(config.getClassName());
311+
obj.setClassName(jsClassName);
309312
ScriptableObject.defineProperty(jsScope, obj.getClassName(), obj, ScriptableObject.DONTENUM);
310313
// this obj won't have prototype, constants need to be configured on it again
311314
configureConstants(config, obj);
312315
}
313-
prototypes.put(config.getHostClass(), prototype);
314-
prototypesPerJSName.put(config.getClassName(), prototype);
316+
prototypes.put(config.getHostClass(), classPrototype);
317+
prototypesPerJSName.put(jsClassName, classPrototype);
315318

316-
if (!StringUtils.isEmpty(config.getExtendedClassName())) {
317-
final Scriptable parentPrototype = prototypesPerJSName.get(config.getExtendedClassName());
318-
prototype.setPrototype(parentPrototype);
319+
if (StringUtils.isEmpty(config.getExtendedClassName())) {
320+
classPrototype.setPrototype(objectPrototype);
319321
}
320322
else {
321-
prototype.setPrototype(objectPrototype);
323+
final Scriptable parentPrototype = prototypesPerJSName.get(config.getExtendedClassName());
324+
classPrototype.setPrototype(parentPrototype);
322325
}
323326
}
324-
}
325327

326-
// setup constructors
327-
final Map<String, Function> ctorPrototypesPerJSName = new HashMap<>();
328-
for (final ClassConfiguration config : jsConfig.getAll()) {
329-
final String jsClassName = config.getClassName();
328+
// setup constructors
330329
final Scriptable prototype = prototypesPerJSName.get(jsClassName);
331330

332331
if (config == scopeConfig) {
@@ -338,28 +337,28 @@ public static void configureScope(final HtmlUnitScriptable jsScope,
338337
final Scriptable parentPrototype = ctorPrototypesPerJSName.get(config.getExtendedClassName());
339338
scopeContructorFunctionObject.setPrototype(parentPrototype);
340339
}
341-
continue;
342340
}
343-
344-
final Map.Entry<String, Member> jsConstructor = config.getJsConstructor();
345-
if (prototype != null && config.isJsObject()) {
346-
if (jsConstructor == null) {
347-
final ScriptableObject constructor = config.getHostClass().getDeclaredConstructor().newInstance();
348-
((HtmlUnitScriptable) constructor).setClassName(jsClassName);
349-
defineConstructor(jsScope, prototype, constructor);
350-
configureConstantsStaticPropertiesAndStaticFunctions(config, constructor);
351-
}
352-
else {
353-
final FunctionObject function = new FunctionObject(jsConstructor.getKey(), jsConstructor.getValue(), jsScope);
354-
ctorPrototypesPerJSName.put(jsClassName, function);
355-
356-
addAsConstructorAndAlias(function, jsScope, prototype, config);
357-
configureConstantsStaticPropertiesAndStaticFunctions(config, function);
358-
359-
// adjust prototype if needed
360-
if (!StringUtils.isEmpty(config.getExtendedClassName())) {
361-
final Scriptable parentPrototype = ctorPrototypesPerJSName.get(config.getExtendedClassName());
362-
function.setPrototype(parentPrototype);
341+
else {
342+
final Map.Entry<String, Member> jsConstructor = config.getJsConstructor();
343+
if (prototype != null && config.isJsObject()) {
344+
if (jsConstructor == null) {
345+
final ScriptableObject constructor = config.getHostClass().getDeclaredConstructor().newInstance();
346+
((HtmlUnitScriptable) constructor).setClassName(jsClassName);
347+
defineConstructor(jsScope, prototype, constructor);
348+
configureConstantsStaticPropertiesAndStaticFunctions(config, constructor);
349+
}
350+
else {
351+
final FunctionObject function = new FunctionObject(jsConstructor.getKey(), jsConstructor.getValue(), jsScope);
352+
ctorPrototypesPerJSName.put(jsClassName, function);
353+
354+
addAsConstructorAndAlias(function, jsScope, prototype, config);
355+
configureConstantsStaticPropertiesAndStaticFunctions(config, function);
356+
357+
// adjust prototype if needed
358+
if (!StringUtils.isEmpty(config.getExtendedClassName())) {
359+
final Scriptable parentPrototype = ctorPrototypesPerJSName.get(config.getExtendedClassName());
360+
function.setPrototype(parentPrototype);
361+
}
363362
}
364363
}
365364
}

0 commit comments

Comments
 (0)