Skip to content

Commit ad72601

Browse files
committed
Revert the changes from PR #1044 and #1045 and standardize on Object ** encoding in ptrcall
1 parent c669f0b commit ad72601

File tree

8 files changed

+70
-51
lines changed

8 files changed

+70
-51
lines changed

binding_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ def get_encoded_arg(arg_name, type_name, type_meta):
18871887
elif is_engine_class(type_name):
18881888
# `{name}` is a C++ wrapper, it contains a field which is the object's pointer Godot expects.
18891889
# We have to check `nullptr` because when the caller sends `nullptr`, the wrapper itself will be null.
1890-
name = f"({name} != nullptr ? {name}->_owner : nullptr)"
1890+
name = f"({name} != nullptr ? &{name}->_owner : nullptr)"
18911891
else:
18921892
name = f"&{name}"
18931893

include/godot_cpp/classes/ref.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ class Ref {
229229
template <class T>
230230
struct PtrToArg<Ref<T>> {
231231
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
232-
// Important: p_ptr is T*, not Ref<T>*, since Object* is what engine gives to ptrcall.
232+
GDExtensionRefPtr ref = (GDExtensionRefPtr)p_ptr;
233233
ERR_FAIL_NULL_V(p_ptr, Ref<T>());
234-
return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)))));
234+
return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(godot::internal::gdextension_interface_ref_get_object(ref))));
235235
}
236236

237237
typedef Ref<T> EncodeT;
@@ -253,8 +253,9 @@ struct PtrToArg<const Ref<T> &> {
253253
typedef Ref<T> EncodeT;
254254

255255
_FORCE_INLINE_ static Ref<T> convert(const void *p_ptr) {
256+
GDExtensionRefPtr ref = const_cast<GDExtensionRefPtr>(p_ptr);
256257
ERR_FAIL_NULL_V(p_ptr, Ref<T>());
257-
return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr)))));
258+
return Ref<T>(reinterpret_cast<T *>(godot::internal::get_object_instance_binding(godot::internal::gdextension_interface_ref_get_object(ref))));
258259
}
259260
};
260261

include/godot_cpp/core/method_ptrcall.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ MAKE_PTRARG_BY_REFERENCE(Variant);
169169
template <class T>
170170
struct PtrToArg<T *> {
171171
_FORCE_INLINE_ static T *convert(const void *p_ptr) {
172-
return reinterpret_cast<T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr))));
172+
return reinterpret_cast<T *>(godot::internal::get_object_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr))));
173173
}
174174
typedef Object *EncodeT;
175175
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
@@ -180,7 +180,7 @@ struct PtrToArg<T *> {
180180
template <class T>
181181
struct PtrToArg<const T *> {
182182
_FORCE_INLINE_ static const T *convert(const void *p_ptr) {
183-
return reinterpret_cast<const T *>(godot::internal::get_object_instance_binding(reinterpret_cast<GDExtensionObjectPtr>(const_cast<void *>(p_ptr))));
183+
return reinterpret_cast<const T *>(godot::internal::get_object_instance_binding(*reinterpret_cast<GDExtensionObjectPtr *>(const_cast<void *>(p_ptr))));
184184
}
185185
typedef const Object *EncodeT;
186186
_FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {

test/project/main.gd

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,106 @@ var custom_signal_emitted = null
44

55

66
func _ready():
7+
var example: Example = $Example
8+
79
# Signal.
8-
$Example.emit_custom_signal("Button", 42)
10+
example.emit_custom_signal("Button", 42)
911
assert_equal(custom_signal_emitted, ["Button", 42])
1012

1113
# To string.
12-
assert_equal($Example.to_string(),'Example:[ GDExtension::Example <--> Instance ID:%s ]' % $Example.get_instance_id())
14+
assert_equal(example.to_string(),'Example:[ GDExtension::Example <--> Instance ID:%s ]' % example.get_instance_id())
1315
# It appears there's a bug with instance ids :-(
1416
#assert_equal($Example/ExampleMin.to_string(), 'ExampleMin:[Wrapped:%s]' % $Example/ExampleMin.get_instance_id())
1517

1618
# Call static methods.
17-
assert_equal($Example.test_static(9, 100), 109);
19+
assert_equal(Example.test_static(9, 100), 109);
1820
# It's void and static, so all we know is that it didn't crash.
19-
$Example.test_static2()
21+
Example.test_static2()
2022

2123
# Property list.
22-
$Example.property_from_list = Vector3(100, 200, 300)
23-
assert_equal($Example.property_from_list, Vector3(100, 200, 300))
24+
example.property_from_list = Vector3(100, 200, 300)
25+
assert_equal(example.property_from_list, Vector3(100, 200, 300))
2426

2527
# Call simple methods.
26-
$Example.simple_func()
28+
example.simple_func()
2729
assert_equal(custom_signal_emitted, ['simple_func', 3])
28-
($Example as Example).simple_const_func() # Force use of ptrcall
30+
example.simple_const_func()
2931
assert_equal(custom_signal_emitted, ['simple_const_func', 4])
3032

3133
# Pass custom reference.
32-
assert_equal($Example.custom_ref_func(null), -1)
34+
assert_equal(example.custom_ref_func(null), -1)
3335
var ref1 = ExampleRef.new()
3436
ref1.id = 27
35-
assert_equal($Example.custom_ref_func(ref1), 27)
37+
assert_equal(example.custom_ref_func(ref1), 27)
3638
ref1.id += 1;
37-
assert_equal($Example.custom_const_ref_func(ref1), 28)
39+
assert_equal(example.custom_const_ref_func(ref1), 28)
3840

3941
# Pass core reference.
40-
assert_equal($Example.image_ref_func(null), "invalid")
41-
assert_equal($Example.image_const_ref_func(null), "invalid")
42+
assert_equal(example.image_ref_func(null), "invalid")
43+
assert_equal(example.image_const_ref_func(null), "invalid")
4244
var image = Image.new()
43-
assert_equal($Example.image_ref_func(image), "valid")
44-
assert_equal($Example.image_const_ref_func(image), "valid")
45+
assert_equal(example.image_ref_func(image), "valid")
46+
assert_equal(example.image_const_ref_func(image), "valid")
4547

4648
# Return values.
47-
assert_equal($Example.return_something("some string"), "some string42")
48-
assert_equal($Example.return_something_const(), get_viewport())
49-
var null_ref = $Example.return_empty_ref()
49+
assert_equal(example.return_something("some string"), "some string42")
50+
assert_equal(example.return_something_const(), get_viewport())
51+
var null_ref = example.return_empty_ref()
5052
assert_equal(null_ref, null)
51-
var ret_ref = $Example.return_extended_ref()
53+
var ret_ref = example.return_extended_ref()
5254
assert_not_equal(ret_ref.get_instance_id(), 0)
5355
assert_equal(ret_ref.get_id(), 0)
54-
assert_equal($Example.get_v4(), Vector4(1.2, 3.4, 5.6, 7.8))
55-
assert_equal($Example.test_node_argument($Example), $Example)
56+
assert_equal(example.get_v4(), Vector4(1.2, 3.4, 5.6, 7.8))
57+
assert_equal(example.test_node_argument(example), example)
5658

5759
# VarArg method calls.
5860
var var_ref = ExampleRef.new()
59-
assert_not_equal($Example.extended_ref_checks(var_ref).get_instance_id(), var_ref.get_instance_id())
60-
assert_equal($Example.varargs_func("some", "arguments", "to", "test"), 4)
61-
assert_equal($Example.varargs_func_nv("some", "arguments", "to", "test"), 46)
62-
$Example.varargs_func_void("some", "arguments", "to", "test")
61+
assert_not_equal(example.extended_ref_checks(var_ref).get_instance_id(), var_ref.get_instance_id())
62+
assert_equal(example.varargs_func("some", "arguments", "to", "test"), 4)
63+
assert_equal(example.varargs_func_nv("some", "arguments", "to", "test"), 46)
64+
example.varargs_func_void("some", "arguments", "to", "test")
6365
assert_equal(custom_signal_emitted, ["varargs_func_void", 5])
6466

6567
# Method calls with default values.
66-
assert_equal($Example.def_args(), 300)
67-
assert_equal($Example.def_args(50), 250)
68-
assert_equal($Example.def_args(50, 100), 150)
68+
assert_equal(example.def_args(), 300)
69+
assert_equal(example.def_args(50), 250)
70+
assert_equal(example.def_args(50, 100), 150)
6971

7072
# Array and Dictionary
71-
assert_equal($Example.test_array(), [1, 2])
72-
assert_equal($Example.test_tarray(), [ Vector2(1, 2), Vector2(2, 3) ])
73-
assert_equal($Example.test_dictionary(), {"hello": "world", "foo": "bar"})
73+
assert_equal(example.test_array(), [1, 2])
74+
assert_equal(example.test_tarray(), [ Vector2(1, 2), Vector2(2, 3) ])
75+
assert_equal(example.test_dictionary(), {"hello": "world", "foo": "bar"})
7476
var array: Array[int] = [1, 2, 3]
75-
assert_equal($Example.test_tarray_arg(array), 6)
77+
assert_equal(example.test_tarray_arg(array), 6)
7678

7779
# String += operator
78-
assert_equal($Example.test_string_ops(), "ABCĎE")
80+
assert_equal(example.test_string_ops(), "ABCĎE")
7981

8082
# PackedArray iterators
81-
assert_equal($Example.test_vector_ops(), 105)
83+
assert_equal(example.test_vector_ops(), 105)
8284

8385
# Properties.
84-
assert_equal($Example.group_subgroup_custom_position, Vector2(0, 0))
85-
$Example.group_subgroup_custom_position = Vector2(50, 50)
86-
assert_equal($Example.group_subgroup_custom_position, Vector2(50, 50))
86+
assert_equal(example.group_subgroup_custom_position, Vector2(0, 0))
87+
example.group_subgroup_custom_position = Vector2(50, 50)
88+
assert_equal(example.group_subgroup_custom_position, Vector2(50, 50))
8789

8890
# Constants.
89-
assert_equal($Example.FIRST, 0)
90-
assert_equal($Example.ANSWER_TO_EVERYTHING, 42)
91-
assert_equal($Example.CONSTANT_WITHOUT_ENUM, 314)
91+
assert_equal(Example.FIRST, 0)
92+
assert_equal(Example.ANSWER_TO_EVERYTHING, 42)
93+
assert_equal(Example.CONSTANT_WITHOUT_ENUM, 314)
9294

9395
# BitFields.
9496
assert_equal(Example.FLAG_ONE, 1)
9597
assert_equal(Example.FLAG_TWO, 2)
96-
assert_equal($Example.test_bitfield(0), 0)
97-
assert_equal($Example.test_bitfield(Example.FLAG_ONE | Example.FLAG_TWO), 3)
98+
assert_equal(example.test_bitfield(0), 0)
99+
assert_equal(example.test_bitfield(Example.FLAG_ONE | Example.FLAG_TWO), 3)
100+
101+
# Virtual method.
102+
var event = InputEventKey.new()
103+
event.key_label = KEY_H
104+
event.unicode = 72
105+
get_viewport().push_input(event)
106+
assert_equal(custom_signal_emitted, ["_input: H", 72])
98107

99108
exit_with_status()
100109

test/project/main.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[gd_scene load_steps=2 format=3 uid="uid://dmx2xuigcpvt4"]
22

3-
[ext_resource type="Script" path="res://main.gd" id="1_c326s"]
3+
[ext_resource type="Script" path="res://main.gd" id="1_qesh5"]
44

55
[node name="Node" type="Node"]
6-
script = ExtResource("1_c326s")
6+
script = ExtResource("1_qesh5")
77

88
[node name="Example" type="Example" parent="."]
99

test/project/project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ config_version=5
1212

1313
config/name="GDExtension Test Project"
1414
run/main_scene="res://main.tscn"
15-
config/features=PackedStringArray("4.0")
15+
config/features=PackedStringArray("4.1")
1616
config/icon="res://icon.png"
1717

1818
[native_extensions]

test/src/example.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,10 @@ bool Example::_has_point(const Vector2 &point) const {
348348

349349
return false;
350350
}
351+
352+
void Example::_input(const Ref<InputEvent> &event) {
353+
const InputEventKey *key_event = Object::cast_to<const InputEventKey>(*event);
354+
if (key_event) {
355+
emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode());
356+
}
357+
}

test/src/example.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <godot_cpp/classes/control.hpp>
1818
#include <godot_cpp/classes/global_constants.hpp>
1919
#include <godot_cpp/classes/image.hpp>
20+
#include <godot_cpp/classes/input_event_key.hpp>
2021
#include <godot_cpp/classes/viewport.hpp>
2122

2223
#include <godot_cpp/core/binder_common.hpp>
@@ -129,6 +130,7 @@ class Example : public Control {
129130

130131
// Virtual function override (no need to bind manually).
131132
virtual bool _has_point(const Vector2 &point) const override;
133+
virtual void _input(const Ref<InputEvent> &event) override;
132134
};
133135

134136
VARIANT_ENUM_CAST(Example::Constants);

0 commit comments

Comments
 (0)