Skip to content

Commit e326961

Browse files
authored
revert the change from b5e7f85 and add some tests (#2226)
1 parent 4778f31 commit e326961

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ public static ScriptableObject initSafeStandardObjects(
236236
NativeStringIterator.init(scope, sealed);
237237
registerRegExp(cx, scope, sealed);
238238

239+
NativeJavaObject.init(scope, sealed);
240+
NativeJavaMap.init(scope, sealed);
241+
239242
// define lazy-loaded properties using their class name
240243
// Depends on the old reflection-based lazy loading mechanism
241244
// to property initialize the prototype.
@@ -299,9 +302,6 @@ public static ScriptableObject initStandardObjects(
299302
Context cx, ScriptableObject scope, boolean sealed) {
300303
ScriptableObject s = initSafeStandardObjects(cx, scope, sealed);
301304

302-
NativeJavaObject.init(s, sealed);
303-
NativeJavaMap.init(s, sealed);
304-
305305
// These depend on the legacy initialization behavior of the lazy loading mechanism
306306
new LazilyLoadedCtor(
307307
s, "Packages", "org.mozilla.javascript.NativeJavaTopPackage", sealed, true);

rhino/src/test/java/org/mozilla/javascript/tests/InitializationTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Set;
58
import org.junit.Test;
69
import org.mozilla.javascript.Context;
710
import org.mozilla.javascript.ScriptableObject;
@@ -37,6 +40,88 @@ public void safeStandard() {
3740
}
3841
}
3942

43+
@Test
44+
public void safeStandardSupportsStringFromContext() {
45+
String code =
46+
"let res = '';\n"
47+
+ "res += str[0];\n"
48+
+ "res += str.length;\n"
49+
+ "res += ' ';\n"
50+
+ "res += str;\n"
51+
+ "res;";
52+
53+
try (Context cx = Context.enter()) {
54+
ScriptableObject root = cx.initSafeStandardObjects();
55+
root.put("str", root, "Rhino");
56+
Object result = cx.evaluateString(root, code, "test", 1, null);
57+
assertEquals("R5 Rhino", result);
58+
}
59+
}
60+
61+
@Test
62+
public void safeStandardSupportsArrayFromContext() {
63+
String code =
64+
"let res = '';\n"
65+
+ "res += arr[0];\n"
66+
+ "res += arr.length;\n"
67+
+ "res += ' ';\n"
68+
+ "for (let elem in arr) { res += arr[elem]; }\n"
69+
+ "res += ' ';\n"
70+
+ "for (let elem of arr) { res += elem; }\n"
71+
+ "res;";
72+
73+
try (Context cx = Context.enter()) {
74+
ScriptableObject root = cx.initSafeStandardObjects();
75+
root.put("arr", root, new String[] {"a", "b", "d"});
76+
Object result = cx.evaluateString(root, code, "test", 1, null);
77+
assertEquals("a3 abd abd", result);
78+
}
79+
}
80+
81+
@Test
82+
public void safeStandardSupportsListFromContext() {
83+
String code =
84+
"let res = '';\n"
85+
+ "res += lst[0];\n"
86+
+ "res += lst.length;\n"
87+
+ "res += ' ';\n"
88+
+ "for (let elem in lst) { res += lst[elem]; }\n"
89+
+ "res += ' ';\n"
90+
+ "for (let elem of lst) { res += elem; }\n"
91+
+ "res;";
92+
93+
try (Context cx = Context.enter()) {
94+
ScriptableObject root = cx.initSafeStandardObjects();
95+
root.put("lst", root, List.of("a", "b", "c"));
96+
Object result = cx.evaluateString(root, code, "test", 1, null);
97+
assertEquals("a3 abc abc", result);
98+
}
99+
}
100+
101+
@Test
102+
public void safeStandardSupportsMapFromContext() {
103+
String code = "let res = '';\n" + "for (let elem of mp) { res += elem; }\n" + "res;";
104+
105+
try (Context cx = Context.enter()) {
106+
ScriptableObject root = cx.initSafeStandardObjects();
107+
root.put("mp", root, Map.of("k0", "v0"));
108+
Object result = cx.evaluateString(root, code, "test", 1, null);
109+
assertEquals("k0,v0", result);
110+
}
111+
}
112+
113+
@Test
114+
public void safeStandardSupportsSetFromContext() {
115+
String code = "let res = '';\n" + "for (let elem of mp) { res += elem; }\n" + "res;";
116+
117+
try (Context cx = Context.enter()) {
118+
ScriptableObject root = cx.initSafeStandardObjects();
119+
root.put("mp", root, Set.of("v0"));
120+
Object result = cx.evaluateString(root, code, "test", 1, null);
121+
assertEquals("v0", result);
122+
}
123+
}
124+
40125
@Test
41126
public void standardSealed() {
42127
try (Context cx = Context.enter()) {

0 commit comments

Comments
 (0)