Skip to content

Commit 13ead7b

Browse files
committed
support croutine.close
1 parent 2fa5598 commit 13ead7b

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

resources/luajitCompact.lua

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1+
local select = select
2+
local type = type
3+
local getmetatable = getmetatable
4+
local xpcall = xpcall
5+
local math_floor = math.floor
6+
local math_ceil = math.ceil
7+
18
table.unpack = unpack
29

310
table.pack = function(...)
411
return { n = select('#', ...), ... }
512
end
613

7-
function defer(toBeClosed, callback)
8-
local ctype = type(toBeClosed)
9-
local meta = getmetatable(toBeClosed)
10-
if meta and meta.__close then
11-
local ok, result = xpcall(callback, log.error)
12-
meta.__close(toBeClosed)
13-
if ok then
14-
return result
15-
end
16-
elseif ctype == 'function' then
17-
local ok, result = xpcall(callback, log.error)
18-
toBeClosed()
19-
if ok then
20-
return result
21-
end
22-
else
23-
local ok, result = xpcall(callback, log.error)
24-
if ok then
25-
return result
26-
end
27-
end
28-
end
29-
3014
math.maxinteger = 0x7FFFFFFFLL
31-
function math.tointeger(x)
15+
local function tointeger(x)
3216
if type(x) ~= "number" then
3317
return nil
3418
end
3519

36-
local int = x >= 0 and math.floor(x) or math.ceil(x)
20+
local int = x >= 0 and math_floor(x) or math_ceil(x)
3721

3822
if int == x then
3923
return int
@@ -42,10 +26,62 @@ function math.tointeger(x)
4226
end
4327
end
4428

29+
math.tointeger = tointeger
30+
4531
function math.type(x)
4632
if type(x) == "number" then
47-
return math.tointeger(x) and "integer" or "number"
33+
return tointeger(x) and "integer" or "number"
4834
else
4935
return nil
5036
end
5137
end
38+
39+
local coroutine_resume = coroutine.resume
40+
local coroutine_status = coroutine.status
41+
local coroutine_running = coroutine.running
42+
43+
local cancel_table = setmetatable({}, { __mode = 'kv' })
44+
45+
function coroutine.resume(co, ...)
46+
if cancel_table[co] then
47+
return false, 'cannot resume dead coroutine'
48+
end
49+
50+
return coroutine_resume(co, ...)
51+
end
52+
53+
function coroutine.status(co)
54+
if cancel_table[co] then
55+
return 'dead'
56+
end
57+
58+
return coroutine_status(co)
59+
end
60+
61+
function coroutine.close(co)
62+
if coroutine_status(co) == 'suspended' then
63+
cancel_table[co] = true
64+
end
65+
end
66+
67+
function defer(toBeClosed, callback)
68+
local ctype = type(toBeClosed)
69+
local meta = getmetatable(toBeClosed)
70+
local closeCallback = nil
71+
local ok, result
72+
73+
local co = coroutine_running()
74+
if coroutine.status(co) ~= 'dead' then
75+
ok, result = xpcall(callback, log.error)
76+
end
77+
78+
if meta and meta.__close then
79+
meta.__close(toBeClosed)
80+
elseif ctype == 'function' then
81+
toBeClosed()
82+
end
83+
84+
if ok then
85+
return result
86+
end
87+
end

resources/override_script/await.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ function m.close(id)
100100
if type(callback) == 'function' then
101101
xpcall(callback, log.error)
102102
end
103-
-- luajit donot support coroutine.close
104-
-- coroutine.close(co)
103+
coroutine.close(co)
105104
end
106105
end
107106
end

0 commit comments

Comments
 (0)