diff options
author | Li Jin <dragon-fly@qq.com> | 2024-03-19 10:01:17 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2024-03-19 10:01:17 +0800 |
commit | 62ddb888d67d047324aa6c411d47aaeac9b658fd (patch) | |
tree | 60879c8e772ceda2b7926254f09fdd6c6cbc551f /src | |
parent | a291e14a5dc25cbe517a3fa3f2a5f33b2cfe05ba (diff) | |
download | yuescript-62ddb888d67d047324aa6c411d47aaeac9b658fd.tar.gz yuescript-62ddb888d67d047324aa6c411d47aaeac9b658fd.tar.bz2 yuescript-62ddb888d67d047324aa6c411d47aaeac9b658fd.zip |
fix Lua 5.1 close attribute compatible function.
Diffstat (limited to 'src')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index de41b45..a443f39 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -4352,21 +4352,11 @@ private: | |||
4352 | } | 4352 | } |
4353 | popScope(); | 4353 | popScope(); |
4354 | auto okVar = getUnusedName("_ok_"sv); | 4354 | auto okVar = getUnusedName("_ok_"sv); |
4355 | std::string pCallStmtStr; | 4355 | std::string pCallStmtStr = okVar + ", ... = try\n\tnil"s; |
4356 | if (!_varArgs.empty() && _varArgs.top().hasVar) { | ||
4357 | pCallStmtStr = okVar + ", ... = pcall((...)->, ...)"s; | ||
4358 | } else { | ||
4359 | pCallStmtStr = okVar + ", ... = pcall(->)"s; | ||
4360 | } | ||
4361 | auto pCallStmt = toAst<Statement_t>(pCallStmtStr, x); | 4356 | auto pCallStmt = toAst<Statement_t>(pCallStmtStr, x); |
4362 | auto pCallExp = ast_to<Exp_t>(pCallStmt->content.to<ExpListAssign_t>()->action.to<Assign_t>()->values.back()); | 4357 | auto pCallExp = ast_to<Exp_t>(pCallStmt->content.to<ExpListAssign_t>()->action.to<Assign_t>()->values.back()); |
4363 | auto value = singleValueFrom(pCallExp); | 4358 | auto value = singleValueFrom(pCallExp); |
4364 | auto invoke = ast_to<Invoke_t>(value->item.to<ChainValue_t>()->items.back()); | 4359 | value->item.to<SimpleValue_t>()->value.to<Try_t>()->func.set(followingBlock); |
4365 | auto pCallValue = singleValueFrom(ast_to<Exp_t>(invoke->args.front())); | ||
4366 | auto pCallFunc = pCallValue->item.to<SimpleValue_t>()->value.to<FunLit_t>(); | ||
4367 | auto pCallBody = x->new_ptr<Body_t>(); | ||
4368 | pCallBody->content.set(followingBlock); | ||
4369 | pCallFunc->body.set(pCallBody); | ||
4370 | for (const auto& stmt : getCloses) { | 4360 | for (const auto& stmt : getCloses) { |
4371 | newBlock->statements.push_back(toAst<Statement_t>(stmt, x)); | 4361 | newBlock->statements.push_back(toAst<Statement_t>(stmt, x)); |
4372 | } | 4362 | } |
@@ -9028,6 +9018,7 @@ private: | |||
9028 | BREAK_IF(tryBlock->statements.size() != 1); | 9018 | BREAK_IF(tryBlock->statements.size() != 1); |
9029 | auto stmt = static_cast<Statement_t*>(tryBlock->statements.front()); | 9019 | auto stmt = static_cast<Statement_t*>(tryBlock->statements.front()); |
9030 | auto expListAssign = stmt->content.as<ExpListAssign_t>(); | 9020 | auto expListAssign = stmt->content.as<ExpListAssign_t>(); |
9021 | BREAK_IF(!expListAssign); | ||
9031 | BREAK_IF(expListAssign->action); | 9022 | BREAK_IF(expListAssign->action); |
9032 | auto value = singleValueFrom(expListAssign->expList); | 9023 | auto value = singleValueFrom(expListAssign->expList); |
9033 | BREAK_IF(!value); | 9024 | BREAK_IF(!value); |
@@ -9052,13 +9043,17 @@ private: | |||
9052 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); | 9043 | auto invoke = ast_to<Invoke_t>(xpcall->items.back()); |
9053 | invoke->args.push_back(toAst<Exp_t>(funcName, x)); | 9044 | invoke->args.push_back(toAst<Exp_t>(funcName, x)); |
9054 | invoke->args.push_back(errHandler); | 9045 | invoke->args.push_back(errHandler); |
9055 | invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs); | 9046 | if (!args.empty()) { |
9047 | invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs); | ||
9048 | } | ||
9056 | transformChainValue(xpcall, out, ExpUsage::Closure); | 9049 | transformChainValue(xpcall, out, ExpUsage::Closure); |
9057 | } else { | 9050 | } else { |
9058 | auto pcall = toAst<ChainValue_t>("pcall()", x); | 9051 | auto pcall = toAst<ChainValue_t>("pcall()", x); |
9059 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); | 9052 | auto invoke = ast_to<Invoke_t>(pcall->items.back()); |
9060 | invoke->args.push_back(toAst<Exp_t>(funcName, x)); | 9053 | invoke->args.push_back(toAst<Exp_t>(funcName, x)); |
9061 | invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs); | 9054 | if (!args.empty()) { |
9055 | invoke->args.dup(toAst<ExpList_t>(join(args, ","sv), x)->exprs); | ||
9056 | } | ||
9062 | transformChainValue(pcall, out, ExpUsage::Closure); | 9057 | transformChainValue(pcall, out, ExpUsage::Closure); |
9063 | } | 9058 | } |
9064 | if (usage == ExpUsage::Common) { | 9059 | if (usage == ExpUsage::Common) { |