Clear interface between references and predefines

The reference system has a defined way to add initial values to the
table where it operates.
This commit is contained in:
Roberto Ierusalimschy
2024-01-15 11:31:49 -03:00
parent 8eb0abc9db
commit 17e0c29d9b
7 changed files with 92 additions and 43 deletions

View File

@@ -467,7 +467,7 @@ for i = 1,lim do
prog[#prog + 1] = "pushnum " .. i * 10
end
prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
prog[#prog + 1] = "rawgeti R !G" -- get global table in registry
prog[#prog + 1] = "insert " .. -(2*lim + 2)
for i = 1,lim do
@@ -930,28 +930,30 @@ checkerr("FILE%* expected, got userdata", io.input, x)
assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
local d = T.ref(a);
local e = T.ref(b);
local f = T.ref(c);
t = {T.getref(d), T.getref(e), T.getref(f)}
-- Test references in an arbitrary table
local reftable = {}
local d = T.ref(a, reftable);
local e = T.ref(b, reftable);
local f = T.ref(c, reftable);
t = {T.getref(d, reftable), T.getref(e, reftable), T.getref(f, reftable)}
assert(t[1] == a and t[2] == b and t[3] == c)
t=nil; a=nil; c=nil;
T.unref(e); T.unref(f)
T.unref(e, reftable); T.unref(f, reftable)
collectgarbage()
-- check that unref objects have been collected
assert(#cl == 1 and cl[1] == nc)
x = T.getref(d)
x = T.getref(d, reftable)
assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
x =nil
tt.b = b -- create cycle
tt=nil -- frees tt for GC
A = nil
b = nil
T.unref(d);
T.unref(d, reftable);
local n5 = T.newuserdata(0)
debug.setmetatable(n5, {__gc=F})
n5 = T.udataval(n5)
@@ -960,6 +962,21 @@ assert(#cl == 4)
-- check order of collection
assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
-- reuse a reference in 'reftable'
T.unref(T.ref(23, reftable), reftable)
do -- check reftable
local count = 0
local i = 1
while reftable[i] ~= 0 do
i = reftable[i] -- traverse linked list of free references
count = count + 1
end
-- maximum number of simultaneously locked objects was 3
assert(count == 3 and #reftable == 3 + 1) -- +1 for reserved [1]
end
collectgarbage"restart"
@@ -1363,8 +1380,8 @@ end)
-- testing threads
-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
local mt = T.testC("rawgeti R 1; return 1")
-- get main thread from registry
local mt = T.testC("rawgeti R !M; return 1")
assert(type(mt) == "thread" and coroutine.running() == mt)

View File

@@ -681,7 +681,7 @@ else
c == "ERRRUN" and d == 4)
a, b, c, d = T.testC([[
rawgeti R 1 # get main thread
rawgeti R !M # get main thread
pushnum 10;
pushnum 20;
resume -3 2;
@@ -699,7 +699,7 @@ else
assert(T.testC(state, "newthread; isyieldable -1; remove 1; return 1"))
-- main thread is not yieldable
assert(not T.testC(state, "rawgeti R 1; isyieldable -1; remove 1; return 1"))
assert(not T.testC(state, "rawgeti R !M; isyieldable -1; remove 1; return 1"))
T.testC(state, "settop 0")
@@ -711,7 +711,7 @@ else
return 'ok']]))
local t = table.pack(T.testC(state, [[
rawgeti R 1 # get main thread
rawgeti R !M # get main thread
pushstring 'XX'
getglobal X # get function for body
pushstring AA # arg
@@ -720,7 +720,7 @@ else
setglobal T # top
setglobal B # second yielded value
setglobal A # fist yielded value
rawgeti R 1 # get main thread
rawgeti R !M # get main thread
pushnum 5 # arg (noise)
resume 1 1 # after coroutine ends, previous stack is back
pushstatus