fix match thread, table key

This commit is contained in:
zixun
2019-10-19 11:25:42 +08:00
committed by 云风
parent 4ddc1577ef
commit 8a1d7006eb
2 changed files with 83 additions and 2 deletions

View File

@@ -83,6 +83,34 @@ clone_table(lua_State *L) {
return 1; return 1;
} }
static int
lco_stackvalues(lua_State* L) {
lua_State *cL = lua_tothread(L, 1);
luaL_argcheck(L, cL, 1, "thread expected");
int n = 0;
if(cL != L) {
luaL_checktype(L, 2, LUA_TTABLE);
n = lua_gettop(cL);
if(n > 0) {
lua_checkstack(L, n+1);
int top = lua_gettop(L);
lua_xmove(cL, L, n);
int i=0;
for(i=0; i<n; i++) {
lua_pushvalue(L, top+1+i);
lua_seti(L, 2, i+1);
}
lua_xmove(L, cL, n);
} else {
n = 0;
}
}
lua_pushinteger(L, n);
return 1;
}
struct state_ud { struct state_ud {
lua_State *L; lua_State *L;
}; };
@@ -218,6 +246,7 @@ luaopen_skynet_sharetable_core(lua_State *L) {
luaL_checkversion(L); luaL_checkversion(L);
luaL_Reg l[] = { luaL_Reg l[] = {
{ "clone", clone_table }, { "clone", clone_table },
{ "stackvalues", lco_stackvalues },
{ "matrix", matrix_from_file }, { "matrix", matrix_from_file },
{ "is_sharedtable", lis_sharedtable }, { "is_sharedtable", lis_sharedtable },
{ NULL, NULL }, { NULL, NULL },

View File

@@ -2,6 +2,7 @@ local skynet = require "skynet"
local service = require "skynet.service" local service = require "skynet.service"
local core = require "skynet.sharetable.core" local core = require "skynet.sharetable.core"
local is_sharedtable = core.is_sharedtable local is_sharedtable = core.is_sharedtable
local stackvalues = core.stackvalues
local function sharetable_service() local function sharetable_service()
local skynet = require "skynet" local skynet = require "skynet"
@@ -257,6 +258,10 @@ local function resolve_replace(replace_map)
local function match_value(v) local function match_value(v)
assert(v ~= nil) assert(v ~= nil)
if v == RECORD then
return
end
local tv = type(v) local tv = type(v)
local f = match[tv] local f = match[tv]
if record_map[v] or is_sharedtable(v) then if record_map[v] or is_sharedtable(v) then
@@ -283,7 +288,13 @@ local function resolve_replace(replace_map)
end end
local function match_table(t) local function match_table(t)
local keys = {}
for k,v in next, t do for k,v in next, t do
local tk = type(k)
if match[tk] then
keys[#keys+1] = k
end
local nv = replace_map[v] local nv = replace_map[v]
if nv then if nv then
nv = getnv(v) nv = getnv(v)
@@ -292,6 +303,20 @@ local function resolve_replace(replace_map)
match_value(v) match_value(v)
end end
end end
for _, old_k in ipairs(keys) do
local new_k = replace_map[old_k]
if new_k then
local value = rawget(t, old_k)
new_k = getnv(old_k)
rawset(t, old_k, nil)
if new_k then
rawset(t, new_k, value)
end
else
match_value(old_k)
end
end
match_mt(t) match_mt(t)
end end
@@ -345,8 +370,33 @@ local function resolve_replace(replace_map)
match_funcinfo(info) match_funcinfo(info)
end end
local stack_values_tmp = {}
local function match_thread(co) local function match_thread(co)
-- match stackvalues
local n = stackvalues(co, stack_values_tmp)
for i=1,n do
local v = stack_values_tmp[i]
stack_values_tmp[i] = nil
match_value(v)
end
-- match callinfo
local level = 1 local level = 1
-- jump the fucntion from sharetable.update to top
local is_self = coroutine.running() == co
if is_self then
while true do
local info = getinfo(co, level, "uf")
level = level + 1
if not info then
level = 1
break
elseif info.func == sharetable.update then
break
end
end
end
while true do while true do
local info = getinfo(co, level, "uf") local info = getinfo(co, level, "uf")
if not info then if not info then
@@ -380,14 +430,16 @@ function sharetable.update(...)
for old_t,_ in pairs(map) do for old_t,_ in pairs(map) do
if old_t ~= new_t then if old_t ~= new_t then
insert_replace(old_t, new_t, replace_map) insert_replace(old_t, new_t, replace_map)
map[old_t] = nil
end end
end end
RECORD[name] = nil
end end
end end
if next(replace_map) then
resolve_replace(replace_map) resolve_replace(replace_map)
end end
end
return sharetable return sharetable