GDScript: fix subclass methods not inheriting RPC info

This commit is contained in:
ocean (they/them) 2023-08-31 13:49:04 -04:00
parent 0bca424239
commit 711ffabcfe
4 changed files with 25 additions and 26 deletions

View file

@ -2503,7 +2503,10 @@ Error GDScriptCompiler::_parse_setter_getter(GDScript *p_script, const GDScriptP
return err;
}
Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
// Prepares given script, and inner class scripts, for compilation. It populates class members and initializes method
// RPC info for its base classes first, then for itself, then for inner classes.
// Warning: this function cannot initiate compilation of other classes, or it will result in cyclic dependency issues.
Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
if (parsed_classes.has(p_script)) {
return OK;
}
@ -2562,6 +2565,7 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri
p_script->implicit_initializer = nullptr;
p_script->implicit_ready = nullptr;
p_script->static_initializer = nullptr;
p_script->rpc_config.clear();
p_script->clearing = false;
@ -2592,7 +2596,7 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri
}
if (main_script->has_class(base.ptr())) {
Error err = _populate_class_members(base.ptr(), p_class->base_type.class_type, p_keep_state);
Error err = _prepare_compilation(base.ptr(), p_class->base_type.class_type, p_keep_state);
if (err) {
return err;
}
@ -2611,7 +2615,7 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri
return ERR_COMPILATION_FAILED;
}
err = _populate_class_members(base.ptr(), p_class->base_type.class_type, p_keep_state);
err = _prepare_compilation(base.ptr(), p_class->base_type.class_type, p_keep_state);
if (err) {
_set_error(vformat(R"(Could not populate class members of base class "%s" in "%s".)", base->fully_qualified_name, base->path), nullptr);
return err;
@ -2628,6 +2632,12 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri
} break;
}
// Duplicate RPC information from base GDScript
// Base script isn't valid because it should not have been compiled yet, but the reference contains relevant info.
if (base_type.kind == GDScriptDataType::GDSCRIPT && p_script->base.is_valid()) {
p_script->rpc_config = p_script->base->rpc_config.duplicate();
}
for (int i = 0; i < p_class->members.size(); i++) {
const GDScriptParser::ClassNode::Member &member = p_class->members[i];
switch (member.type) {
@ -2746,6 +2756,14 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri
p_script->members.insert(Variant());
} break;
case GDScriptParser::ClassNode::Member::FUNCTION: {
const GDScriptParser::FunctionNode *function_n = member.function;
Variant config = function_n->rpc_config;
if (config.get_type() != Variant::NIL) {
p_script->rpc_config[function_n->identifier->name] = config;
}
} break;
default:
break; // Nothing to do here.
}
@ -2756,7 +2774,7 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri
parsed_classes.insert(p_script);
parsing_classes.erase(p_script);
// Populate sub-classes.
// Populate inner classes.
for (int i = 0; i < p_class->members.size(); i++) {
const GDScriptParser::ClassNode::Member &member = p_class->members[i];
if (member.type != member.CLASS) {
@ -2769,7 +2787,7 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri
// Subclass might still be parsing, just skip it
if (!parsing_classes.has(subclass_ptr)) {
Error err = _populate_class_members(subclass_ptr, inner_class, p_keep_state);
Error err = _prepare_compilation(subclass_ptr, inner_class, p_keep_state);
if (err) {
return err;
}
@ -2905,8 +2923,6 @@ Error GDScriptCompiler::_compile_class(GDScript *p_script, const GDScriptParser:
has_static_data = has_static_data || inner_class->has_static_data;
}
p_script->_init_rpc_methods_properties();
p_script->valid = true;
return OK;
}
@ -2979,7 +2995,7 @@ Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_scri
make_scripts(p_script, root, p_keep_state);
main_script->_owner = nullptr;
Error err = _populate_class_members(main_script, parser->get_tree(), p_keep_state);
Error err = _prepare_compilation(main_script, parser->get_tree(), p_keep_state);
if (err) {
return err;