public Res graft(Object newResource) { return new Graft(parent, key, newResource); }
}
+ // FEATURE: eliminate code duplication with JS.Graft
/** shadow resource which replaces the graft */
public static class Graft extends Res {
Res graftee;
public Object get(Object key) { return replaced_key.equals(key) ? replaced_val : graftee.get(key); }
public String getDescriptiveName() { return graftee.getDescriptiveName(); }
public Res getParent() { return graftee.getParent(); }
+ 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(); }
}
/** shadow resource which replaces the graft */
if (checkOnly) return Boolean.TRUE;
return new XWT((Res)args.elementAt(0));
+ } else if (method.equals("graft")) {
+ if (checkOnly) return Boolean.TRUE;
+ if (args.elementAt(0) instanceof Box) throw new JS.Exn("can't graft onto Boxes (yet)");
+ if (args.elementAt(0) instanceof Number) throw new JS.Exn("can't graft onto Numbers (yet)");
+ if (args.elementAt(0) instanceof String) throw new JS.Exn("can't graft onto Strings (yet)");
+ if (args.elementAt(0) instanceof Res)
+ return new Res.Graft((Res)args.elementAt(0), args.elementAt(1), args.elementAt(2));
+ return new JS.Graft((JS)args.elementAt(0), args.elementAt(1), args.elementAt(2));
+
} else if (method.equals("watchProgress")) {
if (checkOnly) return Boolean.TRUE;
return new Res.ProgressWatcher((Res)args.elementAt(0), (JS.Callable)args.elementAt(1));
public void declare(String s) { super.declare(s); }
}
+ /** 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 void put(Object key, Object val) { graftee.put(key, val); }
+ 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;
+ }
+ }
+
/** anything that is callable with the () operator */
public static abstract class Callable extends JS.Obj {
public abstract Object call(JS.Array args) throws JS.Exn;