summaryrefslogtreecommitdiff
path: root/server/src/vm/special.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-07-08 10:03:41 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-07-08 10:03:41 +0800
commit5781f2a991da34c47b0b65b3d94b75f96a92c4ca (patch)
tree196a04e1c5fdc689edac13fdb123e1ec8144956d /server/src/vm/special.lua
parentaed744d56a684a2b014e6fcc88ecbdde9833c929 (diff)
downloadlua-language-server-5781f2a991da34c47b0b65b3d94b75f96a92c4ca.zip
emmy支持 require 的特殊处理
Diffstat (limited to 'server/src/vm/special.lua')
-rw-r--r--server/src/vm/special.lua32
1 files changed, 32 insertions, 0 deletions
diff --git a/server/src/vm/special.lua b/server/src/vm/special.lua
index f8a03a34..bf02f620 100644
--- a/server/src/vm/special.lua
+++ b/server/src/vm/special.lua
@@ -1,5 +1,7 @@
local mt = require 'vm.manager'
local multi = require 'vm.multi'
+local library = require 'core.library'
+local libraryBuilder = require 'vm.library'
---@param func emmyFunction
---@param values table
@@ -24,6 +26,8 @@ function mt:checkEmmyParam(func, values, index, special, source)
self:callEmmyLoadFile(func, values, index)
elseif special == 'pcall:1' then
self:callEmmyPCall(func, values, index, source)
+ elseif special == 'require:1' then
+ self:callEmmyRequire(func, values, index, source)
end
end
@@ -68,6 +72,7 @@ end
---@param func emmyFunction
---@param values table
---@param index integer
+---@param source source
function mt:callEmmyPCall(func, values, index, source)
local funcValue = values[index]
if not funcValue then
@@ -91,3 +96,30 @@ function mt:callEmmyPCall(func, values, index, source)
end)
end
end
+
+---@param func emmyFunction
+---@param values table
+---@param index integer
+function mt:callEmmyRequire(func, values, index)
+ if not values[index] then
+ values[index] = self:createValue('any', self:getDefaultSource())
+ end
+ local str = values[index]:getLiteral()
+ if type(str) ~= 'string' then
+ return
+ end
+ local lib = library.library[str]
+ if lib then
+ local value = libraryBuilder.value(lib)
+ value:markGlobal()
+ func:setReturn(1, value)
+ return
+ else
+ local requireValue = self:tryRequireOne(values[1], 'require')
+ if not requireValue then
+ requireValue = self:createValue('any', self:getDefaultSource())
+ requireValue:set('cross file', true)
+ end
+ func:setReturn(1, requireValue)
+ end
+end