aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spec/outputs/5.1/attrib.lua16
-rw-r--r--spec/outputs/unicode/vararg.lua28
-rw-r--r--spec/outputs/vararg.lua24
-rw-r--r--src/yuescript/yue_compiler.cpp24
4 files changed, 50 insertions, 42 deletions
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
50 a, b = _obj_0[1], _obj_0[2] 50 a, b = _obj_0[1], _obj_0[2]
51 end 51 end
52end 52end
53local _anon_func_0 = function(f, error, _close_1, _arg_0, ...) 53local _anon_func_0 = function(_close_1, error, f, _arg_0, ...)
54 do 54 do
55 local _ok_0 = _arg_0 55 local _ok_0 = _arg_0
56 _close_1(f) 56 _close_1(f)
@@ -88,10 +88,10 @@ do
88 end)(pcall(function(...) 88 end)(pcall(function(...)
89 local f = _anon_func_1(io) 89 local f = _anon_func_1(io)
90 local _close_1 = assert(getmetatable(f).__close) 90 local _close_1 = assert(getmetatable(f).__close)
91 return _anon_func_0(f, error, _close_1, pcall(function(...) end, ...)) 91 return _anon_func_0(_close_1, error, f, pcall(function(...) end, ...))
92 end, ...)) 92 end, ...))
93end 93end
94local _anon_func_2 = function(d, error, _close_1, _arg_0, ...) 94local _anon_func_2 = function(_close_1, d, error, _arg_0, ...)
95 do 95 do
96 local _ok_0 = _arg_0 96 local _ok_0 = _arg_0
97 _close_1(d) 97 _close_1(d)
@@ -149,10 +149,10 @@ do
149 local c = _anon_func_3(x) 149 local c = _anon_func_3(x)
150 local d = _anon_func_4(a, b) 150 local d = _anon_func_4(a, b)
151 local _close_1 = assert(getmetatable(d).__close) 151 local _close_1 = assert(getmetatable(d).__close)
152 return _anon_func_2(d, error, _close_1, pcall(function(...) end, ...)) 152 return _anon_func_2(_close_1, d, error, pcall(function(...) end, ...))
153 end, ...)) 153 end, ...))
154end 154end
155local _anon_func_6 = function(_, error, _close_1, _arg_0, ...) 155local _anon_func_6 = function(_, _close_1, error, _arg_0, ...)
156 do 156 do
157 local _ok_0 = _arg_0 157 local _ok_0 = _arg_0
158 _close_1(_) 158 _close_1(_)
@@ -196,7 +196,7 @@ do
196 end 196 end
197 }) 197 })
198 local _close_1 = assert(getmetatable(_).__close) 198 local _close_1 = assert(getmetatable(_).__close)
199 return _anon_func_6(_, error, _close_1, pcall(function(...) 199 return _anon_func_6(_, _close_1, error, pcall(function(...)
200 local _ = setmetatable({ }, { 200 local _ = setmetatable({ }, {
201 __close = function() 201 __close = function()
202 return print("first") 202 return print("first")
@@ -220,7 +220,7 @@ def = function(item)
220 _defers[#_defers + 1] = item 220 _defers[#_defers + 1] = item
221 return _defers 221 return _defers
222end 222end
223local _anon_func_8 = function(_, error, _close_1, _arg_0, ...) 223local _anon_func_8 = function(_, _close_1, error, _arg_0, ...)
224 do 224 do
225 local _ok_0 = _arg_0 225 local _ok_0 = _arg_0
226 _close_1(_) 226 _close_1(_)
@@ -260,7 +260,7 @@ do
260 return print(2) 260 return print(2)
261 end) 261 end)
262 local _close_1 = assert(getmetatable(_).__close) 262 local _close_1 = assert(getmetatable(_).__close)
263 return _anon_func_8(_, error, _close_1, pcall(function(...) 263 return _anon_func_8(_, _close_1, error, pcall(function(...)
264 local _ = def(function() 264 local _ = def(function()
265 return print(1) 265 return print(1)
266 end) 266 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, ...)
44 end 44 end
45 return _accum_0 45 return _accum_0
46end 46end
47local _anon_func_6 = function(setmetatable, _u51fd_u6570) 47local _anon_func_6 = function(_u51fd_u6570, setmetatable)
48 local _u53d8_u91cfA 48 local _u53d8_u91cfA
49 do 49 do
50 local _class_0 50 local _class_0
@@ -71,7 +71,7 @@ local _anon_func_6 = function(setmetatable, _u51fd_u6570)
71 return _class_0 71 return _class_0
72 end 72 end
73end 73end
74local _anon_func_7 = function(setmetatable, _u51fd_u6570, ...) 74local _anon_func_7 = function(_u51fd_u6570, setmetatable, ...)
75 local _u53d8_u91cfA 75 local _u53d8_u91cfA
76 do 76 do
77 local _class_0 77 local _class_0
@@ -98,14 +98,14 @@ local _anon_func_7 = function(setmetatable, _u51fd_u6570, ...)
98 return _class_0 98 return _class_0
99 end 99 end
100end 100end
101local _anon_func_8 = function(pairs, _u8868) 101local _anon_func_8 = function(_u8868, pairs)
102 local _tbl_0 = { } 102 local _tbl_0 = { }
103 for _u952e, _u503c in pairs(_u8868) do 103 for _u952e, _u503c in pairs(_u8868) do
104 _tbl_0[_u952e] = _u503c 104 _tbl_0[_u952e] = _u503c
105 end 105 end
106 return _tbl_0 106 return _tbl_0
107end 107end
108local _anon_func_9 = function(pairs, _u8868, ...) 108local _anon_func_9 = function(_u8868, pairs, ...)
109 local _tbl_0 = { } 109 local _tbl_0 = { }
110 for _u952e, _u503c in pairs(_u8868) do 110 for _u952e, _u503c in pairs(_u8868) do
111 _tbl_0[_u952e] = _u503c(...) 111 _tbl_0[_u952e] = _u503c(...)
@@ -176,12 +176,12 @@ local _anon_func_19 = function(_u51fd_u6570, ...)
176 return _u51fd_u6570(...) 176 return _u51fd_u6570(...)
177 end 177 end
178end 178end
179local _anon_func_20 = function(_u53d8_u91cfx, _u51fd_u6570) 179local _anon_func_20 = function(_u51fd_u6570, _u53d8_u91cfx)
180 if "abc" == _u53d8_u91cfx then 180 if "abc" == _u53d8_u91cfx then
181 return _u51fd_u6570() 181 return _u51fd_u6570()
182 end 182 end
183end 183end
184local _anon_func_21 = function(_u53d8_u91cfx, _u51fd_u6570, ...) 184local _anon_func_21 = function(_u51fd_u6570, _u53d8_u91cfx, ...)
185 if "abc" == _u53d8_u91cfx then 185 if "abc" == _u53d8_u91cfx then
186 return _u51fd_u6570(...) 186 return _u51fd_u6570(...)
187 end 187 end
@@ -198,7 +198,7 @@ local _anon_func_23 = function(_u51fd_u6570, ...)
198 end 198 end
199 return nil 199 return nil
200end 200end
201local _anon_func_24 = function(select, _u6253_u5370, ...) 201local _anon_func_24 = function(_u6253_u5370, select, ...)
202 do 202 do
203 _u6253_u5370(select("#", ...)) 203 _u6253_u5370(select("#", ...))
204 return _u6253_u5370(...) 204 return _u6253_u5370(...)
@@ -241,10 +241,10 @@ _u8fde_u63a5 = function(...)
241 _u5217_u8868_u751f_u6210(_anon_func_3(_u9879_u76ee, ...)) 241 _u5217_u8868_u751f_u6210(_anon_func_3(_u9879_u76ee, ...))
242 _u5217_u8868_u751f_u6210(_anon_func_4(_u9879_u76ee)) 242 _u5217_u8868_u751f_u6210(_anon_func_4(_u9879_u76ee))
243 _u5217_u8868_u751f_u6210(_anon_func_5(_u9879_u76ee, ...)) 243 _u5217_u8868_u751f_u6210(_anon_func_5(_u9879_u76ee, ...))
244 _u7c7b_u751f_u6210(_anon_func_6(setmetatable, _u51fd_u6570)) 244 _u7c7b_u751f_u6210(_anon_func_6(_u51fd_u6570, setmetatable))
245 _u7c7b_u751f_u6210(_anon_func_7(setmetatable, _u51fd_u6570, ...)) 245 _u7c7b_u751f_u6210(_anon_func_7(_u51fd_u6570, setmetatable, ...))
246 _u8868_u751f_u6210(_anon_func_8(pairs, _u8868)) 246 _u8868_u751f_u6210(_anon_func_8(_u8868, pairs))
247 _u8868_u751f_u6210(_anon_func_9(pairs, _u8868, ...)) 247 _u8868_u751f_u6210(_anon_func_9(_u8868, pairs, ...))
248 _u8868_u751f_u6210(_anon_func_10(_u9879_u76ee)) 248 _u8868_u751f_u6210(_anon_func_10(_u9879_u76ee))
249 _u8868_u751f_u6210(_anon_func_11(_u9879_u76ee, ...)) 249 _u8868_u751f_u6210(_anon_func_11(_u9879_u76ee, ...))
250 _u505a_u64cd_u4f5c(_anon_func_12(_u51fd_u6570)) 250 _u505a_u64cd_u4f5c(_anon_func_12(_u51fd_u6570))
@@ -255,8 +255,8 @@ _u8fde_u63a5 = function(...)
255 _u5982_u679c_u64cd_u4f5c(_anon_func_17(_u51fd_u6570, ...)) 255 _u5982_u679c_u64cd_u4f5c(_anon_func_17(_u51fd_u6570, ...))
256 _u9664_u975e_u64cd_u4f5c(_anon_func_18(_u51fd_u6570)) 256 _u9664_u975e_u64cd_u4f5c(_anon_func_18(_u51fd_u6570))
257 _u9664_u975e_u64cd_u4f5c(_anon_func_19(_u51fd_u6570, ...)) 257 _u9664_u975e_u64cd_u4f5c(_anon_func_19(_u51fd_u6570, ...))
258 _u5207_u6362_u64cd_u4f5c(_anon_func_20(_u53d8_u91cfx, _u51fd_u6570)) 258 _u5207_u6362_u64cd_u4f5c(_anon_func_20(_u51fd_u6570, _u53d8_u91cfx))
259 _u5207_u6362_u64cd_u4f5c(_anon_func_21(_u53d8_u91cfx, _u51fd_u6570, ...)) 259 _u5207_u6362_u64cd_u4f5c(_anon_func_21(_u51fd_u6570, _u53d8_u91cfx, ...))
260 _u8868_u8fbe_u5f0f_u64cd_u4f5c(_anon_func_22(_u51fd_u6570)) 260 _u8868_u8fbe_u5f0f_u64cd_u4f5c(_anon_func_22(_u51fd_u6570))
261 _u8868_u8fbe_u5f0f_u64cd_u4f5c(_anon_func_23(_u51fd_u6570, ...)) 261 _u8868_u8fbe_u5f0f_u64cd_u4f5c(_anon_func_23(_u51fd_u6570, ...))
262 _u5192_u53f7((function() 262 _u5192_u53f7((function()
@@ -293,7 +293,7 @@ _u8fde_u63a5 = function(...)
293 _u591a_u53c2_u6570_u51fd_u6570 = function() 293 _u591a_u53c2_u6570_u51fd_u6570 = function()
294 return 10, nil, 20, nil, 30 294 return 10, nil, 20, nil, 30
295 end 295 end
296 return _anon_func_24(select, _u6253_u5370, _u591a_u53c2_u6570_u51fd_u6570()) 296 return _anon_func_24(_u6253_u5370, select, _u591a_u53c2_u6570_u51fd_u6570())
297 end)(_u51fd_u6570_u540d(true)) 297 end)(_u51fd_u6570_u540d(true))
298 end 298 end
299 do 299 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, ...)
44 end 44 end
45 return _accum_0 45 return _accum_0
46end 46end
47local _anon_func_6 = function(setmetatable, func) 47local _anon_func_6 = function(func, setmetatable)
48 local A 48 local A
49 do 49 do
50 local _class_0 50 local _class_0
@@ -71,7 +71,7 @@ local _anon_func_6 = function(setmetatable, func)
71 return _class_0 71 return _class_0
72 end 72 end
73end 73end
74local _anon_func_7 = function(setmetatable, func, ...) 74local _anon_func_7 = function(func, setmetatable, ...)
75 local A 75 local A
76 do 76 do
77 local _class_0 77 local _class_0
@@ -176,12 +176,12 @@ local _anon_func_19 = function(func, ...)
176 return func(...) 176 return func(...)
177 end 177 end
178end 178end
179local _anon_func_20 = function(x, func) 179local _anon_func_20 = function(func, x)
180 if "abc" == x then 180 if "abc" == x then
181 return func() 181 return func()
182 end 182 end
183end 183end
184local _anon_func_21 = function(x, func, ...) 184local _anon_func_21 = function(func, x, ...)
185 if "abc" == x then 185 if "abc" == x then
186 return func(...) 186 return func(...)
187 end 187 end
@@ -198,7 +198,7 @@ local _anon_func_23 = function(func, ...)
198 end 198 end
199 return nil 199 return nil
200end 200end
201local _anon_func_24 = function(select, print, ...) 201local _anon_func_24 = function(print, select, ...)
202 do 202 do
203 print(select("#", ...)) 203 print(select("#", ...))
204 return print(...) 204 return print(...)
@@ -209,7 +209,7 @@ local _anon_func_25 = function(print, ...)
209 return print(...) 209 return print(...)
210 end 210 end
211end 211end
212local _anon_func_26 = function(x, tb, tb2) 212local _anon_func_26 = function(tb, tb2, x)
213 if 1 == x then 213 if 1 == x then
214 tb.x = 123 214 tb.x = 123
215 return tb 215 return tb
@@ -241,8 +241,8 @@ join = function(...)
241 f_listcomp(_anon_func_3(items, ...)) 241 f_listcomp(_anon_func_3(items, ...))
242 f_listcomp(_anon_func_4(items)) 242 f_listcomp(_anon_func_4(items))
243 f_listcomp(_anon_func_5(items, ...)) 243 f_listcomp(_anon_func_5(items, ...))
244 f_class(_anon_func_6(setmetatable, func)) 244 f_class(_anon_func_6(func, setmetatable))
245 f_class(_anon_func_7(setmetatable, func, ...)) 245 f_class(_anon_func_7(func, setmetatable, ...))
246 f_tblcomp(_anon_func_8(pairs, tb)) 246 f_tblcomp(_anon_func_8(pairs, tb))
247 f_tblcomp(_anon_func_9(pairs, tb, ...)) 247 f_tblcomp(_anon_func_9(pairs, tb, ...))
248 f_tblcomp(_anon_func_10(items)) 248 f_tblcomp(_anon_func_10(items))
@@ -255,8 +255,8 @@ join = function(...)
255 f_if(_anon_func_17(func, ...)) 255 f_if(_anon_func_17(func, ...))
256 f_unless(_anon_func_18(func)) 256 f_unless(_anon_func_18(func))
257 f_unless(_anon_func_19(func, ...)) 257 f_unless(_anon_func_19(func, ...))
258 f_switch(_anon_func_20(x, func)) 258 f_switch(_anon_func_20(func, x))
259 f_switch(_anon_func_21(x, func, ...)) 259 f_switch(_anon_func_21(func, x, ...))
260 f_eop(_anon_func_22(func)) 260 f_eop(_anon_func_22(func))
261 f_eop(_anon_func_23(func, ...)) 261 f_eop(_anon_func_23(func, ...))
262 f_colon((function() 262 f_colon((function()
@@ -293,11 +293,11 @@ join = function(...)
293 fn_many_args = function() 293 fn_many_args = function()
294 return 10, nil, 20, nil, 30 294 return 10, nil, 20, nil, 30
295 end 295 end
296 return _anon_func_24(select, print, fn_many_args()) 296 return _anon_func_24(print, select, fn_many_args())
297 end)(fn(true)) 297 end)(fn(true))
298 end 298 end
299 do 299 do
300 _anon_func_25(print, _anon_func_26(x, tb, tb2)) 300 _anon_func_25(print, _anon_func_26(tb, tb2, x))
301 end 301 end
302 do 302 do
303 _anon_func_27(print, 1, 2, _anon_func_28(cond)) 303 _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 @@ private:
3653 } 3653 }
3654 } 3654 }
3655 3655
3656 std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgList = nullptr) { 3656 std::optional<std::pair<std::string, str_list>> upValueFuncFrom(Exp_t* exp, str_list* ensureArgListInTheEnd = nullptr) {
3657 if (_funcLevel <= 1) return std::nullopt; 3657 if (_funcLevel <= 1) return std::nullopt;
3658 auto result = exp->traverse([&](ast_node* node) { 3658 auto result = exp->traverse([&](ast_node* node) {
3659 switch (node->get_id()) { 3659 switch (node->get_id()) {
@@ -3725,25 +3725,33 @@ private:
3725 } 3725 }
3726 if (!upVarsAssignedOrCaptured) { 3726 if (!upVarsAssignedOrCaptured) {
3727 auto x = exp; 3727 auto x = exp;
3728 if (usedVar) { 3728 if (ensureArgListInTheEnd) {
3729 args.push_back("..."s);
3730 }
3731 if (ensureArgList) {
3732 std::unordered_set<std::string> vars; 3729 std::unordered_set<std::string> vars;
3733 for (const auto& arg : args) { 3730 for (const auto& arg : args) {
3734 vars.insert(arg); 3731 vars.insert(arg);
3735 } 3732 }
3736 for (const auto& arg : *ensureArgList) { 3733 for (const auto& arg : *ensureArgListInTheEnd) {
3737 vars.erase(arg); 3734 vars.erase(arg);
3738 } 3735 }
3739 str_list finalArgs; 3736 str_list finalArgs;
3740 for (const auto& arg : vars) { 3737 for (const auto& arg : vars) {
3741 finalArgs.push_back(arg); 3738 finalArgs.push_back(arg);
3742 } 3739 }
3743 for (const auto& arg : *ensureArgList) { 3740 finalArgs.sort();
3741 for (const auto& arg : *ensureArgListInTheEnd) {
3744 finalArgs.push_back(arg); 3742 finalArgs.push_back(arg);
3745 } 3743 }
3744 if (usedVar) {
3745 if (finalArgs.back() != "..."sv) {
3746 finalArgs.push_back("..."s);
3747 }
3748 }
3746 args = std::move(finalArgs); 3749 args = std::move(finalArgs);
3750 } else {
3751 args.sort();
3752 if (usedVar) {
3753 args.push_back("..."s);
3754 }
3747 } 3755 }
3748 auto funLit = toAst<FunLit_t>("("s + join(args, ","sv) + ")-> nil"s, x); 3756 auto funLit = toAst<FunLit_t>("("s + join(args, ","sv) + ")-> nil"s, x);
3749 funLit->body->content.set(stmt.get()); 3757 funLit->body->content.set(stmt.get());
@@ -8070,7 +8078,7 @@ private:
8070 auto chainValue = static_cast<ChainValue_t*>(value->item.get()); 8078 auto chainValue = static_cast<ChainValue_t*>(value->item.get());
8071 if (auto callable = ast_cast<Callable_t>(chainValue->items.front()); callable && chainValue->items.size() == 1) { 8079 if (auto callable = ast_cast<Callable_t>(chainValue->items.front()); callable && chainValue->items.size() == 1) {
8072 if (auto self = callable->item.as<SelfItem_t>()) { 8080 if (auto self = callable->item.as<SelfItem_t>()) {
8073 if (auto selfVar = self->name.as<Self_t>()) { 8081 if (self->name.as<Self_t>()) {
8074 classTextName = "\"self\""; 8082 classTextName = "\"self\"";
8075 } 8083 }
8076 } else if (auto var = callable->item.as<Variable_t>()) { 8084 } else if (auto var = callable->item.as<Variable_t>()) {