From a0dad951f50e877c9ffaabc71ea09f708231f3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 12 Dec 2018 14:05:17 +0800 Subject: =?UTF-8?q?vm=E6=94=AF=E6=8C=81require=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/matcher/vm.lua | 60 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 18 deletions(-) (limited to 'server/src/matcher/vm.lua') diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua index af2e9bc2..7c3124fb 100644 --- a/server/src/matcher/vm.lua +++ b/server/src/matcher/vm.lua @@ -316,17 +316,41 @@ function mt:checkMetaIndex(value, meta) end function mt:callSetMetaTable(func, values) + if not values[1] then + values[1] = self:createValue('nil') + end + if not values[2] then + values[2] = self:createValue('nil') + end self:setFunctionReturn(func, 1, values[1]) -- 检查 __index self:checkMetaIndex(values[1], values[2]) end +function mt:callRequire(func, values) + if not values[1] then + values[1] = self:createValue('nil') + end + local str = values[1].value + if type(str) == 'string' then + local lib = library.library[str] + if lib then + local value = self:getLibValue(lib) + self:setFunctionReturn(func, 1, value) + return + end + end + self:setFunctionReturn(func, 1, nil) +end + function mt:call(func, values) local lib = func.lib if lib and lib.special then if lib.special == 'setmetatable' then self:callSetMetaTable(func, values) + elseif lib.special == 'require' then + self:callRequire(func, values) end end @@ -363,10 +387,10 @@ function mt:createValue(type, source, v) end function mt:getLibValue(lib) - local tp = lib.type - if not tp then - return + if self.libraryValue[lib] then + return self.libraryValue[lib] end + local tp = lib.type local value if tp == 'table' then value = self:buildTable() @@ -397,6 +421,17 @@ function mt:getLibValue(lib) else value = self:createValue(tp) end + self.libraryValue[lib] = value + value.lib = lib + + if lib.child then + for fName, fLib in pairs(lib.child) do + local fField = self:createField(value, fName) + local fValue = self:getLibValue(fLib) + self:setValue(fField, fValue) + end + end + return value end @@ -802,22 +837,10 @@ function mt:createEnvironment() local pValue = self:setValue(parent, self:buildTable()) -- 设置全局变量 - for name, info in pairs(library.global) do + for name, lib in pairs(library.global) do local field = self:createField(pValue, name) - if info.lib then - local value = self:getLibValue(info.lib) - value = self:setValue(field, value) - value.lib = info.lib - end - if info.child then - local fValue = self:getValue(field) - for fname, flib in pairs(info.child) do - local ffield = self:createField(fValue, fname) - local value = self:getLibValue(flib) - value = self:setValue(ffield, value) - value.lib = flib - end - end + local value = self:getLibValue(lib) + value = self:setValue(field, value) end -- 设置 _G 等于 _ENV @@ -840,6 +863,7 @@ local function compile(ast) funcs = {}, calls = {}, }, + libraryValue = {}, }, mt) -- 创建初始环境 -- cgit v1.2.3