Skip to content

Commit 024d2a7

Browse files
committed
ShellTest/Environment: Do not use reflection for init
1 parent c6df71d commit 024d2a7

File tree

3 files changed

+89
-77
lines changed

3 files changed

+89
-77
lines changed

rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Environment.java

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,84 +14,95 @@
1414

1515
package org.mozilla.javascript.tools.shell;
1616

17-
import java.util.Map;
17+
import org.mozilla.javascript.Context;
18+
import org.mozilla.javascript.LambdaConstructor;
19+
import org.mozilla.javascript.NativeArray;
1820
import org.mozilla.javascript.ScriptRuntime;
1921
import org.mozilla.javascript.Scriptable;
2022
import org.mozilla.javascript.ScriptableObject;
2123

24+
import java.util.Map;
25+
2226
/**
2327
* Environment, intended to be instantiated at global scope, provides a natural way to access System
2428
* properties from JavaScript.
2529
*
2630
* @author Patrick C. Beard
2731
*/
2832
public class Environment extends ScriptableObject {
29-
static final long serialVersionUID = -430727378460177065L;
30-
31-
private Environment thePrototypeInstance = null;
32-
33-
public static void defineClass(ScriptableObject scope) {
34-
try {
35-
ScriptableObject.defineClass(scope, Environment.class);
36-
} catch (Exception e) {
37-
throw new Error(e.getMessage());
38-
}
39-
}
40-
41-
@Override
42-
public String getClassName() {
43-
return "Environment";
44-
}
45-
46-
public Environment() {
47-
if (thePrototypeInstance == null) thePrototypeInstance = this;
48-
}
49-
50-
public Environment(ScriptableObject scope) {
51-
setParentScope(scope);
52-
Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment");
53-
if (ctor != null && ctor instanceof Scriptable) {
54-
Scriptable s = (Scriptable) ctor;
55-
setPrototype((Scriptable) s.get("prototype", s));
56-
}
57-
}
58-
59-
@Override
60-
public boolean has(String name, Scriptable start) {
61-
if (this == thePrototypeInstance) return super.has(name, start);
62-
63-
return (System.getProperty(name) != null);
64-
}
65-
66-
@Override
67-
public Object get(String name, Scriptable start) {
68-
if (this == thePrototypeInstance) return super.get(name, start);
69-
70-
String result = System.getProperty(name);
71-
if (result != null) return ScriptRuntime.toObject(getParentScope(), result);
72-
else return Scriptable.NOT_FOUND;
73-
}
74-
75-
@Override
76-
public void put(String name, Scriptable start, Object value) {
77-
if (this == thePrototypeInstance) super.put(name, start, value);
78-
else System.getProperties().put(name, ScriptRuntime.toString(value));
79-
}
80-
81-
private Object[] collectIds() {
82-
Map<Object, Object> props = System.getProperties();
83-
return props.keySet().toArray();
84-
}
85-
86-
@Override
87-
public Object[] getIds() {
88-
if (this == thePrototypeInstance) return super.getIds();
89-
return collectIds();
90-
}
91-
92-
@Override
93-
public Object[] getAllIds() {
94-
if (this == thePrototypeInstance) return super.getAllIds();
95-
return collectIds();
96-
}
33+
static final long serialVersionUID = -430727378460177065L;
34+
35+
private Environment thePrototypeInstance = null;
36+
private static final String CLASS_NAME = "Environment";
37+
38+
static Object init(Context cx, ScriptableObject scope, boolean sealed) {
39+
LambdaConstructor ctor = new LambdaConstructor(scope, CLASS_NAME, 1, Environment::new);
40+
41+
var proto = new NativeArray(0);
42+
ctor.setPrototypeScriptable(proto);
43+
Environment environment = new Environment(cx, scope, new Object[0]);
44+
scope.defineProperty("environment", environment, ScriptableObject.DONTENUM);
45+
if (sealed) {
46+
ctor.sealObject();
47+
((ScriptableObject) ctor.getPrototypeProperty()).sealObject();
48+
}
49+
return ctor;
50+
}
51+
52+
@Override
53+
public String getClassName() {
54+
return CLASS_NAME;
55+
}
56+
57+
public Environment() {
58+
if (thePrototypeInstance == null) thePrototypeInstance = this;
59+
}
60+
61+
public Environment(Context cx, Scriptable scope, Object[] args) {
62+
setParentScope(scope);
63+
Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment");
64+
if (ctor != null && ctor instanceof Scriptable) {
65+
Scriptable s = (Scriptable) ctor;
66+
setPrototype((Scriptable) s.get("prototype", s));
67+
}
68+
}
69+
70+
@Override
71+
public boolean has(String name, Scriptable start) {
72+
if (this == thePrototypeInstance) return super.has(name, start);
73+
74+
return (System.getProperty(name) != null);
75+
}
76+
77+
@Override
78+
public Object get(String name, Scriptable start) {
79+
if (this == thePrototypeInstance) return super.get(name, start);
80+
81+
String result = System.getProperty(name);
82+
if (result != null) return ScriptRuntime.toObject(getParentScope(), result);
83+
else return Scriptable.NOT_FOUND;
84+
}
85+
86+
@Override
87+
public void put(String name, Scriptable start, Object value) {
88+
if (this == thePrototypeInstance) super.put(name, start, value);
89+
else System.getProperties().put(name, ScriptRuntime.toString(value));
90+
}
91+
92+
private Object[] collectIds() {
93+
Map<Object, Object> props = System.getProperties();
94+
return props.keySet().toArray();
95+
}
96+
97+
@Override
98+
public Object[] getIds() {
99+
if (this == thePrototypeInstance) return super.getIds();
100+
return collectIds();
101+
}
102+
103+
@Override
104+
public Object[] getAllIds() {
105+
if (this == thePrototypeInstance) return super.getAllIds();
106+
return collectIds();
107+
}
97108
}

rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Global.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ public void init(Context cx) {
245245

246246
// Set up "environment" in the global scope to provide access to the
247247
// System environment variables.
248-
Environment.defineClass(this);
249-
Environment environment = new Environment(this);
250-
defineProperty("environment", environment, ScriptableObject.DONTENUM);
248+
Environment.init(cx,this, false);
251249

252250
history = (NativeArray) cx.newArray(this, 0);
253251
defineProperty("history", history, ScriptableObject.DONTENUM);

tests/src/test/java/org/mozilla/javascript/drivers/ShellTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,15 @@ public static void runNoFork(
428428
global.init(cx);
429429

430430
// invoke after init(...) to make sure ClassCache is available for FunctionObject
431-
global.defineFunctionProperties(
432-
new String[] {"options"},
433-
ShellTest.class,
431+
global.defineProperty(
432+
global,
433+
"options",
434+
0,
435+
ShellTest::options,
434436
ScriptableObject.DONTENUM
435437
| ScriptableObject.PERMANENT
436-
| ScriptableObject.READONLY);
438+
| ScriptableObject.READONLY,
439+
ScriptableObject.DONTENUM | ScriptableObject.READONLY);
437440

438441
try {
439442
runFileIfExists(
@@ -491,7 +494,7 @@ public static void runNoFork(
491494
// It looks like this toggles jit compiler mode in spidermonkey
492495
// when called with "jit" as argument. Our version is a no-op
493496
// and returns an empty string.
494-
public static String options() {
497+
public static String options(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
495498
return "";
496499
}
497500
}

0 commit comments

Comments
 (0)