Skip to content

Commit 4c192e8

Browse files
committed
Add _rawptr suffix to unsafe virtual methods
1 parent 1e7d006 commit 4c192e8

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

godot-codegen/src/conv/name_conversions.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
//! Identifier renamings (Godot -> Rust)
99
10-
use proc_macro2::Ident;
11-
1210
use crate::util::ident;
11+
use proc_macro2::Ident;
1312

1413
// ----------------------------------------------------------------------------------------------------------------------------------------------
1514
// Case conversions
@@ -104,6 +103,13 @@ pub fn shout_to_pascal(shout_case: &str) -> String {
104103
result
105104
}
106105

106+
// ----------------------------------------------------------------------------------------------------------------------------------------------
107+
// Virtual functions
108+
109+
pub fn make_unsafe_virtual_fn_name(rust_fn_name: &str) -> String {
110+
format!("{rust_fn_name}_rawptr")
111+
}
112+
107113
// ----------------------------------------------------------------------------------------------------------------------------------------------
108114
// Enum conversions
109115

godot-codegen/src/models/domain_mapping.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ impl ClassMethod {
457457
},
458458
};
459459

460+
// May still be renamed further, for unsafe methods. Not done here because data to determine safety is not available yet.
460461
let rust_method_name = Self::make_virtual_method_name(class_name, &method.name);
461462

462463
Self::from_json_inner(method, rust_method_name, class_name, direction, ctx)
@@ -512,9 +513,18 @@ impl ClassMethod {
512513
let return_value = FnReturn::new(&method.return_value, ctx);
513514
let is_unsafe = Self::function_uses_pointers(&parameters, &return_value);
514515

516+
// Future note: if further changes are made to the virtual method name, make sure to make it reversible so that #[godot_api]
517+
// can match on the Godot name of the virtual method.
518+
let rust_method_name = if is_unsafe && method.is_virtual {
519+
// If the method is unsafe, we need to rename it to avoid conflicts with the safe version.
520+
conv::make_unsafe_virtual_fn_name(rust_method_name)
521+
} else {
522+
rust_method_name.to_string()
523+
};
524+
515525
Some(Self {
516526
common: FunctionCommon {
517-
name: rust_method_name.to_string(),
527+
name: rust_method_name,
518528
godot_name: godot_method_name,
519529
parameters,
520530
return_value,

godot-core/src/obj/script.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ use crate::obj::Inherits;
4242
///
4343
/// To use script instances, implement this trait for your own type.
4444
///
45-
/// You can use the [`create_script_instance()`] function to create a low-level pointer to your script instance.
46-
/// This pointer should then be returned from [`IScriptExtension::instance_create()`](crate::classes::IScriptExtension::instance_create).
45+
/// You can use the [`create_script_instance()`] function to create a low-level pointer to your script instance. This pointer should then be
46+
/// returned from [`IScriptExtension::instance_create_rawptr()`](crate::classes::IScriptExtension::instance_create_rawptr).
4747
///
4848
/// # Example
4949
///

itest/rust/src/builtin_tests/script/script_instance_tests.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl IScriptExtension for TestScript {
4242
true
4343
}
4444

45-
unsafe fn instance_create(&self, for_object: Gd<Object>) -> *mut c_void {
45+
unsafe fn instance_create_rawptr(&self, for_object: Gd<Object>) -> *mut c_void {
4646
create_script_instance(TestScriptInstance::new(self.to_gd().upcast()), for_object)
4747
}
4848

@@ -55,7 +55,7 @@ impl IScriptExtension for TestScript {
5555
fn get_global_name(&self) -> StringName { unreachable!() }
5656
fn inherits_script(&self, _script: Gd<Script>) -> bool { unreachable!() }
5757
fn get_instance_base_type(&self) -> StringName { unreachable!() }
58-
unsafe fn placeholder_instance_create(&self, _for_object: Gd<Object>) -> *mut c_void { unreachable!() }
58+
unsafe fn placeholder_instance_create_rawptr(&self, _for_object: Gd<Object>) -> *mut c_void { unreachable!() }
5959
fn instance_has(&self, _object: Gd<Object>) -> bool { unreachable!() }
6060
fn has_source_code(&self) -> bool { unreachable!() }
6161
fn get_source_code(&self) -> GString { unreachable!() }
@@ -76,8 +76,8 @@ impl IScriptExtension for TestScript {
7676
fn get_script_method_list(&self) -> Array<Dictionary> { unreachable!() }
7777
fn get_script_property_list(&self) -> Array<Dictionary> { unreachable!() }
7878
fn get_member_line(&self, _member: StringName) -> i32 { unreachable!() }
79-
fn get_constants(&self) -> godot::prelude::Dictionary { unreachable!() }
80-
fn get_members(&self) -> godot::prelude::Array<StringName> { unreachable!() }
79+
fn get_constants(&self) -> Dictionary { unreachable!() }
80+
fn get_members(&self) -> Array<StringName> { unreachable!() }
8181
fn is_placeholder_fallback_enabled(&self) -> bool { unreachable!() }
8282
fn get_rpc_config(&self) -> Variant { unreachable!() }
8383

@@ -190,7 +190,10 @@ impl ScriptInstance for TestScriptInstance {
190190
Ok(result)
191191
}
192192

193-
_ => Err(sys::GDEXTENSION_CALL_ERROR_INVALID_METHOD),
193+
other => {
194+
println!("CALL: {other} with args: {args:?}");
195+
Err(sys::GDEXTENSION_CALL_ERROR_INVALID_METHOD)
196+
}
194197
}
195198
}
196199

@@ -302,7 +305,7 @@ impl IScriptLanguageExtension for TestScriptLanguage {
302305
fn debug_get_stack_level_function(&self, _level: i32) -> GString { unreachable!() }
303306
fn debug_get_stack_level_locals(&mut self, _level: i32, _max_subitems: i32, _max_depth: i32) -> Dictionary { unreachable!() }
304307
fn debug_get_stack_level_members(&mut self, _level: i32, _max_subitems: i32, _max_depth: i32) -> Dictionary { unreachable!() }
305-
unsafe fn debug_get_stack_level_instance(&mut self, _level: i32) -> *mut c_void { unreachable!() }
308+
unsafe fn debug_get_stack_level_instance_rawptr(&mut self, _level: i32) -> *mut c_void { unreachable!() }
306309
fn debug_get_globals(&mut self, _max_subitems: i32,_max_depthh: i32) -> Dictionary { unreachable!() }
307310
fn debug_parse_stack_level_expression(&mut self, _level: i32, _expression: GString, _max_subitems: i32, _max_depth: i32) -> GString { unreachable!() }
308311
fn debug_get_current_stack_info(&mut self) -> Array<Dictionary> { unreachable!() }
@@ -314,8 +317,8 @@ impl IScriptLanguageExtension for TestScriptLanguage {
314317
fn get_public_annotations(&self) -> Array<Dictionary> { unreachable!() }
315318
fn profiling_start(&mut self) { unreachable!() }
316319
fn profiling_stop(&mut self) { unreachable!() }
317-
unsafe fn profiling_get_accumulated_data(&mut self, _info_array: *mut godot::classes::native::ScriptLanguageExtensionProfilingInfo, _info_max: i32) -> i32 { unreachable!() }
318-
unsafe fn profiling_get_frame_data(&mut self, _info_array: *mut godot::classes::native::ScriptLanguageExtensionProfilingInfo, _info_max: i32) -> i32 { unreachable!() }
320+
unsafe fn profiling_get_accumulated_data_rawptr(&mut self, _info_array: *mut godot::classes::native::ScriptLanguageExtensionProfilingInfo, _info_max: i32) -> i32 { unreachable!() }
321+
unsafe fn profiling_get_frame_data_rawptr(&mut self, _info_array: *mut godot::classes::native::ScriptLanguageExtensionProfilingInfo, _info_max: i32) -> i32 { unreachable!() }
319322
fn frame(&mut self) { unreachable!() }
320323
fn handles_global_class_type(&self, _type_: GString) -> bool { unreachable!() }
321324
fn get_global_class_name(&self, _path: GString) -> Dictionary { unreachable!() }

itest/rust/src/engine_tests/native_st_niche_audio_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct AudioEffectReceiverInstance {
4646

4747
#[godot_api]
4848
impl IAudioEffectInstance for AudioEffectReceiverInstance {
49-
unsafe fn process(
49+
unsafe fn process_rawptr(
5050
&mut self,
5151
_src_buffer: *const std::ffi::c_void,
5252
dst_buffer: *mut AudioFrame,
@@ -80,7 +80,7 @@ struct AudioEffectAsserterInstance {
8080

8181
#[godot_api]
8282
impl IAudioEffectInstance for AudioEffectAsserterInstance {
83-
unsafe fn process(
83+
unsafe fn process_rawptr(
8484
&mut self,
8585
src_buffer: *const std::ffi::c_void,
8686
_dst_buffer: *mut AudioFrame,

0 commit comments

Comments
 (0)