Skip to content

Commit bf23c38

Browse files
committed
Utility functions with "arg1" parameter are now full vararg
Removes the need for manually implementing print() and print_rich()
1 parent f7ab59e commit bf23c38

File tree

6 files changed

+35
-68
lines changed

6 files changed

+35
-68
lines changed

godot-codegen/src/models/domain_mapping.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,15 @@ impl UtilityFunction {
486486
return None;
487487
}
488488

489+
// Some vararg functions like print() or str() are declared with a single argument "arg1: Variant", but that seems
490+
// to be a mistake. We change their parameter list by removing that.
491+
let args = option_as_slice(&function.arguments);
492+
let parameters = if function.is_vararg && args.len() == 1 && args[0].name == "arg1" {
493+
vec![]
494+
} else {
495+
FnParam::new_range(&function.arguments, ctx)
496+
};
497+
489498
let godot_method_name = function.name.clone();
490499
let rust_method_name = godot_method_name.clone(); // No change for now.
491500

@@ -498,7 +507,7 @@ impl UtilityFunction {
498507
common: FunctionCommon {
499508
name: rust_method_name,
500509
godot_name: godot_method_name,
501-
parameters: FnParam::new_range(&function.arguments, ctx),
510+
parameters,
502511
return_value: FnReturn::new(&return_value, ctx),
503512
is_vararg: function.is_vararg,
504513
is_private: false,

godot-codegen/src/special_cases/special_cases.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,13 @@ pub fn is_builtin_type_scalar(name: &str) -> bool {
282282

283283
#[rustfmt::skip]
284284
pub fn is_utility_function_deleted(function: &JsonUtilityFunction, ctx: &mut Context) -> bool {
285-
let hardcoded = match function.name.as_str() {
286-
// We have prints manually defined, with (&[Variant]) instead of (Variant, &[Variant]) signature.
287-
| "print"
288-
| "print_rich"
289-
// Should we map str, printt, prints, printerr, printraw manually as well?
285+
/*let hardcoded = match function.name.as_str() {
286+
| "..."
290287
291288
=> true, _ => false
292289
};
293290
294-
295-
hardcoded || codegen_special_cases::is_utility_function_excluded(function, ctx)
291+
hardcoded ||*/ codegen_special_cases::is_utility_function_excluded(function, ctx)
296292
}
297293

298294
pub fn maybe_rename_class_method<'m>(class_name: &TyName, godot_method_name: &'m str) -> &'m str {

godot-core/src/global/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
//!
1010
//! See also [Godot docs for `@GlobalScope`](https://docs.godotengine.org/en/stable/classes/class_@globalscope.html#methods).
1111
12-
pub(crate) mod print;
12+
mod print;
13+
14+
pub use crate::{godot_error, godot_print, godot_print_rich, godot_script_error, godot_warn};
1315

1416
pub use crate::gen::central::global_enums::*;
1517
pub use crate::gen::utilities::*;
16-
17-
pub use print::*;

godot-core/src/global/print.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -105,58 +105,3 @@ macro_rules! godot_print_rich {
105105
])
106106
};
107107
}
108-
109-
pub use crate::{godot_error, godot_print, godot_print_rich, godot_script_error, godot_warn};
110-
111-
use crate::builtin::{StringName, Variant};
112-
use crate::sys::{self, GodotFfi};
113-
114-
/// Prints to the Godot console (raw variant args).
115-
///
116-
/// Typically, you'd want to use the [`godot_print!`] macro instead, which uses this function internally.
117-
pub fn print(varargs: &[Variant]) {
118-
unsafe {
119-
let method_name = StringName::from("print");
120-
let call_fn = sys::interface_fn!(variant_get_ptr_utility_function)(
121-
method_name.string_sys(),
122-
2648703342i64,
123-
);
124-
let call_fn = call_fn.unwrap_unchecked();
125-
126-
let mut args = Vec::new();
127-
args.extend(varargs.iter().map(Variant::sys));
128-
129-
let args_ptr = args.as_ptr();
130-
let _variant = Variant::new_with_init(|return_ptr| {
131-
call_fn(return_ptr, args_ptr, args.len() as i32);
132-
});
133-
}
134-
135-
// TODO use generated method, but figure out how print() with zero args can be called
136-
// crate::engine::utilities::print(head, rest);
137-
}
138-
139-
/// Prints rich text to the Godot console (raw variant args).
140-
///
141-
/// Typically, you'd want to use the [`godot_print_rich!`] macro instead, which uses this function internally.
142-
pub fn print_rich(varargs: &[Variant]) {
143-
unsafe {
144-
let method_name = StringName::from("print_rich");
145-
let call_fn = sys::interface_fn!(variant_get_ptr_utility_function)(
146-
method_name.string_sys(),
147-
2648703342i64,
148-
);
149-
let call_fn = call_fn.unwrap_unchecked();
150-
151-
let mut args = Vec::new();
152-
args.extend(varargs.iter().map(Variant::sys));
153-
154-
let args_ptr = args.as_ptr();
155-
let _variant = Variant::new_with_init(|return_ptr| {
156-
call_fn(return_ptr, args_ptr, args.len() as i32);
157-
});
158-
}
159-
160-
// TODO use generated method, but figure out how print_rich() with zero args can be called
161-
// crate::engine::utilities::print_rich(head, rest);
162-
}

godot-core/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ pub mod obj;
1616

1717
#[deprecated = "Print macros have been moved to `godot::global`."]
1818
pub mod log {
19-
pub use crate::global::print::*;
19+
pub use crate::global::{
20+
godot_error, godot_print, godot_print_rich, godot_script_error, godot_warn,
21+
};
2022
}
2123

2224
#[doc(hidden)]

itest/rust/src/engine_tests/utilities_test.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use crate::framework::itest;
99

10-
use godot::builtin::Variant;
10+
use godot::builtin::{GString, Variant};
1111
use godot::global::*;
1212

1313
#[itest]
@@ -26,6 +26,21 @@ fn utilities_sign() {
2626
assert_eq!(output, Variant::from(-1));
2727
}
2828

29+
#[itest]
30+
fn utilities_str() {
31+
let concat = str(&[
32+
Variant::from(12),
33+
Variant::from(" is a "),
34+
Variant::from(true),
35+
Variant::from(" number"),
36+
]);
37+
38+
let empty = str(&[]);
39+
40+
assert_eq!(concat, "12 is a true number".into());
41+
assert_eq!(empty, GString::new());
42+
}
43+
2944
#[itest]
3045
fn utilities_wrap() {
3146
let output = wrap(Variant::from(3.4), Variant::from(2.0), Variant::from(3.0));

0 commit comments

Comments
 (0)