Merge pull request #92616 from rune-scape/rune-invalidate-parser-chain

GDScript: Invalidate cached parser chain when reloading
This commit is contained in:
Rémi Verschelde 2024-07-02 17:27:25 +02:00
commit 1ebd12ac5f
No known key found for this signature in database
GPG Key ID: C3336907360768E1
2 changed files with 25 additions and 8 deletions

View File

@ -42,6 +42,10 @@ GDScriptParserRef::Status GDScriptParserRef::get_status() const {
return status; return status;
} }
String GDScriptParserRef::get_path() const {
return path;
}
uint32_t GDScriptParserRef::get_source_hash() const { uint32_t GDScriptParserRef::get_source_hash() const {
return source_hash; return source_hash;
} }
@ -131,9 +135,7 @@ void GDScriptParserRef::clear() {
GDScriptParserRef::~GDScriptParserRef() { GDScriptParserRef::~GDScriptParserRef() {
clear(); clear();
GDScriptCache::remove_parser(path);
MutexLock lock(GDScriptCache::singleton->mutex);
GDScriptCache::singleton->parser_map.erase(path);
} }
GDScriptCache *GDScriptCache::singleton = nullptr; GDScriptCache *GDScriptCache::singleton = nullptr;
@ -154,6 +156,11 @@ void GDScriptCache::move_script(const String &p_from, const String &p_to) {
} }
singleton->parser_map.erase(p_from); 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()) { if (singleton->shallow_gdscript_cache.has(p_from) && !p_from.is_empty()) {
singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from]; singleton->shallow_gdscript_cache[p_to] = singleton->shallow_gdscript_cache[p_from];
} }
@ -177,13 +184,11 @@ void GDScriptCache::remove_script(const String &p_path) {
} }
if (singleton->parser_map.has(p_path)) { if (singleton->parser_map.has(p_path)) {
// Keep a local reference until it goes out of scope. singleton->parser_map[p_path]->clear();
// 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();
} }
remove_parser(p_path);
singleton->dependencies.erase(p_path); singleton->dependencies.erase(p_path);
singleton->shallow_gdscript_cache.erase(p_path); singleton->shallow_gdscript_cache.erase(p_path);
singleton->full_gdscript_cache.erase(p_path); singleton->full_gdscript_cache.erase(p_path);
@ -194,6 +199,7 @@ Ref<GDScriptParserRef> GDScriptCache::get_parser(const String &p_path, GDScriptP
Ref<GDScriptParserRef> ref; Ref<GDScriptParserRef> ref;
if (!p_owner.is_empty()) { if (!p_owner.is_empty()) {
singleton->dependencies[p_owner].insert(p_path); singleton->dependencies[p_owner].insert(p_path);
singleton->parser_inverse_dependencies[p_path].insert(p_owner);
} }
if (singleton->parser_map.has(p_path)) { if (singleton->parser_map.has(p_path)) {
ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]); ref = Ref<GDScriptParserRef>(singleton->parser_map[p_path]);
@ -225,6 +231,13 @@ void GDScriptCache::remove_parser(const String &p_path) {
MutexLock lock(singleton->mutex); MutexLock lock(singleton->mutex);
// Can't clear the parser because some other parser might be currently using it in the chain of calls. // 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); 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) { String GDScriptCache::get_source_code(const String &p_path) {
@ -417,6 +430,8 @@ void GDScriptCache::clear() {
} }
singleton->cleared = true; singleton->cleared = true;
singleton->parser_inverse_dependencies.clear();
RBSet<Ref<GDScriptParserRef>> parser_map_refs; RBSet<Ref<GDScriptParserRef>> parser_map_refs;
for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) { for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map) {
parser_map_refs.insert(E.value); parser_map_refs.insert(E.value);

View File

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