From 4487c28ced3dcf47c3ee19b6f6eeb0089ec64ba5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Jun 2019 17:38:58 -0300 Subject: [PATCH] A few more tests for table access in the API Added tests where the table being accessed is also the index or value in the operation. --- ltests.c | 16 ++++++++++++++++ testes/api.lua | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/ltests.c b/ltests.c index f786eeb3..dc830657 100644 --- a/ltests.c +++ b/ltests.c @@ -1488,6 +1488,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("pushfstringP") { lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); } + else if EQ("rawget") { + int t = getindex; + lua_rawget(L1, t); + } else if EQ("rawgeti") { int t = getindex; lua_rawgeti(L1, t, getnum); @@ -1496,6 +1500,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int t = getindex; lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); } + else if EQ("rawset") { + int t = getindex; + lua_rawset(L1, t); + } + else if EQ("rawseti") { + int t = getindex; + lua_rawseti(L1, t, getnum); + } else if EQ("rawsetp") { int t = getindex; lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); @@ -1538,6 +1550,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *s = getstring; lua_setfield(L1, t, s); } + else if EQ("seti") { + int t = getindex; + lua_seti(L1, t, getnum); + } else if EQ("setglobal") { const char *s = getstring; lua_setglobal(L1, s); diff --git a/testes/api.lua b/testes/api.lua index bcc04dac..8f4e89ac 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -498,7 +498,53 @@ do -- getp/setp local a = {} T.testC("rawsetp 2 1", a, 20) assert(a[T.pushuserdata(1)] == 20) - assert(T.testC("rawgetp 2 1; return 1", a) == 20) + assert(T.testC("rawgetp -1 1; return 1", a) == 20) +end + + +do -- using the table itself as index + local a = {} + a[a] = 10 + local prog = "gettable -1; return *" + local res = {T.testC(prog, a)} + assert(#res == 2 and res[1] == prog and res[2] == 10) + + local prog = "settable -2; return *" + local res = {T.testC(prog, a, 20)} + assert(a[a] == 20) + assert(#res == 1 and res[1] == prog) + + -- raw + a[a] = 10 + local prog = "rawget -1; return *" + local res = {T.testC(prog, a)} + assert(#res == 2 and res[1] == prog and res[2] == 10) + + local prog = "rawset -2; return *" + local res = {T.testC(prog, a, 20)} + assert(a[a] == 20) + assert(#res == 1 and res[1] == prog) + + -- using the table as the value to set + local prog = "rawset -1; return *" + local res = {T.testC(prog, 30, a)} + assert(a[30] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "settable -1; return *" + local res = {T.testC(prog, 40, a)} + assert(a[40] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "rawseti -1 100; return *" + local res = {T.testC(prog, a)} + assert(a[100] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "seti -1 200; return *" + local res = {T.testC(prog, a)} + assert(a[200] == a) + assert(#res == 1 and res[1] == prog) end a = {x=0, y=12}