From 7426f1c8b300afc0ac5f14686c6244f37b1b010c 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, 11 Oct 2019 16:36:53 +0800 Subject: =?UTF-8?q?=E9=A2=84=E5=A4=84=E7=90=86=E7=89=B9=E6=AE=8AAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-beta/src/core/engineer.lua | 79 +++++++++++++++++++++----------------- server-beta/src/core/getglobal.lua | 4 ++ server-beta/src/parser/guide.lua | 2 +- 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/server-beta/src/core/engineer.lua b/server-beta/src/core/engineer.lua index f11d6a9d..036095dc 100644 --- a/server-beta/src/core/engineer.lua +++ b/server-beta/src/core/engineer.lua @@ -34,32 +34,41 @@ local specials = { } function mt:getSpecialName(source) - if source.type == 'getglobal' then - local node = source.node - if node.tag ~= '_ENV' then - return nil - end - local name = guide.getKeyName(source) - if name:sub(1, 2) ~= 's|' then - return nil - end - local spName = name:sub(3) - if not specials[spName] then - return nil - end - return spName - elseif source.type == 'local' then - if source.tag == '_ENV' then - return '_G' + local spName = self.cache.specialName[source] + if spName ~= nil then + if spName then + return spName end return nil - elseif source.type == 'getlocal' then - local loc = source.loc - if loc.tag == '_ENV' then - return '_G' + end + local function getName(src) + if src.type == 'getglobal' then + local node = src.node + if node.tag ~= '_ENV' then + return nil + end + local name = guide.getKeyName(src) + if name:sub(1, 2) ~= 's|' then + return nil + end + spName = name:sub(3) + if not specials[spName] then + spName = nil + end + elseif src.type == 'local' then + if src.tag == '_ENV' then + spName = '_G' + end + elseif src.type == 'getlocal' then + local loc = src.loc + if loc.tag == '_ENV' then + spName = '_G' + end end end - return nil + self:eachValue(source, getName) + self.cache.specialName[source] = spName or false + return spName end function mt:eachSpecial(callback) @@ -70,23 +79,20 @@ function mt:eachSpecial(callback) end return end - local env = guide.getENV(self.ast) - if not env then - return - end - local refs = env.ref - if not refs then - return - end cache = {} self.cache.special = cache - for i = 1, #refs do - local ref = refs[i] - local name = self:getSpecialName(ref) - if name then - cache[#cache+1] = {name, ref} + guide.eachSource(self.ast, function (source) + if source.type == 'getlocal' + or source.type == 'getglobal' + or source.type == 'local' + or source.type == 'field' + or source.type == 'string' then + local name = self:getSpecialName(source) + if name then + cache[#cache+1] = { name, source } + end end - end + end) for i = 1, #cache do callback(cache[i][1], cache[i][2]) end @@ -312,6 +318,7 @@ return function (ast) ref = {}, field = {}, value = {}, + specialName = {}, }, }, mt) return self diff --git a/server-beta/src/core/getglobal.lua b/server-beta/src/core/getglobal.lua index 8bf45d39..d7f56146 100644 --- a/server-beta/src/core/getglobal.lua +++ b/server-beta/src/core/getglobal.lua @@ -84,4 +84,8 @@ function m:field(source, key, callback) end) end +function m:value(source, callback) + callback(source) +end + return m diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index 9530f9ce..e2138fc9 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -259,7 +259,7 @@ function m.eachSourceContain(ast, offset, callback) end end ---- 遍历所有包含offset的source +--- 遍历所有的source function m.eachSource(ast, callback) local map = m.childMap local list = { ast } -- cgit v1.2.3