diff options
author | Li Jin <dragon-fly@qq.com> | 2023-07-28 11:53:04 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-07-28 11:53:04 +0800 |
commit | 5497775534d20ba06ab9c13bc4db1c5bee877513 (patch) | |
tree | be68d03cf0928efca4a579125e4ba15ef3ab9325 | |
parent | f415df9617d251abd802257d9750618ccc71ca93 (diff) | |
download | yuescript-5497775534d20ba06ab9c13bc4db1c5bee877513.tar.gz yuescript-5497775534d20ba06ab9c13bc4db1c5bee877513.tar.bz2 yuescript-5497775534d20ba06ab9c13bc4db1c5bee877513.zip |
fix xpcall usages in different Lua version.
-rw-r--r-- | makefile | 6 | ||||
-rw-r--r-- | spec/generated/5.1/try_catch.lua | 71 | ||||
-rw-r--r-- | spec/inputs/macro.yue | 4 | ||||
-rw-r--r-- | spec/inputs/macro_export.yue (renamed from spec/inputs/macro-export.yue) | 4 | ||||
-rw-r--r-- | spec/inputs/macro_teal.yue (renamed from spec/inputs/macro-teal.yue) | 0 | ||||
-rw-r--r-- | spec/inputs/macro_todo.yue (renamed from spec/inputs/macro-todo.yue) | 0 | ||||
-rw-r--r-- | spec/inputs/teal_lang.yue (renamed from spec/inputs/teal-lang.yue) | 2 | ||||
-rw-r--r-- | spec/inputs/try_catch.yue (renamed from spec/inputs/try-catch.yue) | 0 | ||||
-rw-r--r-- | spec/outputs/teal_lang.lua (renamed from spec/outputs/teal-lang.lua) | 0 | ||||
-rw-r--r-- | spec/outputs/teal_lang.tl (renamed from spec/outputs/teal-lang.tl) | 0 | ||||
-rw-r--r-- | spec/outputs/try_catch.lua (renamed from spec/outputs/try-catch.lua) | 0 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 59 | ||||
-rw-r--r-- | src/yuescript/yuescript.h | 17 |
13 files changed, 130 insertions, 33 deletions
@@ -284,8 +284,9 @@ test: debug | |||
284 | @echo "Compiling Yuescript codes..." | 284 | @echo "Compiling Yuescript codes..." |
285 | @$(START_TIME) | 285 | @$(START_TIME) |
286 | @./$(BIN_NAME) $(TEST_INPUT) -t $(TEST_OUTPUT) --tl_enabled | 286 | @./$(BIN_NAME) $(TEST_INPUT) -t $(TEST_OUTPUT) --tl_enabled |
287 | @./$(BIN_NAME) $(TEST_INPUT)/teal-lang.yue -o $(TEST_OUTPUT)/teal-lang.lua | 287 | @./$(BIN_NAME) $(TEST_INPUT)/teal_lang.yue -o $(TEST_OUTPUT)/teal_lang.lua |
288 | @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(TEST_OUTPUT)/5.1/loops.lua --target=5.1 | 288 | @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(TEST_OUTPUT)/5.1/loops.lua --target=5.1 |
289 | @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(TEST_OUTPUT)/5.1/try_catch.lua --target=5.1 | ||
289 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(TEST_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 | 290 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(TEST_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 |
290 | @echo -en "Compile time: " | 291 | @echo -en "Compile time: " |
291 | @$(END_TIME) | 292 | @$(END_TIME) |
@@ -299,8 +300,9 @@ gen: release | |||
299 | @echo "Compiling Yuescript codes..." | 300 | @echo "Compiling Yuescript codes..." |
300 | @$(START_TIME) | 301 | @$(START_TIME) |
301 | @./$(BIN_NAME) $(TEST_INPUT) -t $(GEN_OUTPUT) --tl_enabled | 302 | @./$(BIN_NAME) $(TEST_INPUT) -t $(GEN_OUTPUT) --tl_enabled |
302 | @./$(BIN_NAME) $(TEST_INPUT)/teal-lang.yue -o $(GEN_OUTPUT)/teal-lang.lua | 303 | @./$(BIN_NAME) $(TEST_INPUT)/teal_lang.yue -o $(GEN_OUTPUT)/teal_lang.lua |
303 | @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(GEN_OUTPUT)/5.1/loops.lua --target=5.1 | 304 | @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(GEN_OUTPUT)/5.1/loops.lua --target=5.1 |
305 | @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(TEST_OUTPUT)/5.1/try_catch.lua --target=5.1 | ||
304 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(GEN_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 | 306 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(GEN_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 |
305 | @echo -en "Compile time: " | 307 | @echo -en "Compile time: " |
306 | @$(END_TIME) | 308 | @$(END_TIME) |
diff --git a/spec/generated/5.1/try_catch.lua b/spec/generated/5.1/try_catch.lua new file mode 100644 index 0000000..577df16 --- /dev/null +++ b/spec/generated/5.1/try_catch.lua | |||
@@ -0,0 +1,71 @@ | |||
1 | xpcall(function() | ||
2 | return func(1, 2, 3) | ||
3 | end, function(err) | ||
4 | return print(err) | ||
5 | end) | ||
6 | xpcall(function() | ||
7 | return func(1, 2, 3) | ||
8 | end, function(err) | ||
9 | return print(err) | ||
10 | end) | ||
11 | pcall(function() | ||
12 | print("trying") | ||
13 | return func(1, 2, 3) | ||
14 | end) | ||
15 | do | ||
16 | local success, result = xpcall(function() | ||
17 | return func(1, 2, 3) | ||
18 | end, function(err) | ||
19 | return print(err) | ||
20 | end) | ||
21 | success, result = pcall(func, 1, 2, 3) | ||
22 | end | ||
23 | pcall(tb.func) | ||
24 | pcall(tb.func) | ||
25 | pcall(tb.func) | ||
26 | pcall((tb.func)) | ||
27 | pcall(((function() | ||
28 | local _base_0 = tb | ||
29 | local _fn_0 = _base_0.func | ||
30 | return _fn_0 and function(...) | ||
31 | return _fn_0(_base_0, ...) | ||
32 | end | ||
33 | end)()), 1, 2, 3) | ||
34 | pcall(tb.func, 1) | ||
35 | pcall(tb.func, 1) | ||
36 | if (xpcall(function() | ||
37 | return func(1) | ||
38 | end, function(err) | ||
39 | return print(err) | ||
40 | end)) then | ||
41 | print("OK") | ||
42 | end | ||
43 | if xpcall(function() | ||
44 | return func(1) | ||
45 | end, function(err) | ||
46 | return print(err) | ||
47 | end) then | ||
48 | print("OK") | ||
49 | end | ||
50 | do | ||
51 | do | ||
52 | local success, result = pcall(func, "abc", 123) | ||
53 | if success then | ||
54 | print(result) | ||
55 | end | ||
56 | end | ||
57 | local success, result = xpcall(function() | ||
58 | return func("abc", 123) | ||
59 | end, function(err) | ||
60 | return print(err) | ||
61 | end) | ||
62 | success, result = xpcall(function() | ||
63 | return func("abc", 123) | ||
64 | end, function(err) | ||
65 | return print(err) | ||
66 | end) | ||
67 | if success then | ||
68 | print(result) | ||
69 | end | ||
70 | end | ||
71 | return nil | ||
diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue index ac51d85..ae14f53 100644 --- a/spec/inputs/macro.yue +++ b/spec/inputs/macro.yue | |||
@@ -1,9 +1,9 @@ | |||
1 | import "macro-export" as { | 1 | import "macro_export" as { |
2 | $, -- import all macros | 2 | $, -- import all macros |
3 | $config: $myconfig, -- rename macro $config to $myconfig | 3 | $config: $myconfig, -- rename macro $config to $myconfig |
4 | } | 4 | } |
5 | 5 | ||
6 | import "macro-todo" as $ | 6 | import "macro_todo" as $ |
7 | 7 | ||
8 | $asserts item == nil | 8 | $asserts item == nil |
9 | 9 | ||
diff --git a/spec/inputs/macro-export.yue b/spec/inputs/macro_export.yue index ff0d273..eec5848 100644 --- a/spec/inputs/macro-export.yue +++ b/spec/inputs/macro_export.yue | |||
@@ -1,6 +1,6 @@ | |||
1 | import "macro-todo" as $ | 1 | import "macro_todo" as $ |
2 | 2 | ||
3 | import "macro-todo" as {$, :$todo} | 3 | import "macro_todo" as {$, :$todo} |
4 | 4 | ||
5 | export macro config = (debugging = true)-> | 5 | export macro config = (debugging = true)-> |
6 | global debugMode = debugging == "true" | 6 | global debugMode = debugging == "true" |
diff --git a/spec/inputs/macro-teal.yue b/spec/inputs/macro_teal.yue index a443614..a443614 100644 --- a/spec/inputs/macro-teal.yue +++ b/spec/inputs/macro_teal.yue | |||
diff --git a/spec/inputs/macro-todo.yue b/spec/inputs/macro_todo.yue index 752c9cb..752c9cb 100644 --- a/spec/inputs/macro-todo.yue +++ b/spec/inputs/macro_todo.yue | |||
diff --git a/spec/inputs/teal-lang.yue b/spec/inputs/teal_lang.yue index b28c915..e01682f 100644 --- a/spec/inputs/teal-lang.yue +++ b/spec/inputs/teal_lang.yue | |||
@@ -1,4 +1,4 @@ | |||
1 | import "macro-teal" as $ | 1 | import "macro_teal" as $ |
2 | 2 | ||
3 | $local "a:{string:number}", {value:123} | 3 | $local "a:{string:number}", {value:123} |
4 | $local "b:number", a.value | 4 | $local "b:number", a.value |
diff --git a/spec/inputs/try-catch.yue b/spec/inputs/try_catch.yue index e38cbef..e38cbef 100644 --- a/spec/inputs/try-catch.yue +++ b/spec/inputs/try_catch.yue | |||
diff --git a/spec/outputs/teal-lang.lua b/spec/outputs/teal_lang.lua index 28ba6f8..28ba6f8 100644 --- a/spec/outputs/teal-lang.lua +++ b/spec/outputs/teal_lang.lua | |||
diff --git a/spec/outputs/teal-lang.tl b/spec/outputs/teal_lang.tl index 0dc25a1..0dc25a1 100644 --- a/spec/outputs/teal-lang.tl +++ b/spec/outputs/teal_lang.tl | |||
diff --git a/spec/outputs/try-catch.lua b/spec/outputs/try_catch.lua index 129d412..129d412 100644 --- a/spec/outputs/try-catch.lua +++ b/spec/outputs/try_catch.lua | |||
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 1378436..4d8ed86 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -72,7 +72,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
72 | "close"s // Lua 5.4 | 72 | "close"s // Lua 5.4 |
73 | }; | 73 | }; |
74 | 74 | ||
75 | const std::string_view version = "0.17.11"sv; | 75 | const std::string_view version = "0.17.12"sv; |
76 | const std::string_view extension = "yue"sv; | 76 | const std::string_view extension = "yue"sv; |
77 | 77 | ||
78 | class CompileError : public std::logic_error { | 78 | class CompileError : public std::logic_error { |
@@ -7978,31 +7978,50 @@ private: | |||
7978 | auto chainValue = value->item.as<ChainValue_t>(); | 7978 | auto chainValue = value->item.as<ChainValue_t>(); |
7979 | BREAK_IF(!chainValue); | 7979 | BREAK_IF(!chainValue); |
7980 | BREAK_IF(!isChainValueCall(chainValue)); | 7980 | BREAK_IF(!isChainValueCall(chainValue)); |
7981 | ast_ptr<true, ast_node> last = chainValue->items.back(); | 7981 | if (errHandler && getLuaTarget(x) < 502) { |
7982 | chainValue->items.pop_back(); | 7982 | auto tryExp = toAst<Exp_t>("->"sv, x); |
7983 | _ast_list* args = nullptr; | 7983 | auto funLit = simpleSingleValueFrom(tryExp)->value.to<FunLit_t>(); |
7984 | if (auto invoke = ast_cast<InvokeArgs_t>(last)) { | 7984 | auto expList = x->new_ptr<ExpList_t>(); |
7985 | args = &invoke->args; | 7985 | expList->exprs.push_back(tryNode->func); |
7986 | } else { | 7986 | auto expListAssign = x->new_ptr<ExpListAssign_t>(); |
7987 | args = &(ast_to<Invoke_t>(last)->args); | 7987 | expListAssign->expList.set(expList); |
7988 | } | 7988 | auto stmt = x->new_ptr<Statement_t>(); |
7989 | if (errHandler) { | 7989 | stmt->content.set(expListAssign); |
7990 | auto body = x->new_ptr<Body_t>(); | ||
7991 | body->content.set(stmt); | ||
7992 | funLit->body.set(body); | ||
7990 | auto xpcall = toAst<ChainValue_t>("xpcall()", x); | 7993 | auto xpcall = toAst<ChainValue_t>("xpcall()", x); |
7991 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); | 7994 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); |
7992 | invoke->args.push_back(tryNode->func); | 7995 | invoke->args.push_back(tryExp); |
7993 | invoke->args.push_back(errHandler); | 7996 | invoke->args.push_back(errHandler); |
7994 | for (auto arg : args->objects()) { | ||
7995 | invoke->args.push_back(arg); | ||
7996 | } | ||
7997 | transformChainValue(xpcall, out, ExpUsage::Closure); | 7997 | transformChainValue(xpcall, out, ExpUsage::Closure); |
7998 | } else { | 7998 | } else { |
7999 | auto pcall = toAst<ChainValue_t>("pcall()", x); | 7999 | ast_ptr<true, ast_node> last = chainValue->items.back(); |
8000 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); | 8000 | chainValue->items.pop_back(); |
8001 | invoke->args.push_back(tryNode->func); | 8001 | _ast_list* args = nullptr; |
8002 | for (auto arg : args->objects()) { | 8002 | if (auto invoke = ast_cast<InvokeArgs_t>(last)) { |
8003 | invoke->args.push_back(arg); | 8003 | args = &invoke->args; |
8004 | } else { | ||
8005 | args = &(ast_to<Invoke_t>(last)->args); | ||
8006 | } | ||
8007 | if (errHandler) { | ||
8008 | auto xpcall = toAst<ChainValue_t>("xpcall()", x); | ||
8009 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); | ||
8010 | invoke->args.push_back(tryNode->func); | ||
8011 | invoke->args.push_back(errHandler); | ||
8012 | for (auto arg : args->objects()) { | ||
8013 | invoke->args.push_back(arg); | ||
8014 | } | ||
8015 | transformChainValue(xpcall, out, ExpUsage::Closure); | ||
8016 | } else { | ||
8017 | auto pcall = toAst<ChainValue_t>("pcall()", x); | ||
8018 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); | ||
8019 | invoke->args.push_back(tryNode->func); | ||
8020 | for (auto arg : args->objects()) { | ||
8021 | invoke->args.push_back(arg); | ||
8022 | } | ||
8023 | transformChainValue(pcall, out, ExpUsage::Closure); | ||
8004 | } | 8024 | } |
8005 | transformChainValue(pcall, out, ExpUsage::Closure); | ||
8006 | } | 8025 | } |
8007 | if (usage == ExpUsage::Common) { | 8026 | if (usage == ExpUsage::Common) { |
8008 | out.back().append(nlr(x)); | 8027 | out.back().append(nlr(x)); |
diff --git a/src/yuescript/yuescript.h b/src/yuescript/yuescript.h index 3d8d65c..a160f55 100644 --- a/src/yuescript/yuescript.h +++ b/src/yuescript/yuescript.h | |||
@@ -106,9 +106,12 @@ local function yue_loader(name) | |||
106 | return concat(tried, "\n\t") | 106 | return concat(tried, "\n\t") |
107 | end | 107 | end |
108 | local function yue_call(f, ...) | 108 | local function yue_call(f, ...) |
109 | return xpcall(f, function(err) | 109 | local args = {...} |
110 | return yue.traceback(err, 1) | 110 | return xpcall(function() |
111 | end, ...) | 111 | return f(unpack(args)) |
112 | end, function(err) | ||
113 | return yue.traceback(err, 2) | ||
114 | end) | ||
112 | end | 115 | end |
113 | yue_loadstring = function(...) | 116 | yue_loadstring = function(...) |
114 | local options, str, chunk_name, mode, env = get_options(...) | 117 | local options, str, chunk_name, mode, env = get_options(...) |
@@ -166,9 +169,11 @@ local function yue_traceback(err, level) | |||
166 | end | 169 | end |
167 | local function yue_require(name) | 170 | local function yue_require(name) |
168 | insert_loader() | 171 | insert_loader() |
169 | local success, res = xpcall(require, function(err) | 172 | local success, res = xpcall(function() |
170 | return yue_traceback(err, 2) | 173 | return require(name) |
171 | end, name) | 174 | end, function(err) |
175 | return yue_traceback(err, 3) | ||
176 | end) | ||
172 | if success then | 177 | if success then |
173 | return res | 178 | return res |
174 | else | 179 | else |