From 8c074d6ac60dced7aa8eb3dce6a35a27ac6bb5dd Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Fri, 23 May 2008 07:39:57 +0000 Subject: [PATCH] refactor get table with metatable --- src/core/org/luaj/vm/LFunction.java | 4 ++ src/core/org/luaj/vm/LValue.java | 5 +++ src/core/org/luaj/vm/LuaState.java | 60 +++++++++++++++++++++++------ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/core/org/luaj/vm/LFunction.java b/src/core/org/luaj/vm/LFunction.java index ea8d028f..e33e6179 100644 --- a/src/core/org/luaj/vm/LFunction.java +++ b/src/core/org/luaj/vm/LFunction.java @@ -29,6 +29,10 @@ public class LFunction extends LValue { return "function: "+hashCode(); } + public boolean isFunction() { + return true; + } + public void luaSetTable(LuaState vm, LValue table, LValue key, LValue val) { vm.pushlvalue( this ); vm.pushlvalue( table ); diff --git a/src/core/org/luaj/vm/LValue.java b/src/core/org/luaj/vm/LValue.java index aed88d7d..640e0e25 100644 --- a/src/core/org/luaj/vm/LValue.java +++ b/src/core/org/luaj/vm/LValue.java @@ -340,4 +340,9 @@ public class LValue { public boolean isTable() { return false; } + + /** Return true if this is a LFunction */ + public boolean isFunction() { + return false; + } } diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 92939988..5948868b 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -77,7 +77,8 @@ public class LuaState extends Lua { private static final int LUA_ERRERR = 5; private static final int LUA_MINSTACK = 20; - private static final int LUA_MINCALLS = 10; + private static final int LUA_MINCALLS = 10; + private static final int MAXTAGLOOP = 100; public int base = 0; public int top = 0; @@ -560,7 +561,7 @@ public class LuaState extends Lua { key = k[b]; table = cl.env; this.top = base + a; - this.stack[base+a] = table.luaGetTable(this, table, key); + luaV_gettable(table, key); continue; } case LuaState.OP_GETTABLE: { @@ -568,7 +569,7 @@ public class LuaState extends Lua { key = GETARG_RKC(k, i); table = this.stack[base + b]; this.top = base + a; - this.stack[base+a] = table.luaGetTable(this, table, key); + luaV_gettable(table, key); continue; } case LuaState.OP_SETGLOBAL: { @@ -603,7 +604,7 @@ public class LuaState extends Lua { rkb = GETARG_RKB(k, i); rkc = GETARG_RKC(k, i); this.top = base + a; - this.stack[base + a] = rkb.luaGetTable(this, rkb, rkc); + luaV_gettable(rkb, rkc); this.stack[base + a + 1] = rkb; // StkId rb = RB(i); // setobjs2s(L, ra+1, rb); @@ -797,8 +798,8 @@ public class LuaState extends Lua { case LuaState.OP_TFORLOOP: { cb = base + a + 3; /* call base */ base = cb; - adjustTop( cb + 3 ); System.arraycopy(this.stack, cb-3, this.stack, cb, 3); + top = cb + 3; // call the iterator c = LuaState.GETARG_C(i); @@ -907,6 +908,46 @@ public class LuaState extends Lua { return calls[cc-callStackDepth]; } + private LValue mtget(LValue t, LString tag) { + LTable mt = t.luaGetMetatable(); + if ( mt == null ) + return null; + LValue h = mt.get(tag); + return h.isNil()? null: h; + } + + private void luaV_gettable(LValue table, LValue key) { + LTable m; + LValue v,h=LNil.NIL,t=table; + for ( int loop=0; loop */ public void getglobal(String s) { - LTable t = this._G; - pushlvalue( t.luaGetTable(this, t, new LString(s)) ); + luaV_gettable(_G, new LString(s)); } /** @@ -1385,9 +1425,7 @@ public class LuaState extends Lua { public void gettable(int index) { LValue t = totable(index); LValue k = poplvalue(); - // todo: what if this triggers metatable ops - // pushlvalue( t.luaGetTable(this, t, k) ); - pushlvalue( t.luaGetTable(this, t, k) ); + luaV_gettable(t, k); } /**