Skip to content

Commit cc194f8

Browse files
committed
Fixed issue in global variables collecting.
1 parent c54df2d commit cc194f8

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/yuescript/yue_compiler.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static std::unordered_set<std::string> Metamethods = {
7878
"close"s // Lua 5.4
7979
};
8080

81-
const std::string_view version = "0.33.5"sv;
81+
const std::string_view version = "0.33.6"sv;
8282
const std::string_view extension = "yue"sv;
8383

8484
class CompileError : public std::logic_error {
@@ -436,6 +436,7 @@ class YueCompilerImpl {
436436
};
437437
struct Scope;
438438
struct ImportedGlobal {
439+
Block_t* currentBlock = nullptr;
439440
std::string* globalCodeLine = nullptr;
440441
std::unordered_map<std::string, VarType>* vars = nullptr;
441442
std::string indent;
@@ -5372,6 +5373,7 @@ class YueCompilerImpl {
53725373
auto& scope = currentScope();
53735374
scope.importedGlobal = std::make_unique<ImportedGlobal>();
53745375
_importedGlobal = scope.importedGlobal.get();
5376+
_importedGlobal->currentBlock = block;
53755377
_importedGlobal->vars = scope.vars.get();
53765378
_importedGlobal->indent = indent();
53775379
_importedGlobal->nl = nl(stmt);
@@ -5420,7 +5422,9 @@ class YueCompilerImpl {
54205422
transformNode();
54215423
}
54225424
}
5423-
if (auto importedGlobal = currentScope().importedGlobal.get()) {
5425+
5426+
if (auto importedGlobal = currentScope().importedGlobal.get();
5427+
importedGlobal && importedGlobal->currentBlock == block) {
54245428
int target = getLuaTarget(block);
54255429
auto attrib = target >= 504 ? " <const>"s : Empty;
54265430
str_list globalCodes;
@@ -9338,7 +9342,7 @@ class YueCompilerImpl {
93389342
auto breakLoopType = getBreakLoopType(forNum->body, true, vars);
93399343
bool isScoped = true;
93409344
if (currentScope().lastStatement) {
9341-
isScoped = false;
9345+
isScoped = false;
93429346
} else if (!extraVar && hasBreakWithValue(breakLoopType)) {
93439347
isScoped = false;
93449348
}
@@ -10162,9 +10166,9 @@ class YueCompilerImpl {
1016210166
_buf << indent() << "}, {"sv << nl(classDecl);
1016310167
if (extend) {
1016410168
_buf << indent(1) << "__index = function(cls, name)"sv << nl(classDecl);
10165-
_buf << indent(2) << "local val = "sv << globalVar("rawget", classDecl, AccessType::Read) << '(' << baseVar << ", name)"sv << nl(classDecl);
10169+
_buf << indent(2) << "local val = "sv << globalVar("rawget"sv, classDecl, AccessType::Read) << '(' << baseVar << ", name)"sv << nl(classDecl);
1016610170
_buf << indent(2) << "if val == nil then"sv << nl(classDecl);
10167-
_buf << indent(3) << "local parent = "sv << globalVar("rawget", classDecl, AccessType::Read) << "(cls, \"__parent\")"sv << nl(classDecl);
10171+
_buf << indent(3) << "local parent = "sv << globalVar("rawget"sv, classDecl, AccessType::Read) << "(cls, \"__parent\")"sv << nl(classDecl);
1016810172
_buf << indent(3) << "if parent then"sv << nl(classDecl);
1016910173
_buf << indent(4) << "return parent[name]"sv << nl(classDecl);
1017010174
_buf << indent(3) << "end"sv << nl(classDecl);
@@ -10179,7 +10183,7 @@ class YueCompilerImpl {
1017910183
pushScope();
1018010184
auto selfVar = getUnusedName("_self_"sv);
1018110185
addToScope(selfVar);
10182-
_buf << indent(1) << "local "sv << selfVar << " = "sv << globalVar("setmetatable", classDecl, AccessType::Read) << "({ }, "sv << baseVar << ")"sv << nl(classDecl);
10186+
_buf << indent(1) << "local "sv << selfVar << " = "sv << globalVar("setmetatable"sv, classDecl, AccessType::Read) << "({ }, "sv << baseVar << ")"sv << nl(classDecl);
1018310187
_buf << indent(1) << "cls.__init("sv << selfVar << ", ...)"sv << nl(classDecl);
1018410188
_buf << indent(1) << "return "sv << selfVar << nl(classDecl);
1018510189
popScope();

0 commit comments

Comments
 (0)