From 02910188fc07492b8b31b2701fe1987025196f85 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 23 Jan 2026 12:07:38 +0800 Subject: Fixed some `import global` related issue. --- src/yuescript/yue_compiler.cpp | 34 +++++++++++++++++++++------------- src/yuescript/yue_compiler.h | 1 + 2 files changed, 22 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 2ed0265..f2a4c7a 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -527,6 +527,7 @@ private: } bool isDefined(const std::string& name) const { + if (name.empty()) return false; bool isDefined = false; int mode = int(std::isupper(name[0]) ? GlobalMode::Capital : GlobalMode::Any); const auto& current = _scopes.back(); @@ -554,6 +555,7 @@ private: } bool isSolidDefined(const std::string& name) const { + if (name.empty()) return false; bool defined = false; for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { auto vars = it->vars.get(); @@ -567,6 +569,7 @@ private: } bool isSolidLocal(const std::string& name) { + if (name.empty()) return false; bool local = false; for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { auto vars = it->vars.get(); @@ -585,6 +588,7 @@ private: } bool isLocal(const std::string& name) { + if (name.empty()) return false; bool local = false; bool defined = false; for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { @@ -613,6 +617,7 @@ private: } bool isDeclaredAsGlobal(const std::string& name) const { + if (name.empty()) return false; bool global = false; for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { auto vars = it->vars.get(); @@ -631,6 +636,7 @@ private: } bool isConst(const std::string& name) const { + if (name.empty()) return false; bool isConst = false; decltype(_scopes.back().allows.get()) allows = nullptr; for (auto it = _scopes.rbegin(); it != _scopes.rend(); ++it) { @@ -1990,7 +1996,7 @@ private: _config.lintGlobalVariable = false; if (!assignment->action.is()) return vars; for (auto exp : assignment->expList->exprs.objects()) { - auto var = singleVariableFrom(exp, AccessType::Write); + auto var = singleVariableFrom(exp, AccessType::None); vars.push_back(var.empty() ? Empty : var); } _config.lintGlobalVariable = lintGlobal; @@ -2002,7 +2008,7 @@ private: bool lintGlobal = _config.lintGlobalVariable; _config.lintGlobalVariable = false; for (auto exp : with->valueList->exprs.objects()) { - auto var = singleVariableFrom(exp, AccessType::Write); + auto var = singleVariableFrom(exp, AccessType::None); vars.push_back(var.empty() ? Empty : var); } _config.lintGlobalVariable = lintGlobal; @@ -4412,7 +4418,7 @@ private: auto result = YueCompiler{}.compile(codes, config); #endif // YUE_NO_MACRO if (result.error) { - YUEE("failed to compile dues to Yue formatter", x); + YUEE("failed to compile dues to Yue formatter\n"s + result.error.value().displayMessage, x); } usedVar = result.usedVar; if (result.globals) { @@ -5353,16 +5359,18 @@ private: auto stmt = static_cast(node); if (auto importNode = stmt->content.as(); importNode && importNode->content.is()) { - if (_importedGlobal) { - throw CompileError("import global redeclared in same scope"sv, importNode); - } else { - auto& scope = currentScope(); - scope.importedGlobal = std::make_unique(); - _importedGlobal = scope.importedGlobal.get(); - _importedGlobal->vars = scope.vars.get(); - _importedGlobal->indent = indent(); - _importedGlobal->nl = nl(stmt); - _importedGlobal->globalCodeLine = &temp.emplace_back(); + if (!_config.disableGlobalImport) { + if (_importedGlobal) { + throw CompileError("import global redeclared in same scope"sv, importNode); + } else { + auto& scope = currentScope(); + scope.importedGlobal = std::make_unique(); + _importedGlobal = scope.importedGlobal.get(); + _importedGlobal->vars = scope.vars.get(); + _importedGlobal->indent = indent(); + _importedGlobal->nl = nl(stmt); + _importedGlobal->globalCodeLine = &temp.emplace_back(); + } } } else { transformStatement(stmt, temp); diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index eb5fb16..706e688 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -33,6 +33,7 @@ struct YueConfig { bool reserveComment = false; bool lax = false; // internal options + bool disableGlobalImport = false; bool exporting = false; bool profiling = false; int lineOffset = 0; -- cgit v1.2.3-55-g6feb