From 8860d44ef8b00355f87ccdeb82b143e999ee912e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 25 Jan 2019 10:46:13 +0800 Subject: =?UTF-8?q?=E5=85=A8=E5=B1=80=E7=8E=AF=E5=A2=83=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/core/vm.lua | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'server') diff --git a/server/src/core/vm.lua b/server/src/core/vm.lua index a0f82458..054e111f 100644 --- a/server/src/core/vm.lua +++ b/server/src/core/vm.lua @@ -3,7 +3,7 @@ local library = require 'core.library' local createValue = require 'core.value' local DefaultSource = { start = 0, finish = 0 } -local GlobalChild +local GlobalValue -- 根据赋值顺序决定遍历顺序的表 local function orderTable() @@ -1435,36 +1435,35 @@ function mt:doActions(actions) end end +function mt:getGlobalValue() + if GlobalValue then + return GlobalValue + end + GlobalValue = self:createValue('table') + GlobalValue.GLOBAL = true + for name, lib in pairs(library.global) do + local field = GlobalValue:createField(name) + local value = self:getLibValue(lib, 'global') + self:setValue(field, value) + end + return GlobalValue +end + function mt:createEnvironment() self.scope.block = { start = 0, finish = math.maxinteger } -- 整个文件是一个函数 self.chunk.func = self:buildFunction() self.results.main = self.chunk.func -- 隐藏的上值`_ENV` - local parent = self:createLocal('_ENV') - parent.hide = true - local envValue = self:setValue(parent, self:buildTable()) - -- _ENV 有个特殊标记 - envValue.ENV = true + local evnField = self:createLocal('_ENV') + evnField.hide = true -- 隐藏的参数`...` self:createDots(1) - - -- 设置全局变量 - if not GlobalChild then - for name, lib in pairs(library.global) do - local field = envValue:createField(name) - local value = self:getLibValue(lib, 'global') - value = self:setValue(field, value) - end - GlobalChild = envValue.child - end - envValue.child = readOnly(GlobalChild) - - -- 设置 _G 使用 _ENV 的child - local g = envValue:getField('_G') - local gValue = self:getValue(g) - gValue.child = envValue.child - self.env = envValue + -- 全局变量`_G` + local globalValue = self:getGlobalValue() + -- 使用_G初始化_ENV + self:setValue(evnField, globalValue) + self.env = globalValue end local function compile(ast, lsp, uri) -- cgit v1.2.3