diff --git a/spec/outputs/5.1/attrib.lua b/spec/outputs/5.1/attrib.lua index e28dca4..7391e6e 100644 --- a/spec/outputs/5.1/attrib.lua +++ b/spec/outputs/5.1/attrib.lua @@ -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) @@ -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) @@ -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(_) @@ -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") @@ -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(_) @@ -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) diff --git a/spec/outputs/unicode/vararg.lua b/spec/outputs/unicode/vararg.lua index 39fbf0c..4acb6cc 100644 --- a/spec/outputs/unicode/vararg.lua +++ b/spec/outputs/unicode/vararg.lua @@ -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 @@ -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 @@ -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(...) @@ -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 @@ -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(...) @@ -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)) @@ -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() @@ -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 diff --git a/spec/outputs/vararg.lua b/spec/outputs/vararg.lua index 39becd4..27918e9 100644 --- a/spec/outputs/vararg.lua +++ b/spec/outputs/vararg.lua @@ -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 @@ -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 @@ -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 @@ -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(...) @@ -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 @@ -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)) @@ -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() @@ -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)) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index d416dbd..de41b45 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -3653,7 +3653,7 @@ class YueCompilerImpl { } } - std::optional> upValueFuncFrom(Exp_t* exp, str_list* ensureArgList = nullptr) { + std::optional> 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()) { @@ -3725,25 +3725,33 @@ class YueCompilerImpl { } if (!upVarsAssignedOrCaptured) { auto x = exp; - if (usedVar) { - args.push_back("..."s); - } - if (ensureArgList) { + if (ensureArgListInTheEnd) { std::unordered_set 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("("s + join(args, ","sv) + ")-> nil"s, x); funLit->body->content.set(stmt.get()); @@ -8070,7 +8078,7 @@ class YueCompilerImpl { auto chainValue = static_cast(value->item.get()); if (auto callable = ast_cast(chainValue->items.front()); callable && chainValue->items.size() == 1) { if (auto self = callable->item.as()) { - if (auto selfVar = self->name.as()) { + if (self->name.as()) { classTextName = "\"self\""; } } else if (auto var = callable->item.as()) {