Skip to content

Commit

Permalink
fix #29:解析require函数调用的返回值;用于支持setmetatable
Browse files Browse the repository at this point in the history
  • Loading branch information
liwangqian committed Sep 8, 2018
1 parent 79b7b84 commit d1d2d5a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
Binary file removed luacoderassist-2.0.1-beta.2.rar
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -3,7 +3,7 @@
"displayName": "LuaCoderAssist",
"description": "lua coder assistant in javascript for vscode",
"icon": "images/icon.png",
"version": "2.0.1",
"version": "2.0.2",
"publisher": "liwangqian",
"engines": {
"vscode": "^1.25.0"
Expand Down Expand Up @@ -252,4 +252,4 @@
"type": "git",
"url": "https://github.com/liwangqian/LuaCoderAssist"
}
}
}
21 changes: 13 additions & 8 deletions server/lib/engine/completion.js
Expand Up @@ -98,16 +98,21 @@ function completionProvider(context) {
}

const filter = item => context.functionOnly && !Is.luaFunction(item.type);
const children = Object.create(null);
def.type.walk(fields => {
for (const name in fields) {
const symbol = fields[name];
if (!children[name]) {
children[name] = symbol;
let children
if (Is.luaModule(def.type)) {
children = def.type.fields;
} else {
children = Object.create(null);
def.type.walk(fields => {
for (const name in fields) {
const symbol = fields[name];
if (!children[name]) {
children[name] = symbol;
}
}
}
});
}

});
return object2Array(def.type.return || children, filter);
}

Expand Down
2 changes: 2 additions & 0 deletions server/lib/engine/core.js
Expand Up @@ -168,7 +168,9 @@ function analysis(code, uri) {
parent.type = symbol.type; // local xzy; xzy = 1
}
} else {
(currentFunc || theModule).addChild(symbol);
if (moduleType.moduleMode) {
currentScope.push(symbol);
moduleType.set(name, symbol);
} else {
_G.set(name, symbol);
Expand Down
2 changes: 1 addition & 1 deletion server/lib/engine/definition.js
Expand Up @@ -60,7 +60,7 @@ function definitionProvider(context) {

for (let i = 1; i < (length - 1); i++) {
const name = names[i];
def = def.type.search(name).value;
def = def.type.search(name, context.range).value;
if (!def || !Is.luaTable(typeOf(def))) {
return [];
}
Expand Down
22 changes: 19 additions & 3 deletions server/lib/engine/typeof.js
Expand Up @@ -54,7 +54,7 @@ function typeOf(symbol) {
}

symbol.type = type;
return symbol.type;
return type;
}

function deduceType(type) {
Expand Down Expand Up @@ -111,13 +111,29 @@ function parseCallExpression(node, type) {
return null;
}

const fname = node.base.name;
if (fname === 'require') {
let moduleName = node.arguments[0].value;
let shortPath = moduleName.replace('.', '/');
let mdls = LoadedPackages[moduleName]
// TODO:增加配置项,用来配置搜索路径,然后模拟lua的搜索方法搜索最优匹配模块
for (const uri in mdls) {
if (uri.includes(shortPath)) { // 查找最优匹配,如果存在多个最优匹配,则返回第一个
const ret = mdls[uri].type.return;
return ret && ret.type;
}
}

return null;
}

let R = ftype.returns[type.index || 0];
if (!Is.lazyValue(R.type)) {
return R.type;
}

// 推导调用参数类型,用来支持推导返回值类型
const func_argt = type.node.arguments.map((arg, index) => {
const func_argt = node.arguments.map((arg, index) => {
return { name: ftype.args[index].name, type: parseAstNode(arg, type) };
});

Expand Down Expand Up @@ -161,7 +177,7 @@ function parseMemberExpression(node, type) {
return null;
}
const name = names[i];
def = t.search(name).value;
def = t.search(name, node.base.range).value;
}

return typeOf(def);
Expand Down
6 changes: 5 additions & 1 deletion stdlibs/lua_5_1.lua
Expand Up @@ -41,7 +41,7 @@ function _new.iterator()
end

function _as.table()
return _table
return {}
end

function _as.number()
Expand Down Expand Up @@ -564,6 +564,10 @@ function rawset(table, index, value)
return _as.table(table)
end

function require(fileName)
return _as.table()
end

function select(index, ...)
return _as.any('value') or _as.number('for index is #')
end
Expand Down

0 comments on commit d1d2d5a

Please sign in to comment.