瀏覽代碼

Joined common code in 'lua_rawset' and 'lua_rawsetp'

Roberto Ierusalimschy 5 年之前
父節點
當前提交
c646e57fd6
共有 3 個文件被更改,包括 23 次插入21 次删除
  1. 14 17
      lapi.c
  2. 4 2
      testes/api.lua
  3. 5 2
      testes/strings.lua

+ 14 - 17
lapi.c

@@ -848,42 +848,39 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
 }
 
 
-LUA_API void lua_rawset (lua_State *L, int idx) {
+static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
   Table *t;
   TValue *slot;
   lua_lock(L);
-  api_checknelems(L, 2);
+  api_checknelems(L, n);
   t = gettable(L, idx);
-  slot = luaH_set(L, t, s2v(L->top - 2));
+  slot = luaH_set(L, t, key);
   setobj2t(L, slot, s2v(L->top - 1));
+  L->top -= n;
   invalidateTMcache(t);
   luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
-  L->top -= 2;
   lua_unlock(L);
 }
 
 
-LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
-  Table *t;
-  lua_lock(L);
-  api_checknelems(L, 1);
-  t = gettable(L, idx);
-  luaH_setint(L, t, n, s2v(L->top - 1));
-  luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
-  L->top--;
-  lua_unlock(L);
+LUA_API void lua_rawset (lua_State *L, int idx) {
+  aux_rawset(L, idx, s2v(L->top - 2), 2);
 }
 
 
 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
+  TValue k;
+  setpvalue(&k, cast_voidp(p));
+  aux_rawset(L, idx, &k, 1);
+}
+
+
+LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
   Table *t;
-  TValue k, *slot;
   lua_lock(L);
   api_checknelems(L, 1);
   t = gettable(L, idx);
-  setpvalue(&k, cast_voidp(p));
-  slot = luaH_set(L, t, &k);
-  setobj2t(L, slot, s2v(L->top - 1));
+  luaH_setint(L, t, n, s2v(L->top - 1));
   luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
   L->top--;
   lua_unlock(L);

+ 4 - 2
testes/api.lua

@@ -516,9 +516,11 @@ print"+"
 
 do   -- getp/setp
   local a = {}
-  T.testC("rawsetp 2 1", a, 20)
+  local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
+  assert(a == a1)
   assert(a[T.pushuserdata(1)] == 20)
-  assert(T.testC("rawgetp -1 1; return 1", a) == 20)
+  local a1, res = T.testC("rawgetp -1 1; return 2", a)
+  assert(a == a1 and res == 20)
 end
 
 

+ 5 - 2
testes/strings.lua

@@ -161,18 +161,21 @@ do  -- tests for '%p' format
   local null = string.format("%p", nil)
   assert(string.format("%p", {}) ~= null)
   assert(string.format("%p", 4) == null)
+  assert(string.format("%p", true) == null)
   assert(string.format("%p", print) ~= null)
   assert(string.format("%p", coroutine.running()) ~= null)
+  assert(string.format("%p", io.stdin) ~= null)
+  assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
   do
     local t1 = {}; local t2 = {}
     assert(string.format("%p", t1) ~= string.format("%p", t2))
   end
-  do     -- short strings
+  do     -- short strings are internalized
     local s1 = string.rep("a", 10)
     local s2 = string.rep("a", 10)
   assert(string.format("%p", s1) == string.format("%p", s2))
   end
-  do     -- long strings
+  do     -- long strings aren't internalized
     local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
     assert(string.format("%p", s1) ~= string.format("%p", s2))
   end