From 217d96a4658cd50933b3456b7f452ff26f66b8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 20 Dec 2018 16:09:18 +0800 Subject: =?UTF-8?q?=E6=89=BE=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/matcher/vm.lua | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'server/src/matcher') diff --git a/server/src/matcher/vm.lua b/server/src/matcher/vm.lua index f2b5b47b..3a272f3a 100644 --- a/server/src/matcher/vm.lua +++ b/server/src/matcher/vm.lua @@ -454,7 +454,12 @@ function mt:callRequire(func, values) return end end - self:setFunctionReturn(func, 1, nil) + local requireValue = self:createValue('boolean', nil, true) + self:setFunctionReturn(func, 1, requireValue) + self.requires[#self.requires+1] = { + str = str, + value = requireValue, + } end function mt:call(func, values) @@ -1152,7 +1157,23 @@ function mt:createEnvironment() gValue.child = envValue.child end -local function compile(ast) +function mt:loadRequires() + if not self.lsp or not self.lsp.workspace then + return + end + for _, req in ipairs(self.requires) do + local str = req.str + if type(str) == 'string' then + local uri = self.lsp.workspace:searchPath(str) + -- 如果循环require,这里会返回nil + local destVM = self.lsp:loadText(uri) + if destVM then + end + end + end +end + +local function compile(ast, lsp) local vm = setmetatable({ scope = env { locals = {}, @@ -1170,6 +1191,8 @@ local function compile(ast) }, libraryValue = {}, libraryChild = {}, + requires = {}, + lsp = lsp, }, mt) -- 创建初始环境 @@ -1178,14 +1201,17 @@ local function compile(ast) -- 执行代码 vm:doActions(ast) + -- 合并requires + vm:loadRequires() + return vm end -return function (ast) +return function (ast, lsp) if not ast then return nil end - local suc, res = xpcall(compile, log.error, ast) + local suc, res = xpcall(compile, log.error, ast, lsp) if not suc then return nil end -- cgit v1.2.3