renamed Script to JSU
[org.ibex.js.git] / src / org / ibex / js / Interpreter.java
index 80de88d..693ad9c 100644 (file)
@@ -107,16 +107,16 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
             switch(op) {
             case LITERAL: stack.push((JS)arg); break;
             case OBJECT: stack.push(new JS.Obj()); break;
-            case ARRAY: stack.push(new JSArray(Script.toInt((JS)arg))); break;
+            case ARRAY: stack.push(new JSArray(JSU.toInt((JS)arg))); break;
             //case DECLARE: scope.declare((JS)(arg==null ? stack.peek() : arg)); if(arg != null) stack.push((JS)arg); break;
-            case JT: if (Script.toBoolean((JS)stack.pop())) pc += Script.toInt((JS)arg) - 1; break;
-            case JF: if (!Script.toBoolean((JS)stack.pop())) pc += Script.toInt((JS)arg) - 1; break;
-            case JMP: pc += Script.toInt((JS)arg) - 1; break;
+            case JT: if (JSU.toBoolean((JS)stack.pop())) pc += JSU.toInt((JS)arg) - 1; break;
+            case JF: if (!JSU.toBoolean((JS)stack.pop())) pc += JSU.toInt((JS)arg) - 1; break;
+            case JMP: pc += JSU.toInt((JS)arg) - 1; break;
             case POP: stack.pop(); break;
             case SWAP: stack.swap(); break;
             case DUP: stack.push(stack.peek()); break;
             case NEWSCOPE: {
-                int n = Script.toInt((JS)arg);
+                int n = JSU.toInt((JS)arg);
                 scope = new JSScope(scope,(n>>>16)&0xffff,(n>>>0)&0xffff);
                 break;
             }
@@ -124,19 +124,19 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
             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 ASSERT: if (!Script.toBoolean((JS)stack.pop())) throw je("ibex.assertion.failed"); break;
-            case BITNOT: stack.push(Script.N(~Script.toLong((JS)stack.pop()))); break;
-            case BANG: stack.push(Script.B(!Script.toBoolean((JS)stack.pop()))); 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;
             case NEWFUNCTION: stack.push(((JSFunction)arg)._cloneWithNewParentScope(scope)); break;
             case LABEL: break;
 
             case TYPEOF: {
                 Object o = stack.pop();
                 if (o == null) stack.push(null);
-                else if (o instanceof JSString) stack.push(Script.S("string"));
-                else if (o instanceof JSNumber.B) stack.push(Script.S("boolean"));
-                else if (o instanceof JSNumber) stack.push(Script.S("number"));
-                else stack.push(Script.S("object"));
+                else if (o instanceof JSString) stack.push(JSU.S("string"));
+                else if (o instanceof JSNumber.B) stack.push(JSU.S("boolean"));
+                else if (o instanceof JSNumber) stack.push(JSU.S("number"));
+                else stack.push(JSU.S("object"));
                 break;
             }
 
@@ -148,7 +148,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
 
             case LOOP:
                 stack.push(new LoopMarker(pc, (String)(pc > 0 && f.op[pc - 1] == LABEL ? f.arg[pc - 1] : null), scope));
-                stack.push(Script.T);
+                stack.push(JSU.T);
                 break;
 
             case BREAK:
@@ -166,9 +166,9 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     if (o instanceof LoopMarker) {
                         if (arg == null || arg.equals(((LoopMarker)o).label)) {
                             int loopInstructionLocation = ((LoopMarker)o).location;
-                            int endOfLoop = Script.toInt((JS)f.arg[loopInstructionLocation]) + loopInstructionLocation;
+                            int endOfLoop = JSU.toInt((JS)f.arg[loopInstructionLocation]) + loopInstructionLocation;
                             scope = ((LoopMarker)o).scope;
-                            if (op == CONTINUE) { stack.push(o); stack.push(Script.F); }
+                            if (op == CONTINUE) { stack.push(o); stack.push(JSU.F); }
                             pc = op == BREAK ? endOfLoop - 1 : loopInstructionLocation;
                             continue OUTER;
                         }
@@ -200,7 +200,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                         boolean didTrapPut = false;
                         if (o instanceof TrapMarker) { // handles return component of a write trap
                             TrapMarker tm = (TrapMarker) o;
-                            boolean cascade = tm.t.isWriteTrap() && !tm.cascadeHappened && !Script.toBoolean(retval);
+                            boolean cascade = tm.t.isWriteTrap() && !tm.cascadeHappened && !JSU.toBoolean(retval);
                             if(cascade) {
                                 JS.Trap t = tm.t.nextWrite();
                                 if(t == null && tm.t.target() instanceof JS.Clone) {
@@ -236,7 +236,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
             }
                 
             case CASCADE: {
-                boolean write = Script.toBoolean((JS)arg);
+                boolean write = JSU.toBoolean((JS)arg);
                 JS val = write ? (JS)stack.pop() : null;
                 CallMarker o = stack.findCall();
                 if(!(o instanceof TrapMarker)) throw new JSExn("tried to CASCADE while not in a trap");
@@ -276,8 +276,8 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                 JS val = (JS)stack.pop();
                 JS key = (JS)stack.pop();
                 JS target = (JS)stack.peek();
-                if (target == null) throw je("tried to put " + Script.str(val) + " to the " + Script.str(key) + " property on the null value");
-                if (key == null) throw je("tried to assign \"" + Script.str(val) + "\" to the null key");
+                if (target == null) throw je("tried to put " + JSU.str(val) + " to the " + JSU.str(key) + " property on the null value");
+                if (key == null) throw je("tried to assign \"" + JSU.str(val) + "\" to the null key");
                 
                 JS.Trap t = target.getTrap(key);
                 if(t != null) t = t.write();
@@ -312,8 +312,8 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     stack.push(key);
                 }
                 JS ret = null;
-                if (key == null) throw je("tried to get the null key from " + Script.str(target));
-                if (target == null) throw je("tried to get property \"" + Script.str(key) + "\" from the null object");
+                if (key == null) throw je("tried to get the null key from " + JSU.str(target));
+                if (target == null) throw je("tried to get property \"" + JSU.str(key) + "\" from the null object");
                 
                 JS.Trap t = target.getTrap(key);
                 if(t != null) t = t.read();
@@ -338,7 +338,13 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
             }
             
             case CALL: case CALLMETHOD: {
-                JS[] jsargs = (JS[])arg;
+                // FIXME: can a lot of simple cases in Parser be
+                // reduced so creating a new JS[] is not necessary?
+                JS[] jsargs;
+                if (arg instanceof JSNumber.I) {
+                    jsargs = new JS[((JSNumber.I)arg).toInt()];
+                    for (int i=0; i < jsargs.length; i++) jsargs[i] = (JS)stack.pop();
+                } else jsargs = (JS[])arg;
 
                 JS method = null;
                 JS ret = null;
@@ -348,7 +354,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     if (object == null) {
                         method = (JS)stack.pop();
                         object = (JS)stack.pop();
-                        throw new JSExn("function '"+Script.str(method)+"' not found in " + object.getClass().getName());
+                        throw new JSExn("function '"+JSU.str(method)+"' not found in " + object.getClass().getName());
                     } else if (object instanceof JS.Method) {
                         method = (JS)stack.pop();
                         object = (JS)stack.pop();
@@ -406,7 +412,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                 JS key = (JS)stack.pop();
                 JS js = (JS)stack.peek();
                 // A trap addition/removal
-                if(!(val instanceof JSFunction)) throw new JSExn("tried to add/remove a non-function trap");
+                if(!(val instanceof JSFunction)) throw new JSExn("tried to add/remove a non-function trap"); // FIXME
                 if(op == ADD_TRAP) js.addTrap(key, val);
                 else js.delTrap(key, val);
                 break;
@@ -421,13 +427,13 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     JS left = (JS)stack.pop();
                     JS ret;
                     if(left instanceof JSString || right instanceof JSString)
-                        ret = Script.S(Script.toString(left).concat(Script.toString(right)));
+                        ret = JSU.S(JSU.toString(left).concat(JSU.toString(right)));
                     else if(left instanceof JSNumber.D || right instanceof JSNumber.D)
-                        ret = Script.N(Script.toDouble(left) + Script.toDouble(right));
+                        ret = JSU.N(JSU.toDouble(left) + JSU.toDouble(right));
                     else {
-                        long l = Script.toLong(left) + Script.toLong(right);
-                        if(l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) ret = Script.N(l);
-                        ret = Script.N((int)l);
+                        long l = JSU.toLong(left) + JSU.toLong(right);
+                        if(l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) ret = JSU.N(l);
+                        ret = JSU.N((int)l);
                     }
                     stack.push(ret);
                 } else {
@@ -435,27 +441,27 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     while(--count >= 0) args[count] = (JS)stack.pop();
                     if(args[0] instanceof JSString) {
                         StringBuffer sb = new StringBuffer(64);
-                        for(int i=0;i<args.length;i++) sb.append(Script.toString(args[i]));
-                        stack.push(Script.S(sb.toString()));
+                        for(int i=0;i<args.length;i++) sb.append(JSU.toString(args[i]));
+                        stack.push(JSU.S(sb.toString()));
                     } else {
                         int numStrings = 0;
                         for(int i=0;i<args.length;i++) if(args[i] instanceof JSString) numStrings++;
                         if(numStrings == 0) {
                             double d = 0.0;
-                            for(int i=0;i<args.length;i++) d += Script.toDouble(args[i]);
-                            stack.push(Script.N(d));
+                            for(int i=0;i<args.length;i++) d += JSU.toDouble(args[i]);
+                            stack.push(JSU.N(d));
                         } else {
                             int i=0;
                             StringBuffer sb = new StringBuffer(64);
                             if(!(args[0] instanceof JSString || args[1] instanceof JSString)) {
                                 double d=0.0;
                                 do {
-                                    d += Script.toDouble(args[i++]);
+                                    d += JSU.toDouble(args[i++]);
                                 } while(!(args[i] instanceof JSString));
-                                sb.append(Script.toString(Script.N(d)));
+                                sb.append(JSU.toString(JSU.N(d)));
                             }
-                            while(i < args.length) sb.append(Script.toString(args[i++]));
-                            stack.push(Script.S(sb.toString()));
+                            while(i < args.length) sb.append(JSU.toString(args[i++]));
+                            stack.push(JSU.S(sb.toString()));
                         }
                     }
                 }
@@ -467,25 +473,25 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                 JS left = (JS)stack.pop();
                 switch(op) {
                         
-                case BITOR: stack.push(Script.N(Script.toLong(left) | Script.toLong(right))); break;
-                case BITXOR: stack.push(Script.N(Script.toLong(left) ^ Script.toLong(right))); break;
-                case BITAND: stack.push(Script.N(Script.toLong(left) & Script.toLong(right))); break;
-
-                case SUB: stack.push(Script.N(Script.toDouble(left) - Script.toDouble(right))); break;
-                case MUL: stack.push(Script.N(Script.toDouble(left) * Script.toDouble(right))); break;
-                case DIV: stack.push(Script.N(Script.toDouble(left) / Script.toDouble(right))); break;
-                case MOD: stack.push(Script.N(Script.toDouble(left) % Script.toDouble(right))); break;
+                case BITOR: stack.push(JSU.N(JSU.toLong(left) | JSU.toLong(right))); break;
+                case BITXOR: stack.push(JSU.N(JSU.toLong(left) ^ JSU.toLong(right))); break;
+                case BITAND: stack.push(JSU.N(JSU.toLong(left) & JSU.toLong(right))); break;
+
+                case SUB: stack.push(JSU.N(JSU.toDouble(left) - JSU.toDouble(right))); break;
+                case MUL: stack.push(JSU.N(JSU.toDouble(left) * JSU.toDouble(right))); break;
+                case DIV: stack.push(JSU.N(JSU.toDouble(left) / JSU.toDouble(right))); break;
+                case MOD: stack.push(JSU.N(JSU.toDouble(left) % JSU.toDouble(right))); break;
                         
-                case LSH: stack.push(Script.N(Script.toLong(left) << Script.toLong(right))); break;
-                case RSH: stack.push(Script.N(Script.toLong(left) >> Script.toLong(right))); break;
-                case URSH: stack.push(Script.N(Script.toLong(left) >>> Script.toLong(right))); break;
+                case LSH: stack.push(JSU.N(JSU.toLong(left) << JSU.toLong(right))); break;
+                case RSH: stack.push(JSU.N(JSU.toLong(left) >> JSU.toLong(right))); break;
+                case URSH: stack.push(JSU.N(JSU.toLong(left) >>> JSU.toLong(right))); break;
                         
                 //#repeat </<=/>/>= LT/LE/GT/GE
                 case LT: {
                     if(left instanceof JSString && right instanceof JSString)
-                        stack.push(Script.B(Script.toString(left).compareTo(Script.toString(right)) < 0));
+                        stack.push(JSU.B(JSU.toString(left).compareTo(JSU.toString(right)) < 0));
                     else
-                        stack.push(Script.B(Script.toDouble(left) < Script.toDouble(right)));
+                        stack.push(JSU.B(JSU.toDouble(left) < JSU.toDouble(right)));
                 }
                 //#end
                     
@@ -495,7 +501,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     if(left == null && right == null) ret = true;
                     else if(left == null || right == null) ret = false;
                     else ret = left.equals(right);
-                    stack.push(Script.B(op == EQ ? ret : !ret)); break;
+                    stack.push(JSU.B(op == EQ ? ret : !ret)); break;
                 }
 
                 default: throw new Error("unknown opcode " + op);
@@ -515,7 +521,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
     */
     void catchException(JSExn e) throws JSExn {
         while(!stack.empty()) {
-            JS o = (JS)stack.pop();
+            Object o = stack.pop();
             if (o instanceof CatchMarker || o instanceof TryMarker) {
                 boolean inCatch = o instanceof CatchMarker;
                 if(inCatch) {
@@ -620,12 +626,12 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
         private JS val;
         public TrapArgs(Trap t, JS val) { this.t = t; this.val = val; }
         public JS get(JS key) throws JSExn {
-            if(Script.isInt(key) && Script.toInt(key) == 0) return val;
-            //#switch(Script.str(key))
+            if(JSU.isInt(key) && JSU.toInt(key) == 0) return val;
+            //#switch(JSU.str(key))
             case "trapee": return t.target();
             case "callee": return t.function();
             case "trapname": return t.key();
-            case "length": return t.isWriteTrap() ? Script.ONE : Script.ZERO;
+            case "length": return t.isWriteTrap() ? JSU.ONE : JSU.ZERO;
             //#end
             return super.get(key);
         }
@@ -671,7 +677,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     if(cm.f == null) break;
                     String s = cm.f.sourceName + ":" + cm.f.line[cm.pc-1];
                     if(cm instanceof Interpreter.TrapMarker) 
-                        s += " (trap on " + Script.str(((Interpreter.TrapMarker)cm).t.key()) + ")";
+                        s += " (trap on " + JSU.str(((Interpreter.TrapMarker)cm).t.key()) + ")";
                     e.addBacktrace(s);
                 }
             }