summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/vm/pcall.lua50
-rw-r--r--server/src/vm/vm.lua5
2 files changed, 55 insertions, 0 deletions
diff --git a/server/src/vm/pcall.lua b/server/src/vm/pcall.lua
new file mode 100644
index 00000000..e5d1e26f
--- /dev/null
+++ b/server/src/vm/pcall.lua
@@ -0,0 +1,50 @@
+local mt = require 'vm.manager'
+local multi = require 'vm.multi'
+
+function mt:callPcall(func, values, source)
+ local funcValue = values:first()
+ if not funcValue then
+ return
+ end
+ local realFunc = funcValue:getFunction()
+ if not realFunc then
+ return
+ end
+ local argList = multi()
+ values:eachValue(function (i, v)
+ if i >= 2 then
+ argList:push(v)
+ end
+ end)
+ self:call(funcValue, argList, source)
+ if realFunc ~= func then
+ func:setReturn(1, self:createValue('boolean', source))
+ realFunc:getReturn():eachValue(function (i, v)
+ func:setReturn(i + 1, v)
+ end)
+ end
+end
+
+function mt:callXpcall(func, values, source)
+ local funcValue = values:first()
+ if not funcValue then
+ return
+ end
+ local realFunc = funcValue:getFunction()
+ if not realFunc then
+ return
+ end
+ local argList = multi()
+ values:eachValue(function (i, v)
+ if i >= 3 then
+ argList:push(v)
+ end
+ end)
+ self:call(funcValue, argList, source)
+ if realFunc ~= func then
+ func:setReturn(1, self:createValue('boolean', source))
+ realFunc:getReturn():eachValue(function (i, v)
+ func:setReturn(i + 1, v)
+ end)
+ end
+end
diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua
index aafe7a10..5091a1bc 100644
--- a/server/src/vm/vm.lua
+++ b/server/src/vm/vm.lua
@@ -12,6 +12,7 @@ local mt = require 'vm.manager'
require 'vm.module'
require 'vm.raw'
+require 'vm.pcall'
-- TODO source测试
--rawset(_G, 'CachedSource', setmetatable({}, { __mode = 'kv' }))
@@ -282,6 +283,10 @@ function mt:callLibrary(func, values, source, lib)
self:callRawSet(func, values, source)
elseif lib.special == 'rawget' then
self:callRawGet(func, values, source)
+ elseif lib.special == 'pcall' then
+ self:callPcall(func, values, source)
+ elseif lib.special == 'xpcall' then
+ self:callXpcall(func, values, source)
end
else
-- 如果lib的参数中有function,则立即执行function