Skip to content

Commit b7fe10e

Browse files
bors[bot]Bromeon
authored andcommitted
Merge #115
115: Implement static methods for engine + builtin classes r=Bromeon a=Bromeon Also fixes a memory leak when `RefCounted` instances are returned from engine methods. Closes #43. Co-authored-by: Jan Haller <[email protected]>
2 parents f25f65c + 3158825 commit b7fe10e

File tree

20 files changed

+991
-475
lines changed

20 files changed

+991
-475
lines changed

examples/dodge-the-creeps/rust/src/player.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl GodotExt for Player {
9595
animated_sprite.set_flip_v(velocity.y > 0.0)
9696
}
9797

98-
animated_sprite.play(animation.into(), 1.0, false);
98+
animated_sprite.play(animation.into(), false);
9999
} else {
100100
animated_sprite.stop();
101101
}

godot-codegen/src/api_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub struct ClassMethod {
172172
pub name: String,
173173
pub is_const: bool,
174174
pub is_vararg: bool,
175-
//pub is_static: bool,
175+
pub is_static: bool,
176176
pub is_virtual: bool,
177177
pub hash: Option<i64>,
178178
pub return_value: Option<MethodReturn>,

godot-codegen/src/class_generator.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -510,20 +510,21 @@ fn make_method_definition(
510510
/*if method.map_args(|args| args.is_empty()) {
511511
// Getters (i.e. 0 arguments) will be stripped of their `get_` prefix, to conform to Rust convention
512512
if let Some(remainder) = method_name.strip_prefix("get_") {
513-
// Do not apply for get_16 etc
514-
// TODO also not for get_u16 etc, in StreamPeer
513+
// TODO Do not apply for FileAccess::get_16, StreamPeer::get_u16, etc
515514
if !remainder.chars().nth(0).unwrap().is_ascii_digit() {
516515
method_name = remainder;
517516
}
518517
}
519518
}*/
520519

521520
let method_name_str = special_cases::maybe_renamed(class_name, &method.name);
522-
let receiver = if method.is_const {
523-
quote! { &self, }
524-
} else {
525-
quote! { &mut self, }
526-
};
521+
522+
let (receiver, receiver_arg) = make_receiver(
523+
method.is_static,
524+
method.is_const,
525+
quote! { self.object_ptr },
526+
);
527+
527528
let hash = method.hash;
528529
let is_varcall = method.is_vararg;
529530

@@ -546,10 +547,10 @@ fn make_method_definition(
546547
let __call_fn = sys::interface_fn!(#function_provider);
547548
};
548549
let varcall_invocation = quote! {
549-
__call_fn(__method_bind, self.object_ptr, __args_ptr, __args.len() as i64, return_ptr, std::ptr::addr_of_mut!(__err));
550+
__call_fn(__method_bind, #receiver_arg, __args_ptr, __args.len() as i64, return_ptr, std::ptr::addr_of_mut!(__err));
550551
};
551552
let ptrcall_invocation = quote! {
552-
__call_fn(__method_bind, self.object_ptr, __args_ptr, return_ptr);
553+
__call_fn(__method_bind, #receiver_arg, __args_ptr, return_ptr);
553554
};
554555

555556
make_function_definition(
@@ -579,11 +580,8 @@ fn make_builtin_method_definition(
579580

580581
let method_name_str = &method.name;
581582

582-
let receiver = if method.is_const {
583-
quote! { &self, }
584-
} else {
585-
quote! { &mut self, }
586-
};
583+
let (receiver, receiver_arg) =
584+
make_receiver(method.is_static, method.is_const, quote! { self.sys_ptr });
587585

588586
let return_value = method.return_type.as_deref().map(MethodReturn::from_type);
589587
let hash = method.hash;
@@ -602,7 +600,7 @@ fn make_builtin_method_definition(
602600
let __call_fn = __call_fn.unwrap_unchecked();
603601
};
604602
let ptrcall_invocation = quote! {
605-
__call_fn(self.sys_ptr, __args_ptr, return_ptr, __args.len() as i32);
603+
__call_fn(#receiver_arg, __args_ptr, return_ptr, __args.len() as i32);
606604
};
607605

608606
make_function_definition(
@@ -746,6 +744,28 @@ fn make_function_definition(
746744
}
747745
}
748746

747+
fn make_receiver(
748+
is_static: bool,
749+
is_const: bool,
750+
receiver_arg: TokenStream,
751+
) -> (TokenStream, TokenStream) {
752+
let receiver = if is_static {
753+
quote! {}
754+
} else if is_const {
755+
quote! { &self, }
756+
} else {
757+
quote! { &mut self, }
758+
};
759+
760+
let receiver_arg = if is_static {
761+
quote! { std::ptr::null_mut() }
762+
} else {
763+
receiver_arg
764+
};
765+
766+
(receiver, receiver_arg)
767+
}
768+
749769
fn make_params(
750770
method_args: &Option<Vec<MethodArg>>,
751771
is_varcall: bool,

godot-codegen/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ const SELECTED_CLASSES: &[&str] = &[
270270
"Control",
271271
"FileAccess",
272272
"HTTPRequest",
273+
"Image",
274+
"ImageTextureLayered",
273275
"Input",
274276
"Label",
275277
"MainLoop",
@@ -290,6 +292,9 @@ const SELECTED_CLASSES: &[&str] = &[
290292
"SceneTree",
291293
"Sprite2D",
292294
"SpriteFrames",
295+
"Texture",
296+
"Texture2DArray",
297+
"TextureLayered",
293298
"Time",
294299
"Timer",
295300
];

0 commit comments

Comments
 (0)