Skip to content

Commit e0b5c6e

Browse files
committed
Verify that marshalling errors cause failed *GDScript* function
1 parent 5789b22 commit e0b5c6e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

itest/godot/ManualFfiTests.gd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,44 @@ func test_renamed_func_get_set():
482482
assert_eq(obj.f1(), 84)
483483

484484
obj.free()
485+
486+
# -----------------------------------------------------------------------------------------------------------------------------------------------
487+
# Tests below verify the following:
488+
# Calling a typed Rust function with a Variant that cannot be converted to the Rust type will cause a failed function call on _GDScript_ side,
489+
# meaning the GDScript function aborts immediately. This happens because a `Variant -> T` conversion occurs dynamically *on GDScript side*,
490+
# before the Rust function is called.In contrast, panics inside the Rust function (e.g. variant.to::<T>()) just cause the *Rust* function to fail.
491+
#
492+
# Store arguments as Variant, as GDScript wouldn't parse script otherwise. Results in varcall being used.
493+
494+
func test_marshalling_fail_variant_type():
495+
# Expects Object, pass GString.
496+
var obj := ObjectTest.new()
497+
var arg: Variant = "not an object"
498+
obj.pass_object(arg)
499+
500+
assert_fail("GDScript function should fail after marshalling error (bad variant type)")
501+
502+
func test_marshalling_fail_non_null():
503+
# Expects Object, pass null.
504+
var obj := ObjectTest.new()
505+
obj.pass_object(null)
506+
507+
assert_fail("GDScript function should fail after marshalling error (required non-null)")
508+
509+
func test_marshalling_fail_integer_overflow():
510+
# Expects i32. This overflows.
511+
var obj := ObjectTest.new()
512+
var arg: Variant = 9223372036854775807
513+
obj.pass_i32(arg)
514+
515+
assert_fail("GDScript function should fail after marshalling error (int overflow)")
516+
517+
func test_marshalling_continues_on_panic():
518+
mark_test_pending()
519+
520+
# Expects i32. This overflows.
521+
var obj := ObjectTest.new()
522+
var result = obj.cause_panic()
523+
524+
assert_eq(result, Vector3.ZERO, "Default value returned on failed function call")
525+
mark_test_succeeded()

itest/rust/src/object_tests/object_test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,14 @@ pub mod object_test_gd {
10231023
// Forward compat: .upcast() here becomes a breaking change if we generalize AsArg to include derived->base conversions.
10241024
array![&Self::return_self().upcast()]
10251025
}
1026+
1027+
#[func]
1028+
fn pass_i32(&self, _i: i32) {}
1029+
1030+
#[func]
1031+
fn cause_panic(&self) -> Vector3 {
1032+
panic!("Rust panics")
1033+
}
10261034
}
10271035

10281036
// ----------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)