From 7aed5da1177a7b01a2bb8327a9a2ddfc362fb5c1 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 5 Feb 2023 15:07:23 +0100 Subject: [PATCH] Support static class + builtin methods --- godot-codegen/src/api_parser.rs | 2 +- godot-codegen/src/class_generator.rs | 50 +++++++++++++++++++--------- godot-core/src/builtin/color.rs | 1 - itest/rust/src/codegen_test.rs | 31 +++++++++++++++-- 4 files changed, 64 insertions(+), 20 deletions(-) diff --git a/godot-codegen/src/api_parser.rs b/godot-codegen/src/api_parser.rs index e4852fde0..da5d8643b 100644 --- a/godot-codegen/src/api_parser.rs +++ b/godot-codegen/src/api_parser.rs @@ -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, pub return_value: Option, diff --git a/godot-codegen/src/class_generator.rs b/godot-codegen/src/class_generator.rs index 3b414f7a8..91186a687 100644 --- a/godot-codegen/src/class_generator.rs +++ b/godot-codegen/src/class_generator.rs @@ -510,8 +510,7 @@ 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; } @@ -519,11 +518,13 @@ fn make_method_definition( }*/ 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; @@ -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( @@ -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; @@ -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( @@ -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>, is_varcall: bool, diff --git a/godot-core/src/builtin/color.rs b/godot-core/src/builtin/color.rs index 395046dbc..b2785867b 100644 --- a/godot-core/src/builtin/color.rs +++ b/godot-core/src/builtin/color.rs @@ -17,7 +17,6 @@ pub struct Color { } impl Color { - #[allow(dead_code)] pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self { Self { r, g, b, a } } diff --git a/itest/rust/src/codegen_test.rs b/itest/rust/src/codegen_test.rs index 82f0761c5..f220c9b68 100644 --- a/itest/rust/src/codegen_test.rs +++ b/itest/rust/src/codegen_test.rs @@ -4,17 +4,20 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -// This file tests the presence and naming of generated symbols, not their functionality. +// This file tests the presence, naming and accessibility of generated symbols. +// Functionality is only tested on a superficial level (to make sure general FFI mechanisms work). use crate::itest; - -use godot::engine::HttpRequest; +use godot::builtin::inner::{InnerColor, InnerString}; +use godot::engine::{FileAccess, HttpRequest}; use godot::prelude::*; pub fn run() -> bool { let mut ok = true; ok &= codegen_class_renamed(); ok &= codegen_base_renamed(); + ok &= codegen_static_builtin_method(); + ok &= codegen_static_class_method(); ok } @@ -36,6 +39,28 @@ fn codegen_base_renamed() { obj.free(); } +#[itest] +fn codegen_static_builtin_method() { + let pi = InnerString::num(std::f64::consts::PI, 3); + assert_eq!(pi, GodotString::from("3.142")); + + let col = InnerColor::html("#663399cc".into()); + assert_eq!(col, Color::new(0.4, 0.2, 0.6, 0.8)); +} + +#[itest] +fn codegen_static_class_method() { + let exists = FileAccess::file_exists("inexistent".into()); + assert!(!exists); + + let exists = FileAccess::file_exists("res://itest.gdextension".into()); + assert!(exists); + + // see also object_test for reference count verification +} + +// ---------------------------------------------------------------------------------------------------------------------------------------------- + #[derive(GodotClass)] #[class(base=HttpRequest)] pub struct TestBaseRenamed {