1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.ibex.util.*;
7 /** Encapsulates a single JS interpreter (ie call stack) */
8 class Interpreter implements ByteCodes, Tokens {
9 private static final int MAX_STACK_SIZE = 512;
11 // Thread-Interpreter Mapping /////////////////////////////////////////////////////////////////////////
13 static Interpreter current() { return (Interpreter)threadToInterpreter.get(Thread.currentThread()); }
14 private static Hashtable threadToInterpreter = new Hashtable();
17 // Instance members and methods //////////////////////////////////////////////////////////////////////
19 int pausecount; ///< the number of times pause() has been invoked; -1 indicates unpauseable
20 JSFunction f = null; ///< the currently-executing JSFunction
21 JSScope scope; ///< the current top-level scope (LIFO stack via NEWSCOPE/OLDSCOPE)
22 Vec stack = new Vec(); ///< the object stack
23 int pc = 0; ///< the program counter
25 Interpreter(JSFunction f, boolean pauseable, JSArray args) {
26 stack.push(new Interpreter.CallMarker(this)); // the "root function returned" marker -- f==null
28 this.pausecount = pauseable ? 0 : -1;
29 this.scope = new JSScope(f.parentScope);
33 /** this is the only synchronization point we need in order to be threadsafe */
34 synchronized Object resume() throws JSExn {
35 Thread t = Thread.currentThread();
36 Interpreter old = (Interpreter)threadToInterpreter.get(t);
37 threadToInterpreter.put(t, this);
41 if (old == null) threadToInterpreter.remove(t);
42 else threadToInterpreter.put(t, old);
46 static int getLine() {
47 Interpreter c = Interpreter.current();
48 return c == null || c.f == null || c.pc < 0 || c.pc >= c.f.size ? -1 : c.f.line[c.pc];
51 static String getSourceName() {
52 Interpreter c = Interpreter.current();
53 return c == null || c.f == null ? null : c.f.sourceName;
56 private static JSExn je(String s) { return new JSExn(getSourceName() + ":" + getLine() + " " + s); }
58 // FIXME: double check the trap logic
59 private Object run() throws JSExn {
61 // if pausecount changes after a get/put/call, we know we've been paused
62 final int initialPauseCount = pausecount;
66 if (f == null) return stack.pop();
68 Object arg = f.arg[pc];
69 if(op == FINALLY_DONE) {
70 FinallyData fd = (FinallyData) stack.pop();
71 if(fd == null) continue OUTER; // NOP
72 if(fd.exn != null) throw fd.exn;
77 case LITERAL: stack.push(arg); break;
78 case OBJECT: stack.push(new JS()); break;
79 case ARRAY: stack.push(new JSArray(JS.toNumber(arg).intValue())); break;
80 case DECLARE: scope.declare((String)(arg==null ? stack.peek() : arg)); if(arg != null) stack.push(arg); break;
81 case TOPSCOPE: stack.push(scope); break;
82 case JT: if (JS.toBoolean(stack.pop())) pc += JS.toNumber(arg).intValue() - 1; break;
83 case JF: if (!JS.toBoolean(stack.pop())) pc += JS.toNumber(arg).intValue() - 1; break;
84 case JMP: pc += JS.toNumber(arg).intValue() - 1; break;
85 case POP: stack.pop(); break;
87 int depth = (arg == null ? 1 : JS.toInt(arg));
88 Object save = stack.elementAt(stack.size() - 1);
89 for(int i=stack.size() - 1; i > stack.size() - 1 - depth; i--)
90 stack.setElementAt(stack.elementAt(i-1), i);
91 stack.setElementAt(save, stack.size() - depth - 1);
93 case DUP: stack.push(stack.peek()); break;
94 case NEWSCOPE: scope = new JSScope(scope); break;
95 case OLDSCOPE: scope = scope.getParentScope(); break;
97 if (JS.checkAssertions && !JS.toBoolean(stack.pop()))
98 throw je("ibex.assertion.failed" /*FEATURE: line number*/); break;
99 case BITNOT: stack.push(JS.N(~JS.toLong(stack.pop()))); break;
100 case BANG: stack.push(JS.B(!JS.toBoolean(stack.pop()))); break;
101 case NEWFUNCTION: stack.push(((JSFunction)arg)._cloneWithNewParentScope(scope)); break;
105 Object o = stack.pop();
106 if (o == null) stack.push(null);
107 else if (o instanceof JS) stack.push("object");
108 else if (o instanceof String) stack.push("string");
109 else if (o instanceof Number) stack.push("number");
110 else if (o instanceof Boolean) stack.push("boolean");
111 else throw new Error("this should not happen");
116 Object o = stack.peek();
117 Enumeration e = ((JS)o).keys();
118 JSArray a = new JSArray();
119 while(e.hasMoreElements()) a.addElement(e.nextElement());
125 stack.push(new LoopMarker(pc, pc > 0 && f.op[pc - 1] == LABEL ? (String)f.arg[pc - 1] : (String)null, scope));
126 stack.push(Boolean.TRUE);
131 while(stack.size() > 0) {
132 Object o = stack.pop();
133 if (o instanceof CallMarker) je("break or continue not within a loop");
134 if (o instanceof TryMarker) {
135 if(((TryMarker)o).finallyLoc < 0) continue; // no finally block, keep going
136 stack.push(new FinallyData(op, arg));
137 scope = ((TryMarker)o).scope;
138 pc = ((TryMarker)o).finallyLoc - 1;
141 if (o instanceof LoopMarker) {
142 if (arg == null || arg.equals(((LoopMarker)o).label)) {
143 int loopInstructionLocation = ((LoopMarker)o).location;
144 int endOfLoop = ((Integer)f.arg[loopInstructionLocation]).intValue() + loopInstructionLocation;
145 scope = ((LoopMarker)o).scope;
146 if (op == CONTINUE) { stack.push(o); stack.push(Boolean.FALSE); }
147 pc = op == BREAK ? endOfLoop - 1 : loopInstructionLocation;
152 throw new Error("CONTINUE/BREAK invoked but couldn't find LoopMarker at " +
153 getSourceName() + ":" + getLine());
156 int[] jmps = (int[]) arg;
157 // jmps[0] is how far away the catch block is, jmps[1] is how far away the finally block is
158 // each can be < 0 if the specified block does not exist
159 stack.push(new TryMarker(jmps[0] < 0 ? -1 : pc + jmps[0], jmps[1] < 0 ? -1 : pc + jmps[1], this));
164 Object retval = stack.pop();
165 while(stack.size() > 0) {
166 Object o = stack.pop();
167 if (o instanceof TryMarker) {
168 if(((TryMarker)o).finallyLoc < 0) continue;
170 stack.push(new FinallyData(RETURN));
171 scope = ((TryMarker)o).scope;
172 pc = ((TryMarker)o).finallyLoc - 1;
174 } else if (o instanceof CallMarker) {
175 if (scope instanceof Trap.TrapScope) { // handles return component of a read trap
176 Trap.TrapScope ts = (Trap.TrapScope)scope;
177 if (retval != null && retval instanceof Boolean && ((Boolean)retval).booleanValue())
178 ts.cascadeHappened = true;
179 if (!ts.cascadeHappened) {
180 ts.cascadeHappened = true;
182 while (t != null && t.f.numFormalArgs == 0) t = t.next;
184 ((JS)ts.t.trapee).put(ts.t.name, ts.val);
185 if (pausecount > initialPauseCount) { pc++; return null; } // we were paused
188 JSArray args = new JSArray();
189 args.addElement(ts.val);
192 scope = new Trap.TrapScope(f.parentScope, t, ts.val);
198 scope = ((CallMarker)o).scope;
199 pc = ((CallMarker)o).pc - 1;
200 f = (JSFunction)((CallMarker)o).f;
205 throw new Error("error: RETURN invoked but couldn't find a CallMarker!");
209 Object val = stack.pop();
210 Object key = stack.pop();
211 Object target = stack.peek();
213 throw je("tried to put a value to the " + key + " property on the null value");
214 if (!(target instanceof JS))
215 throw je("tried to put a value to the " + key + " property on a " + target.getClass().getName());
217 throw je("tried to assign \"" + (val==null?"(null)":val.toString()) + "\" to the null key");
220 Trap.TrapScope ts = null;
221 if (target instanceof JSScope && key.equals("cascade")) {
222 JSScope p = (JSScope)target; // search the scope-path for the trap
223 if (target instanceof Trap.TrapScope) {
224 ts = (Trap.TrapScope)target;
226 while (ts == null && p.getParentScope() != null) {
227 p = p.getParentScope();
228 if (p instanceof Trap.TrapScope) ts = (Trap.TrapScope)p;
233 ts.cascadeHappened = true;
234 while (t != null && t.f.numFormalArgs == 0) t = t.next;
235 if (t == null) { target = ts.t.trapee; key = ts.t.name; }
239 if (target instanceof JSScope) {
240 JSScope p = (JSScope)target; // search the scope-path for the trap
242 while (t == null && p.getParentScope() != null) { p = p.getParentScope(); t = p.getTrap(key); }
244 t = ((JS)target).getTrap(key);
246 while (t != null && t.f.numFormalArgs == 0) t = t.next; // find the first write trap
249 stack.push(new CallMarker(this));
250 if(stack.size() > MAX_STACK_SIZE) throw new JSExn("stack overflow");
251 JSArray args = new JSArray();
252 args.addElement(val);
255 scope = new Trap.TrapScope(f.parentScope, t, val);
259 ((JS)target).put(key, val);
260 if (pausecount > initialPauseCount) { pc++; return null; } // we were paused
269 v = arg == null ? stack.pop() : arg;
277 if (v == null) throw je("tried to get the null key from " + o);
278 if (o == null) throw je("tried to get property \"" + v + "\" from the null object");
279 if (o instanceof String || o instanceof Number || o instanceof Boolean) {
280 ret = getFromPrimitive(o,v);
283 } else if (o instanceof JS) {
285 if (o instanceof Trap.TrapScope && v.equals("cascade")) {
286 t = ((Trap.TrapScope)o).t.next;
287 while (t != null && t.f.numFormalArgs != 0) t = t.next;
288 if (t == null) { v = ((Trap.TrapScope)o).t.name; o = ((Trap.TrapScope)o).t.trapee; }
290 } else if (o instanceof JS) {
291 if (o instanceof JSScope) {
292 JSScope p = (JSScope)o; // search the scope-path for the trap
294 while (t == null && p.getParentScope() != null) { p = p.getParentScope(); t = p.getTrap(v); }
296 t = ((JS)o).getTrap(v);
298 while (t != null && t.f.numFormalArgs != 0) t = t.next; // get first read trap
301 stack.push(new CallMarker(this));
302 if(stack.size() > MAX_STACK_SIZE) throw new JSExn("stack overflow");
303 JSArray args = new JSArray();
306 scope = new Trap.TrapScope(f.parentScope, t, null);
307 ((Trap.TrapScope)scope).cascadeHappened = true;
311 ret = ((JS)o).get(v);
312 if (ret == JS.METHOD) ret = new Stub((JS)o, v);
313 if (pausecount > initialPauseCount) { pc++; return null; } // we were paused
317 throw je("tried to get property " + v + " from a " + o.getClass().getName());
320 case CALL: case CALLMETHOD: {
321 int numArgs = JS.toInt(arg);
322 Object method = null;
324 Object object = stack.pop();
326 if (op == CALLMETHOD) {
327 if (object == JS.METHOD) {
328 method = stack.pop();
329 object = stack.pop();
330 } else if (object == null) {
331 Object name = stack.pop();
333 throw new JSExn("function '"+name+"' not found");
339 Object[] rest = numArgs > 3 ? new Object[numArgs - 3] : null;
340 for(int i=numArgs - 1; i>2; i--) rest[i-3] = stack.pop();
341 Object a2 = numArgs <= 2 ? null : stack.pop();
342 Object a1 = numArgs <= 1 ? null : stack.pop();
343 Object a0 = numArgs <= 0 ? null : stack.pop();
345 if (object instanceof String || object instanceof Number || object instanceof Boolean) {
346 ret = callMethodOnPrimitive(object, method, a0, a1, a2, null, numArgs);
348 } else if (object instanceof JSFunction) {
349 // FIXME: use something similar to call0/call1/call2 here
350 JSArray arguments = new JSArray();
351 for(int i=0; i<numArgs; i++) arguments.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
352 stack.push(new CallMarker(this));
353 if(stack.size() > MAX_STACK_SIZE) throw new JSExn("stack overflow");
354 stack.push(arguments);
355 f = (JSFunction)object;
356 scope = new JSScope(f.parentScope);
360 } else if (object instanceof JS) {
362 ret = method == null ? c.call(a0, a1, a2, rest, numArgs) : c.callMethod(method, a0, a1, a2, rest, numArgs);
365 throw new JSExn("can't call a " + object + " @" + pc + "\n" + f.dump());
368 if (pausecount > initialPauseCount) { pc++; return null; }
374 throw new JSExn(stack.pop(), stack, f, pc, scope);
378 final Grammar r = (Grammar)arg;
379 final JSScope final_scope = scope;
380 Grammar r2 = new Grammar() {
381 public int match(String s, int start, Hash v, JSScope scope) throws JSExn {
382 return r.match(s, start, v, final_scope);
384 public int matchAndWrite(String s, int start, Hash v, JSScope scope, String key) throws JSExn {
385 return r.matchAndWrite(s, start, v, final_scope, key);
387 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
389 r.matchAndWrite((String)a0, 0, v, final_scope, "foo");
393 Object obj = stack.pop();
394 if (obj != null && obj instanceof Grammar) r2 = new Grammar.Alternative((Grammar)obj, r2);
399 case ADD_TRAP: case DEL_TRAP: {
400 Object val = stack.pop();
401 Object key = stack.pop();
402 Object obj = stack.peek();
403 // A trap addition/removal
405 if(js instanceof JSScope) {
406 JSScope s = (JSScope) js;
407 while(s.getParentScope() != null) {
408 if(s.has(key)) throw new JSExn("cannot trap a variable that isn't at the top level scope");
409 s = s.getParentScope();
414 // if(!js.has(key)) throw new JSExn("tried to add/remove a trap from an uninitialized variable");
415 if(op == ADD_TRAP) js.addTrap(key, (JSFunction)val);
416 else js.delTrap(key, (JSFunction)val);
420 case ASSIGN_SUB: case ASSIGN_ADD: {
421 Object val = stack.pop();
422 Object key = stack.pop();
423 Object obj = stack.peek();
424 // The following setup is VERY important. The generated bytecode depends on the stack
425 // being setup like this (top to bottom) KEY, OBJ, VAL, KEY, OBJ
434 int count = ((Number)arg).intValue();
435 if(count < 2) throw new Error("this should never happen");
438 Object right = stack.pop();
439 Object left = stack.pop();
440 if(left instanceof String || right instanceof String)
441 stack.push(JS.toString(left).concat(JS.toString(right)));
442 else stack.push(JS.N(JS.toDouble(left) + JS.toDouble(right)));
444 Object[] args = new Object[count];
445 while(--count >= 0) args[count] = stack.pop();
446 if(args[0] instanceof String) {
447 StringBuffer sb = new StringBuffer(64);
448 for(int i=0;i<args.length;i++) sb.append(JS.toString(args[i]));
449 stack.push(sb.toString());
452 for(int i=0;i<args.length;i++) if(args[i] instanceof String) numStrings++;
453 if(numStrings == 0) {
455 for(int i=0;i<args.length;i++) d += JS.toDouble(args[i]);
459 StringBuffer sb = new StringBuffer(64);
460 if(!(args[0] instanceof String || args[1] instanceof String)) {
463 d += JS.toDouble(args[i++]);
464 } while(!(args[i] instanceof String));
465 sb.append(JS.toString(JS.N(d)));
467 while(i < args.length) sb.append(JS.toString(args[i++]));
468 stack.push(sb.toString());
476 Object right = stack.pop();
477 Object left = stack.pop();
480 case BITOR: stack.push(JS.N(JS.toLong(left) | JS.toLong(right))); break;
481 case BITXOR: stack.push(JS.N(JS.toLong(left) ^ JS.toLong(right))); break;
482 case BITAND: stack.push(JS.N(JS.toLong(left) & JS.toLong(right))); break;
484 case SUB: stack.push(JS.N(JS.toDouble(left) - JS.toDouble(right))); break;
485 case MUL: stack.push(JS.N(JS.toDouble(left) * JS.toDouble(right))); break;
486 case DIV: stack.push(JS.N(JS.toDouble(left) / JS.toDouble(right))); break;
487 case MOD: stack.push(JS.N(JS.toDouble(left) % JS.toDouble(right))); break;
489 case LSH: stack.push(JS.N(JS.toLong(left) << JS.toLong(right))); break;
490 case RSH: stack.push(JS.N(JS.toLong(left) >> JS.toLong(right))); break;
491 case URSH: stack.push(JS.N(JS.toLong(left) >>> JS.toLong(right))); break;
493 case LT: case LE: case GT: case GE: {
494 if (left == null) left = JS.N(0);
495 if (right == null) right = JS.N(0);
497 if (left instanceof String || right instanceof String) {
498 result = JS.toString(left).compareTo(JS.toString(right));
500 result = (int)java.lang.Math.ceil(JS.toDouble(left) - JS.toDouble(right));
502 stack.push(JS.B((op == LT && result < 0) || (op == LE && result <= 0) ||
503 (op == GT && result > 0) || (op == GE && result >= 0)));
509 // FIXME: This is not correct, see ECMA-262 11.9.3
513 if (l == null) { Object tmp = r; r = l; l = tmp; }
514 if (l == null && r == null) ret = true;
515 else if (r == null) ret = false; // l != null, so its false
516 else if (l instanceof Boolean) ret = JS.B(JS.toBoolean(r)).equals(l);
517 else if (l instanceof Number) ret = JS.toNumber(r).doubleValue() == JS.toNumber(l).doubleValue();
518 else if (l instanceof String) ret = r != null && l.equals(JS.toString(r));
519 else ret = l.equals(r);
520 stack.push(JS.B(op == EQ ? ret : !ret)); break;
523 default: throw new Error("unknown opcode " + op);
528 while(stack.size() > 0) {
529 Object o = stack.pop();
530 if (o instanceof CatchMarker || o instanceof TryMarker) {
531 boolean inCatch = o instanceof CatchMarker;
534 if(((TryMarker)o).finallyLoc < 0) continue; // no finally block, keep going
536 if(!inCatch && ((TryMarker)o).catchLoc >= 0) {
537 // run the catch block, this will implicitly run the finally block, if it exists
539 stack.push(catchMarker);
540 stack.push(e.getObject());
541 f = ((TryMarker)o).f;
542 scope = ((TryMarker)o).scope;
543 pc = ((TryMarker)o).catchLoc - 1;
546 stack.push(new FinallyData(e));
547 f = ((TryMarker)o).f;
548 scope = ((TryMarker)o).scope;
549 pc = ((TryMarker)o).finallyLoc - 1;
561 // Markers //////////////////////////////////////////////////////////////////////
563 public static class CallMarker {
567 public CallMarker(Interpreter cx) { pc = cx.pc + 1; scope = cx.scope; f = cx.f; }
570 public static class CatchMarker { }
571 private static CatchMarker catchMarker = new CatchMarker();
573 public static class LoopMarker {
576 public JSScope scope;
577 public LoopMarker(int location, String label, JSScope scope) {
578 this.location = location;
583 public static class TryMarker {
585 public int finallyLoc;
586 public JSScope scope;
588 public TryMarker(int catchLoc, int finallyLoc, Interpreter cx) {
589 this.catchLoc = catchLoc;
590 this.finallyLoc = finallyLoc;
591 this.scope = cx.scope;
595 public static class FinallyData {
599 public FinallyData(int op) { this(op,null); }
600 public FinallyData(int op, Object arg) { this.op = op; this.arg = arg; }
601 public FinallyData(JSExn exn) { this.exn = exn; } // Just throw this exn
605 // Operations on Primitives //////////////////////////////////////////////////////////////////////
607 static Object callMethodOnPrimitive(Object o, Object method, Object arg0, Object arg1, Object arg2, Object[] rest, int alength) throws JSExn {
608 if (method == null || !(method instanceof String) || "".equals(method))
609 throw new JSExn("attempt to call a non-existant method on a primitive");
611 if (o instanceof Number) {
613 case "toFixed": throw new JSExn("toFixed() not implemented");
614 case "toExponential": throw new JSExn("toExponential() not implemented");
615 case "toPrecision": throw new JSExn("toPrecision() not implemented");
617 int radix = alength >= 1 ? JS.toInt(arg0) : 10;
618 return Long.toString(((Number)o).longValue(),radix);
621 } else if (o instanceof Boolean) {
622 // No methods for Booleans
623 throw new JSExn("attempt to call a method on a Boolean");
626 String s = JS.toString(o);
627 int slength = s.length();
630 int a = alength >= 1 ? JS.toInt(arg0) : 0;
631 int b = alength >= 2 ? JS.toInt(arg1) : slength;
632 if (a > slength) a = slength;
633 if (b > slength) b = slength;
636 if (a > b) { int tmp = a; a = b; b = tmp; }
637 return s.substring(a,b);
640 int start = alength >= 1 ? JS.toInt(arg0) : 0;
641 int len = alength >= 2 ? JS.toInt(arg1) : Integer.MAX_VALUE;
642 if (start < 0) start = slength + start;
643 if (start < 0) start = 0;
644 if (len < 0) len = 0;
645 if (len > slength - start) len = slength - start;
646 if (len <= 0) return "";
647 return s.substring(start,start+len);
650 int p = alength >= 1 ? JS.toInt(arg0) : 0;
651 if (p < 0 || p >= slength) return "";
652 return s.substring(p,p+1);
655 int p = alength >= 1 ? JS.toInt(arg0) : 0;
656 if (p < 0 || p >= slength) return JS.N(Double.NaN);
657 return JS.N(s.charAt(p));
660 StringBuffer sb = new StringBuffer(slength*2).append(s);
661 for(int i=0;i<alength;i++) sb.append(i==0?arg0:i==1?arg1:i==2?arg2:rest[i-3]);
662 return sb.toString();
665 String search = alength >= 1 ? JS.toString(arg0) : "null";
666 int start = alength >= 2 ? JS.toInt(arg1) : 0;
667 // Java's indexOf handles an out of bounds start index, it'll return -1
668 return JS.N(s.indexOf(search,start));
670 case "lastIndexOf": {
671 String search = alength >= 1 ? JS.toString(arg0) : "null";
672 int start = alength >= 2 ? JS.toInt(arg1) : 0;
673 // Java's indexOf handles an out of bounds start index, it'll return -1
674 return JS.N(s.lastIndexOf(search,start));
676 case "match": return JSRegexp.stringMatch(s,arg0);
677 case "replace": return JSRegexp.stringReplace(s,arg0,arg1);
678 case "search": return JSRegexp.stringSearch(s,arg0);
679 case "split": return JSRegexp.stringSplit(s,arg0,arg1,alength);
680 case "toLowerCase": return s.toLowerCase();
681 case "toUpperCase": return s.toUpperCase();
682 case "toString": return s;
684 int a = alength >= 1 ? JS.toInt(arg0) : 0;
685 int b = alength >= 2 ? JS.toInt(arg1) : slength;
686 if (a < 0) a = slength + a;
687 if (b < 0) b = slength + b;
690 if (a > slength) a = slength;
691 if (b > slength) b = slength;
692 if (a > b) return "";
693 return s.substring(a,b);
696 throw new JSExn("Attempted to call non-existent method: " + method);
699 static Object getFromPrimitive(Object o, Object key) throws JSExn {
700 boolean returnJS = false;
701 if (o instanceof Boolean) {
702 throw new JSExn("Booleans do not have properties");
703 } else if (o instanceof Number) {
704 if (key.equals("toPrecision") || key.equals("toExponential") || key.equals("toFixed"))
708 // the string stuff applies to everything
709 String s = JS.toString(o);
711 // this is sort of ugly, but this list should never change
712 // These should provide a complete (enough) implementation of the ECMA-262 String object
715 case "length": return JS.N(s.length());
716 case "substring": returnJS = true; break;
717 case "charAt": returnJS = true; break;
718 case "charCodeAt": returnJS = true; break;
719 case "concat": returnJS = true; break;
720 case "indexOf": returnJS = true; break;
721 case "lastIndexOf": returnJS = true; break;
722 case "match": returnJS = true; break;
723 case "replace": returnJS = true; break;
724 case "search": returnJS = true; break;
725 case "slice": returnJS = true; break;
726 case "split": returnJS = true; break;
727 case "toLowerCase": returnJS = true; break;
728 case "toUpperCase": returnJS = true; break;
729 case "toString": returnJS = true; break;
730 case "substr": returnJS = true; break;
734 final Object target = o;
735 final String method = JS.toString(o);
737 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
738 if (nargs > 2) throw new JSExn("cannot call that method with that many arguments");
739 return callMethodOnPrimitive(target, method, a0, a1, a2, rest, nargs);
746 private static class Stub extends JS {
747 private Object method;
749 public Stub(JS obj, Object method) { this.obj = obj; this.method = method; }
750 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
751 return ((JS)obj).callMethod(method, a0, a1, a2, rest, nargs);