Skip to content

Commit 3667d22

Browse files
committed
Test for varcall/ptrcall from GDScript, with relaxed conversions
1 parent 00aaa2b commit 3667d22

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

itest/godot/ManualFfiTests.gd

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ func test_export_dyn_gd_should_fail_for_wrong_type():
9898

9999
assert_fail("`DynGdExporter.second` should only accept NodeHealth and only if it implements `InstanceIdProvider` trait")
100100

101+
102+
# Test that relaxed conversions (Variant::try_to_relaxed) are used in both varcall/ptrcall.
103+
func test_ffi_relaxed_conversions_in_varcall_ptrcall():
104+
mark_test_pending()
105+
106+
# Enforce varcall by having untyped object, and ptrcall by object + arguments typed.
107+
var varcaller: Variant = ConversionTest.new()
108+
var ptrcaller: ConversionTest = ConversionTest.new()
109+
110+
var result1: String = ptrcaller.accept_f32(42)
111+
assert_eq(result1, "42", "ptrcall int->f32 should work with relaxed conversion")
112+
113+
var result2: String = ptrcaller.accept_i32(42.7)
114+
assert_eq(result2, "42", "ptrcall float->i32 should work with relaxed conversion")
115+
116+
var untyped_int: Variant = 42
117+
var result3 = varcaller.accept_f32(untyped_int)
118+
assert_eq(result3, "42", "varcall int->f32 should work with relaxed conversion")
119+
120+
var untyped_float: Variant = 42.7
121+
var result4 = varcaller.accept_i32(untyped_float)
122+
assert_eq(result4, "42", "varcall float->i32 should work with relaxed conversion")
123+
124+
# If we reach this point, all conversions succeeded.
125+
assert_eq(ConversionTest.successful_calls(), 4, "all calls should succeed with relaxed conversion")
126+
mark_test_succeeded()
127+
128+
101129
class MockObjGd extends Object:
102130
var i: int = 0
103131

@@ -454,4 +482,3 @@ func test_renamed_func_get_set():
454482
assert_eq(obj.f1(), 84)
455483

456484
obj.free()
457-

itest/rust/src/register_tests/conversion_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
*/
77

88
use godot::prelude::*;
9+
use std::sync::atomic::{AtomicI32, Ordering};
10+
11+
static SUCCESSFUL_CALLS: AtomicI32 = AtomicI32::new(0);
912

1013
#[derive(GodotClass)]
1114
#[class(init)]
@@ -15,21 +18,30 @@ struct ConversionTest {}
1518
impl ConversionTest {
1619
#[func]
1720
fn accept_i32(value: i32) -> String {
21+
SUCCESSFUL_CALLS.fetch_add(1, Ordering::SeqCst);
1822
value.to_string()
1923
}
2024

2125
#[func]
2226
fn accept_f32(value: f32) -> String {
27+
SUCCESSFUL_CALLS.fetch_add(1, Ordering::SeqCst);
2328
value.to_string()
2429
}
2530

2631
#[func]
2732
fn return_i32() -> i32 {
33+
SUCCESSFUL_CALLS.fetch_add(1, Ordering::SeqCst);
2834
123
2935
}
3036

3137
#[func]
3238
fn return_f32() -> f32 {
39+
SUCCESSFUL_CALLS.fetch_add(1, Ordering::SeqCst);
3340
123.45
3441
}
42+
43+
#[func]
44+
fn successful_calls() -> i32 {
45+
SUCCESSFUL_CALLS.load(Ordering::SeqCst)
46+
}
3547
}

0 commit comments

Comments
 (0)