Skip to content

Commit

Permalink
fix anonymous function args order.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Mar 19, 2024
1 parent afc8661 commit 8279fa0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
16 changes: 8 additions & 8 deletions spec/outputs/5.1/attrib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ do
a, b = _obj_0[1], _obj_0[2]
end
end
local _anon_func_0 = function(f, error, _close_1, _arg_0, ...)
local _anon_func_0 = function(_close_1, error, f, _arg_0, ...)
do
local _ok_0 = _arg_0
_close_1(f)
Expand Down Expand Up @@ -88,10 +88,10 @@ do
end)(pcall(function(...)
local f = _anon_func_1(io)
local _close_1 = assert(getmetatable(f).__close)
return _anon_func_0(f, error, _close_1, pcall(function(...) end, ...))
return _anon_func_0(_close_1, error, f, pcall(function(...) end, ...))
end, ...))
end
local _anon_func_2 = function(d, error, _close_1, _arg_0, ...)
local _anon_func_2 = function(_close_1, d, error, _arg_0, ...)
do
local _ok_0 = _arg_0
_close_1(d)
Expand Down Expand Up @@ -149,10 +149,10 @@ do
local c = _anon_func_3(x)
local d = _anon_func_4(a, b)
local _close_1 = assert(getmetatable(d).__close)
return _anon_func_2(d, error, _close_1, pcall(function(...) end, ...))
return _anon_func_2(_close_1, d, error, pcall(function(...) end, ...))
end, ...))
end
local _anon_func_6 = function(_, error, _close_1, _arg_0, ...)
local _anon_func_6 = function(_, _close_1, error, _arg_0, ...)
do
local _ok_0 = _arg_0
_close_1(_)
Expand Down Expand Up @@ -196,7 +196,7 @@ do
end
})
local _close_1 = assert(getmetatable(_).__close)
return _anon_func_6(_, error, _close_1, pcall(function(...)
return _anon_func_6(_, _close_1, error, pcall(function(...)
local _ = setmetatable({ }, {
__close = function()
return print("first")
Expand All @@ -220,7 +220,7 @@ def = function(item)
_defers[#_defers + 1] = item
return _defers
end
local _anon_func_8 = function(_, error, _close_1, _arg_0, ...)
local _anon_func_8 = function(_, _close_1, error, _arg_0, ...)
do
local _ok_0 = _arg_0
_close_1(_)
Expand Down Expand Up @@ -260,7 +260,7 @@ do
return print(2)
end)
local _close_1 = assert(getmetatable(_).__close)
return _anon_func_8(_, error, _close_1, pcall(function(...)
return _anon_func_8(_, _close_1, error, pcall(function(...)
local _ = def(function()
return print(1)
end)
Expand Down
28 changes: 14 additions & 14 deletions spec/outputs/unicode/vararg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ local _anon_func_5 = function(_u9879_u76ee, ...)
end
return _accum_0
end
local _anon_func_6 = function(setmetatable, _u51fd_u6570)
local _anon_func_6 = function(_u51fd_u6570, setmetatable)
local _u53d8_u91cfA
do
local _class_0
Expand All @@ -71,7 +71,7 @@ local _anon_func_6 = function(setmetatable, _u51fd_u6570)
return _class_0
end
end
local _anon_func_7 = function(setmetatable, _u51fd_u6570, ...)
local _anon_func_7 = function(_u51fd_u6570, setmetatable, ...)
local _u53d8_u91cfA
do
local _class_0
Expand All @@ -98,14 +98,14 @@ local _anon_func_7 = function(setmetatable, _u51fd_u6570, ...)
return _class_0
end
end
local _anon_func_8 = function(pairs, _u8868)
local _anon_func_8 = function(_u8868, pairs)
local _tbl_0 = { }
for _u952e, _u503c in pairs(_u8868) do
_tbl_0[_u952e] = _u503c
end
return _tbl_0
end
local _anon_func_9 = function(pairs, _u8868, ...)
local _anon_func_9 = function(_u8868, pairs, ...)
local _tbl_0 = { }
for _u952e, _u503c in pairs(_u8868) do
_tbl_0[_u952e] = _u503c(...)
Expand Down Expand Up @@ -176,12 +176,12 @@ local _anon_func_19 = function(_u51fd_u6570, ...)
return _u51fd_u6570(...)
end
end
local _anon_func_20 = function(_u53d8_u91cfx, _u51fd_u6570)
local _anon_func_20 = function(_u51fd_u6570, _u53d8_u91cfx)
if "abc" == _u53d8_u91cfx then
return _u51fd_u6570()
end
end
local _anon_func_21 = function(_u53d8_u91cfx, _u51fd_u6570, ...)
local _anon_func_21 = function(_u51fd_u6570, _u53d8_u91cfx, ...)
if "abc" == _u53d8_u91cfx then
return _u51fd_u6570(...)
end
Expand All @@ -198,7 +198,7 @@ local _anon_func_23 = function(_u51fd_u6570, ...)
end
return nil
end
local _anon_func_24 = function(select, _u6253_u5370, ...)
local _anon_func_24 = function(_u6253_u5370, select, ...)
do
_u6253_u5370(select("#", ...))
return _u6253_u5370(...)
Expand Down Expand Up @@ -241,10 +241,10 @@ _u8fde_u63a5 = function(...)
_u5217_u8868_u751f_u6210(_anon_func_3(_u9879_u76ee, ...))
_u5217_u8868_u751f_u6210(_anon_func_4(_u9879_u76ee))
_u5217_u8868_u751f_u6210(_anon_func_5(_u9879_u76ee, ...))
_u7c7b_u751f_u6210(_anon_func_6(setmetatable, _u51fd_u6570))
_u7c7b_u751f_u6210(_anon_func_7(setmetatable, _u51fd_u6570, ...))
_u8868_u751f_u6210(_anon_func_8(pairs, _u8868))
_u8868_u751f_u6210(_anon_func_9(pairs, _u8868, ...))
_u7c7b_u751f_u6210(_anon_func_6(_u51fd_u6570, setmetatable))
_u7c7b_u751f_u6210(_anon_func_7(_u51fd_u6570, setmetatable, ...))
_u8868_u751f_u6210(_anon_func_8(_u8868, pairs))
_u8868_u751f_u6210(_anon_func_9(_u8868, pairs, ...))
_u8868_u751f_u6210(_anon_func_10(_u9879_u76ee))
_u8868_u751f_u6210(_anon_func_11(_u9879_u76ee, ...))
_u505a_u64cd_u4f5c(_anon_func_12(_u51fd_u6570))
Expand All @@ -255,8 +255,8 @@ _u8fde_u63a5 = function(...)
_u5982_u679c_u64cd_u4f5c(_anon_func_17(_u51fd_u6570, ...))
_u9664_u975e_u64cd_u4f5c(_anon_func_18(_u51fd_u6570))
_u9664_u975e_u64cd_u4f5c(_anon_func_19(_u51fd_u6570, ...))
_u5207_u6362_u64cd_u4f5c(_anon_func_20(_u53d8_u91cfx, _u51fd_u6570))
_u5207_u6362_u64cd_u4f5c(_anon_func_21(_u53d8_u91cfx, _u51fd_u6570, ...))
_u5207_u6362_u64cd_u4f5c(_anon_func_20(_u51fd_u6570, _u53d8_u91cfx))
_u5207_u6362_u64cd_u4f5c(_anon_func_21(_u51fd_u6570, _u53d8_u91cfx, ...))
_u8868_u8fbe_u5f0f_u64cd_u4f5c(_anon_func_22(_u51fd_u6570))
_u8868_u8fbe_u5f0f_u64cd_u4f5c(_anon_func_23(_u51fd_u6570, ...))
_u5192_u53f7((function()
Expand Down Expand Up @@ -293,7 +293,7 @@ _u8fde_u63a5 = function(...)
_u591a_u53c2_u6570_u51fd_u6570 = function()
return 10, nil, 20, nil, 30
end
return _anon_func_24(select, _u6253_u5370, _u591a_u53c2_u6570_u51fd_u6570())
return _anon_func_24(_u6253_u5370, select, _u591a_u53c2_u6570_u51fd_u6570())
end)(_u51fd_u6570_u540d(true))
end
do
Expand Down
24 changes: 12 additions & 12 deletions spec/outputs/vararg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ local _anon_func_5 = function(items, ...)
end
return _accum_0
end
local _anon_func_6 = function(setmetatable, func)
local _anon_func_6 = function(func, setmetatable)
local A
do
local _class_0
Expand All @@ -71,7 +71,7 @@ local _anon_func_6 = function(setmetatable, func)
return _class_0
end
end
local _anon_func_7 = function(setmetatable, func, ...)
local _anon_func_7 = function(func, setmetatable, ...)
local A
do
local _class_0
Expand Down Expand Up @@ -176,12 +176,12 @@ local _anon_func_19 = function(func, ...)
return func(...)
end
end
local _anon_func_20 = function(x, func)
local _anon_func_20 = function(func, x)
if "abc" == x then
return func()
end
end
local _anon_func_21 = function(x, func, ...)
local _anon_func_21 = function(func, x, ...)
if "abc" == x then
return func(...)
end
Expand All @@ -198,7 +198,7 @@ local _anon_func_23 = function(func, ...)
end
return nil
end
local _anon_func_24 = function(select, print, ...)
local _anon_func_24 = function(print, select, ...)
do
print(select("#", ...))
return print(...)
Expand All @@ -209,7 +209,7 @@ local _anon_func_25 = function(print, ...)
return print(...)
end
end
local _anon_func_26 = function(x, tb, tb2)
local _anon_func_26 = function(tb, tb2, x)
if 1 == x then
tb.x = 123
return tb
Expand Down Expand Up @@ -241,8 +241,8 @@ join = function(...)
f_listcomp(_anon_func_3(items, ...))
f_listcomp(_anon_func_4(items))
f_listcomp(_anon_func_5(items, ...))
f_class(_anon_func_6(setmetatable, func))
f_class(_anon_func_7(setmetatable, func, ...))
f_class(_anon_func_6(func, setmetatable))
f_class(_anon_func_7(func, setmetatable, ...))
f_tblcomp(_anon_func_8(pairs, tb))
f_tblcomp(_anon_func_9(pairs, tb, ...))
f_tblcomp(_anon_func_10(items))
Expand All @@ -255,8 +255,8 @@ join = function(...)
f_if(_anon_func_17(func, ...))
f_unless(_anon_func_18(func))
f_unless(_anon_func_19(func, ...))
f_switch(_anon_func_20(x, func))
f_switch(_anon_func_21(x, func, ...))
f_switch(_anon_func_20(func, x))
f_switch(_anon_func_21(func, x, ...))
f_eop(_anon_func_22(func))
f_eop(_anon_func_23(func, ...))
f_colon((function()
Expand Down Expand Up @@ -293,11 +293,11 @@ join = function(...)
fn_many_args = function()
return 10, nil, 20, nil, 30
end
return _anon_func_24(select, print, fn_many_args())
return _anon_func_24(print, select, fn_many_args())
end)(fn(true))
end
do
_anon_func_25(print, _anon_func_26(x, tb, tb2))
_anon_func_25(print, _anon_func_26(tb, tb2, x))
end
do
_anon_func_27(print, 1, 2, _anon_func_28(cond))
Expand Down
24 changes: 16 additions & 8 deletions src/yuescript/yue_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3653,7 +3653,7 @@ class YueCompilerImpl {
}
}

std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgList = nullptr) {
std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgListInTheEnd = nullptr) {
if (_funcLevel <= 1) return std::nullopt;
auto result = exp->traverse([&](ast_node* node) {
switch (node->get_id()) {
Expand Down Expand Up @@ -3725,25 +3725,33 @@ class YueCompilerImpl {
}
if (!upVarsAssignedOrCaptured) {
auto x = exp;
if (usedVar) {
args.push_back("..."s);
}
if (ensureArgList) {
if (ensureArgListInTheEnd) {
std::unordered_set<std::string> vars;
for (const auto& arg : args) {
vars.insert(arg);
}
for (const auto& arg : *ensureArgList) {
for (const auto& arg : *ensureArgListInTheEnd) {
vars.erase(arg);
}
str_list finalArgs;
for (const auto& arg : vars) {
finalArgs.push_back(arg);
}
for (const auto& arg : *ensureArgList) {
finalArgs.sort();
for (const auto& arg : *ensureArgListInTheEnd) {
finalArgs.push_back(arg);
}
if (usedVar) {
if (finalArgs.back() != "..."sv) {
finalArgs.push_back("..."s);
}
}
args = std::move(finalArgs);
} else {
args.sort();
if (usedVar) {
args.push_back("..."s);
}
}
auto funLit = toAst<FunLit_t>("("s + join(args, ","sv) + ")-> nil"s, x);
funLit->body->content.set(stmt.get());
Expand Down Expand Up @@ -8070,7 +8078,7 @@ class YueCompilerImpl {
auto chainValue = static_cast<ChainValue_t*>(value->item.get());
if (auto callable = ast_cast<Callable_t>(chainValue->items.front()); callable && chainValue->items.size() == 1) {
if (auto self = callable->item.as<SelfItem_t>()) {
if (auto selfVar = self->name.as<Self_t>()) {
if (self->name.as<Self_t>()) {
classTextName = "\"self\"";
}
} else if (auto var = callable->item.as<Variable_t>()) {
Expand Down

0 comments on commit 8279fa0

Please sign in to comment.