From 6b88c86cec1f38934730beca613ee29df5643f6a Mon Sep 17 00:00:00 2001 From: rune-scape Date: Fri, 12 Apr 2024 16:13:25 -0700 Subject: [PATCH] GDScript: invalidate GDScriptParserRef when reloading --- modules/gdscript/gdscript.cpp | 16 +++- modules/gdscript/gdscript_analyzer.cpp | 69 ++++++---------- modules/gdscript/gdscript_analyzer.h | 3 - modules/gdscript/gdscript_cache.cpp | 104 +++++++++++++++---------- modules/gdscript/gdscript_cache.h | 11 ++- modules/gdscript/gdscript_editor.cpp | 2 +- modules/gdscript/gdscript_parser.cpp | 36 ++++++--- modules/gdscript/gdscript_parser.h | 4 + 8 files changed, 139 insertions(+), 106 deletions(-) diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 8e74de42421..921ed0b416c 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -739,10 +739,18 @@ Error GDScript::reload(bool p_keep_state) { if (source_path.is_empty()) { source_path = get_path(); } - Ref cached_script = GDScriptCache::get_cached_script(source_path); - if (!source_path.is_empty() && cached_script.is_null()) { - MutexLock lock(GDScriptCache::singleton->mutex); - GDScriptCache::singleton->shallow_gdscript_cache[source_path] = Ref(this); + if (!source_path.is_empty()) { + if (GDScriptCache::get_cached_script(source_path).is_null()) { + MutexLock lock(GDScriptCache::singleton->mutex); + GDScriptCache::singleton->shallow_gdscript_cache[source_path] = Ref(this); + } + if (GDScriptCache::has_parser(source_path)) { + Error err = OK; + Ref parser_ref = GDScriptCache::get_parser(source_path, GDScriptParserRef::EMPTY, err); + if (parser_ref.is_valid() && parser_ref->get_source_hash() != source.hash()) { + GDScriptCache::remove_parser(source_path); + } + } } } diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index a30a87f3a1c..a641cc25d5c 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -325,7 +325,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c if (!parser->has_class(p_class)) { String script_path = p_class->get_datatype().script_path; - Ref parser_ref = get_parser_for(script_path); + Ref parser_ref = parser->get_depended_parser_for(script_path); if (parser_ref.is_null()) { push_error(vformat(R"(Could not find script "%s".)", script_path), p_source); return ERR_PARSE_ERROR; @@ -400,7 +400,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c if (p_class->extends_path.is_relative_path()) { p_class->extends_path = class_type.script_path.get_base_dir().path_join(p_class->extends_path).simplify_path(); } - Ref ext_parser = get_parser_for(p_class->extends_path); + Ref ext_parser = parser->get_depended_parser_for(p_class->extends_path); if (ext_parser.is_null()) { push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class); return ERR_PARSE_ERROR; @@ -428,7 +428,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c if (GDScript::is_canonically_equal_paths(base_path, parser->script_path)) { base = parser->head->get_datatype(); } else { - Ref base_parser = get_parser_for(base_path); + Ref base_parser = parser->get_depended_parser_for(base_path); if (base_parser.is_null()) { push_error(vformat(R"(Could not resolve super class "%s".)", name), id); return ERR_PARSE_ERROR; @@ -448,7 +448,7 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c return ERR_PARSE_ERROR; } - Ref info_parser = get_parser_for(info.path); + Ref info_parser = parser->get_depended_parser_for(info.path); if (info_parser.is_null()) { push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), id); return ERR_PARSE_ERROR; @@ -644,7 +644,7 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type } else if (Ref