From f4d0ca3c7b2e32e68e4d4823e0a5b9bad04d3500 Mon Sep 17 00:00:00 2001 From: David Matejcek Date: Fri, 15 Apr 2016 20:33:36 +0200 Subject: [PATCH 1/2] Fixed #1779 of HtmlUnit - detect cycles between elements - bug caused indefinite loop --- .../mozilla/javascript/ScriptableObject.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/org/mozilla/javascript/ScriptableObject.java b/src/org/mozilla/javascript/ScriptableObject.java index 5218a68e01..3bc2465319 100644 --- a/src/org/mozilla/javascript/ScriptableObject.java +++ b/src/org/mozilla/javascript/ScriptableObject.java @@ -2200,14 +2200,28 @@ public static Scriptable getClassPrototype(Scriptable scope, * @param obj a JavaScript object * @return the corresponding global scope */ - public static Scriptable getTopLevelScope(Scriptable obj) - { + public static Scriptable getTopLevelScope(final Scriptable obj) + { + // FIXME: some classes throw NPE in toString methods! + // FIXME: some classes throw StackOverflow in toString methods! + System.out.println("getTopLevelScope: obj.class=" + obj.getClass()); + final HashSet cache = new HashSet(); + cache.add(obj); + Scriptable temp = obj; for (;;) { - Scriptable parent = obj.getParentScope(); + final Scriptable parent = temp.getParentScope(); + System.out.println("parent: " + parent); + // prevent indefinite loop. + // FIXME: HtmlUnit's Window handles are sometimes each other's descendants. + if (cache.contains(parent)) { + return parent; +// throw new IllegalStateException("Scriptable is it's own descendant!: " + obj); + } if (parent == null) { - return obj; + return temp; } - obj = parent; + temp = parent; + cache.add(parent); } } From c07fabdab6a6a82f128865566724fe8568d1adf8 Mon Sep 17 00:00:00 2001 From: David Matejcek Date: Sat, 16 Apr 2016 23:14:52 +0200 Subject: [PATCH 2/2] Removed System.out.println from the previous commit --- src/org/mozilla/javascript/ScriptableObject.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/org/mozilla/javascript/ScriptableObject.java b/src/org/mozilla/javascript/ScriptableObject.java index 3bc2465319..2898e5c955 100644 --- a/src/org/mozilla/javascript/ScriptableObject.java +++ b/src/org/mozilla/javascript/ScriptableObject.java @@ -2204,13 +2204,11 @@ public static Scriptable getTopLevelScope(final Scriptable obj) { // FIXME: some classes throw NPE in toString methods! // FIXME: some classes throw StackOverflow in toString methods! - System.out.println("getTopLevelScope: obj.class=" + obj.getClass()); final HashSet cache = new HashSet(); cache.add(obj); Scriptable temp = obj; for (;;) { final Scriptable parent = temp.getParentScope(); - System.out.println("parent: " + parent); // prevent indefinite loop. // FIXME: HtmlUnit's Window handles are sometimes each other's descendants. if (cache.contains(parent)) {