Skip to content

Conversation

@Bromeon
Copy link
Member

@Bromeon Bromeon commented Oct 28, 2025

For inbound (GDScript->Rust) calls done via ptrcall convention, the library used to panic on parameter conversions.

While ptrcalls typically don't fail as their type is already verified at the call site (e.g. in GDScript), there are still some edge cases:

  • user panics
  • enum values outside the valid range
  • narrowing integer conversions (not yet tested)

Except for user panics, the new implementation no longer causes a panic, and instead uses Result internally. This should be beneficial in panic=abort setups or for platforms which don't always have panics/exceptions enabled (e.g. Wasm).

Thanks a lot to @Houtamelo for bringing this to our attention! 👍


Implementation details

Ptrcalls look like this on the FFI level:

unsafe extern "C" fn ptrcall_fn(
    method_data: *mut std::ffi::c_void,
    instance_ptr: sys::GDExtensionClassInstancePtr,
    args_ptr: *const sys::GDExtensionConstTypePtr,
    ret: sys::GDExtensionTypePtr,
);

Godot pre-initializes the ret value: if it is not assigned by the extension binding (godot-rust), the default value for that type is used: null pointer for objects, 0 for numbers, identity for transforms, etc. So failing ptrcalls will not cause UB -- this PR also adds tests for this behavior.

@Bromeon Bromeon added quality-of-life No new functionality, but improves ergonomics/internals c: ffi Low-level components and interaction with GDExtension API labels Oct 28, 2025
@Bromeon Bromeon marked this pull request as ready for review October 28, 2025 11:22
@Bromeon Bromeon changed the title Verify that panicking ptrcalls return Godot default value Ptrcalls no longer panic on failed conversions, instead print Godot error and return default value Oct 28, 2025
@Bromeon Bromeon changed the title Ptrcalls no longer panic on failed conversions, instead print Godot error and return default value Failed ptrcalls: instead of panic, print error + return default Oct 28, 2025
@Bromeon Bromeon added this pull request to the merge queue Oct 28, 2025
Merged via the queue into master with commit 47aef2e Oct 28, 2025
20 checks passed
@Bromeon Bromeon deleted the qol/ptrcall-panics branch October 28, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c: ffi Low-level components and interaction with GDExtension API quality-of-life No new functionality, but improves ergonomics/internals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant