Refactor gettable metatable processing

This commit is contained in:
James Roseborough
2008-05-23 14:58:20 +00:00
parent 23609197b9
commit 1b284e26c2

View File

@@ -578,7 +578,7 @@ public class LuaState extends Lua {
val = this.stack[base + a]; val = this.stack[base + a];
table = cl.env; table = cl.env;
this.top = base + a; this.top = base + a;
table.luaSetTable(this, table, key, val); luaV_settable(table, key, val);
continue; continue;
} }
case LuaState.OP_SETUPVAL: { case LuaState.OP_SETUPVAL: {
@@ -591,7 +591,7 @@ public class LuaState extends Lua {
val = GETARG_RKC(k, i); val = GETARG_RKC(k, i);
table = this.stack[base + a]; table = this.stack[base + a];
this.top = base + a; this.top = base + a;
table.luaSetTable(this, table, key, val); luaV_settable(table, key, val);
continue; continue;
} }
case LuaState.OP_NEWTABLE: { case LuaState.OP_NEWTABLE: {
@@ -916,7 +916,11 @@ public class LuaState extends Lua {
return h.isNil()? null: h; return h.isNil()? null: h;
} }
private void luaV_gettable(LValue table, LValue key) { private void indexError(LValue nontable) {
error( "attempt to index ? (a "+nontable.luaGetTypeName()+" value)", 1 );
}
public void luaV_gettable(LValue table, LValue key) {
LTable m; LTable m;
LValue v,h=LNil.NIL,t=table; LValue v,h=LNil.NIL,t=table;
for ( int loop=0; loop<MAXTAGLOOP; loop++ ) { for ( int loop=0; loop<MAXTAGLOOP; loop++ ) {
@@ -934,7 +938,7 @@ public class LuaState extends Lua {
} else { } else {
h = mtget(t, LTable.TM_INDEX); h = mtget(t, LTable.TM_INDEX);
if ( h == null ) { if ( h == null ) {
error("type error: "+t.luaGetTypeName()+" (index)"); indexError(t);
} }
} }
if (h.isFunction()) { if (h.isFunction()) {
@@ -949,6 +953,40 @@ public class LuaState extends Lua {
error("loop in gettable"); error("loop in gettable");
} }
public void luaV_settable(LValue table, LValue key, LValue val) {
LTable m;
LValue v,h=LNil.NIL,t=table;
for ( int loop=0; loop<MAXTAGLOOP; loop++ ) {
if ( t.isTable() ) {
LTable lt = (LTable) t;
if ( lt.containsKey(key) ) {
lt.put(key, val);
return;
}
h = mtget(t, LTable.TM_NEWINDEX);
if ( h == null ) {
lt.put(key, val);
return;
}
} else {
h = mtget(t, LTable.TM_NEWINDEX);
if ( h == null ) {
indexError(t);
}
}
if (h.isFunction()) {
pushlvalue(h);
pushlvalue(table);
pushlvalue(key);
pushlvalue(val);
call(3,0);
return;
}
t = h;
}
error("loop in settable");
}
//=============================================================== //===============================================================
// Lua Java API // Lua Java API
//=============================================================== //===============================================================
@@ -2038,8 +2076,7 @@ public class LuaState extends Lua {
*/ */
public void setfield(int index, LString k) { public void setfield(int index, LString k) {
LTable t = totable(index); LTable t = totable(index);
LValue v = poplvalue(); luaV_settable(t, k, poplvalue());
t.luaSetTable(this, t, k, v);
} }
/** /**
@@ -2056,9 +2093,7 @@ public class LuaState extends Lua {
* </pre> * </pre>
*/ */
public void setglobal(String name) { public void setglobal(String name) {
LTable g = this._G; luaV_settable(_G, new LString(name), poplvalue());
LValue v = poplvalue();
g.luaSetTable(this, g, new LString(name), v);
} }
/** /**
@@ -2098,7 +2133,7 @@ public class LuaState extends Lua {
LTable t = totable(index); LTable t = totable(index);
LValue v = poplvalue(); LValue v = poplvalue();
LValue k = poplvalue(); LValue k = poplvalue();
t.luaSetTable(this, t, k, v); luaV_settable(t, k, v);
} }
/** /**