diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-12-17 15:45:13 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-12-17 15:45:13 -0300 |
commit | c646e57fd6307bd891e4e50ef5d6ee56b34e4cac (patch) | |
tree | e8f2d8a7bcb9f5c51ddc03f94f576f2fc6f43f06 | |
parent | e0ab13c62f2c1af0af955f173beb3ea6473e8064 (diff) | |
download | lua-c646e57fd6307bd891e4e50ef5d6ee56b34e4cac.tar.gz lua-c646e57fd6307bd891e4e50ef5d6ee56b34e4cac.tar.bz2 lua-c646e57fd6307bd891e4e50ef5d6ee56b34e4cac.zip |
Joined common code in 'lua_rawset' and 'lua_rawsetp'
-rw-r--r-- | lapi.c | 31 | ||||
-rw-r--r-- | testes/api.lua | 6 | ||||
-rw-r--r-- | testes/strings.lua | 7 |
3 files changed, 23 insertions, 21 deletions
@@ -848,42 +848,39 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { | |||
848 | } | 848 | } |
849 | 849 | ||
850 | 850 | ||
851 | LUA_API void lua_rawset (lua_State *L, int idx) { | 851 | static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { |
852 | Table *t; | 852 | Table *t; |
853 | TValue *slot; | 853 | TValue *slot; |
854 | lua_lock(L); | 854 | lua_lock(L); |
855 | api_checknelems(L, 2); | 855 | api_checknelems(L, n); |
856 | t = gettable(L, idx); | 856 | t = gettable(L, idx); |
857 | slot = luaH_set(L, t, s2v(L->top - 2)); | 857 | slot = luaH_set(L, t, key); |
858 | setobj2t(L, slot, s2v(L->top - 1)); | 858 | setobj2t(L, slot, s2v(L->top - 1)); |
859 | L->top -= n; | ||
859 | invalidateTMcache(t); | 860 | invalidateTMcache(t); |
860 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); | 861 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); |
861 | L->top -= 2; | ||
862 | lua_unlock(L); | 862 | lua_unlock(L); |
863 | } | 863 | } |
864 | 864 | ||
865 | 865 | ||
866 | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { | 866 | LUA_API void lua_rawset (lua_State *L, int idx) { |
867 | Table *t; | 867 | aux_rawset(L, idx, s2v(L->top - 2), 2); |
868 | lua_lock(L); | ||
869 | api_checknelems(L, 1); | ||
870 | t = gettable(L, idx); | ||
871 | luaH_setint(L, t, n, s2v(L->top - 1)); | ||
872 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); | ||
873 | L->top--; | ||
874 | lua_unlock(L); | ||
875 | } | 868 | } |
876 | 869 | ||
877 | 870 | ||
878 | LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { | 871 | LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { |
872 | TValue k; | ||
873 | setpvalue(&k, cast_voidp(p)); | ||
874 | aux_rawset(L, idx, &k, 1); | ||
875 | } | ||
876 | |||
877 | |||
878 | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { | ||
879 | Table *t; | 879 | Table *t; |
880 | TValue k, *slot; | ||
881 | lua_lock(L); | 880 | lua_lock(L); |
882 | api_checknelems(L, 1); | 881 | api_checknelems(L, 1); |
883 | t = gettable(L, idx); | 882 | t = gettable(L, idx); |
884 | setpvalue(&k, cast_voidp(p)); | 883 | luaH_setint(L, t, n, s2v(L->top - 1)); |
885 | slot = luaH_set(L, t, &k); | ||
886 | setobj2t(L, slot, s2v(L->top - 1)); | ||
887 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); | 884 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); |
888 | L->top--; | 885 | L->top--; |
889 | lua_unlock(L); | 886 | lua_unlock(L); |
diff --git a/testes/api.lua b/testes/api.lua index b2680633..b5657416 100644 --- a/testes/api.lua +++ b/testes/api.lua | |||
@@ -516,9 +516,11 @@ print"+" | |||
516 | 516 | ||
517 | do -- getp/setp | 517 | do -- getp/setp |
518 | local a = {} | 518 | local a = {} |
519 | T.testC("rawsetp 2 1", a, 20) | 519 | local a1 = T.testC("rawsetp 2 1; return 1", a, 20) |
520 | assert(a == a1) | ||
520 | assert(a[T.pushuserdata(1)] == 20) | 521 | assert(a[T.pushuserdata(1)] == 20) |
521 | assert(T.testC("rawgetp -1 1; return 1", a) == 20) | 522 | local a1, res = T.testC("rawgetp -1 1; return 2", a) |
523 | assert(a == a1 and res == 20) | ||
522 | end | 524 | end |
523 | 525 | ||
524 | 526 | ||
diff --git a/testes/strings.lua b/testes/strings.lua index f2f61413..2ce3ebc3 100644 --- a/testes/strings.lua +++ b/testes/strings.lua | |||
@@ -161,18 +161,21 @@ do -- tests for '%p' format | |||
161 | local null = string.format("%p", nil) | 161 | local null = string.format("%p", nil) |
162 | assert(string.format("%p", {}) ~= null) | 162 | assert(string.format("%p", {}) ~= null) |
163 | assert(string.format("%p", 4) == null) | 163 | assert(string.format("%p", 4) == null) |
164 | assert(string.format("%p", true) == null) | ||
164 | assert(string.format("%p", print) ~= null) | 165 | assert(string.format("%p", print) ~= null) |
165 | assert(string.format("%p", coroutine.running()) ~= null) | 166 | assert(string.format("%p", coroutine.running()) ~= null) |
167 | assert(string.format("%p", io.stdin) ~= null) | ||
168 | assert(string.format("%p", io.stdin) == string.format("%p", io.stdin)) | ||
166 | do | 169 | do |
167 | local t1 = {}; local t2 = {} | 170 | local t1 = {}; local t2 = {} |
168 | assert(string.format("%p", t1) ~= string.format("%p", t2)) | 171 | assert(string.format("%p", t1) ~= string.format("%p", t2)) |
169 | end | 172 | end |
170 | do -- short strings | 173 | do -- short strings are internalized |
171 | local s1 = string.rep("a", 10) | 174 | local s1 = string.rep("a", 10) |
172 | local s2 = string.rep("a", 10) | 175 | local s2 = string.rep("a", 10) |
173 | assert(string.format("%p", s1) == string.format("%p", s2)) | 176 | assert(string.format("%p", s1) == string.format("%p", s2)) |
174 | end | 177 | end |
175 | do -- long strings | 178 | do -- long strings aren't internalized |
176 | local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) | 179 | local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) |
177 | assert(string.format("%p", s1) ~= string.format("%p", s2)) | 180 | assert(string.format("%p", s1) ~= string.format("%p", s2)) |
178 | end | 181 | end |