summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-02-12 17:16:53 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-02-12 17:16:53 +0800
commit0e3510e7169c7075f3a4430d3259a6b319dd7021 (patch)
tree8df0625e0b942bc695e789ab209feee4e1e0f0e2
parent01077bdf1f3574f8d0c72778f5da8f50473fec63 (diff)
downloadlua-language-server-0e3510e7169c7075f3a4430d3259a6b319dd7021.zip
暂存
-rw-r--r--server/src/vm/library.lua23
-rw-r--r--server/src/vm/vm.lua21
-rw-r--r--server/test/definition/init.lua3
3 files changed, 26 insertions, 21 deletions
diff --git a/server/src/vm/library.lua b/server/src/vm/library.lua
index bf18d177..263b8d73 100644
--- a/server/src/vm/library.lua
+++ b/server/src/vm/library.lua
@@ -1,17 +1,11 @@
local createValue = require 'vm.value'
local createFunction = require 'vm.function'
+local CHILD_CACHE = {}
+
local buildLibValue
local buildLibChild
-local function getDefaultSource()
- return {
- start = 0,
- finish = 0,
- uri = '',
- }
-end
-
function buildLibValue(lib)
local tp = lib.type
local value
@@ -55,6 +49,19 @@ function buildLibValue(lib)
return value
end
+function buildLibChild(lib)
+ if CHILD_CACHE[lib] then
+ return CHILD_CACHE[lib]
+ end
+ local child = {}
+ for fName, fLib in pairs(lib.child) do
+ local fValue = buildLibValue(fLib)
+ child[fName] = fValue
+ end
+ CHILD_CACHE[lib] = child
+ return child
+end
+
return {
value = buildLibValue,
child = buildLibChild,
diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua
index 0e5d4055..5be0830f 100644
--- a/server/src/vm/vm.lua
+++ b/server/src/vm/vm.lua
@@ -6,6 +6,7 @@ local createFunction = require 'vm.function'
local instantSource = require 'vm.source'
local buildGlobal = require 'vm.global'
local createMulti = require 'vm.multi'
+local libraryBuilder = require 'vm.library'
local mt = {}
mt.__index = mt
@@ -277,7 +278,7 @@ function mt:getRequire(strValue, destVM)
-- 获取主函数返回值,注意不能修改对方的环境
local mainValue
if main.returns then
- mainValue = self:selectList(main.returns, 1)
+ mainValue = self:getFirstInMulti(main.returns)
else
mainValue = self:createValue('boolean', nil, true)
mainValue.uri = destVM.uri
@@ -467,7 +468,10 @@ function mt:createValue(tp, source)
local value = createValue(tp, source)
local lib = library.object[tp]
if lib then
- self:getLibChild(value, lib, 'object')
+ local child = libraryBuilder.child(lib)
+ for k, v in pairs(child) do
+ value:setChild(k, v)
+ end
end
return value
end
@@ -628,18 +632,11 @@ function mt:isTrue(v)
return true
end
-function mt:selectList(list, n)
- if list.type ~= 'list' then
- return list
- end
- return list[n] or self:createValue('nil')
-end
-
function mt:getBinary(exp)
local v1 = self:getExp(exp[1])
local v2 = self:getExp(exp[2])
- v1 = self:selectList(v1, 1)
- v2 = self:selectList(v2, 1)
+ v1 = self:getFirstInMulti(v1)
+ v2 = self:getFirstInMulti(v2)
local op = exp.op
-- TODO 搜索元方法
if op == 'or' then
@@ -743,7 +740,7 @@ end
function mt:getUnary(exp)
local v1 = self:getExp(exp[1])
- v1 = self:selectList(v1, 1)
+ v1 = self:getFirstInMulti(v1)
local op = exp.op
-- TODO 搜索元方法
if op == 'not' then
diff --git a/server/test/definition/init.lua b/server/test/definition/init.lua
index d55457f6..19e353cc 100644
--- a/server/test/definition/init.lua
+++ b/server/test/definition/init.lua
@@ -1,5 +1,6 @@
local core = require 'core'
local parser = require 'parser'
+local buildVM = require 'vm'
rawset(_G, 'TEST', true)
@@ -41,7 +42,7 @@ function TEST(script)
local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ')
local ast = parser:ast(new_script)
assert(ast)
- local vm = core.vm(ast)
+ local vm = buildVM(ast)
assert(vm)
local result = core.findResult(vm, pos)