diff --git a/src/core/org/luaj/lib/BaseLib.java b/src/core/org/luaj/lib/BaseLib.java index 2331ef5c..475d00ee 100644 --- a/src/core/org/luaj/lib/BaseLib.java +++ b/src/core/org/luaj/lib/BaseLib.java @@ -194,7 +194,7 @@ public class BaseLib extends LFunction { LTable t = vm.checktable(2); LValue v = vm.checkany(3); vm.argcheck(v.isTable() || v.isNil(), 3, "table or nil expected"); - t.luaSetMetatable(v); + t = t.luaSetMetatable(v); vm.resettop(); vm.pushlvalue(t); break; diff --git a/src/core/org/luaj/vm/LTable.java b/src/core/org/luaj/vm/LTable.java index 24ca9e70..36ba588c 100644 --- a/src/core/org/luaj/vm/LTable.java +++ b/src/core/org/luaj/vm/LTable.java @@ -267,7 +267,7 @@ public class LTable extends LValue { } /** Valid for tables */ - public LValue luaSetMetatable(LValue metatable) { + public LTable luaSetMetatable(LValue metatable) { if ( m_metatable != null && m_metatable.containsKey(TM_METATABLE) ) throw new LuaErrorException("cannot change a protected metatable"); if ( metatable == null || metatable.isNil() ) @@ -279,10 +279,10 @@ public class LTable extends LValue { return new LWeakTable(this); } this.m_metatable = t; - } - else - throw new LuaErrorException("nil or table expected, got "+metatable.luaGetTypeName()); - return null; + } else { + throw new LuaErrorException("not a table: "+metatable.luaGetTypeName()); + } + return this; } public String toJavaString() { diff --git a/src/core/org/luaj/vm/LValue.java b/src/core/org/luaj/vm/LValue.java index f83d6eb2..6d21ac93 100644 --- a/src/core/org/luaj/vm/LValue.java +++ b/src/core/org/luaj/vm/LValue.java @@ -177,9 +177,11 @@ public class LValue { } /** Valid for tables - * @return TODO*/ - public LValue luaSetMetatable(LValue metatable) { - throw new LuaErrorException( "bad argument #1 to 'setmetatable' (table expected, got "+metatable.luaGetTypeName()+")"); + * @param the new LTable, or null or LNil.NIL to reset the metatable to none + * @return this if unchanged, or new LTable if copied using weak table + */ + public LTable luaSetMetatable(LValue metatable) { + throw new LuaErrorException( "cannot set metatable for "+metatable.luaGetTypeName()); } /** Valid for all types: return the int value identifying the type of this value */ diff --git a/src/test/res/weaktable.lua b/src/test/res/weaktable.lua index 62dc189b..60a9140d 100644 --- a/src/test/res/weaktable.lua +++ b/src/test/res/weaktable.lua @@ -1,5 +1,4 @@ - -- normalized printing function eles(t,f) f = f or pairs