From 079f563e63d2d12db92db9226e248d5d5f064e3d Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 15 Jan 2026 17:20:24 +0800 Subject: Fixed globals importing order. --- spec/outputs/codes_from_doc.lua | 4 ++-- spec/outputs/codes_from_doc_zh.lua | 4 ++-- spec/outputs/import_global.lua | 14 +++++++------- src/yuescript/yue_compiler.cpp | 29 ++++++++++++++--------------- src/yuescript/yue_parser.cpp | 2 +- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua index 02f36d1..84dfc4e 100644 --- a/spec/outputs/codes_from_doc.lua +++ b/spec/outputs/codes_from_doc.lua @@ -409,8 +409,8 @@ local tb = { } } do - local math = math local print = print + local math = math print("hello") math.random(3) end @@ -2928,8 +2928,8 @@ local tb = { } } do - local math = math local print = print + local math = math print("hello") math.random(3) end diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua index 87944e0..d4e8b6a 100644 --- a/spec/outputs/codes_from_doc_zh.lua +++ b/spec/outputs/codes_from_doc_zh.lua @@ -409,8 +409,8 @@ local tb = { } } do - local math = math local print = print + local math = math print("hello") math.random(3) end @@ -2922,8 +2922,8 @@ local tb = { } } do - local math = math local print = print + local math = math print("hello") math.random(3) end diff --git a/spec/outputs/import_global.lua b/spec/outputs/import_global.lua index 08a90e2..c748c78 100644 --- a/spec/outputs/import_global.lua +++ b/spec/outputs/import_global.lua @@ -1,6 +1,6 @@ do - local math = math local print = print + local math = math print("hello") math.random(10) end @@ -33,19 +33,19 @@ end do local func func = function(x, y) + local type = type local tostring = tostring local print = print - local type = type return type(x, tostring(y, print)) end func(1, 2) end do - local print = print - local tostring = tostring + local xpcall = xpcall local func = func local world = world - local xpcall = xpcall + local tostring = tostring + local print = print xpcall(function() return func("hello " .. tostring(world)) end, function(err) @@ -77,16 +77,16 @@ do end end do + local lowercase = lowercase local tostring = tostring local Uppercase = Uppercase - local lowercase = lowercase local foobar = "all " .. tostring(lowercase) FooBar = "pascal case" FOOBAR = "all " .. tostring(Uppercase) end do - local print = print local setmetatable = setmetatable + local print = print do local _class_0 local _base_0 = { } diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 10f8719..e79b857 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -440,7 +440,8 @@ private: Scope* importingScope = nullptr; std::string indent; std::string nl; - std::unordered_set globals; + std::unordered_set globals; + str_list globalList; }; struct Scope { GlobalMode mode = GlobalMode::None; @@ -1620,13 +1621,18 @@ private: return !_varArgs.empty() && _varArgs.top().usedVar ? "end)(...)"s : "end)()"s; } + void markGlobalImported(const std::string& name) { + if (_importedGlobal->globals.find(name) == _importedGlobal->globals.end() && !isSolidDefined(name)) { + const auto& global = _importedGlobal->globalList.emplace_back(name); + _importedGlobal->globals.insert(global); + _importedGlobal->importingScope->vars->insert_or_assign(name, VarType::LocalConst); + } + } + std::string globalVar(std::string_view var, ast_node* x, AccessType accessType) { std::string str(var); if (_importedGlobal) { - if (_importedGlobal->globals.find(str) == _importedGlobal->globals.end() && !isSolidDefined(str)) { - _importedGlobal->globals.insert(str); - _importedGlobal->importingScope->vars->insert_or_assign(str, VarType::LocalConst); - } + markGlobalImported(str); } else if (_config.lintGlobalVariable) { if (!isLocal(str)) { auto key = str + ':' + std::to_string(x->m_begin.m_line) + ':' + std::to_string(x->m_begin.m_col); @@ -4634,11 +4640,7 @@ private: transformVariable(static_cast(item), out); if (accessType != AccessType::None) { if (_importedGlobal) { - const auto& str = out.back(); - if (_importedGlobal->globals.find(str) == _importedGlobal->globals.end() && !isSolidDefined(str)) { - _importedGlobal->globals.insert(str); - _importedGlobal->importingScope->vars->insert_or_assign(str, VarType::LocalConst); - } + markGlobalImported(out.back()); } else if (_config.lintGlobalVariable && !isLocal(out.back())) { auto key = out.back() + ':' + std::to_string(item->m_begin.m_line) + ':' + std::to_string(item->m_begin.m_col); if (_globals.find(key) == _globals.end()) { @@ -5379,7 +5381,7 @@ private: } if (auto importedGlobal = currentScope().importedGlobal.get()) { str_list globalCodes; - for (const auto& global : importedGlobal->globals) { + for (const auto& global : importedGlobal->globalList) { globalCodes.emplace_back(importedGlobal->indent + "local "s + global + " = "s + global + importedGlobal->nl); } *importedGlobal->globalCodeLine = join(globalCodes); @@ -9361,10 +9363,7 @@ private: out.push_back(name + " = "s + name); } if (_importedGlobal) { - if (_importedGlobal->globals.find(name) == _importedGlobal->globals.end() && !isSolidDefined(name)) { - _importedGlobal->globals.insert(name); - _importedGlobal->importingScope->vars->insert_or_assign(name, VarType::LocalConst); - } + markGlobalImported(name); } else if (_config.lintGlobalVariable && !isLocal(name)) { auto key = name + ':' + std::to_string(pair->name->m_begin.m_line) + ':' + std::to_string(pair->name->m_begin.m_col); if (_globals.find(key) == _globals.end()) { diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 6990e31..ab1ba7a 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -1128,7 +1128,7 @@ YueParser::YueParser() { Statement = ( Import | Export | Global | Macro | MacroInPlace | Label - ) | ( + ) >> space | ( Local | While | Repeat | For | Return | BreakLoop | Goto | ShortTabAppending | LocalAttrib | Backcall | PipeBody | ExpListAssign | ChainAssign | -- cgit v1.2.3-55-g6feb