Skip to content

Commit

Permalink
fix Lua 5.1 close attribute compatible function.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Mar 19, 2024
1 parent a291e14 commit 62ddb88
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
72 changes: 41 additions & 31 deletions spec/outputs/5.1/attrib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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(_)
Expand All @@ -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(_)
Expand All @@ -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)
Expand All @@ -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(_)
Expand All @@ -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(_)
Expand All @@ -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)
Expand All @@ -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
23 changes: 9 additions & 14 deletions src/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4352,21 +4352,11 @@ class YueCompilerImpl {
}
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<Statement_t>(pCallStmtStr, x);
auto pCallExp = ast_to<Exp_t>(pCallStmt->content.to<ExpListAssign_t>()->action.to<Assign_t>()->values.back());
auto value = singleValueFrom(pCallExp);
auto invoke = ast_to<Invoke_t>(value->item.to<ChainValue_t>()->items.back());
auto pCallValue = singleValueFrom(ast_to<Exp_t>(invoke->args.front()));
auto pCallFunc = pCallValue->item.to<SimpleValue_t>()->value.to<FunLit_t>();
auto pCallBody = x->new_ptr<Body_t>();
pCallBody->content.set(followingBlock);
pCallFunc->body.set(pCallBody);
value->item.to<SimpleValue_t>()->value.to<Try_t>()->func.set(followingBlock);
for (const auto& stmt : getCloses) {
newBlock->statements.push_back(toAst<Statement_t>(stmt, x));
}
Expand Down Expand Up @@ -9028,6 +9018,7 @@ class YueCompilerImpl {
BREAK_IF(tryBlock->statements.size() != 1);
auto stmt = static_cast<Statement_t*>(tryBlock->statements.front());
auto expListAssign = stmt->content.as<ExpListAssign_t>();
BREAK_IF(!expListAssign);
BREAK_IF(expListAssign->action);
auto value = singleValueFrom(expListAssign->expList);
BREAK_IF(!value);
Expand All @@ -9052,13 +9043,17 @@ class YueCompilerImpl {
auto invoke = ast_to<Invoke_t>(xpcall->items.back());
invoke->args.push_back(toAst<Exp_t>(funcName, x));
invoke->args.push_back(errHandler);
invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs);
if (!args.empty()) {
invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs);
}
transformChainValue(xpcall, out, ExpUsage::Closure);
} else {
auto pcall = toAst<ChainValue_t>("pcall()", x);
auto invoke = ast_to<Invoke_t>(pcall->items.back());
invoke->args.push_back(toAst<Exp_t>(funcName, x));
invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs);
if (!args.empty()) {
invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs);
}
transformChainValue(pcall, out, ExpUsage::Closure);
}
if (usage == ExpUsage::Common) {
Expand Down

0 comments on commit 62ddb88

Please sign in to comment.