diff options
-rw-r--r-- | script/core/linker.lua | 20 | ||||
-rw-r--r-- | test/basic/linker.lua | 26 |
2 files changed, 36 insertions, 10 deletions
diff --git a/script/core/linker.lua b/script/core/linker.lua index 7ebff65c..d7f53fa4 100644 --- a/script/core/linker.lua +++ b/script/core/linker.lua @@ -1,4 +1,3 @@ -local guide = require 'parser.guide' local util = require 'utility' local function getKey(source) @@ -41,11 +40,18 @@ local function checkTableField(source) return nil end -local function checkFileReturn(source) +local function checkFunctionReturn(source) if source.parent - and source.parent.type == 'return' - and source.parent.parent.type == 'main' then - return true + and source.parent.type == 'return' then + if source.parent.parent.type == 'main' then + return 0 + elseif source.parent.parent.type == 'function' then + for i = 1, #source.parent do + if source.parent[i] == source then + return i + end + end + end end return nil end @@ -83,8 +89,8 @@ local function createLink(source) global = checkGlobal(node), -- 字面量表中的字段 tfield = checkTableField(node), - -- 文件的返回值 - freturn = checkFileReturn(node), + -- 返回值,文件返回值总是0,函数返回值为第几个返回值 + freturn = checkFunctionReturn(node), } end diff --git a/test/basic/linker.lua b/test/basic/linker.lua index 1ce3f726..bf4e50bb 100644 --- a/test/basic/linker.lua +++ b/test/basic/linker.lua @@ -2,7 +2,6 @@ local linker = require 'core.linker' local files = require 'files' local util = require 'utility' local guide = require 'core.guide' -local glob = require "glob" local function getSource(pos) local ast = files.getAst('') @@ -19,6 +18,7 @@ local function getSource(pos) end) end +local CARE = {} local function TEST(script) return function (expect) files.removeAll() @@ -30,10 +30,13 @@ local function TEST(script) local source = getSource(pos) assert(source) local result = linker.getLink(source) - assert(util.equal(result, expect)) + for key in pairs(CARE) do + assert(result[key] == expect[key]) + end end end +CARE['id'] = true TEST [[ local <?x?> ]] { @@ -54,6 +57,7 @@ local x id = '7', } +CARE['global'] = true TEST [[ print(<?X?>) ]] { @@ -96,6 +100,7 @@ function x:<?f?>() end global = true, } +CARE['tfield'] = true TEST [[ { <?x?> = 1, @@ -105,10 +110,25 @@ TEST [[ tfield = true, } +CARE['freturn'] = true TEST [[ return <?X?> ]] { id = '"X"', global = true, - freturn = true, + freturn = 0, } + +TEST [[ +function f() + return <?X?> +end +]] { + id = '"X"', + global = true, + freturn = 1, +} + +TEST [[ + +]] |