|
14 | 14 |
|
15 | 15 | package org.mozilla.javascript.tools.shell; |
16 | 16 |
|
17 | | -import java.util.Map; |
| 17 | +import org.mozilla.javascript.Context; |
| 18 | +import org.mozilla.javascript.LambdaConstructor; |
| 19 | +import org.mozilla.javascript.NativeArray; |
18 | 20 | import org.mozilla.javascript.ScriptRuntime; |
19 | 21 | import org.mozilla.javascript.Scriptable; |
20 | 22 | import org.mozilla.javascript.ScriptableObject; |
21 | 23 |
|
| 24 | +import java.util.Map; |
| 25 | + |
22 | 26 | /** |
23 | 27 | * Environment, intended to be instantiated at global scope, provides a natural way to access System |
24 | 28 | * properties from JavaScript. |
25 | 29 | * |
26 | 30 | * @author Patrick C. Beard |
27 | 31 | */ |
28 | 32 | 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 | + } |
97 | 108 | } |
0 commit comments