From 8a01e9c4ec201ad7079f6863c9236851d162b864 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 15 Jan 2026 18:58:08 +0800 Subject: Fixed a crash issue. --- spec/inputs/import_global.yue | 7 +++++++ spec/outputs/5.1/import_global.lua | 13 ++++++++++++- spec/outputs/import_global.lua | 13 ++++++++++++- src/yuescript/yue_compiler.cpp | 8 ++++---- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/spec/inputs/import_global.yue b/spec/inputs/import_global.yue index 30a274e..18f0e85 100644 --- a/spec/inputs/import_global.yue +++ b/spec/inputs/import_global.yue @@ -84,3 +84,10 @@ do \func 1, 2, 3 .tag = "abc" +do + import global + + f = -> + func! + try func + diff --git a/spec/outputs/5.1/import_global.lua b/spec/outputs/5.1/import_global.lua index c748c78..3b8334a 100644 --- a/spec/outputs/5.1/import_global.lua +++ b/spec/outputs/5.1/import_global.lua @@ -116,5 +116,16 @@ do local X = X X:func(1, 2, 3) X.tag = "abc" - return X +end +local _anon_func_0 = function(func) + return func +end +do + local func = func + local pcall = pcall + local f + f = function() + func() + return pcall(_anon_func_0, func) + end end diff --git a/spec/outputs/import_global.lua b/spec/outputs/import_global.lua index f76b4fe..895daf9 100644 --- a/spec/outputs/import_global.lua +++ b/spec/outputs/import_global.lua @@ -116,5 +116,16 @@ do local X = X X:func(1, 2, 3) X.tag = "abc" - return X +end +local _anon_func_0 = function(func) + return func +end +do + local func = func + local pcall = pcall + local f + f = function() + func() + return pcall(_anon_func_0, func) + end end diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 09d0bd1..a2d49af 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.31.0"sv; +const std::string_view version = "0.31.1"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -437,7 +437,7 @@ private: struct Scope; struct ImportedGlobal { std::string* globalCodeLine = nullptr; - Scope* importingScope = nullptr; + std::unordered_map* vars = nullptr; std::string indent; std::string nl; std::unordered_set globals; @@ -1625,7 +1625,7 @@ private: 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); + _importedGlobal->vars->insert_or_assign(name, VarType::LocalConst); } } @@ -5333,7 +5333,7 @@ private: auto& scope = currentScope(); scope.importedGlobal = std::make_unique(); _importedGlobal = scope.importedGlobal.get(); - _importedGlobal->importingScope = &scope; + _importedGlobal->vars = scope.vars.get(); _importedGlobal->indent = indent(); _importedGlobal->nl = nl(stmt); _importedGlobal->globalCodeLine = &temp.emplace_back(); -- cgit v1.2.3-55-g6feb