From 88253d2cc1f9d1c5042c5a93692abbdda0667333 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, 24 Feb 2022 21:21:39 +0800 Subject: update --- script/vm/getGlobals.lua | 52 -------------------------------- script/vm/global-manager.lua | 72 ++++++++++++++++++++++++++++++-------------- script/vm/init.lua | 1 - script/vm/vm.lua | 17 ----------- 4 files changed, 50 insertions(+), 92 deletions(-) delete mode 100644 script/vm/getGlobals.lua (limited to 'script/vm') diff --git a/script/vm/getGlobals.lua b/script/vm/getGlobals.lua deleted file mode 100644 index 2ba734ba..00000000 --- a/script/vm/getGlobals.lua +++ /dev/null @@ -1,52 +0,0 @@ -local collector = require 'core.collector' 'searcher' -local guide = require 'parser.guide' ----@class vm -local vm = require 'vm.vm' - -function vm.hasGlobalSets(uri, name) - local id - if type(name) == 'string' then - id = ('def:g:%s%s'):format(noder.STRING_CHAR, name) - else - id = ('def:g:%s'):format(noder.STRING_CHAR, name) - end - return collector:has(uri, id) -end - -function vm.getGlobalSets(uri, name) - local cache = vm.getCache 'getGlobalSets' - if cache[name] then - return cache[name] - end - local results = {} - cache[name] = results - if name == '*' then - for noders in collector:each(uri, 'def:g:') do - for id in noder.eachID(noders) do - if id:sub(1, 2) == 'g:' - and not id:find(noder.SPLIT_CHAR) then - for source in noder.eachSource(noders, id) do - if guide.isSet(source) then - results[#results+1] = source - end - end - end - end - end - else - local id - if type(name) == 'string' then - id = ('g:%s%s'):format(noder.STRING_CHAR, name) - else - id = ('g:%s'):format(noder.STRING_CHAR, name) - end - for noders in collector:each(uri, 'def:' .. id) do - for source in noder.eachSource(noders, id) do - if guide.isSet(source) then - results[#results+1] = source - end - end - end - end - return results -end diff --git a/script/vm/global-manager.lua b/script/vm/global-manager.lua index b9f22b43..8176a2af 100644 --- a/script/vm/global-manager.lua +++ b/script/vm/global-manager.lua @@ -16,25 +16,34 @@ m.ID_SPLITE = '\x1F' local compilerGlobalMap = util.switch() : case 'local' - : call(function (uri, source) - if source.tag ~= '_ENV' then + : call(function (source) + if source.special ~= '_G' then return end if source.ref then for _, ref in ipairs(source.ref) do - m.compileObject(uri, ref) + m.compileObject(ref) end end end) + : case 'getlocal' + : call(function (source) + if source.special ~= '_G' then + return + end + m.compileObject(source.next) + end) : case 'setglobal' - : call(function (uri, source) + : call(function (source) + local uri = guide.getUri(source) local name = guide.getKeyName(source) local global = m.declareGlobal(name, uri) global:addSet(uri, source) source._globalNode = global end) : case 'getglobal' - : call(function (uri, source) + : call(function (source) + local uri = guide.getUri(source) local name = guide.getKeyName(source) local global = m.declareGlobal(name, uri) global:addGet(uri, source) @@ -42,20 +51,34 @@ local compilerGlobalMap = util.switch() local nxt = source.next if nxt then - m.compileObject(uri, nxt) + m.compileObject(nxt) + end + + if source.special == 'rawset' + or source.special == 'rawget' then + m.compileObject(source.parent) end end) : case 'setfield' : case 'setmethod' : case 'setindex' - ---@param uri uri ---@param source parser.object - : call(function (uri, source) - local parent = source.node._globalNode - if not parent then + : call(function (source) + local name + if source.node._globalNode then + local parentName = source.node._globalNode:getName() + if parentName == '_G' then + name = guide.getKeyName(source) + else + name = parentName .. m.ID_SPLITE .. guide.getKeyName(source) + end + elseif source.node.special == '_G' then + name = guide.getKeyName(source) + end + if not name then return end - local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source) + local uri = guide.getUri(source) local global = m.declareGlobal(name, uri) global:addSet(uri, source) source._globalNode = global @@ -63,21 +86,27 @@ local compilerGlobalMap = util.switch() : case 'getfield' : case 'getmethod' : case 'getindex' - ---@param uri uri ---@param source parser.object - : call(function (uri, source) - local parent = source.node._globalNode - if not parent then - return + : call(function (source) + local name + if source.node._globalNode then + local parentName = source.node._globalNode:getName() + if parentName == '_G' then + name = guide.getKeyName(source) + else + name = parentName .. m.ID_SPLITE .. guide.getKeyName(source) + end + elseif source.node.special == '_G' then + name = guide.getKeyName(source) end - local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source) + local uri = guide.getUri(source) local global = m.declareGlobal(name, uri) global:addGet(uri, source) source._globalNode = global local nxt = source.next if nxt then - m.compileObject(uri, nxt) + m.compileObject(nxt) end end) : getMap() @@ -105,22 +134,21 @@ function m.getGlobal(name, field) end ---@param source parser.object -function m.compileObject(uri, source) +function m.compileObject(source) if source._globalNode ~= nil then return end source._globalNode = false local compiler = compilerGlobalMap[source.type] if compiler then - compiler(uri, source) + compiler(source) end end ---@param source parser.object function m.compileAst(source) - local uri = guide.getUri(source) local env = guide.getENV(source) - m.compileObject(uri, env) + m.compileObject(env) end ---@return vm.node.global diff --git a/script/vm/init.lua b/script/vm/init.lua index a34b5d71..79cecbb7 100644 --- a/script/vm/init.lua +++ b/script/vm/init.lua @@ -1,6 +1,5 @@ local vm = require 'vm.vm' require 'vm.manager' -require 'vm.getGlobals' require 'vm.getDocs' require 'vm.getLibrary' require 'vm.getDef' diff --git a/script/vm/vm.lua b/script/vm/vm.lua index ff893f24..e524589c 100644 --- a/script/vm/vm.lua +++ b/script/vm/vm.lua @@ -14,23 +14,6 @@ _ENV = nil ---@class vm local m = {} -function m.getArgInfo(source) - local callargs = source.parent - if not callargs or callargs.type ~= 'callargs' then - return nil - end - local call = callargs.parent - if not call or call.type ~= 'call' then - return nil - end - for i = 1, #callargs do - if callargs[i] == source then - return call.node, i - end - end - return nil -end - function m.getSpecial(source) if not source then return nil -- cgit v1.2.3