- /** Any object which becomes part of the scope chain must support this interface */
- public static class Scope extends Obj {
- private Scope parentScope;
- private static Object NULL = new Object();
- public Scope(Scope parentScope) { this.parentScope = parentScope; }
- public JS getParentScope() { return parentScope; }
- public boolean has(Object key) { return super.get(key) != null; }
- public Object get(Object key) {
- if (!has(key)) return getParentScope().get(key);
- Object ret = super.get(key); return ret == NULL ? null : ret;
- }
- public void put(Object key, Object val) {
- if (!has(key)) getParentScope().put(key, val);
- else super.put(key, val == null ? NULL : val);
- }
- public Object[] enumerateProperties() { throw new Error("you can't enumerate the properties of a Scope"); }
- public void declare(String s) { super.put(s, NULL); }
- }
-
+ /** the result of a graft */
+ // FIXME: uber-broken
+ public static class Graft extends JSCallable {
+ private JS graftee;
+ private Object replaced_key;
+ private Object replaced_val;
+ public Graft(Object graftee, Object key, Object val) {
+ if (graftee instanceof JSArray) throw new JS.Exn("can't graft onto JSArrays (yet)");
+ if (graftee instanceof JSCallable) throw new JS.Exn("can't graft onto JSCallables (yet)");
+ if (graftee instanceof JSScope) throw new JS.Exn("can't graft onto JSScopes (yet)");
+ this.graftee = (JS)graftee;
+ replaced_key = key;
+ replaced_val = val;
+ }
+ public boolean equals(Object o) { return (this == o || graftee.equals(o)); }
+ public int hashCode() { return graftee.hashCode(); }
+ public Object get(Object key) { return replaced_key.equals(key) ? replaced_val : graftee.get(key); }
+ public void put(Object key, Object val) { graftee.put(key, val); }
+ /*
+ public Object call(Object method, JSArray args) {
+ if (replaced_key.equals(method)) return ((JSCallable)replaced_val).call(null, args);
+ else if (graftee instanceof JSCallable) return ((JSCallable)graftee).call(method, args);
+ else throw new JS.Exn("cannot call this value");
+ }
+ */
+ public Number coerceToNumber() { return graftee.coerceToNumber(); }
+ public String coerceToString() { return graftee.coerceToString(); }
+ public boolean coerceToBoolean() { return graftee.coerceToBoolean(); }
+ public String typeName() { return graftee.typeName(); }
+ public Enumeration keys() {
+ return new Enumeration() {
+ Enumeration graftee_enumeration = graftee.keys();
+ boolean returned_replaced = false;
+ public boolean hasMoreElements() {
+ if (graftee_enumeration.hasMoreElements()) return true;
+ return !returned_replaced;
+ }
+ public Object nextElement() {
+ if (!graftee_enumeration.hasMoreElements()) {
+ if (returned_replaced) throw new NoSuchElementException();
+ returned_replaced = true;
+ return replaced_key;
+ } else {
+ Object ret = graftee_enumeration.nextElement();
+ if (!returned_replaced && ret.equals(replaced_key)) returned_replaced = true;
+ return ret;
+ }
+ }
+ };
+ }
+ }