Skip to content

Commit 079f563

Browse files
committed
Fixed globals importing order.
1 parent fced0c4 commit 079f563

5 files changed

Lines changed: 26 additions & 27 deletions

File tree

spec/outputs/codes_from_doc.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ local tb = {
409409
}
410410
}
411411
do
412-
local math = math
413412
local print = print
413+
local math = math
414414
print("hello")
415415
math.random(3)
416416
end
@@ -2928,8 +2928,8 @@ local tb = {
29282928
}
29292929
}
29302930
do
2931-
local math = math
29322931
local print = print
2932+
local math = math
29332933
print("hello")
29342934
math.random(3)
29352935
end

spec/outputs/codes_from_doc_zh.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ local tb = {
409409
}
410410
}
411411
do
412-
local math = math
413412
local print = print
413+
local math = math
414414
print("hello")
415415
math.random(3)
416416
end
@@ -2922,8 +2922,8 @@ local tb = {
29222922
}
29232923
}
29242924
do
2925-
local math = math
29262925
local print = print
2926+
local math = math
29272927
print("hello")
29282928
math.random(3)
29292929
end

spec/outputs/import_global.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
do
2-
local math = math
32
local print = print
3+
local math = math
44
print("hello")
55
math.random(10)
66
end
@@ -33,19 +33,19 @@ end
3333
do
3434
local func
3535
func = function(x, y)
36+
local type = type
3637
local tostring = tostring
3738
local print = print
38-
local type = type
3939
return type(x, tostring(y, print))
4040
end
4141
func(1, 2)
4242
end
4343
do
44-
local print = print
45-
local tostring = tostring
44+
local xpcall = xpcall
4645
local func = func
4746
local world = world
48-
local xpcall = xpcall
47+
local tostring = tostring
48+
local print = print
4949
xpcall(function()
5050
return func("hello " .. tostring(world))
5151
end, function(err)
@@ -77,16 +77,16 @@ do
7777
end
7878
end
7979
do
80+
local lowercase = lowercase
8081
local tostring = tostring
8182
local Uppercase = Uppercase
82-
local lowercase = lowercase
8383
local foobar = "all " .. tostring(lowercase)
8484
FooBar = "pascal case"
8585
FOOBAR = "all " .. tostring(Uppercase)
8686
end
8787
do
88-
local print = print
8988
local setmetatable = setmetatable
89+
local print = print
9090
do
9191
local _class_0
9292
local _base_0 = { }

src/yuescript/yue_compiler.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ class YueCompilerImpl {
440440
Scope* importingScope = nullptr;
441441
std::string indent;
442442
std::string nl;
443-
std::unordered_set<std::string> globals;
443+
std::unordered_set<std::string_view> globals;
444+
str_list globalList;
444445
};
445446
struct Scope {
446447
GlobalMode mode = GlobalMode::None;
@@ -1620,13 +1621,18 @@ class YueCompilerImpl {
16201621
return !_varArgs.empty() && _varArgs.top().usedVar ? "end)(...)"s : "end)()"s;
16211622
}
16221623

1624+
void markGlobalImported(const std::string& name) {
1625+
if (_importedGlobal->globals.find(name) == _importedGlobal->globals.end() && !isSolidDefined(name)) {
1626+
const auto& global = _importedGlobal->globalList.emplace_back(name);
1627+
_importedGlobal->globals.insert(global);
1628+
_importedGlobal->importingScope->vars->insert_or_assign(name, VarType::LocalConst);
1629+
}
1630+
}
1631+
16231632
std::string globalVar(std::string_view var, ast_node* x, AccessType accessType) {
16241633
std::string str(var);
16251634
if (_importedGlobal) {
1626-
if (_importedGlobal->globals.find(str) == _importedGlobal->globals.end() && !isSolidDefined(str)) {
1627-
_importedGlobal->globals.insert(str);
1628-
_importedGlobal->importingScope->vars->insert_or_assign(str, VarType::LocalConst);
1629-
}
1635+
markGlobalImported(str);
16301636
} else if (_config.lintGlobalVariable) {
16311637
if (!isLocal(str)) {
16321638
auto key = str + ':' + std::to_string(x->m_begin.m_line) + ':' + std::to_string(x->m_begin.m_col);
@@ -4634,11 +4640,7 @@ class YueCompilerImpl {
46344640
transformVariable(static_cast<Variable_t*>(item), out);
46354641
if (accessType != AccessType::None) {
46364642
if (_importedGlobal) {
4637-
const auto& str = out.back();
4638-
if (_importedGlobal->globals.find(str) == _importedGlobal->globals.end() && !isSolidDefined(str)) {
4639-
_importedGlobal->globals.insert(str);
4640-
_importedGlobal->importingScope->vars->insert_or_assign(str, VarType::LocalConst);
4641-
}
4643+
markGlobalImported(out.back());
46424644
} else if (_config.lintGlobalVariable && !isLocal(out.back())) {
46434645
auto key = out.back() + ':' + std::to_string(item->m_begin.m_line) + ':' + std::to_string(item->m_begin.m_col);
46444646
if (_globals.find(key) == _globals.end()) {
@@ -5379,7 +5381,7 @@ class YueCompilerImpl {
53795381
}
53805382
if (auto importedGlobal = currentScope().importedGlobal.get()) {
53815383
str_list globalCodes;
5382-
for (const auto& global : importedGlobal->globals) {
5384+
for (const auto& global : importedGlobal->globalList) {
53835385
globalCodes.emplace_back(importedGlobal->indent + "local "s + global + " = "s + global + importedGlobal->nl);
53845386
}
53855387
*importedGlobal->globalCodeLine = join(globalCodes);
@@ -9361,10 +9363,7 @@ class YueCompilerImpl {
93619363
out.push_back(name + " = "s + name);
93629364
}
93639365
if (_importedGlobal) {
9364-
if (_importedGlobal->globals.find(name) == _importedGlobal->globals.end() && !isSolidDefined(name)) {
9365-
_importedGlobal->globals.insert(name);
9366-
_importedGlobal->importingScope->vars->insert_or_assign(name, VarType::LocalConst);
9367-
}
9366+
markGlobalImported(name);
93689367
} else if (_config.lintGlobalVariable && !isLocal(name)) {
93699368
auto key = name + ':' + std::to_string(pair->name->m_begin.m_line) + ':' + std::to_string(pair->name->m_begin.m_col);
93709369
if (_globals.find(key) == _globals.end()) {

src/yuescript/yue_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ YueParser::YueParser() {
11281128
Statement =
11291129
(
11301130
Import | Export | Global | Macro | MacroInPlace | Label
1131-
) | (
1131+
) >> space | (
11321132
Local | While | Repeat | For | Return |
11331133
BreakLoop | Goto | ShortTabAppending |
11341134
LocalAttrib | Backcall | PipeBody | ExpListAssign | ChainAssign |

0 commit comments

Comments
 (0)