godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.68k stars 509 forks source link

Script freezes Godot on load due to name collision #172

Open Faless opened 6 years ago

Faless commented 6 years ago

Branch Nativescript 1.1 Given the following code:

#include "Sprite.hpp"
class Sprite : public godot::Sprite {

        GODOT_CLASS(Sprite, godot::Sprite);

public:
        static void _register_methods() {};
        void _init() {};
};

/* Godot export stuff */
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
        godot::Godot::gdnative_init(o);
}

extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
        godot::Godot::gdnative_terminate(o);
}

extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
        godot::Godot::nativescript_init(handle);

        godot::register_class<Sprite>();
}

This does not happen if Sprite extends something different than Sprite (e.g. Sprite3D)

It compiles fine, but when loading the .gdns that uses Sprite the game freezes in some kind of loop. Not sure this is relevant, but here's a trace from SIGINT I sent:

String::copy_from (this=0x7fffffffa678, p_cstr=0x5555596bce2b "NativeScript") at core/ustring.cpp:145
145     for (int i = 0; i < len + 1; i++) {
(gdb) bt
#0  String::copy_from (this=0x7fffffffa678, p_cstr=0x5555596bce2b "NativeScript") at core/ustring.cpp:145
#1  0x000055555936fda5 in String::String (this=0x7fffffffa678, p_str=0x5555596bce2b "NativeScript") at core/ustring.cpp:1588
#2  0x000055555637a6ab in NativeScript::get_class_static () at modules/gdnative/nativescript/nativescript.h:102
#3  NativeScript::_get_class_namev (this=0x55555e6eadf0) at modules/gdnative/nativescript/nativescript.h:102
#4  0x00005555591a0637 in Object::_postinitialize (this=0x55555e6eadf0) at core/object.cpp:394
#5  0x00005555591b03d3 in postinitialize_handler (p_object=0x55555e6eadf0) at core/object.cpp:1923
#6  0x00005555563730db in _post_initialize<NativeScript> (p_obj=0x55555e6eadf0) at ./core/os/memory.h:93
#7  NativeScriptLanguage::create_script (this=0x55555b3af130) at modules/gdnative/nativescript/nativescript.cpp:1068
#8  0x000055555636de00 in NativeScript::get_base_script (this=0x55555e6eafe0) at modules/gdnative/nativescript/nativescript.cpp:152
#9  0x00005555564e0361 in GDScriptParser::_get_function_signature (this=0x7fffffffcc00, p_base_type=..., p_function=..., r_return_type=..., r_arg_types=..., 
    r_default_arg_count=@0x7fffffffad4c: 0, r_static=@0x7fffffffae48: false, r_vararg=@0x7fffffffad47: false) at modules/gdscript/gdscript_parser.cpp:6238
#10 0x00005555564e40ea in GDScriptParser::_reduce_function_call_type (this=0x7fffffffcc00, p_call=0x55555e6e0bd0) at modules/gdscript/gdscript_parser.cpp:6486
#11 0x00005555564da50d in GDScriptParser::_reduce_node_type (this=0x7fffffffcc00, p_node=0x55555e6e0bd0) at modules/gdscript/gdscript_parser.cpp:5753
#12 0x00005555564eb3eb in GDScriptParser::_check_class_level_types (this=0x7fffffffcc00, p_class=0x55555b9da250) at modules/gdscript/gdscript_parser.cpp:6976
#13 0x00005555564f5aa8 in GDScriptParser::_parse (this=0x7fffffffcc00, p_base_path=...) at modules/gdscript/gdscript_parser.cpp:7614
#14 0x00005555564f5cbc in GDScriptParser::parse (this=0x7fffffffcc00, p_code=..., p_base_path=..., p_just_validate=false, p_self_path=..., p_for_completion=false, r_safe_lines=0x0)
    at modules/gdscript/gdscript_parser.cpp:7658
#15 0x00005555563e83c0 in GDScript::reload (this=0x55555ac67660, p_keep_state=false) at modules/gdscript/gdscript.cpp:573
#16 0x00005555563f2f84 in ResourceFormatLoaderGDScript::load (this=0x55555bf49c20, p_path=..., p_original_path=..., r_error=0x0) at modules/gdscript/gdscript.cpp:1954
#17 0x00005555594acd41 in ResourceLoader::_load (p_path=..., p_original_path=..., p_type_hint=..., p_no_cache=false, r_error=0x0) at core/io/resource_loader.cpp:173
#18 0x00005555594ad1de in ResourceLoader::load (p_path=..., p_type_hint=..., p_no_cache=false, r_error=0x0) at core/io/resource_loader.cpp:218
#19 0x00005555584d2935 in ResourceInteractiveLoaderText::poll (this=0x55555c1b2c80) at scene/resources/scene_format_text.cpp:432
#20 0x00005555594acb05 in ResourceFormatLoader::load (this=0x55555b9df810, p_path=..., p_original_path=..., r_error=0x0) at core/io/resource_loader.cpp:138
#21 0x00005555594acd41 in ResourceLoader::_load (p_path=..., p_original_path=..., p_type_hint=..., p_no_cache=false, r_error=0x0) at core/io/resource_loader.cpp:173
#22 0x00005555594ad1de in ResourceLoader::load (p_path=..., p_type_hint=..., p_no_cache=false, r_error=0x0) at core/io/resource_loader.cpp:218
#23 0x0000555556241a2f in Main::start () at main/main.cpp:1673
#24 0x00005555562180bc in main (argc=3, argv=0x7fffffffde48) at platform/x11/godot_x11.cpp:54
karroffel commented 6 years ago

This is mostly a bug with the Godot-side implementation of NativeScript, I have to take a look or at least put a warning that this is not allowed :smile:

Karmavil commented 2 years ago

I have no issues using my own namespace but the GODOT_CLASS must not use the custom namespace just the name of the class (as it is declared in the code above).. Everything else must use the namespace, that includes register_method/property/signal/class