Skip to content

Commit 8822ea3

Browse files
authored
Try #204:
2 parents e0d45a3 + 4908a1c commit 8822ea3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+884
-314
lines changed

godot-codegen/src/central_generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn make_sys_code(central_items: &CentralItems) -> String {
176176
}
177177
}
178178

179-
impl GodotFfi for VariantType {
179+
unsafe impl GodotFfi for VariantType {
180180
ffi_methods! { type GDExtensionTypePtr = *mut Self; .. }
181181
}
182182

@@ -205,7 +205,7 @@ fn make_sys_code(central_items: &CentralItems) -> String {
205205
}
206206
}
207207

208-
impl GodotFfi for VariantOperator {
208+
unsafe impl GodotFfi for VariantOperator {
209209
ffi_methods! { type GDExtensionTypePtr = *mut Self; .. }
210210
}
211211
};

godot-codegen/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ const SELECTED_CLASSES: &[&str] = &[
271271
"Image",
272272
"ImageTextureLayered",
273273
"Input",
274+
"InputEvent",
275+
"InputEventAction",
274276
"Label",
275277
"MainLoop",
276278
"Marker2D",
@@ -296,4 +298,6 @@ const SELECTED_CLASSES: &[&str] = &[
296298
"TextureLayered",
297299
"Time",
298300
"Timer",
301+
"Window",
302+
"Viewport",
299303
];

godot-codegen/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub fn make_enum_definition(enum_: &Enum) -> TokenStream {
9999
self.ord
100100
}
101101
}
102-
impl sys::GodotFfi for #enum_name {
102+
unsafe impl sys::GodotFfi for #enum_name {
103103
sys::ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
104104
}
105105
#bitfield_ops

godot-core/src/builtin/aabb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ impl Aabb {
8282
*/
8383
}
8484

85-
impl GodotFfi for Aabb {
85+
unsafe impl GodotFfi for Aabb {
8686
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
8787
}

godot-core/src/builtin/array.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,19 @@ impl<T: VariantMetadata + ToVariant> Array<T> {
566566
// ...
567567
// }
568568

569-
impl<T: VariantMetadata> GodotFfi for Array<T> {
570-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; .. }
569+
unsafe impl<T: VariantMetadata> GodotFfi for Array<T> {
570+
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque;
571+
fn from_sys;
572+
fn sys;
573+
fn from_sys_init;
574+
fn move_return_ptr;
575+
}
576+
577+
unsafe fn from_arg_ptr(ptr: sys::GDExtensionTypePtr, _call_type: sys::CallType) -> Self {
578+
let array = Self::from_sys(ptr);
579+
std::mem::forget(array.share());
580+
array
581+
}
571582

572583
unsafe fn from_sys_init_default(init_fn: impl FnOnce(sys::GDExtensionTypePtr)) -> Self {
573584
let mut result = Self::default();
@@ -855,7 +866,7 @@ macro_rules! varray {
855866
/// [`set_typed`](https://docs.godotengine.org/en/latest/classes/class_array.html#class-array-method-set-typed).
856867
///
857868
/// We ignore the `script` parameter because it has no impact on typing in Godot.
858-
#[derive(Debug, PartialEq, Eq)]
869+
#[derive(PartialEq, Eq)]
859870
struct TypeInfo {
860871
variant_type: VariantType,
861872
class_name: StringName,
@@ -880,3 +891,16 @@ impl TypeInfo {
880891
self.variant_type != VariantType::Nil
881892
}
882893
}
894+
895+
impl fmt::Debug for TypeInfo {
896+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
897+
let class = self.class_name.to_string();
898+
let class_str = if class.is_empty() {
899+
String::new()
900+
} else {
901+
format!(" (class={class})")
902+
};
903+
904+
write!(f, "{:?}{}", self.variant_type, class_str)
905+
}
906+
}

godot-core/src/builtin/basis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl Mul<Vector3> for Basis {
570570
}
571571
}
572572

573-
impl GodotFfi for Basis {
573+
unsafe impl GodotFfi for Basis {
574574
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
575575
}
576576

godot-core/src/builtin/color.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl Color {
311311
}
312312
}
313313

314-
impl GodotFfi for Color {
314+
unsafe impl GodotFfi for Color {
315315
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
316316
}
317317

godot-core/src/builtin/dictionary.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,19 @@ impl Dictionary {
239239
// ----------------------------------------------------------------------------------------------------------------------------------------------
240240
// Traits
241241

242-
impl GodotFfi for Dictionary {
243-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; .. }
242+
unsafe impl GodotFfi for Dictionary {
243+
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque;
244+
fn from_sys;
245+
fn from_sys_init;
246+
fn sys;
247+
fn move_return_ptr;
248+
}
249+
250+
unsafe fn from_arg_ptr(ptr: sys::GDExtensionTypePtr, _call_type: sys::CallType) -> Self {
251+
let dictionary = Self::from_sys(ptr);
252+
std::mem::forget(dictionary.share());
253+
dictionary
254+
}
244255

245256
unsafe fn from_sys_init_default(init_fn: impl FnOnce(sys::GDExtensionTypePtr)) -> Self {
246257
let mut result = Self::default();

godot-core/src/builtin/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ macro_rules! impl_builtin_stub {
157157
}
158158
}
159159

160-
impl GodotFfi for $Class {
160+
unsafe impl GodotFfi for $Class {
161161
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; .. }
162162
}
163163
};

godot-core/src/builtin/meta/mod.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ pub trait VariantMetadata {
2727
}
2828

2929
fn property_info(property_name: &str) -> PropertyInfo {
30-
PropertyInfo::new(
31-
Self::variant_type(),
32-
Self::class_name(),
33-
StringName::from(property_name),
34-
global::PropertyHint::PROPERTY_HINT_NONE,
35-
GodotString::new(),
36-
)
30+
PropertyInfo {
31+
variant_type: Self::variant_type(),
32+
class_name: Self::class_name(),
33+
property_name: StringName::from(property_name),
34+
hint: global::PropertyHint::PROPERTY_HINT_NONE,
35+
hint_string: GodotString::new(),
36+
usage: global::PropertyUsageFlags::PROPERTY_USAGE_DEFAULT,
37+
}
3738
}
3839

3940
fn param_metadata() -> sys::GDExtensionClassMethodArgumentMetadata {
@@ -52,33 +53,17 @@ impl<T: VariantMetadata> VariantMetadata for Option<T> {
5253
/// Rusty abstraction of sys::GDExtensionPropertyInfo
5354
/// Keeps the actual allocated values (the sys equivalent only keeps pointers, which fall out of scope)
5455
#[derive(Debug)]
56+
// Note: is not #[non_exhaustive], so adding fields is a breaking change. Mostly used internally at the moment though.
5557
pub struct PropertyInfo {
56-
variant_type: VariantType,
57-
class_name: ClassName,
58-
property_name: StringName,
59-
hint: global::PropertyHint,
60-
hint_string: GodotString,
61-
usage: global::PropertyUsageFlags,
58+
pub variant_type: VariantType,
59+
pub class_name: ClassName,
60+
pub property_name: StringName,
61+
pub hint: global::PropertyHint,
62+
pub hint_string: GodotString,
63+
pub usage: global::PropertyUsageFlags,
6264
}
6365

6466
impl PropertyInfo {
65-
pub fn new(
66-
variant_type: VariantType,
67-
class_name: ClassName,
68-
property_name: StringName,
69-
hint: global::PropertyHint,
70-
hint_string: GodotString,
71-
) -> Self {
72-
Self {
73-
variant_type,
74-
class_name,
75-
property_name,
76-
hint,
77-
hint_string,
78-
usage: global::PropertyUsageFlags::PROPERTY_USAGE_DEFAULT,
79-
}
80-
}
81-
8267
/// Converts to the FFI type. Keep this object allocated while using that!
8368
pub fn property_sys(&self) -> sys::GDExtensionPropertyInfo {
8469
use crate::obj::EngineEnum as _;

godot-core/src/builtin/meta/signature.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub trait SignatureTuple {
3434
ret: sys::GDExtensionTypePtr,
3535
func: fn(&mut C, Self::Params) -> Self::Ret,
3636
method_name: &str,
37+
call_type: sys::CallType,
3738
);
3839
}
3940

@@ -143,6 +144,7 @@ macro_rules! impl_signature_for_tuple {
143144
ret: sys::GDExtensionTypePtr,
144145
func: fn(&mut C, Self::Params) -> Self::Ret,
145146
method_name: &str,
147+
call_type: sys::CallType,
146148
) {
147149
$crate::out!("ptrcall: {}", method_name);
148150

@@ -151,19 +153,20 @@ macro_rules! impl_signature_for_tuple {
151153

152154
let args = ( $(
153155
unsafe {
154-
<$Pn as sys::GodotFuncMarshal>::try_from_sys(
155-
sys::force_mut_ptr(*args_ptr.offset($n))
156+
<$Pn as sys::GodotFuncMarshal>::try_from_arg(
157+
sys::force_mut_ptr(*args_ptr.offset($n)),
158+
call_type
156159
)
157160
}
158161
.unwrap_or_else(|e| param_error::<$Pn>(method_name, $n, &e)),
159162
)* );
160163

161164
let ret_val = func(&mut *instance, args);
162-
unsafe { <$R as sys::GodotFuncMarshal>::try_write_sys(&ret_val, ret) }
163-
.unwrap_or_else(|e| return_error::<$R>(method_name, &e));
164-
165-
// FIXME is inc_ref needed here?
166-
// std::mem::forget(ret_val);
165+
// SAFETY:
166+
// `ret` is always a pointer to an initialized value of type $R
167+
// TODO: double-check the above
168+
<$R as sys::GodotFuncMarshal>::try_return(ret_val, ret, call_type)
169+
.unwrap_or_else(|ret_val| return_error::<$R>(method_name, &ret_val));
167170
}
168171
}
169172
};

godot-core/src/builtin/node_path.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ impl NodePath {
2222
}
2323
}
2424

25-
impl GodotFfi for NodePath {
26-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; .. }
25+
unsafe impl GodotFfi for NodePath {
26+
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque;
27+
fn from_sys;
28+
fn sys;
29+
fn from_sys_init;
30+
fn move_return_ptr;
31+
}
32+
33+
unsafe fn from_arg_ptr(ptr: sys::GDExtensionTypePtr, _call_type: sys::CallType) -> Self {
34+
let node_path = Self::from_sys(ptr);
35+
std::mem::forget(node_path.clone());
36+
node_path
37+
}
2738

2839
unsafe fn from_sys_init_default(init_fn: impl FnOnce(GDExtensionTypePtr)) -> Self {
2940
let mut result = Self::default();
@@ -90,5 +101,7 @@ impl_builtin_traits! {
90101
Default => node_path_construct_default;
91102
Clone => node_path_construct_copy;
92103
Drop => node_path_destroy;
104+
Eq => node_path_operator_equal;
105+
// Ord => node_path_operator_less;
93106
}
94107
}

godot-core/src/builtin/packed_array.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,19 @@ macro_rules! impl_packed_array {
390390
}
391391
}
392392

393-
impl GodotFfi for $PackedArray {
394-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; .. }
393+
unsafe impl GodotFfi for $PackedArray {
394+
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque;
395+
fn from_sys;
396+
fn sys;
397+
fn from_sys_init;
398+
fn move_return_ptr;
399+
}
400+
401+
unsafe fn from_arg_ptr(ptr: sys::GDExtensionTypePtr, _call_type: sys::CallType) -> Self {
402+
let array = Self::from_sys(ptr);
403+
std::mem::forget(array.clone());
404+
array
405+
}
395406

396407
unsafe fn from_sys_init_default(init_fn: impl FnOnce(sys::GDExtensionTypePtr)) -> Self {
397408
let mut result = Self::default();

godot-core/src/builtin/plane.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Neg for Plane {
134134
}
135135
}
136136

137-
impl GodotFfi for Plane {
137+
unsafe impl GodotFfi for Plane {
138138
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
139139
}
140140

godot-core/src/builtin/projection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ impl GlamConv for Projection {
486486
type Glam = RMat4;
487487
}
488488

489-
impl GodotFfi for Projection {
489+
unsafe impl GodotFfi for Projection {
490490
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
491491
}
492492

godot-core/src/builtin/quaternion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl Mul<Quaternion> for Quaternion {
254254
}
255255
}
256256

257-
impl GodotFfi for Quaternion {
257+
unsafe impl GodotFfi for Quaternion {
258258
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
259259
}
260260

godot-core/src/builtin/rect2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,6 @@ impl Rect2 {
104104
*/
105105
}
106106

107-
impl GodotFfi for Rect2 {
107+
unsafe impl GodotFfi for Rect2 {
108108
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
109109
}

godot-core/src/builtin/rect2i.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ impl Rect2i {
9393
*/
9494
}
9595

96-
impl GodotFfi for Rect2i {
96+
unsafe impl GodotFfi for Rect2i {
9797
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
9898
}

godot-core/src/builtin/rid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ impl Rid {
8181
}
8282
}
8383

84-
impl GodotFfi for Rid {
84+
unsafe impl GodotFfi for Rid {
8585
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
8686
}

godot-core/src/builtin/string.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ impl GodotString {
3535
fn from_string_sys = from_sys;
3636
fn from_string_sys_init = from_sys_init;
3737
fn string_sys = sys;
38-
fn write_string_sys = write_sys;
38+
}
39+
40+
/// Move `self` into a system pointer.
41+
///
42+
/// # Safety
43+
/// `dst` must be a pointer to a `GodotString` which is suitable for ffi with Godot.
44+
pub unsafe fn move_string_ptr(self, dst: sys::GDExtensionStringPtr) {
45+
self.move_return_ptr(dst as *mut _, sys::CallType::Standard);
3946
}
4047

4148
/// Gets the internal chars slice from a [`GodotString`].
@@ -68,8 +75,19 @@ impl GodotString {
6875
}
6976
}
7077

71-
impl GodotFfi for GodotString {
72-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque; .. }
78+
unsafe impl GodotFfi for GodotString {
79+
ffi_methods! { type sys::GDExtensionTypePtr = *mut Opaque;
80+
fn from_sys;
81+
fn sys;
82+
fn from_sys_init;
83+
fn move_return_ptr;
84+
}
85+
86+
unsafe fn from_arg_ptr(ptr: sys::GDExtensionTypePtr, _call_type: sys::CallType) -> Self {
87+
let string = Self::from_sys(ptr);
88+
std::mem::forget(string.clone());
89+
string
90+
}
7391

7492
unsafe fn from_sys_init_default(init_fn: impl FnOnce(sys::GDExtensionTypePtr)) -> Self {
7593
let mut result = Self::default();

0 commit comments

Comments
 (0)