From 62ddb888d67d047324aa6c411d47aaeac9b658fd Mon Sep 17 00:00:00 2001 From: Li Jin Date: Tue, 19 Mar 2024 10:01:17 +0800 Subject: fix Lua 5.1 close attribute compatible function. --- spec/outputs/5.1/attrib.lua | 72 ++++++++++++++++++++++++------------------ src/yuescript/yue_compiler.cpp | 23 ++++++-------- 2 files changed, 50 insertions(+), 45 deletions(-) diff --git a/spec/outputs/5.1/attrib.lua b/spec/outputs/5.1/attrib.lua index 7391e6e..a9f12e3 100644 --- a/spec/outputs/5.1/attrib.lua +++ b/spec/outputs/5.1/attrib.lua @@ -16,11 +16,11 @@ do else return error(...) end - end)(pcall(function(...) + end)(pcall(function() local c, d = 123, 'abc' close(a, b) return const(c, d) - end, ...)) + end)) end do local a = f() @@ -66,6 +66,10 @@ local _anon_func_1 = function(io) _with_0:write("Hello") return _with_0 end +local _anon_func_2 = function() + do + end +end do local v = (function() if flag then @@ -85,13 +89,13 @@ do else return error(...) end - end)(pcall(function(...) + end)(pcall(function() local f = _anon_func_1(io) local _close_1 = assert(getmetatable(f).__close) - return _anon_func_0(_close_1, error, f, pcall(function(...) end, ...)) - end, ...)) + return _anon_func_0(_close_1, error, f, pcall(_anon_func_2)) + end)) end -local _anon_func_2 = function(_close_1, d, error, _arg_0, ...) +local _anon_func_3 = function(_close_1, d, error, _arg_0, ...) do local _ok_0 = _arg_0 _close_1(d) @@ -102,27 +106,31 @@ local _anon_func_2 = function(_close_1, d, error, _arg_0, ...) end end end -local _anon_func_3 = function(x) +local _anon_func_4 = function(x) if true then if "abc" == x then return 998 end end end -local _anon_func_5 = function(a, b) +local _anon_func_6 = function(a, b) if a ~= nil then return a else return b end end -local _anon_func_4 = function(a, b) - if _anon_func_5(a, b) then +local _anon_func_5 = function(a, b) + if _anon_func_6(a, b) then return { value = value } end end +local _anon_func_7 = function() + do + end +end do local a = (function() if true then @@ -145,14 +153,14 @@ do else return error(...) end - end)(pcall(function(...) - local c = _anon_func_3(x) - local d = _anon_func_4(a, b) + end)(pcall(function() + local c = _anon_func_4(x) + local d = _anon_func_5(a, b) local _close_1 = assert(getmetatable(d).__close) - return _anon_func_2(_close_1, d, error, pcall(function(...) end, ...)) - end, ...)) + return _anon_func_3(_close_1, d, error, pcall(_anon_func_7)) + end)) end -local _anon_func_6 = function(_, _close_1, error, _arg_0, ...) +local _anon_func_8 = function(_, _close_1, error, _arg_0, ...) do local _ok_0 = _arg_0 _close_1(_) @@ -163,7 +171,7 @@ local _anon_func_6 = function(_, _close_1, error, _arg_0, ...) end end end -local _anon_func_7 = function(_, _close_2, error, _arg_0, ...) +local _anon_func_9 = function(_, _close_2, error, _arg_0, ...) do local _ok_0 = _arg_0 _close_2(_) @@ -189,25 +197,23 @@ do else return error(...) end - end)(pcall(function(...) + end)(pcall(function() local _ = setmetatable({ }, { __close = function() return print("second") end }) local _close_1 = assert(getmetatable(_).__close) - return _anon_func_6(_, _close_1, error, pcall(function(...) + return _anon_func_8(_, _close_1, error, pcall(function() local _ = setmetatable({ }, { __close = function() return print("first") end }) local _close_2 = assert(getmetatable(_).__close) - return _anon_func_7(_, _close_2, error, pcall(function(...) - return print("third") - end, ...)) - end, ...)) - end, ...)) + return _anon_func_9(_, _close_2, error, pcall(print, "third")) + end)) + end)) end local _defers = setmetatable({ }, { __close = function(self) @@ -220,7 +226,7 @@ def = function(item) _defers[#_defers + 1] = item return _defers end -local _anon_func_8 = function(_, _close_1, error, _arg_0, ...) +local _anon_func_10 = function(_, _close_1, error, _arg_0, ...) do local _ok_0 = _arg_0 _close_1(_) @@ -231,7 +237,7 @@ local _anon_func_8 = function(_, _close_1, error, _arg_0, ...) end end end -local _anon_func_9 = function(_, _close_2, error, _arg_0, ...) +local _anon_func_11 = function(_, _close_2, error, _arg_0, ...) do local _ok_0 = _arg_0 _close_2(_) @@ -242,6 +248,10 @@ local _anon_func_9 = function(_, _close_2, error, _arg_0, ...) end end end +local _anon_func_12 = function() + do + end +end do local _ = def(function() return print(3) @@ -255,17 +265,17 @@ do else return error(...) end - end)(pcall(function(...) + end)(pcall(function() local _ = def(function() return print(2) end) local _close_1 = assert(getmetatable(_).__close) - return _anon_func_8(_, _close_1, error, pcall(function(...) + return _anon_func_10(_, _close_1, error, pcall(function() local _ = def(function() return print(1) end) local _close_2 = assert(getmetatable(_).__close) - return _anon_func_9(_, _close_2, error, pcall(function(...) end, ...)) - end, ...)) - end, ...)) + return _anon_func_11(_, _close_2, error, pcall(_anon_func_12)) + end)) + end)) end diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index de41b45..a443f39 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -4352,21 +4352,11 @@ private: } popScope(); auto okVar = getUnusedName("_ok_"sv); - std::string pCallStmtStr; - if (!_varArgs.empty() && _varArgs.top().hasVar) { - pCallStmtStr = okVar + ", ... = pcall((...)->, ...)"s; - } else { - pCallStmtStr = okVar + ", ... = pcall(->)"s; - } + std::string pCallStmtStr = okVar + ", ... = try\n\tnil"s; auto pCallStmt = toAst(pCallStmtStr, x); auto pCallExp = ast_to(pCallStmt->content.to()->action.to()->values.back()); auto value = singleValueFrom(pCallExp); - auto invoke = ast_to(value->item.to()->items.back()); - auto pCallValue = singleValueFrom(ast_to(invoke->args.front())); - auto pCallFunc = pCallValue->item.to()->value.to(); - auto pCallBody = x->new_ptr(); - pCallBody->content.set(followingBlock); - pCallFunc->body.set(pCallBody); + value->item.to()->value.to()->func.set(followingBlock); for (const auto& stmt : getCloses) { newBlock->statements.push_back(toAst(stmt, x)); } @@ -9028,6 +9018,7 @@ private: BREAK_IF(tryBlock->statements.size() != 1); auto stmt = static_cast(tryBlock->statements.front()); auto expListAssign = stmt->content.as(); + BREAK_IF(!expListAssign); BREAK_IF(expListAssign->action); auto value = singleValueFrom(expListAssign->expList); BREAK_IF(!value); @@ -9052,13 +9043,17 @@ private: auto invoke = ast_to(xpcall->items.back()); invoke->args.push_back(toAst(funcName, x)); invoke->args.push_back(errHandler); - invoke->args.dup(toAst(join(args, ","sv), x)->exprs); + if (!args.empty()) { + invoke->args.dup(toAst(join(args, ","sv), x)->exprs); + } transformChainValue(xpcall, out, ExpUsage::Closure); } else { auto pcall = toAst("pcall()", x); auto invoke = ast_to(pcall->items.back()); invoke->args.push_back(toAst(funcName, x)); - invoke->args.dup(toAst(join(args, ","sv), x)->exprs); + if (!args.empty()) { + invoke->args.dup(toAst(join(args, ","sv), x)->exprs); + } transformChainValue(pcall, out, ExpUsage::Closure); } if (usage == ExpUsage::Common) { -- cgit v1.2.3-55-g6feb