summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
Diffstat (limited to 'script')
-rw-r--r--script/await.lua2
-rw-r--r--script/core/diagnostics/unused-function.lua15
-rw-r--r--script/proto/proto.lua4
-rw-r--r--script/service/service.lua2
-rw-r--r--script/utility.lua6
5 files changed, 25 insertions, 4 deletions
diff --git a/script/await.lua b/script/await.lua
index 98c88fd3..d319b6e7 100644
--- a/script/await.lua
+++ b/script/await.lua
@@ -60,7 +60,7 @@ function m.await(callback, ...)
end
return m.wait(function (resume, ...)
m.call(function ()
- local returnNil <close> = util.defer(resume)
+ local returnNil <close> = resume
resume(callback())
end, ...)
end, ...)
diff --git a/script/core/diagnostics/unused-function.lua b/script/core/diagnostics/unused-function.lua
index f0bca613..1463d081 100644
--- a/script/core/diagnostics/unused-function.lua
+++ b/script/core/diagnostics/unused-function.lua
@@ -5,6 +5,18 @@ local define = require 'proto.define'
local lang = require 'language'
local await = require 'await'
+local function isToBeClosed(source)
+ if not source.attrs then
+ return false
+ end
+ for _, attr in ipairs(source.attrs) do
+ if attr[1] == 'close' then
+ return true
+ end
+ end
+ return false
+end
+
return function (uri, callback)
local ast = files.getAst(uri)
if not ast then
@@ -20,6 +32,9 @@ return function (uri, callback)
and parent.type ~= 'setlocal' then
return
end
+ if isToBeClosed(source) then
+ return
+ end
local hasGet
local refs = vm.getRefs(source)
for _, src in ipairs(refs) do
diff --git a/script/proto/proto.lua b/script/proto/proto.lua
index a0758ac3..944268c2 100644
--- a/script/proto/proto.lua
+++ b/script/proto/proto.lua
@@ -106,7 +106,7 @@ function m.doMethod(proto)
local ok = true
local res
-- 任务可能在执行过程中被中断,通过close来捕获
- local response <close> = util.defer(function ()
+ local response <close> = function ()
local passed = os.clock() - clock
if passed > 0.2 then
log.debug(('Method [%s] takes [%.3f]sec.'):format(method, passed))
@@ -120,7 +120,7 @@ function m.doMethod(proto)
else
m.responseErr(proto.id, define.ErrorCodes.InternalError, res)
end
- end)
+ end
ok, res = xpcall(abil, log.error, proto.params)
end)
end
diff --git a/script/service/service.lua b/script/service/service.lua
index 11cc7b19..82c192b6 100644
--- a/script/service/service.lua
+++ b/script/service/service.lua
@@ -4,6 +4,7 @@ local await = require 'await'
local timer = require 'timer'
local proto = require 'proto'
local vm = require 'vm'
+local util = require 'utility'
local m = {}
m.type = 'service'
@@ -145,6 +146,7 @@ function m.startTimer()
end
function m.start()
+ util.enableCloseFunction()
await.setErrorHandle(log.error)
pub.recruitBraves(4)
proto.listen()
diff --git a/script/utility.lua b/script/utility.lua
index 2b9c7d2f..2386998b 100644
--- a/script/utility.lua
+++ b/script/utility.lua
@@ -8,7 +8,7 @@ local ipairs = ipairs
local next = next
local rawset = rawset
local move = table.move
-local setmetatable = setmetatable
+local setmetatable = debug.setmetatable
local mathType = math.type
local mathCeil = math.ceil
local getmetatable = getmetatable
@@ -408,6 +408,10 @@ function m.defer(callback)
return setmetatable({ callback }, deferMT)
end
+function m.enableCloseFunction()
+ setmetatable(function () end, { __close = function (f) f() end })
+end
+
local esc = {
["'"] = [[\']],
['"'] = [[\"]],