Cleanup/fixes for from_table().

This commit is contained in:
Bruce Hill 2018-02-27 13:47:06 -08:00
parent 1a06de84dd
commit 096973f06d
2 changed files with 11 additions and 3 deletions

View File

@ -267,11 +267,13 @@ static int Lfrom_table(lua_State *L)
lua_getfield(L, -1, "__fields");
int n;
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getfield(L, 2, "n");
if (lua_isnil(L, -1)) {
luaL_error(L, "table needs an 'n' field to track its length");
n = lua_objlen(L, 2);
} else {
n = luaL_checkinteger(L, -1);
}
n = luaL_checkinteger(L, -1);
lua_pop(L, 1);
if (! lua_checkstack(L, n)) {
luaL_error(L, "Insufficient stack space!");
@ -701,7 +703,7 @@ static int Lmake_class(lua_State *L)
break;
}
default: {
luaL_error(L, "expected number, table, or nil");
luaL_error(L, "immutable expected the fields to be either table or nil");
}
}
// Stack: [CLS]

View File

@ -82,6 +82,12 @@ test("Testing from_table", function()
assert(Vec:from_table({x=1}) == Vec(1, nil))
end)
test("Testing from_table for tuples", function()
local T = immutable(nil)
assert(T(1,2) == T:from_table(table.pack(1,2)))
assert(T(1,2) == T:from_table({1,2}))
end)
test("Testing equality", function()
also_v = Vec(1,3)
assert(v == also_v)