diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-10-25 15:30:15 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-10-25 15:30:15 -0300 |
| commit | 34840301b529686ce8168828b140a478a5d44b53 (patch) | |
| tree | daceb3978c3d40fd135fb60e9e60dbff9a47731c /testes | |
| parent | 41c800b352149e037bdebd5f20d2f25ed2a0e2a5 (diff) | |
| download | lua-34840301b529686ce8168828b140a478a5d44b53.tar.gz lua-34840301b529686ce8168828b140a478a5d44b53.tar.bz2 lua-34840301b529686ce8168828b140a478a5d44b53.zip | |
To-be-closed variables in the C API
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/api.lua | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/testes/api.lua b/testes/api.lua index 6e11c6ed..988250f7 100644 --- a/testes/api.lua +++ b/testes/api.lua | |||
| @@ -967,6 +967,77 @@ T.closestate(L1) | |||
| 967 | L1 = nil | 967 | L1 = nil |
| 968 | 968 | ||
| 969 | print('+') | 969 | print('+') |
| 970 | ------------------------------------------------------------------------- | ||
| 971 | -- testing to-be-closed variables | ||
| 972 | ------------------------------------------------------------------------- | ||
| 973 | print"testing to-be-closed variables" | ||
| 974 | |||
| 975 | do | ||
| 976 | local openresource = {} | ||
| 977 | |||
| 978 | local function newresource () | ||
| 979 | local x = setmetatable({10}, {__close = function(y) | ||
| 980 | assert(openresource[#openresource] == y) | ||
| 981 | openresource[#openresource] = nil | ||
| 982 | y[1] = y[1] + 1 | ||
| 983 | end}) | ||
| 984 | openresource[#openresource + 1] = x | ||
| 985 | return x | ||
| 986 | end | ||
| 987 | |||
| 988 | local a = T.testC([[ | ||
| 989 | call 0 1 # create resource | ||
| 990 | tobeclosed # mark it to be closed | ||
| 991 | return 1 | ||
| 992 | ]], newresource) | ||
| 993 | assert(a[1] == 11) | ||
| 994 | assert(#openresource == 0) -- was closed | ||
| 995 | |||
| 996 | -- repeat the test, but calling function in a 'multret' context | ||
| 997 | local a = {T.testC([[ | ||
| 998 | call 0 1 # create resource | ||
| 999 | tobeclosed # mark it to be closed | ||
| 1000 | return 2 | ||
| 1001 | ]], newresource)} | ||
| 1002 | assert(type(a[1]) == "string" and a[2][1] == 11) | ||
| 1003 | assert(#openresource == 0) -- was closed | ||
| 1004 | |||
| 1005 | -- error | ||
| 1006 | local a, b = pcall(T.testC, [[ | ||
| 1007 | call 0 1 # create resource | ||
| 1008 | tobeclosed # mark it to be closed | ||
| 1009 | error # resource is the error object | ||
| 1010 | ]], newresource) | ||
| 1011 | assert(a == false and b[1] == 11) | ||
| 1012 | assert(#openresource == 0) -- was closed | ||
| 1013 | |||
| 1014 | local function check (n) | ||
| 1015 | assert(#openresource == n) | ||
| 1016 | end | ||
| 1017 | |||
| 1018 | -- closing resources with 'settop' | ||
| 1019 | local a = T.testC([[ | ||
| 1020 | pushvalue 2 | ||
| 1021 | call 0 1 # create resource | ||
| 1022 | tobeclosed # mark it to be closed | ||
| 1023 | pushvalue 2 | ||
| 1024 | call 0 1 # create another resource | ||
| 1025 | tobeclosed # mark it to be closed | ||
| 1026 | pushvalue 3 | ||
| 1027 | pushint 2 # there should be two open resources | ||
| 1028 | call 1 0 | ||
| 1029 | pop 1 # pop second resource from the stack | ||
| 1030 | pushvalue 3 | ||
| 1031 | pushint 1 # there should be one open resource | ||
| 1032 | call 1 0 | ||
| 1033 | pop 1 # pop second resource from the stack | ||
| 1034 | pushint * | ||
| 1035 | return 1 # return stack size | ||
| 1036 | ]], newresource, check) | ||
| 1037 | assert(a == 3) -- no extra items left in the stack | ||
| 1038 | |||
| 1039 | end | ||
| 1040 | |||
| 970 | 1041 | ||
| 971 | ------------------------------------------------------------------------- | 1042 | ------------------------------------------------------------------------- |
| 972 | -- testing memory limits | 1043 | -- testing memory limits |
