Skip to content

Commit

Permalink
feat:special handle multiple return functions. close 5
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangnanscu committed Oct 9, 2024
1 parent 49f2c50 commit 258cbcc
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ js2lua(`let a = 1`, { importStatementHoisting: true });
* [index0To1](#index0To1)
* [keywords](#keywords)
* [loop](#loop)
* [multipleReturnForLuaFunction](#multipleReturnForLuaFunction)
* [object](#object)
* [operator](#operator)
* [optionalNullish](#optionalNullish)
Expand Down Expand Up @@ -662,6 +663,39 @@ do
end
end

```
## multipleReturnForLuaFunction
### js
```js
let [ok, res] = pcall(foo)
let [ok2, res2] = xpcall(foo)
let [e1, e2] = unpack(bar)
// comparision
let [ok3, res3] = pcall2(foo)
let [ok4, ...res4] = xpcall(foo)

```
### lua
```lua
local ok, res = pcall(foo)
local ok2, res2 = xpcall(foo)
local e1, e2 = unpack(bar)
local ok3, res3
do
local __tmp = pcall2(foo)
ok3 = __tmp[1]
res3 = __tmp[2]
end
local ok4, res4
do
local __tmp = xpcall(foo)
ok4 = __tmp[1]
res4 = {}
for __i = 2, #__tmp do
res4[#res4 + 1] = __tmp[__i]
end
end

```
## object
### js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xiangnanscu/js2lua",
"version": "0.42.0",
"version": "0.43.0",
"type": "module",
"description": "Writing LuaJIT with the expressiveness of JavaScript.",
"main": "src/js2lua.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const luacode = computed(() => {
return js2lua(jscode.value, selectOptions.value);
});
const jsast = computed(() => js2ast(jscode.value, selectOptions.value));
const jsast = computed(() => js2ast(jscode.value, selectOptions.value)?.program.body);
function copylua() {
CopyToClipboard("luacode");
}
Expand Down
35 changes: 35 additions & 0 deletions src/js2lua.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ function hasIdentifier(ast, name, depth) {
);
return find;
}
const multiReturnFunctions = ['pcall', 'xpcall', 'unpack'];
function isMultiReturnAssignment(ast) {
// 检查是否为赋值语句
if (ast.type !== 'VariableDeclaration' || ast.declarations.length !== 1) {
return false;
}

const declaration = ast.declarations[0];

// 检查左边是否为数组解构形式
if (declaration.id.type !== 'ArrayPattern') {
return false;
}

// 检查数组解构的元素是否全部为标识符
if (!declaration.id.elements.every(element => element && element.type === 'Identifier')) {
return false;
}

// 检查右边是否为函数调用
if (declaration.init?.type !== 'CallExpression') {
return false;
}

// 检查函数名是否为Lua的多返回值内置函数

const calleeName = declaration.init.callee.name;

return multiReturnFunctions.includes(calleeName);
}

function renameThisToCls(ast) {
walkAst(ast, (node) => {
if (node.type == "ThisExpression") {
Expand Down Expand Up @@ -441,6 +472,10 @@ function ast2lua(ast, opts = {}) {
case "File":
return joinAst(ast.program.body, ";\n");
case "VariableDeclaration": {
if (isMultiReturnAssignment(ast)) {
const declare = ast.declarations[0]
return `local ${declare.id.elements.map(_ast2lua).join(", ")} = ${_ast2lua(declare.init)}`;
}
const declarePrefix = ast.noPrefix ? "" : "local ";
return ast.declarations
.map(_ast2lua)
Expand Down
2 changes: 2 additions & 0 deletions src/jsInit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { a, b: bAlias, ...rest } = { a: 1, b: 2, c: 3, d: 4 }
const [x, y, ...others] = [1, 2, 3, 4]
for (i = 0; i < 10; i = i + 2) {
if (i % 2) {
continue;
Expand Down
6 changes: 6 additions & 0 deletions test/multipleReturnForLuaFunction.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let [ok, res] = pcall(foo)
let [ok2, res2] = xpcall(foo)
let [e1, e2] = unpack(bar)
// comparision
let [ok3, res3] = pcall2(foo)
let [ok4, ...res4] = xpcall(foo)

0 comments on commit 258cbcc

Please sign in to comment.