reintroduce JSArgs
[org.ibex.js.git] / src / org / ibex / js / Interpreter.java
index 693ad9c..3fedf8f 100644 (file)
@@ -29,7 +29,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
         this.scope = f.parentScope;
         try {
             stack.push(new CallMarker(null));    // the "root function returned" marker -- f==null
-            stack.push(args);
+            stack.push(new JSArgs(args, f)); // FIXME: temprorary bug fix
         } catch(JSExn e) {
             throw new Error("should never happen");
         }
@@ -123,7 +123,12 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
             case OLDSCOPE: scope = scope.parent; break;
             case GLOBALSCOPE: stack.push(scope.getGlobal()); break;
             case SCOPEGET: stack.push(scope.get((JS)arg)); break;
-            case SCOPEPUT: scope.put((JS)arg, (JS)stack.peek()); break;
+            case SCOPEPUT: {
+                // FIXME: HACK: share this around more and find the callee.
+                Object val = stack.peek();
+                if (val != null && val instanceof JS[]) val = new JSArgs((JS[])val, null);
+                scope.put((JS)arg, (JS)val); break;
+            }
             case ASSERT: if (!JSU.toBoolean((JS)stack.pop())) throw je("ibex.assertion.failed"); break;
             case BITNOT: stack.push(JSU.N(~JSU.toLong((JS)stack.pop()))); break;
             case BANG: stack.push(JSU.B(!JSU.toBoolean((JS)stack.pop()))); break;
@@ -621,6 +626,22 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
         public FinallyData(JSExn exn) { this.exn = exn; this.op = -1; this.arg = null; } // Just throw this exn
     }
 
+    static class JSArgs extends JS.Immutable {
+        private final JS[] args;
+        private final JS callee;
+        
+        public JSArgs(JS[] args, JS callee) { this.args = args; this.callee = callee; }
+        
+        public JS get(JS key) throws JSExn {
+            if(JSU.isInt(key)) return args[JSU.toInt(key)];
+            //#switch(JSU.toString(key))
+            case "callee": return callee;
+            case "length": return JSU.N(args.length);
+            //#end
+            return super.get(key);
+        }
+    }
+
     static class TrapArgs extends JS.Immutable {
         private Trap t;
         private JS val;