diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-02-26 16:56:26 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-02-26 16:56:26 +0800 |
commit | a7d4e8df1ec6a560a5bbf3a3917b6e89c8cc9c92 (patch) | |
tree | 4452aed6872797d7395ad404d0d24d5589c97ff2 | |
parent | 20e8e3547b9a8a78570edacf33571a1e06e5adda (diff) | |
download | lua-language-server-a7d4e8df1ec6a560a5bbf3a3917b6e89c8cc9c92.zip |
支持self
-rw-r--r-- | server/src/vm/function.lua | 49 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 33 |
2 files changed, 31 insertions, 51 deletions
diff --git a/server/src/vm/function.lua b/server/src/vm/function.lua index 17cc237b..2d9e570d 100644 --- a/server/src/vm/function.lua +++ b/server/src/vm/function.lua @@ -23,6 +23,9 @@ function mt:pop() end function mt:saveLocal(name, loc) + if loc.type ~= 'local' then + error('saveLocal必须是local') + end self.locals[self._top][name] = loc end @@ -113,12 +116,9 @@ function mt:loadDots(expect) return self._dots:get(expect) end -function mt:setObject(object) - self._object = object -end - -function mt:setColon(colon) - self._colon = colon +function mt:setObject(value, source) + self._objectValue = value + self._objectSource = source end function mt:hasRuned() @@ -137,44 +137,13 @@ function mt:run() end -- 如果是面向对象形式的函数,创建隐藏的参数self - if self._object then - self:saveLocal('self', self._object) + if self._objectSource then + local loc = createLocal('self', self._objectSource, self._objectValue) + self:saveLocal('self', loc) end -- 显性声明的参数 self:createArgs() - - --local index = 0 - --if func.object then - -- local var = self:createArg('self', func.colon, self:getValue--(func.object, func.colon)) - -- var.hide = true - -- var.link = func.object - -- if func.argValues[1] then - -- self:setValue(var, func.argValues[1]) - -- end - -- index = 1 - -- func.args[index] = var - --end - - --local stop - --self:forList(func.built.arg, function (arg) - -- if stop then - -- return - -- end - -- index = index + 1 - -- if arg.type == 'name' then - -- local var = self:createArg(arg[1], arg) - -- self:setValue(var, func.argValues[index] or self:createValue--('nil')) - -- func.args[index] = var - -- elseif arg.type == '...' then - -- local dots = self:createDots(index, arg) - -- for i = index, #func.argValues do - -- dots[#dots+1] = func.argValues[i] - -- end - -- func.hasDots = true - -- stop = true - -- end - --end) end function mt:setArgs(values) diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index 3d53feee..32bc3bc2 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -113,7 +113,7 @@ function mt:runFunction(func) self:setCurrentFunction(originFunction) end -function mt:buildFunction(exp, object, colon) +function mt:buildFunction(exp) if exp and exp:bindFunction() then return exp:bindFunction() end @@ -131,9 +131,6 @@ function mt:buildFunction(exp, object, colon) func:saveLocal(name, loc) end) - func:setObject(object) - func:setColon(colon) - return value end @@ -347,7 +344,8 @@ function mt:getName(name, source) if source then self:instantSource(source) if source:bindLocal() then - return source:bindLocal() + local loc = source:bindLocal() + return loc:getValue() end end local loc = self:loadLocal(name) @@ -855,17 +853,30 @@ function mt:doFunction(action) if name.type == 'simple' then local parent = self:getSimple(name, -2) if name[#name-1].type == ':' then - local func = self:buildFunction(action, parent, name[#name-1]) + local value = self:buildFunction(action) local index = self:getIndex(name[#name]) - parent:setChild(index, func) + parent:setChild(index, value) + + local func = value:getFunction() + if #name == 3 then + -- function x:b() + local loc = self:loadLocal(name[1][1]) + if loc then + func:setObject(parent, loc.source) + else + func:setObject(parent, name[#name-2]) + end + else + func:setObject(parent, name[#name-2]) + end else - local func = self:buildFunction(action) + local value = self:buildFunction(action) local index = self:getIndex(name[#name]) - parent:setChild(index, func) + parent:setChild(index, value) end else - local func = self:buildFunction(action) - self:setName(name[1], action, func) + local value = self:buildFunction(action) + self:setName(name[1], action, value) end else self:buildFunction(action) |