- /** the result of a graft */
- public static class Graft extends JS {
- private JS graftee;
- private Object replaced_key;
- private Object replaced_val;
- public Graft(JS graftee, Object key, Object val) {
- if (graftee instanceof Array) throw new JS.Exn("can't graft onto Arrays (yet)");
- if (graftee instanceof Callable) throw new JS.Exn("can't graft onto Callables (yet)");
- if (graftee instanceof Scope) throw new JS.Exn("can't graft onto Scopes (yet)");
- this.graftee = 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 Object put(Object key, Object val) { graftee.put(key, val); return null; }
- public Object callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
- if (!replaced_key.equals(method)) return graftee.callMethod(method, args, checkOnly);
- if (replaced_val instanceof Callable) return checkOnly ? Boolean.TRUE : ((Callable)replaced_val).call(args);
- if (checkOnly) return Boolean.FALSE;
- throw new JS.Exn("attempt to call non-function");
- }
- 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 Object[] keys() {
- Object[] ret = graftee.keys();
- for(int i=0; i<ret.length; i++) if (replaced_key.equals(ret[i])) return ret;
- Object[] ret2 = new Object[ret.length + 1];
- System.arraycopy(ret, 0, ret2, 0, ret.length);
- ret2[ret2.length - 1] = replaced_key;
- return ret2;
- }