1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.mozilla.javascript.*;
10 /** Simple implementation of a JavaScript host object.
12 * If privateVars is set, then any "var" declaration issued on this
13 * scope will create a variable visible only to scripts from the same
14 * directory (package) as the script that made the declaration.
16 * This is implemented by making JSObject.Top the ultimate
17 * parentScope of all privateVars-style JSObjects. Puts to JSObjects
18 * which are var-decls will not climb the scope chain, and will be
19 * put directly to the JSObject. Puts which are <i>not</i> var-decls
20 * will climb the scope chain and will be put to Top, which will then
21 * re-put the value to the JSObject as a global.
23 public class JSObject implements Scriptable {
25 // Static Data ///////////////////////////////////////////////////////////////////////
27 /** helper; retrieves the 'source code filename' (usually the nodeName) of the currently-executing function in this thread */
28 public static String getCurrentFunctionSourceName() {
29 Context cx = Context.getCurrentContext();
31 if (Log.on) Log.log(JSObject.class, "Context.getCurrentContext() is null in getCurrentFunctionSourceName()");
34 Function cf = cx.currentFunction;
35 if (cf == null) return null;
36 return (cf instanceof InterpretedFunction) ?
37 ((InterpretedFunction)cf).getSourceName() : ((InterpretedScript)cf).getSourceName();
40 /** cache for nodeNameToPackageName(); prevents creation of zillions of String objects */
41 private static Hash nodeNameToPackageNameHash = new Hash(1000, 2);
43 /** converts a nodeName (source code filename) into the 'package name' (resource name of its directory) */
44 private static String nodeNameToPackageName(String nodeName) {
45 if (nodeName == null) return null;
46 String ret = (String)nodeNameToPackageNameHash.get(nodeName);
47 if (ret != null) return ret;
49 for(int i=0; i<ret.length() - 1; i++)
50 if (ret.charAt(i) == '.' && (ret.charAt(i + 1) == '_' || Character.isDigit(ret.charAt(i + 1)))) {
51 ret = ret.substring(0, i);
54 if (ret.lastIndexOf('.') != -1)
55 ret = ret.substring(0, ret.lastIndexOf('.'));
57 nodeNameToPackageNameHash.put(nodeName, ret);
61 /** this holds a scope which has been initialized with the standard ECMA objects */
62 public static Scriptable defaultObjects = Context.enter().initStandardObjects(null);
64 // Instance Data ///////////////////////////////////////////////////////////////////////
66 /** The properties that holds our scope */
67 protected Hash properties = null;
69 /** If true, put()s and delete()s are prohibited */
70 boolean sealed = false;
72 /** If true, variables declared with 'var' become visible only to scripts from the same
73 * directory/package as the one which created the variable
75 boolean privateVars = false;
78 public JSObject(boolean privateVars) { this.privateVars = privateVars; }
80 public Object get(String name, Scriptable start) {
81 if (name == null || name.equals("") || properties == null) return null;
83 Object ret = properties.get(name, nodeNameToPackageName(getCurrentFunctionSourceName()));
84 if (ret == NOT_FOUND) return null;
85 if (ret != null) return ret;
86 return properties.get(name, null);
89 /** returns the package-private variable <tt>name</tt> associated with <tt>nodeName</tt> */
90 public Object getPrivately(String name, String nodeName) {
91 return properties == null ? null : properties.get(name, nodeNameToPackageName(nodeName));
94 /** puts a property as a private variable, accessible only to scripts in the source code file <tt>nodeName</tt> */
95 public void putPrivately(String name, Object value, String nodeName) {
97 putGlobally(name, null, value);
100 if (properties == null) properties = new Hash();
102 // we use NOT_FOUND to mark a variable which exists as a script-local, yet has a value of null
103 if (value == null) value = NOT_FOUND;
104 properties.put(name, nodeNameToPackageName(nodeName), value);
107 /** forces a property to be put globally -- accessible to all scripts */
108 public void putGlobally(String name, Scriptable start, Object value) {
109 if (name == null || name.equals("")) return;
110 if (properties == null) properties = new Hash();
111 properties.put(name, null, value);
114 /** if a put is made directly to us (rather than cascading up to Top), it must be a var-declaration */
115 public void put(String name, Scriptable start, Object value) {
117 if (name == null || name.equals("")) return;
118 putPrivately(name, value, getCurrentFunctionSourceName());
121 /** if privateVars is enabled, we always return false, to see if the put propagates up to Top */
122 public boolean has(String name, Scriptable start) {
123 if (!privateVars) return properties != null && properties.get(name) != null;
124 Top.lastByThread.put(Thread.currentThread(), this);
128 public Object[] getIds() {
129 if (properties == null) return new Object[] { };
130 else return properties.keys();
133 /** Parent scope of all JSObjects; we use the has(String) method on this object to determine if a put represents a var-declaration or a plain 'ol put */
134 private static class Top implements Scriptable {
136 /** Last JSObject to perform a has(), keyed by thread.
137 * Assumes that every Top.has() is immediately preceded by a JSObject.has() in the same thread
139 public static Hash lastByThread = new Hash();
141 public static Top singleton = new Top();
144 public boolean has(String name, Scriptable start) { return true; }
145 public void put(String name, Scriptable start, Object value) {
146 JSObject last = (JSObject)lastByThread.get(Thread.currentThread());
147 if (last.properties != null && last.properties.get(name, nodeNameToPackageName(getCurrentFunctionSourceName())) != null)
148 last.put(name, start, value);
149 else last.putGlobally(name, start, value);
152 public Object get(String name, Scriptable start) {
154 // hack since Rhino needs to be able to grab these functions to create new objects
155 if (name.equals("Object")) return JSObject.defaultObjects.get("Object", null);
156 if (name.equals("Array")) return JSObject.defaultObjects.get("Array", null);
157 if (name.equals("Function")) return JSObject.defaultObjects.get("Function", null);
158 if (name.equals("TypeError")) return JSObject.defaultObjects.get("TypeError", null);
159 if (name.equals("ConversionError")) return JSObject.defaultObjects.get("ConversionError", null);
160 return ((JSObject)lastByThread.get(Thread.currentThread())).get(name, start);
164 public String getClassName() { return "Top"; }
165 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
166 public void delete(String name) { }
167 public Scriptable getParentScope() { return null; }
168 public void setParentScope(Scriptable p) { }
169 public boolean hasInstance(Scriptable value) { return false; }
170 public Scriptable getPrototype() { return null; }
171 public void setPrototype(Scriptable p) { }
172 public void delete(int i) { }
173 public Object getDefaultValue(Class hint) { return "Top"; }
174 public void put(int i, Scriptable start, Object value) { }
175 public Object get(int i, Scriptable start) { return null; }
176 public boolean has(int i, Scriptable start) { return false; }
177 public Object[] getIds() { return new Object[] { }; }
182 // Trivial Methods ///////////////////////////////////////////////////////////
184 public void setSeal(boolean doseal) { sealed = doseal; }
185 public void flush() { if (properties != null) properties.clear(); }
186 public void delete(String name) { if (Log.on) Log.log(this, "JSObject.delete() should not be reachable"); }
187 public void delete(int i) { if (Log.on) Log.log(this, "JSObject.delete() should not be reachable"); }
188 public Scriptable getParentScope() { return privateVars ? Top.singleton : null; }
189 public void setParentScope(Scriptable p) { }
190 public boolean hasInstance(Scriptable value) { return false; }
191 public Scriptable getPrototype() { return null; }
192 public void setPrototype(Scriptable p) { }
193 public String getClassName() { return this.getClass().getName(); }
194 public Object getDefaultValue(Class hint) { return toString(); }
195 public void put(int i, Scriptable start, Object value) { }
196 public Object get(int i, Scriptable start) { return null; }
197 public boolean has(int i, Scriptable start) { return false; }