Avoid creating objects that Godot is going to use placement new to initialize

David Snopek 2024-02-01 14:22:26 -06:00
parent 36847f6af0
commit 7d82c487d1
3 changed files with 17 additions and 8 deletions

View File

@ -454,6 +454,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("") result.append("")
result.append("\tstatic struct _MethodBindings {") result.append("\tstatic struct _MethodBindings {")
result.append(f"\t\tGDExtensionTypeFromVariantConstructorFunc from_variant_constructor;")
if "constructors" in builtin_api: if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]: for constructor in builtin_api["constructors"]:
result.append(f'\t\tGDExtensionPtrConstructor constructor_{constructor["index"]};') result.append(f'\t\tGDExtensionPtrConstructor constructor_{constructor["index"]};')
@ -494,6 +496,9 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("\tstatic void init_bindings();") result.append("\tstatic void init_bindings();")
result.append("\tstatic void _init_bindings_constructors_destructor();") result.append("\tstatic void _init_bindings_constructors_destructor();")
result.append("")
result.append(f"\t{class_name}(const Variant *p_variant);")
result.append("") result.append("")
result.append("public:") result.append("public:")
@ -818,6 +823,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
result.append(f"void {class_name}::_init_bindings_constructors_destructor() {{") result.append(f"void {class_name}::_init_bindings_constructors_destructor() {{")
result.append(
f"\t_method_bindings.from_variant_constructor = internal::gdextension_interface_get_variant_to_type_constructor({enum_type_name});"
)
if "constructors" in builtin_api: if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]: for constructor in builtin_api["constructors"]:
result.append( result.append(
@ -899,6 +908,11 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
copy_constructor_index = -1 copy_constructor_index = -1
result.append(f"{class_name}::{class_name}(const Variant *p_variant) {{")
result.append("\t_method_bindings.from_variant_constructor(&opaque, p_variant->_native_ptr());")
result.append("}")
result.append("")
if "constructors" in builtin_api: if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]: for constructor in builtin_api["constructors"]:
method_signature = f"{class_name}::{class_name}(" method_signature = f"{class_name}::{class_name}("

View File

@ -47,8 +47,6 @@ class ObjectID;
class Variant { class Variant {
uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 }; uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 };
_FORCE_INLINE_ GDExtensionVariantPtr _native_ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }
friend class GDExtensionBinding; friend class GDExtensionBinding;
friend class MethodBind; friend class MethodBind;
@ -145,6 +143,7 @@ private:
static GDExtensionTypeFromVariantConstructorFunc to_type_constructor[VARIANT_MAX]; static GDExtensionTypeFromVariantConstructorFunc to_type_constructor[VARIANT_MAX];
public: public:
_FORCE_INLINE_ GDExtensionVariantPtr _native_ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }
Variant(); Variant();
Variant(std::nullptr_t n) : Variant(std::nullptr_t n) :
Variant() {} Variant() {}

View File

@ -459,15 +459,11 @@ Variant::operator Signal() const {
} }
Variant::operator Dictionary() const { Variant::operator Dictionary() const {
Dictionary result; return Dictionary(this);
to_type_constructor[DICTIONARY](result._native_ptr(), _native_ptr());
return result;
} }
Variant::operator Array() const { Variant::operator Array() const {
Array result; return Array(this);
to_type_constructor[ARRAY](result._native_ptr(), _native_ptr());
return result;
} }
Variant::operator PackedByteArray() const { Variant::operator PackedByteArray() const {