001: package jsint;
002:
003: import java.util.Enumeration;
004: import java.util.Hashtable;
005: import java.util.HashSet;
006:
007: /**
008: A Map from Symbol to DynamicVariables.
009: **/
010:
011: public class DynamicEnvironment implements java.io.Serializable {
012: /** This is here for historical reasons. **/
013: public static boolean showJavaDotWarnings = false;
014: /** Is this locked down so it can't be changed? **/
015: private boolean lockedDown = false;
016: protected Hashtable rep;
017:
018: public DynamicEnvironment() {
019: rep = new Hashtable(100);
020: }
021:
022: public DynamicEnvironment(DynamicEnvironment baseEnv) {
023: rep = new Hashtable(baseEnv.rep.size() + 100);
024: Enumeration keys = baseEnv.rep.keys();
025: while (keys.hasMoreElements()) {
026: Object k = keys.nextElement();
027: rep.put(k, baseEnv.rep.get(k));
028: }
029: }
030:
031: public void lockDown() {
032: lockedDown = true;
033: }
034:
035: /** This is used by the (environment-bindings) primitive. **/
036: public static Pair getBindings(Object x) {
037: if (!(x instanceof DynamicEnvironment)) {
038: E.error("ERROR: not an environment: " + x.toString());
039: return null;
040: } else {
041: Pair bindings = Pair.EMPTY;
042: DynamicEnvironment env = (DynamicEnvironment) x;
043: Enumeration keys = env.rep.keys();
044: while (keys.hasMoreElements()) {
045: Symbol k = (Symbol) keys.nextElement();
046: if (!isJavaLiteral(k) && env.isDefined(k)) {
047: // Only export non-Java-literals.
048: Object v = env.getValue(k);
049: bindings = new Pair(new Pair(k, v), bindings);
050: }
051: }
052: return bindings;
053: }
054: }
055:
056: /** KRA 16JUL04: This is just a guess right? **/
057: private static boolean isJavaLiteral(Symbol name) {
058: return (name.toString().indexOf('.') != -1);
059: }
060:
061: public void importBindings(DynamicEnvironment env, String prefix) {
062: importBindings(env, prefix, false);
063: }
064:
065: public void importBindings(DynamicEnvironment env, String prefix,
066: boolean importMacros) {
067: importBindings(env, prefix, importMacros, null);
068: }
069:
070: public void importBindings(DynamicEnvironment env, String prefix,
071: boolean importMacros, jsint.Symbol[] procnames) {
072: HashSet hs = null;
073:
074: if (procnames != null)
075: hs = new HashSet(java.util.Arrays.asList(procnames));
076: if (lockedDown)
077: E
078: .error("ERROR: attempting to import bindings into locked environment");
079: else {
080: Enumeration keys = env.rep.keys();
081: while (keys.hasMoreElements()) {
082: Symbol k = (Symbol) keys.nextElement();
083: // Don't copy java literals, since we look them up when needed.
084: if (env.isDefined(k) && (!isJavaLiteral(k))) {
085: Object v = env.getValue((Symbol) k);
086: if ((importMacros == (v instanceof Macro))
087: && ((hs == null) || hs.contains(k))) {
088: Symbol newk = (prefix != null ? Symbol
089: .intern(prefix + k.toString()) : k);
090: setValue(newk, v);
091: }
092: }
093: }
094: }
095: }
096:
097: public Object getValue(Symbol s) {
098: return intern(s).getDynamicValue();
099: }
100:
101: public boolean isDefined(Symbol s) {
102: DynamicVariable it = ((DynamicVariable) rep.get(s));
103: return it != null && it.isDefined();
104: }
105:
106: public Object setValue(Symbol s, Object newval) {
107: if (lockedDown)
108: return E
109: .error("ERROR: attempting to alter bindings in locked environment:"
110: + s.toString()
111: + " <-- "
112: + newval.toString());
113: return intern(s).setDynamicValue(newval);
114: }
115:
116: public DynamicVariable intern(Symbol x) {
117: DynamicVariable it = ((DynamicVariable) rep.get(x));
118: if (it == null) {
119: it = new DynamicVariable(x);
120: rep.put(x, it);
121: }
122: return it;
123: }
124: }
|