diff options
-rw-r--r-- | server/src/vm/function.lua | 12 | ||||
-rw-r--r-- | server/src/vm/library.lua | 2 | ||||
-rw-r--r-- | server/src/vm/multi.lua | 21 | ||||
-rw-r--r-- | server/src/vm/source.lua | 5 | ||||
-rw-r--r-- | server/src/vm/value.lua | 3 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 5 |
6 files changed, 25 insertions, 23 deletions
diff --git a/server/src/vm/function.lua b/server/src/vm/function.lua index 8625853f..973f984e 100644 --- a/server/src/vm/function.lua +++ b/server/src/vm/function.lua @@ -98,20 +98,18 @@ function mt:setReturn(index, value) end if value then self.returns[index] = value - else - self.returns[index] = createValue('any', self:getSource() or sourceMgr.dummy()) end end function mt:getReturn(index) if self.maxReturns and index and self.maxReturns < index then - return createValue('nil', sourceMgr.dummy()) + return nil end if not self.returns then self.returns = createMulti() end if index then - return self.returns:get(index) or createValue('nil', sourceMgr.dummy()) + return self.returns:get(index) else return self.returns end @@ -174,7 +172,7 @@ function mt:run(vm) -- 向局部变量中填充参数 for i, loc in ipairs(self.args) do - loc:setValue(self.argValues[i] or createValue('nil', sourceMgr.dummy())) + loc:setValue(self.argValues[i]) end if self._dots then self._dots = createMulti() @@ -205,12 +203,12 @@ function mt:createArg(vm, arg) end end -function mt:createLibArg(arg) +function mt:createLibArg(arg, source) if arg.type == '...' then self._dots = createMulti() else local name = arg.name or '_' - local loc = createLocal(name, sourceMgr.dummy(), createValue('any', sourceMgr.dummy())) + local loc = createLocal(name, source, createValue('any', source)) self:saveLocal(name, loc) self.args[#self.args+1] = loc end diff --git a/server/src/vm/library.lua b/server/src/vm/library.lua index 24a17389..67d2d263 100644 --- a/server/src/vm/library.lua +++ b/server/src/vm/library.lua @@ -23,7 +23,7 @@ function buildLibValue(lib) value:setFunction(func) if lib.args then for _, arg in ipairs(lib.args) do - func:createLibArg(arg) + func:createLibArg(arg, sourceMgr.dummy()) end end if lib.returns then diff --git a/server/src/vm/multi.lua b/server/src/vm/multi.lua index aaff97ec..9208656b 100644 --- a/server/src/vm/multi.lua +++ b/server/src/vm/multi.lua @@ -4,39 +4,40 @@ local sourceMgr = require 'vm.source' local mt = {} mt.__index = mt mt.type = 'multi' +mt.len = 0 function mt:push(value, isLast) if value.type == 'list' then if isLast then for _, v in ipairs(value) do - self[#self+1] = v + self.len = self.len + 1 + self[self.len] = v end else - self[#self+1] = value[1] + self.len = self.len + 1 + self[self.len] = value[1] end else - self[#self+1] = value + self.len = self.len + 1 + self[self.len] = value end end function mt:get(index) - for n = #self+1, index do - self[n] = createValue('any', sourceMgr.dummy()) + if index > self.len then + self.len = index end return self[index] end function mt:set(index, value) - for n = #self+1, index-1 do - self[n] = createValue('any', sourceMgr.dummy()) - end self[index] = value end function mt:first() local value = self[1] if not value then - return createValue('nil', sourceMgr.dummy()) + return nil end if value.type == 'multi' then return value:first() @@ -49,7 +50,7 @@ function mt:eachValue(callback) local i = 0 for n, value in ipairs(self) do if value.type == 'multi' then - if n == #self then + if n == self.len then value:eachValue(function (_, nvalue) i = i + 1 callback(i, nvalue) diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua index 365c2757..af8b9ab3 100644 --- a/server/src/vm/source.lua +++ b/server/src/vm/source.lua @@ -109,10 +109,7 @@ local function instant(source) end local function dummy() - local src = { - start = 0, - finish = 0, - } + local src = {} instant(src) return src end diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index c4336ad4..9fd1fdf1 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -250,6 +250,9 @@ function mt:mergeValue(value) if self == value then return end + if not value then + return + end if value._type then for tp, rate in pairs(value._type) do self:setType(tp, rate) diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index 4aa3fd3c..df2cd234 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -291,7 +291,10 @@ function mt:callLibrary(func, values, source, lib) if rtn.type == 'boolean' or rtn.type == 'number' or rtn.type == 'integer' or rtn.type == 'string' then func:setReturn(i, self:createValue(rtn.type, self:getDefaultSource())) end - func:getReturn(i):setType(rtn.type or 'any', 1.0) + local value = func:getReturn(i) + if value then + value:setType(rtn.type or 'any', 1.0) + end end end end |