81f36ea29b222dc1aa738442d5b338b9aa160f75
[org.ibex.core.git] / src / org / xwt / util / JSObject.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.util;
3
4 import org.mozilla.javascript.*;
5 import java.net.*;
6 import java.io.*;
7 import java.util.*;
8 import org.xwt.*;
9
10 /** Simple implementation of a JavaScript host object.
11  *
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.
15  *
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.
22  */
23 public class JSObject implements Scriptable {
24
25     // Static Data ///////////////////////////////////////////////////////////////////////    
26
27     /** helper; retrieves the 'source code filename' (usually the nodeName) of the currently-executing function in this thread */
28     public static String getCurrentFunctionSourceName() {
29         Function cf = Context.getCurrentContext().currentFunction;
30         if (cf == null) return null;
31         return (cf instanceof InterpretedFunction) ?
32             ((InterpretedFunction)cf).getSourceName() : ((InterpretedScript)cf).getSourceName();
33     }
34
35     /** cache for nodeNameToPackageName(); prevents creation of zillions of String objects */
36     private static Hash nodeNameToPackageNameHash = new Hash(1000, 2);
37
38     /** converts a nodeName (source code filename) into the 'package name' (resource name of its directory) */
39     private static String nodeNameToPackageName(String nodeName) {
40         if (nodeName == null) return null;
41         String ret = (String)nodeNameToPackageNameHash.get(nodeName);
42         if (ret != null) return ret;
43         ret = nodeName;
44         for(int i=0; i<ret.length() - 1; i++)
45             if (ret.charAt(i) == '.' && (ret.charAt(i + 1) == '_' || Character.isDigit(ret.charAt(i + 1)))) {
46                 ret = ret.substring(0, i);
47                 break;
48             }
49         if (ret.lastIndexOf('.') != -1)
50             ret = ret.substring(0, ret.lastIndexOf('.'));
51         ret = ret.intern();
52         nodeNameToPackageNameHash.put(nodeName, ret);
53         return ret;
54     }
55     
56     /** this holds a scope which has been initialized with the standard ECMA objects */
57     public static Scriptable defaultObjects = Context.enter().initStandardObjects(null);
58
59     // Instance Data ///////////////////////////////////////////////////////////////////////    
60
61     /** The properties that holds our scope */
62     protected Hash properties = null;
63     
64     /** If true, put()s and delete()s are prohibited */
65     boolean sealed = false;
66
67     /** If true, variables declared with 'var' become visible only to scripts from the same
68      *  directory/package as the one which created the variable
69      */
70     boolean privateVars = false;
71
72     public JSObject() { }
73     public JSObject(boolean privateVars) { this.privateVars = privateVars; }
74
75     public Object get(String name, Scriptable start) {
76         if (name == null || name.equals("") || properties == null) return null;
77
78         Object ret = properties.get(name, nodeNameToPackageName(getCurrentFunctionSourceName()));
79         if (ret == NOT_FOUND) return null;
80         if (ret != null) return ret;
81         return properties.get(name, null);
82     }
83
84     /** returns the package-private variable <tt>name</tt> associated with <tt>nodeName</tt> */
85     public Object getPrivately(String name, String nodeName) {
86         return properties == null ? null : properties.get(name, nodeNameToPackageName(nodeName));
87     }
88
89     /** puts a property as a private variable, accessible only to scripts in the source code file <tt>nodeName</tt> */
90     public void putPrivately(String name, Object value, String nodeName) {
91         if (!privateVars) {
92             putGlobally(name, null, value);
93             return;
94         }
95         if (properties == null) properties = new Hash();
96
97         // we use NOT_FOUND to mark a variable which exists as a script-local, yet has a value of null
98         if (value == null) value = NOT_FOUND;
99         properties.put(name, nodeNameToPackageName(nodeName), value);
100     }
101
102     /** forces a property to be put globally -- accessible to all scripts */
103     public void putGlobally(String name, Scriptable start, Object value) {
104         if (name == null || name.equals("")) return;
105         if (properties == null) properties = new Hash();
106         properties.put(name, null, value);
107     }
108
109     /** if a put is made directly to us (rather than cascading up to Top), it must be a var-declaration */
110     public void put(String name, Scriptable start, Object value) {
111         if (sealed) return;
112         if (name == null || name.equals("")) return;
113         putPrivately(name, value, getCurrentFunctionSourceName());
114     }
115     
116     /** if privateVars is enabled, we always return false, to see if the put propagates up to Top */
117     public boolean has(String name, Scriptable start) {
118         if (!privateVars) return properties != null && properties.get(name) != null;
119         Top.lastByThread.put(Thread.currentThread(), this);
120         return false;
121     }
122     
123     public Object[] getIds() {
124         if (properties == null) return new Object[] { };
125         else return properties.keys();
126     }
127     
128     /** 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 */
129     private static class Top implements Scriptable {
130         
131         /** Last JSObject to perform a has(), keyed by thread.
132          *  Assumes that every Top.has() is immediately preceded by a JSObject.has() in the same thread
133          */
134         public static Hash lastByThread = new Hash();
135         
136         public static Top singleton = new Top();
137         private Top() { }
138         
139         public boolean has(String name, Scriptable start) { return true; }
140         public void put(String name, Scriptable start, Object value) {
141             JSObject last = (JSObject)lastByThread.get(Thread.currentThread());
142             if (last.properties != null && last.properties.get(name, nodeNameToPackageName(getCurrentFunctionSourceName())) != null)
143                 last.put(name, start, value);
144             else last.putGlobally(name, start, value);
145         }
146
147         public Object get(String name, Scriptable start) {
148
149             // hack since Rhino needs to be able to grab these functions to create new objects
150             if (name.equals("Object")) return JSObject.defaultObjects.get("Object", null);
151             if (name.equals("Array")) return JSObject.defaultObjects.get("Array", null);
152             if (name.equals("Function")) return JSObject.defaultObjects.get("Function", null);
153             if (name.equals("TypeError")) return JSObject.defaultObjects.get("TypeError", null);
154             return ((JSObject)lastByThread.get(Thread.currentThread())).get(name, start);
155         }
156
157         // Trivial methods
158         public String getClassName() { return "Top"; }
159         public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
160         public void delete(String name) { }
161         public Scriptable getParentScope() { return null; }
162         public void setParentScope(Scriptable p) { }
163         public boolean hasInstance(Scriptable value) { return false; }
164         public Scriptable getPrototype() { return null; }
165         public void setPrototype(Scriptable p) { }
166         public void delete(int i) { }
167         public Object getDefaultValue(Class hint) { return "Top"; }
168         public void put(int i, Scriptable start, Object value) { }
169         public Object get(int i, Scriptable start) { return null; }
170         public boolean has(int i, Scriptable start) { return false; }
171         public Object[] getIds() { return new Object[] { }; }
172
173     }
174
175
176     // Trivial Methods ///////////////////////////////////////////////////////////
177
178     public void setSeal(boolean doseal) { sealed = doseal; }
179     public void flush() { if (properties != null) properties.clear(); }
180     public void delete(String name) { if (Log.on) Log.log(this, "JSObject.delete() should not be reachable"); }
181     public void delete(int i) { if (Log.on) Log.log(this, "JSObject.delete() should not be reachable"); }
182     public Scriptable getParentScope() { return privateVars ? Top.singleton : null; }
183     public void setParentScope(Scriptable p) { }
184     public boolean hasInstance(Scriptable value) { return false; }
185     public Scriptable getPrototype() { return null; }
186     public void setPrototype(Scriptable p) { }
187     public String getClassName() { return this.getClass().getName(); }
188     public Object getDefaultValue(Class hint) { return toString(); }
189     public void put(int i, Scriptable start, Object value) { }
190     public Object get(int i, Scriptable start) { return null; }
191     public boolean has(int i, Scriptable start) { return false; }
192
193 }
194