Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDScript: Invalidate cached parser chain when reloading #92616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions modules/gdscript/gdscript_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ GDScriptParserRef::Status GDScriptParserRef::get_status() const {
return status;
}

String GDScriptParserRef::get_path() const {
return path;
}

uint32_t GDScriptParserRef::get_source_hash() const {
return source_hash;
}
Expand Down Expand Up @@ -135,9 +139,7 @@ void GDScriptParserRef::clear() {

GDScriptParserRef::~GDScriptParserRef() {
clear();

MutexLock lock(GDScriptCache::singleton->mutex);
GDScriptCache::singleton->parser_map.erase(path);
GDScriptCache::remove_parser(path);
}

GDScriptCache *GDScriptCache::singleton = nullptr;
Expand All @@ -158,6 +160,11 @@ void GDScriptCache::move_script(const String &p_from, const String &p_to) {
}
singleton->parser_map.erase(p_from);

if (singleton->parser_inverse_dependencies.has(p_from) && !p_from.is_empty()) {
singleton->parser_inverse_dependencies[p_to] = singleton->parser_inverse_dependencies[p_from];
}
singleton->parser_inverse_dependencies.erase(p_from);

if (singleton->shallow_gdscript_cache.has(p_from) && !p_from.is_empty()) {
singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from];
}
Expand All @@ -181,13 +188,11 @@ void GDScriptCache::remove_script(const String &p_path) {
}

if (singleton->parser_map.has(p_path)) {
// Keep a local reference until it goes out of scope.
// Clearing it can trigger a reference to itself to go out of scope, destructing it before clear finishes.
Ref<GDScriptParserRef> parser_ref = singleton->parser_map[p_path];
singleton->parser_map.erase(p_path);
parser_ref->clear();
singleton->parser_map[p_path]->clear();
}

remove_parser(p_path);

singleton->dependencies.erase(p_path);
singleton->shallow_gdscript_cache.erase(p_path);
singleton->full_gdscript_cache.erase(p_path);
Expand All @@ -198,6 +203,7 @@ Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptP
Ref<GDScriptParserRef> ref;
if (!p_owner.is_empty()) {
singleton->dependencies[p_owner].insert(p_path);
singleton->parser_inverse_dependencies[p_path].insert(p_owner);
}
if (singleton->parser_map.has(p_path)) {
ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]);
Expand Down Expand Up @@ -229,6 +235,13 @@ void GDScriptCache::remove_parser(const String &p_path) {
MutexLock lock(singleton->mutex);
// Can't clear the parser because some other parser might be currently using it in the chain of calls.
singleton->parser_map.erase(p_path);

// Have to copy while iterating, because parser_inverse_dependencies is modified.
HashSet<String> ideps = singleton->parser_inverse_dependencies[p_path];
singleton->parser_inverse_dependencies.erase(p_path);
for (String idep_path : ideps) {
remove_parser(idep_path);
}
}

String GDScriptCache::get_source_code(const String &p_path) {
Expand Down Expand Up @@ -417,6 +430,8 @@ void GDScriptCache::clear() {
}
singleton->cleared = true;

singleton->parser_inverse_dependencies.clear();

RBSet<Ref<GDScriptParserRef>> parser_map_refs;
for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) {
parser_map_refs.insert(E.value);
Expand Down
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GDScriptParserRef : public RefCounted {

public:
Status get_status() const;
String get_path() const;
uint32_t get_source_hash() const;
GDScriptParser *get_parser();
GDScriptAnalyzer *get_analyzer();
Expand All @@ -83,6 +84,7 @@ class GDScriptCache {
HashMap<String, Ref<GDScript>> full_gdscript_cache;
HashMap<String, Ref<GDScript>> static_gdscript_cache;
HashMap<String, HashSet<String>> dependencies;
HashMap<String, HashSet<String>> parser_inverse_dependencies;

friend class GDScript;
friend class GDScriptParserRef;
Expand Down
Loading