From 14dcf4ac275ea25af9e7bb517e61a82d2b935d0d Mon Sep 17 00:00:00 2001 From: brian Date: Tue, 6 Jul 2004 13:38:28 +0000 Subject: [PATCH] more optimal jsfunction calls darcs-hash:20040706133828-24bed-bcbafea6674085776c6810a57d1412d6268a7915.gz --- src/org/ibex/js/Interpreter.java | 69 +++++++++++++++++++++++++++----------- src/org/ibex/js/JS.java | 2 +- src/org/ibex/js/JSFunction.java | 9 ++--- src/org/ibex/js/Parser.java | 6 ++-- src/org/ibex/js/Test.java | 2 +- 5 files changed, 56 insertions(+), 32 deletions(-) diff --git a/src/org/ibex/js/Interpreter.java b/src/org/ibex/js/Interpreter.java index 73de955..c17fcc3 100644 --- a/src/org/ibex/js/Interpreter.java +++ b/src/org/ibex/js/Interpreter.java @@ -22,7 +22,7 @@ class Interpreter implements ByteCodes, Tokens { final Stack stack = new Stack(); ///< the object stack int pc = 0; ///< the program counter - Interpreter(JSFunction f, boolean pauseable, JSArray args) { + Interpreter(JSFunction f, boolean pauseable, JSArgs args) { this.f = f; this.pausecount = pauseable ? 0 : -1; this.scope = new JSScope(f.parentScope); @@ -188,9 +188,7 @@ class Interpreter implements ByteCodes, Tokens { if(t != null) { tm.t = t; stack.push(tm); - JSArray args = new JSArray(); - args.addElement(tm.val); - stack.push(args); + stack.push(new JSArgs(tm.val,t.f)); f = t.f; scope = new JSScope(f.parentScope); pc = -1; @@ -246,9 +244,7 @@ class Interpreter implements ByteCodes, Tokens { if(t != null) { stack.push(new TrapMarker(this,t,target,key,val)); - JSArray args = new JSArray(); - args.addElement(val); - stack.push(args); + stack.push(new JSArgs(t.f)); f = t.f; scope = new TrapScope(f.parentScope,target,f,key); pc = -1; @@ -300,7 +296,7 @@ class Interpreter implements ByteCodes, Tokens { if(t != null) { stack.push(new TrapMarker(this,t,(JS)target,key,null)); - stack.push(new JSArray()); + stack.push(new JSArgs(t.f)); f = t.f; scope = new TrapScope(f.parentScope,target,f,key); pc = -1; @@ -316,6 +312,13 @@ class Interpreter implements ByteCodes, Tokens { case CALL: case CALLMETHOD: { int numArgs = JS.toInt((JS)arg); + + JS[] rest = numArgs > 3 ? new JS[numArgs - 3] : null; + for(int i=numArgs - 1; i>2; i--) rest[i-3] = stack.pop(); + JS a2 = numArgs <= 2 ? null : stack.pop(); + JS a1 = numArgs <= 1 ? null : stack.pop(); + JS a0 = numArgs <= 0 ? null : stack.pop(); + JS method = null; JS ret = null; JS object = stack.pop(); @@ -333,19 +336,10 @@ class Interpreter implements ByteCodes, Tokens { stack.pop(); } } - JS[] rest = numArgs > 3 ? new JS[numArgs - 3] : null; - for(int i=numArgs - 1; i>2; i--) rest[i-3] = stack.pop(); - JS a2 = numArgs <= 2 ? null : stack.pop(); - JS a1 = numArgs <= 1 ? null : stack.pop(); - JS a0 = numArgs <= 0 ? null : stack.pop(); - if (object instanceof JSFunction) { - // FIXME: use something similar to call0/call1/call2 here - JSArray arguments = new JSArray(); - for(int i=0; i= 0 && n < nargs ? rest[n-3] : null; + } + } + //#switch(JS.toString(key)) + case "callee": return callee; + case "length": return JS.N(nargs); + //#end + return super.get(key); + } + } static class Stub extends JS { private JS method; @@ -653,9 +681,10 @@ class Interpreter implements ByteCodes, Tokens { JS[] stack2 = new JS[stack.length * 2]; System.arraycopy(stack,0,stack2,0,stack.length); } - // FIXME: Eliminate all uses of SWAP n>1 so we don't need this public int size() { return sp; } - public void setElementAt(JS o, int i) { stack[i] = o; } public JS elementAt(int i) { return stack[i]; } + + // FIXME: Eliminate all uses of SWAP n>1 so we don't need this + public void setElementAt(JS o, int i) { stack[i] = o; } } } diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index 68fac37..5d1b58d 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -125,7 +125,7 @@ public abstract class JS { boolean get; switch(i.f.op[i.pc]) { case Tokens.RETURN: case ByteCodes.PUT: get = false; break; - case ByteCodes.GET: get = true; break; + case ByteCodes.GET: case ByteCodes.CALL: get = true; break; default: throw new Error("should never happen"); } i.pausecount++; diff --git a/src/org/ibex/js/JSFunction.java b/src/org/ibex/js/JSFunction.java index 020cb11..61b5268 100644 --- a/src/org/ibex/js/JSFunction.java +++ b/src/org/ibex/js/JSFunction.java @@ -28,7 +28,7 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task { // FEATURE: make sure that this can only be called from the Scheduler... /** if you enqueue a function, it gets invoked in its own pauseable context */ public void perform() throws JSExn { - Interpreter i = new Interpreter(this, true, new JSArray()); + Interpreter i = new Interpreter(this, true, new Interpreter.JSArgs(this)); i.resume(); } @@ -61,12 +61,7 @@ class JSFunction extends JS implements ByteCodes, Tokens, Task { /** Note: code gets run in an unpauseable context. */ public JS call(JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn { - JSArray args = new JSArray(); - if (nargs > 0) args.addElement(a0); - if (nargs > 1) args.addElement(a1); - if (nargs > 2) args.addElement(a2); - for(int i=3; i