Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 godot-rust#43.

Co-authored-by: Jan Haller <[email protected]>
  • Loading branch information
2 people authored and ttencate committed Feb 5, 2023
2 parents f25f65c + 3158825 commit f1e85d9
Show file tree
Hide file tree
Showing 19 changed files with 990 additions and 474 deletions.
2 changes: 1 addition & 1 deletion godot-codegen/src/api_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub struct ClassMethod {
pub name: String,
pub is_const: bool,
pub is_vararg: bool,
//pub is_static: bool,
pub is_static: bool,
pub is_virtual: bool,
pub hash: Option<i64>,
pub return_value: Option<MethodReturn>,
Expand Down
50 changes: 35 additions & 15 deletions godot-codegen/src/class_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,20 +510,21 @@ fn make_method_definition(
/*if method.map_args(|args| args.is_empty()) {
// Getters (i.e. 0 arguments) will be stripped of their `get_` prefix, to conform to Rust convention
if let Some(remainder) = method_name.strip_prefix("get_") {
// Do not apply for get_16 etc
// TODO also not for get_u16 etc, in StreamPeer
// TODO Do not apply for FileAccess::get_16, StreamPeer::get_u16, etc
if !remainder.chars().nth(0).unwrap().is_ascii_digit() {
method_name = remainder;
}
}
}*/

let method_name_str = special_cases::maybe_renamed(class_name, &method.name);
let receiver = if method.is_const {
quote! { &self, }
} else {
quote! { &mut self, }
};

let (receiver, receiver_arg) = make_receiver(
method.is_static,
method.is_const,
quote! { self.object_ptr },
);

let hash = method.hash;
let is_varcall = method.is_vararg;

Expand All @@ -546,10 +547,10 @@ fn make_method_definition(
let __call_fn = sys::interface_fn!(#function_provider);
};
let varcall_invocation = quote! {
__call_fn(__method_bind, self.object_ptr, __args_ptr, __args.len() as i64, return_ptr, std::ptr::addr_of_mut!(__err));
__call_fn(__method_bind, #receiver_arg, __args_ptr, __args.len() as i64, return_ptr, std::ptr::addr_of_mut!(__err));
};
let ptrcall_invocation = quote! {
__call_fn(__method_bind, self.object_ptr, __args_ptr, return_ptr);
__call_fn(__method_bind, #receiver_arg, __args_ptr, return_ptr);
};

make_function_definition(
Expand Down Expand Up @@ -579,11 +580,8 @@ fn make_builtin_method_definition(

let method_name_str = &method.name;

let receiver = if method.is_const {
quote! { &self, }
} else {
quote! { &mut self, }
};
let (receiver, receiver_arg) =
make_receiver(method.is_static, method.is_const, quote! { self.sys_ptr });

let return_value = method.return_type.as_deref().map(MethodReturn::from_type);
let hash = method.hash;
Expand All @@ -602,7 +600,7 @@ fn make_builtin_method_definition(
let __call_fn = __call_fn.unwrap_unchecked();
};
let ptrcall_invocation = quote! {
__call_fn(self.sys_ptr, __args_ptr, return_ptr, __args.len() as i32);
__call_fn(#receiver_arg, __args_ptr, return_ptr, __args.len() as i32);
};

make_function_definition(
Expand Down Expand Up @@ -746,6 +744,28 @@ fn make_function_definition(
}
}

fn make_receiver(
is_static: bool,
is_const: bool,
receiver_arg: TokenStream,
) -> (TokenStream, TokenStream) {
let receiver = if is_static {
quote! {}
} else if is_const {
quote! { &self, }
} else {
quote! { &mut self, }
};

let receiver_arg = if is_static {
quote! { std::ptr::null_mut() }
} else {
receiver_arg
};

(receiver, receiver_arg)
}

fn make_params(
method_args: &Option<Vec<MethodArg>>,
is_varcall: bool,
Expand Down
5 changes: 5 additions & 0 deletions godot-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ const SELECTED_CLASSES: &[&str] = &[
"Control",
"FileAccess",
"HTTPRequest",
"Image",
"ImageTextureLayered",
"Input",
"Label",
"MainLoop",
Expand All @@ -290,6 +292,9 @@ const SELECTED_CLASSES: &[&str] = &[
"SceneTree",
"Sprite2D",
"SpriteFrames",
"Texture",
"Texture2DArray",
"TextureLayered",
"Time",
"Timer",
];
Loading

0 comments on commit f1e85d9

Please sign in to comment.