public static Number toNumber(Object o) {
if (o == null) return new Long(0);
if (o instanceof Number) return ((Number)o);
- // FIXME: There are about 3 pages of rules in ecma262 about string to number conversions
- // We aren't even close to following all those rules
+
+ // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions
+ // We aren't even close to following all those rules. We probably never will be.
if (o instanceof String) try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(Double.NaN); }
if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0);
if (o instanceof JS) return ((JS)o).coerceToNumber();
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;
CompiledFunction currentCompiledFunction = null;
Vec stack = new Vec();
- int pc;
+ public Scope scope = null;
+ int pc = 0;
+ boolean paused = false;
+
+ CompiledFunction tailCallFunction = null;
+ JS.Array tailCallArgs = null;
+
+ public Thread(CompiledFunction function) {
+ this(function, new CompiledFunctionImpl.FunctionScope("unknown", function.parentScope));
+ }
+ public Thread(CompiledFunction function, Scope scope) {
+ stack.push(new CompiledFunctionImpl.CallMarker(this));
+ stack.push(new JS.Array());
+ this.currentCompiledFunction = function;
+ this.scope = scope;
+ }
- /** binds this thread to the current Java Thread */
- public void bindToCurrentJavaThread() { javaThreadToJSThread.put(java.lang.Thread.currentThread(), this); }
+ public void setTailCall(JS.CompiledFunction f, JS.Array args) { tailCallFunction = f; tailCallArgs = args; }
- /** returns the line of code that is currently executing */
- public int getLine() { return currentCompiledFunction == null ? -1 : currentCompiledFunction.getLine(pc); }
+ public static JS.Thread current() { return (JS.Thread)javaThreadToJSThread.get(java.lang.Thread.currentThread()); }
- /** returns the name of the source code file which declared the currently executing function */
+ public Object resume() { bind(); paused = false; Object ret = CompiledFunctionImpl.eval(this); unbind(); return ret; }
+ public void pause() { paused = true; unbind(); }
+ public void bind() { javaThreadToJSThread.put(java.lang.Thread.currentThread(), this); }
+ public void unbind() { if (current() == this) javaThreadToJSThread.remove(java.lang.Thread.currentThread()); }
+ public int getLine() { return currentCompiledFunction == null ? -1 : currentCompiledFunction.getLine(this); }
public String getSourceName() { return currentCompiledFunction == null ? null : currentCompiledFunction.getSourceName(); }
/** fetches the currently-executing javascript function */
public JS.CompiledFunction getCurrentCompiledFunction() { return currentCompiledFunction; }
-
-
- // Statics ///////////////////////////////////////////////////////////////////////
-
private static Hashtable javaThreadToJSThread = new Hashtable();
-
- /** returns the JS thread for a given Java thread, creating one if necessary */
- public static JS.Thread fromJavaThread(java.lang.Thread t) {
- JS.Thread ret = (JS.Thread)javaThreadToJSThread.get(t);
- if (ret == null) {
- ret = new JS.Thread();
- ret.bindToCurrentJavaThread();
- }
- return ret;
- }
-
- public static JS.Thread currentJSThread() {
- return fromJavaThread(java.lang.Thread.currentThread());
- }
}
}