From 880b7eb9a427b263091f6eef5197c0285c723fd7 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 12 Aug 2024 17:12:07 +0800 Subject: fix a Lua stack size not enough issue. --- src/yuescript/yue_ast.h | 2 +- src/yuescript/yue_compiler.cpp | 198 ++++++++++++++++++++--------------------- 2 files changed, 99 insertions(+), 101 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 7bf17fe..cffc92d 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -13,7 +13,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI namespace parserlib { template -std::string_view ast_name() { } +std::string_view ast_name() { return {}; } #define AST_LEAF(type) \ COUNTER_INC; \ diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 8ec131f..32db488 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -14,8 +14,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include #include -#include #include +#include #include "yuescript/yue_compiler.h" #include "yuescript/yue_parser.h" @@ -46,8 +46,7 @@ namespace yue { #define BLOCK_START do { #define BLOCK_END \ } \ - while (false) \ - ; + while (false); #define BREAK_IF(cond) \ if (cond) break @@ -76,7 +75,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.24.0"sv; +const std::string_view version = "0.24.1"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -1475,8 +1474,7 @@ private: } bool isConditionChainingOperator(const std::string& op) { - return op == "=="sv || op == "~="sv || op == "!="sv || - op == "<"sv || op == "<="sv || op == ">"sv || op == ">="sv; + return op == "=="sv || op == "~="sv || op == "!="sv || op == "<"sv || op == "<="sv || op == ">"sv || op == ">="sv; } bool isConditionChaining(Exp_t* exp) { @@ -1522,7 +1520,6 @@ private: _gotoScope++; } - void pushUserFunctionScope() { pushFunctionScope(false); } @@ -3728,7 +3725,7 @@ private: std::list*>> chains; chains.emplace_back(std::string(), &exp->pipeExprs); int conditionChainCount = 0; - auto checkChains = [&]()-> ast_ptr { + auto checkChains = [&]() -> ast_ptr { std::optional result; if (conditionChainCount > 1) { ast_ptr newCondExp; @@ -3774,106 +3771,106 @@ private: { stack.push_back(*item.second); } - reduce: { - if (stack.size() == 3) { - auto condExp = exp->new_ptr(); - const auto& one = std::get>(stack.front()); - condExp->pipeExprs.dup(one); - stack.pop_front(); - auto opValue = exp->new_ptr(); - const auto& two = std::get(stack.front()); - auto op = toAst(two, exp); - opValue->op.set(op); - stack.pop_front(); - const auto& three = std::get>(stack.front()); - opValue->pipeExprs.dup(three); - condExp->opValues.push_back(opValue); - if (preDefine) { - auto ifNode = exp->new_ptr(); - ifNode->type.set(toAst("unless"sv, exp)); - auto ifCond = exp->new_ptr(); - ifCond->condition.set(condExp); - ifNode->nodes.push_back(ifCond); - ifNode->nodes.push_back(toAst("false"sv, exp)); - YueFormat format{}; - auto code = ifNode->to_string(&format); - if (newCondExp) { - if (!nodes) { - auto ifNodePrev = exp->new_ptr(); - ifNodePrev->type.set(toAst("unless"sv, exp)); - auto ifCondPrev = exp->new_ptr(); - ifCondPrev->condition.set(newCondExp); - ifNodePrev->nodes.push_back(ifCondPrev); - ifNodePrev->nodes.push_back(toAst("false", exp)); - auto simpleValue = exp->new_ptr(); - simpleValue->value.set(ifNodePrev); - newCondExp.set(newExp(simpleValue, exp)); - nodes = &ifNodePrev->nodes; - } - auto block = exp->new_ptr(); - auto stmt = exp->new_ptr(); - stmt->content.set(preDefine); - preDefine.set(nullptr); - block->statements.push_back(stmt); + reduce: { + if (stack.size() == 3) { + auto condExp = exp->new_ptr(); + const auto& one = std::get>(stack.front()); + condExp->pipeExprs.dup(one); + stack.pop_front(); + auto opValue = exp->new_ptr(); + const auto& two = std::get(stack.front()); + auto op = toAst(two, exp); + opValue->op.set(op); + stack.pop_front(); + const auto& three = std::get>(stack.front()); + opValue->pipeExprs.dup(three); + condExp->opValues.push_back(opValue); + if (preDefine) { + auto ifNode = exp->new_ptr(); + ifNode->type.set(toAst("unless"sv, exp)); + auto ifCond = exp->new_ptr(); + ifCond->condition.set(condExp); + ifNode->nodes.push_back(ifCond); + ifNode->nodes.push_back(toAst("false"sv, exp)); + YueFormat format{}; + auto code = ifNode->to_string(&format); + if (newCondExp) { + if (!nodes) { + auto ifNodePrev = exp->new_ptr(); + ifNodePrev->type.set(toAst("unless"sv, exp)); + auto ifCondPrev = exp->new_ptr(); + ifCondPrev->condition.set(newCondExp); + ifNodePrev->nodes.push_back(ifCondPrev); + ifNodePrev->nodes.push_back(toAst("false", exp)); auto simpleValue = exp->new_ptr(); - simpleValue->value.set(ifNode); + simpleValue->value.set(ifNodePrev); + newCondExp.set(newExp(simpleValue, exp)); + nodes = &ifNodePrev->nodes; + } + auto block = exp->new_ptr(); + auto stmt = exp->new_ptr(); + stmt->content.set(preDefine); + preDefine.set(nullptr); + block->statements.push_back(stmt); + auto simpleValue = exp->new_ptr(); + simpleValue->value.set(ifNode); + auto explist = exp->new_ptr(); + explist->exprs.push_back(newExp(simpleValue, exp)); + auto expListAssign = exp->new_ptr(); + expListAssign->expList.set(explist); + stmt = exp->new_ptr(); + stmt->content.set(expListAssign); + block->statements.push_back(stmt); + nodes->push_back(block); + nodes = &ifNode->nodes; + } else { + auto block = exp->new_ptr(); + auto stmt = exp->new_ptr(); + stmt->content.set(preDefine); + preDefine.set(nullptr); + block->statements.push_back(stmt); + auto simpleValue = exp->new_ptr(); + simpleValue->value.set(ifNode); + auto explist = exp->new_ptr(); + explist->exprs.push_back(newExp(simpleValue, exp)); + auto expListAssign = exp->new_ptr(); + expListAssign->expList.set(explist); + stmt = exp->new_ptr(); + stmt->content.set(expListAssign); + block->statements.push_back(stmt); + auto body = exp->new_ptr(); + body->content.set(block); + auto doNode = exp->new_ptr(); + doNode->body.set(body); + simpleValue = exp->new_ptr(); + simpleValue->value.set(doNode); + newCondExp.set(newExp(simpleValue, exp)); + nodes = &ifNode->nodes; + } + } else { + if (newCondExp) { + if (nodes) { auto explist = exp->new_ptr(); - explist->exprs.push_back(newExp(simpleValue, exp)); + explist->exprs.push_back(condExp); auto expListAssign = exp->new_ptr(); expListAssign->expList.set(explist); - stmt = exp->new_ptr(); - stmt->content.set(expListAssign); - block->statements.push_back(stmt); - nodes->push_back(block); - nodes = &ifNode->nodes; - } else { - auto block = exp->new_ptr(); auto stmt = exp->new_ptr(); - stmt->content.set(preDefine); - preDefine.set(nullptr); - block->statements.push_back(stmt); - auto simpleValue = exp->new_ptr(); - simpleValue->value.set(ifNode); - auto explist = exp->new_ptr(); - explist->exprs.push_back(newExp(simpleValue, exp)); - auto expListAssign = exp->new_ptr(); - expListAssign->expList.set(explist); - stmt = exp->new_ptr(); stmt->content.set(expListAssign); - block->statements.push_back(stmt); - auto body = exp->new_ptr(); - body->content.set(block); - auto doNode = exp->new_ptr(); - doNode->body.set(body); - simpleValue = exp->new_ptr(); - simpleValue->value.set(doNode); - newCondExp.set(newExp(simpleValue, exp)); - nodes = &ifNode->nodes; - } - } else { - if (newCondExp) { - if (nodes) { - auto explist = exp->new_ptr(); - explist->exprs.push_back(condExp); - auto expListAssign = exp->new_ptr(); - expListAssign->expList.set(explist); - auto stmt = exp->new_ptr(); - stmt->content.set(expListAssign); - nodes->push_back(stmt); - } else { - auto opValue = exp->new_ptr(); - opValue->op.set(toAst("and"sv, exp)); - opValue->pipeExprs.dup(condExp->pipeExprs); - newCondExp->opValues.push_back(opValue); - newCondExp->opValues.dup(condExp->opValues); - } + nodes->push_back(stmt); } else { - newCondExp.set(condExp); + auto opValue = exp->new_ptr(); + opValue->op.set(toAst("and"sv, exp)); + opValue->pipeExprs.dup(condExp->pipeExprs); + newCondExp->opValues.push_back(opValue); + newCondExp->opValues.dup(condExp->opValues); } + } else { + newCondExp.set(condExp); } } } } + } popScope(); return newCondExp; } @@ -4079,7 +4076,6 @@ private: return std::nullopt; } - std::optional> upValueFuncFrom(Block_t* block, str_list* ensureArgListInTheEnd = nullptr, bool noGlobalVarPassing = false) { if (checkUpValueFuncAvailable(block)) { return getUpValueFuncFromBlock(block, ensureArgListInTheEnd, noGlobalVarPassing); @@ -6315,6 +6311,9 @@ private: } // cur macroFunc pushYue("pcall"sv); // cur macroFunc pcall lua_insert(L, -2); // cur pcall macroFunc + if (!lua_checkstack(L, argStrs.size())) { + throw CompileError("too much macro params"s, x); + } for (const auto& arg : argStrs) { lua_pushlstring(L, arg.c_str(), arg.size()); } // cur pcall macroFunc args... @@ -8607,8 +8606,7 @@ private: case id(): { size_t inc = transform_class_member_list(static_cast(content), members, classVar); auto it = members.end(); - for (size_t i = 0; i < inc; ++i, --it) - ; + for (size_t i = 0; i < inc; ++i, --it); for (; it != members.end(); ++it) { auto& member = *it; if (member.type == MemType::Property) { @@ -10179,7 +10177,7 @@ private: if (auto value = singleValueFrom(valueList)) { if (value->item.is()) { tableMatching = true; - } else if (auto sVal = value->item.as()){ + } else if (auto sVal = value->item.as()) { tableMatching = ast_is(sVal->value); } } -- cgit v1.2.3-55-g6feb