diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-15 11:31:49 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-15 11:31:49 -0300 |
commit | 17e0c29d9b435392016b707309ed51409b0aea12 (patch) | |
tree | c7178cf68fac2f1f9c3b22ce50be29b67f416cc0 /testes/api.lua | |
parent | 8eb0abc9db4d47db5192bed18565e3d1aa53566d (diff) | |
download | lua-17e0c29d9b435392016b707309ed51409b0aea12.tar.gz lua-17e0c29d9b435392016b707309ed51409b0aea12.tar.bz2 lua-17e0c29d9b435392016b707309ed51409b0aea12.zip |
Clear interface between references and predefines
The reference system has a defined way to add initial values to the
table where it operates.
Diffstat (limited to 'testes/api.lua')
-rw-r--r-- | testes/api.lua | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/testes/api.lua b/testes/api.lua index 85dadb69..ca4b3fb4 100644 --- a/testes/api.lua +++ b/testes/api.lua | |||
@@ -467,7 +467,7 @@ for i = 1,lim do | |||
467 | prog[#prog + 1] = "pushnum " .. i * 10 | 467 | prog[#prog + 1] = "pushnum " .. i * 10 |
468 | end | 468 | end |
469 | 469 | ||
470 | prog[#prog + 1] = "rawgeti R 2" -- get global table in registry | 470 | prog[#prog + 1] = "rawgeti R !G" -- get global table in registry |
471 | prog[#prog + 1] = "insert " .. -(2*lim + 2) | 471 | prog[#prog + 1] = "insert " .. -(2*lim + 2) |
472 | 472 | ||
473 | for i = 1,lim do | 473 | for i = 1,lim do |
@@ -930,28 +930,30 @@ checkerr("FILE%* expected, got userdata", io.input, x) | |||
930 | 930 | ||
931 | assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) | 931 | assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) |
932 | 932 | ||
933 | local d = T.ref(a); | 933 | -- Test references in an arbitrary table |
934 | local e = T.ref(b); | 934 | local reftable = {} |
935 | local f = T.ref(c); | 935 | local d = T.ref(a, reftable); |
936 | t = {T.getref(d), T.getref(e), T.getref(f)} | 936 | local e = T.ref(b, reftable); |
937 | local f = T.ref(c, reftable); | ||
938 | t = {T.getref(d, reftable), T.getref(e, reftable), T.getref(f, reftable)} | ||
937 | assert(t[1] == a and t[2] == b and t[3] == c) | 939 | assert(t[1] == a and t[2] == b and t[3] == c) |
938 | 940 | ||
939 | t=nil; a=nil; c=nil; | 941 | t=nil; a=nil; c=nil; |
940 | T.unref(e); T.unref(f) | 942 | T.unref(e, reftable); T.unref(f, reftable) |
941 | 943 | ||
942 | collectgarbage() | 944 | collectgarbage() |
943 | 945 | ||
944 | -- check that unref objects have been collected | 946 | -- check that unref objects have been collected |
945 | assert(#cl == 1 and cl[1] == nc) | 947 | assert(#cl == 1 and cl[1] == nc) |
946 | 948 | ||
947 | x = T.getref(d) | 949 | x = T.getref(d, reftable) |
948 | assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) | 950 | assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) |
949 | x =nil | 951 | x =nil |
950 | tt.b = b -- create cycle | 952 | tt.b = b -- create cycle |
951 | tt=nil -- frees tt for GC | 953 | tt=nil -- frees tt for GC |
952 | A = nil | 954 | A = nil |
953 | b = nil | 955 | b = nil |
954 | T.unref(d); | 956 | T.unref(d, reftable); |
955 | local n5 = T.newuserdata(0) | 957 | local n5 = T.newuserdata(0) |
956 | debug.setmetatable(n5, {__gc=F}) | 958 | debug.setmetatable(n5, {__gc=F}) |
957 | n5 = T.udataval(n5) | 959 | n5 = T.udataval(n5) |
@@ -960,6 +962,21 @@ assert(#cl == 4) | |||
960 | -- check order of collection | 962 | -- check order of collection |
961 | assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) | 963 | assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) |
962 | 964 | ||
965 | -- reuse a reference in 'reftable' | ||
966 | T.unref(T.ref(23, reftable), reftable) | ||
967 | |||
968 | do -- check reftable | ||
969 | local count = 0 | ||
970 | local i = 1 | ||
971 | while reftable[i] ~= 0 do | ||
972 | i = reftable[i] -- traverse linked list of free references | ||
973 | count = count + 1 | ||
974 | end | ||
975 | -- maximum number of simultaneously locked objects was 3 | ||
976 | assert(count == 3 and #reftable == 3 + 1) -- +1 for reserved [1] | ||
977 | end | ||
978 | |||
979 | |||
963 | collectgarbage"restart" | 980 | collectgarbage"restart" |
964 | 981 | ||
965 | 982 | ||
@@ -1363,8 +1380,8 @@ end) | |||
1363 | 1380 | ||
1364 | -- testing threads | 1381 | -- testing threads |
1365 | 1382 | ||
1366 | -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) | 1383 | -- get main thread from registry |
1367 | local mt = T.testC("rawgeti R 1; return 1") | 1384 | local mt = T.testC("rawgeti R !M; return 1") |
1368 | assert(type(mt) == "thread" and coroutine.running() == mt) | 1385 | assert(type(mt) == "thread" and coroutine.running() == mt) |
1369 | 1386 | ||
1370 | 1387 | ||