Skip to content

Commit e16f5a9

Browse files
committed
improved toJSON() impl
1 parent 0fd8d70 commit e16f5a9

File tree

9 files changed

+64
-71
lines changed

9 files changed

+64
-71
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.htmlunit.corejs.javascript.NativeArray;
4848
import org.htmlunit.corejs.javascript.NativeArrayIterator;
4949
import org.htmlunit.corejs.javascript.NativeConsole;
50+
import org.htmlunit.corejs.javascript.NativeObject;
5051
import org.htmlunit.corejs.javascript.RhinoException;
5152
import org.htmlunit.corejs.javascript.Script;
5253
import org.htmlunit.corejs.javascript.ScriptRuntime;
@@ -1273,6 +1274,20 @@ public static Scriptable newObject(final Scriptable scope, final String construc
12731274
return ScriptRuntime.newObject(Context.getCurrentContext(), scope, constructorName, args);
12741275
}
12751276

1277+
/**
1278+
* Create a new JavaScript object.
1279+
*
1280+
* <p>Equivalent to evaluating "new Object()".
1281+
*
1282+
* @param scope the scope to search for the constructor and to evaluate against
1283+
* @return the new object
1284+
*/
1285+
public static Scriptable newObject(final Scriptable scope) {
1286+
final NativeObject result = new NativeObject();
1287+
ScriptRuntime.setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Object);
1288+
return result;
1289+
}
1290+
12761291
/**
12771292
* Create an array with a specified initial length.
12781293
*

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
*/
1515
package org.htmlunit.javascript.host;
1616

17-
import org.htmlunit.corejs.javascript.Context;
1817
import org.htmlunit.javascript.HtmlUnitScriptable;
18+
import org.htmlunit.javascript.JavaScriptEngine;
1919

2020
/**
2121
* A JavaScript object for {@code Netscape}.
2222
*
2323
* @author Marc Guillemot
24+
* @author Ronald Brill
2425
*/
2526
public class Netscape extends HtmlUnitScriptable {
2627

@@ -29,7 +30,7 @@ public class Netscape extends HtmlUnitScriptable {
2930
setParentScope(window);
3031

3132
// simply put "new Object()" for property "security"
32-
put("security", this, Context.getCurrentContext().newObject(window));
33+
put("security", this, JavaScriptEngine.newObject(window));
3334
}
3435

3536
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,7 @@ public void initialize(final WebWindow webWindow, final Page pageToEnclose) {
794794
location_.initialize(this, pageToEnclose);
795795

796796
// like a JS new Object()
797-
final Context ctx = Context.getCurrentContext();
798-
controllers_ = ctx.newObject(this);
797+
controllers_ = JavaScriptEngine.newObject(this);
799798

800799
if (webWindow_ instanceof TopLevelWindow) {
801800
final WebWindow opener = ((TopLevelWindow) webWindow_).getOpener();

src/main/java/org/htmlunit/javascript/host/dom/DOMMatrixReadOnly.java

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import org.htmlunit.corejs.javascript.FunctionObject;
2222
import org.htmlunit.corejs.javascript.NativeArray;
2323
import org.htmlunit.corejs.javascript.Scriptable;
24-
import org.htmlunit.corejs.javascript.json.JsonParser;
25-
import org.htmlunit.corejs.javascript.json.JsonParser.ParseException;
2624
import org.htmlunit.corejs.javascript.typedarrays.NativeFloat32Array;
2725
import org.htmlunit.corejs.javascript.typedarrays.NativeFloat64Array;
2826
import org.htmlunit.javascript.HtmlUnitScriptable;
@@ -1085,47 +1083,38 @@ private static StringBuilder appendDouble(final StringBuilder builder, final dou
10851083
* within matrix() or matrix3d() function syntax.
10861084
*/
10871085
@JsxFunction
1088-
public Object toJSON() {
1089-
final String jsonString = new StringBuilder()
1090-
.append("{\"a\":").append(m11_)
1091-
.append(", \"b\":").append(m12_)
1092-
.append(", \"c\":").append(m21_)
1093-
.append(", \"d\":").append(m22_)
1094-
.append(", \"e\":").append(m41_)
1095-
.append(", \"f\":").append(m42_)
1096-
1097-
.append(", \"m11\":").append(m11_)
1098-
.append(", \"m12\":").append(m12_)
1099-
.append(", \"m13\":").append(m13_)
1100-
.append(", \"m14\":").append(m14_)
1101-
1102-
.append(", \"m21\":").append(m21_)
1103-
.append(", \"m22\":").append(m22_)
1104-
.append(", \"m23\":").append(m23_)
1105-
.append(", \"m24\":").append(m24_)
1106-
1107-
.append(", \"m31\":").append(m31_)
1108-
.append(", \"m32\":").append(m32_)
1109-
.append(", \"m33\":").append(m33_)
1110-
.append(", \"m34\":").append(m34_)
1111-
1112-
.append(", \"m41\":").append(m41_)
1113-
.append(", \"m42\":").append(m42_)
1114-
.append(", \"m43\":").append(m43_)
1115-
.append(", \"m44\":").append(m44_)
1116-
1117-
.append(", \"is2D\":").append(is2D_)
1118-
.append(", \"isIdentity\":").append(getIsIdentity())
1119-
1120-
.append('}').toString();
1121-
try {
1122-
return new JsonParser(Context.getCurrentContext(), getParentScope()).parseValue(jsonString);
1123-
}
1124-
catch (final ParseException e) {
1125-
if (LOG.isWarnEnabled()) {
1126-
LOG.warn("Failed parsingJSON '" + jsonString + "'", e);
1127-
}
1128-
}
1129-
return null;
1086+
public Scriptable toJSON() {
1087+
final Scriptable json = JavaScriptEngine.newObject(getParentScope());
1088+
json.put("a", json, m11_);
1089+
json.put("b", json, m12_);
1090+
json.put("c", json, m21_);
1091+
json.put("d", json, m22_);
1092+
json.put("e", json, m41_);
1093+
json.put("f", json, m42_);
1094+
1095+
json.put("m11", json, m11_);
1096+
json.put("m12", json, m12_);
1097+
json.put("m13", json, m13_);
1098+
json.put("m14", json, m14_);
1099+
1100+
json.put("m21", json, m21_);
1101+
json.put("m22", json, m22_);
1102+
json.put("m23", json, m23_);
1103+
json.put("m24", json, m24_);
1104+
1105+
json.put("m31", json, m31_);
1106+
json.put("m32", json, m32_);
1107+
json.put("m33", json, m33_);
1108+
json.put("m34", json, m34_);
1109+
1110+
json.put("m41", json, m41_);
1111+
json.put("m42", json, m42_);
1112+
json.put("m43", json, m43_);
1113+
json.put("m44", json, m44_);
1114+
1115+
json.put("is2D", json, is2D_);
1116+
json.put("isIdentity", json, getIsIdentity());
1117+
1118+
return json;
11301119
}
11311120
}

src/main/java/org/htmlunit/javascript/host/intl/DateTimeFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public String format(final Object object) {
294294
@JsxFunction
295295
public Scriptable resolvedOptions() {
296296
final Context cx = Context.getCurrentContext();
297-
final Scriptable options = cx.newObject(getParentScope());
297+
final Scriptable options = JavaScriptEngine.newObject(getParentScope());
298298
options.put("timeZone", options, cx.getTimeZone().getID());
299299

300300
if (StringUtils.isEmptyOrNull(formatter_.locale_)) {

src/main/java/org/htmlunit/javascript/host/intl/NumberFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public String format(final Object object) {
207207
*/
208208
@JsxFunction
209209
public Scriptable resolvedOptions() {
210-
return Context.getCurrentContext().newObject(getParentScope());
210+
return JavaScriptEngine.newObject(getParentScope());
211211
}
212212

213213
/**

src/main/java/org/htmlunit/javascript/host/performance/PerformanceNavigation.java

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

1717
import org.apache.commons.logging.Log;
1818
import org.apache.commons.logging.LogFactory;
19-
import org.htmlunit.corejs.javascript.Context;
20-
import org.htmlunit.corejs.javascript.json.JsonParser;
21-
import org.htmlunit.corejs.javascript.json.JsonParser.ParseException;
19+
import org.htmlunit.corejs.javascript.Scriptable;
2220
import org.htmlunit.javascript.HtmlUnitScriptable;
21+
import org.htmlunit.javascript.JavaScriptEngine;
2322
import org.htmlunit.javascript.configuration.JsxClass;
2423
import org.htmlunit.javascript.configuration.JsxConstant;
2524
import org.htmlunit.javascript.configuration.JsxConstructor;
@@ -85,21 +84,11 @@ public int getRedirectCount() {
8584
*/
8685
@JsxFunction
8786
public Object toJSON() {
88-
final String jsonString = new StringBuilder()
89-
.append("{\"type\":")
90-
.append(getType())
91-
.append(", \"redirectCount\":")
92-
.append(getRedirectCount())
93-
.append('}').toString();
94-
try {
95-
return new JsonParser(Context.getCurrentContext(), getParentScope()).parseValue(jsonString);
96-
}
97-
catch (final ParseException e) {
98-
if (LOG.isWarnEnabled()) {
99-
LOG.warn("Failed parsingJSON '" + jsonString + "'", e);
100-
}
101-
}
102-
return null;
87+
final Scriptable json = JavaScriptEngine.newObject(getParentScope());
88+
json.put("type", json, getType());
89+
json.put("redirectCount", json, getRedirectCount());
90+
91+
return json;
10392
}
10493

10594
}

src/test/java/org/htmlunit/archunit/ArchitectureTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,18 @@ public boolean test(final JavaClass javaClass) {
345345
.and().doNotHaveFullName("org.htmlunit.javascript.host.Window.setTimeout(org.htmlunit.corejs.javascript.Context, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.Scriptable, [Ljava.lang.Object;, org.htmlunit.corejs.javascript.Function)")
346346
.and().doNotHaveFullName("org.htmlunit.javascript.host.css.CSSRuleList.item(int)")
347347
.and().doNotHaveFullName("org.htmlunit.javascript.host.css.StyleSheetList.item(int)")
348-
.and().doNotHaveFullName("org.htmlunit.javascript.host.dom.DOMMatrixReadOnly.toJSON()")
349348
.and().doNotHaveFullName("org.htmlunit.javascript.host.dom.NodeList.item(java.lang.Object)")
350349
.and().doNotHaveFullName("org.htmlunit.javascript.host.html.HTMLAllCollection.item(java.lang.Object)")
351350
.and().doNotHaveFullName("org.htmlunit.javascript.host.html.HTMLCollection.item(java.lang.Object)")
352351
.and().doNotHaveFullName("org.htmlunit.javascript.host.html.HTMLOptionsCollection.item(int)")
353352
.and().doNotHaveFullName("org.htmlunit.javascript.host.html.HTMLSelectElement.item(int)")
354353
.and().doNotHaveFullName("org.htmlunit.javascript.host.intl.V8BreakIterator.resolvedOptions()")
355-
.and().doNotHaveFullName("org.htmlunit.javascript.host.performance.PerformanceNavigation.toJSON()")
356354
.and().doNotHaveFullName("org.htmlunit.javascript.host.worker.DedicatedWorkerGlobalScope.setInterval(org.htmlunit.corejs.javascript.Context, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.Scriptable, [Ljava.lang.Object;, org.htmlunit.corejs.javascript.Function)")
357355
.and().doNotHaveFullName("org.htmlunit.javascript.host.worker.DedicatedWorkerGlobalScope.setTimeout(org.htmlunit.corejs.javascript.Context, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.Scriptable, [Ljava.lang.Object;, org.htmlunit.corejs.javascript.Function)")
358356
.and().doNotHaveFullName("org.htmlunit.javascript.host.xml.XSLTProcessor.getParameter(java.lang.String, java.lang.String)")
359357

358+
.and().doNotHaveFullName("org.htmlunit.javascript.host.performance.PerformanceNavigation.toJSON()")
359+
360360
.should().haveRawReturnType(String.class)
361361
.orShould().haveRawReturnType("int")
362362
.orShould().haveRawReturnType("long")

src/test/java/org/htmlunit/javascript/host/URLTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ public void testToString() throws Exception {
11961196
*/
11971197
@Test
11981198
@Alerts("https://developer.mozilla.org/")
1199-
public void testToJSON() throws Exception {
1199+
public void toJSON() throws Exception {
12001200
final String html = DOCTYPE_HTML
12011201
+ "<html><body>\n"
12021202
+ "<script>\n"

0 commit comments

Comments
 (0)