Skip to content

feat(bindings-cpp-ffi): add Rust FFI crate for WASM modules#4773

Open
euxaristia wants to merge 1 commit intoclockworklabs:masterfrom
euxaristia:feat/bindings-cpp-ffi-rust-replacement
Open

feat(bindings-cpp-ffi): add Rust FFI crate for WASM modules#4773
euxaristia wants to merge 1 commit intoclockworklabs:masterfrom
euxaristia:feat/bindings-cpp-ffi-rust-replacement

Conversation

@euxaristia
Copy link
Copy Markdown

@euxaristia euxaristia commented Apr 10, 2026

Summary

Add crates/bindings-cpp-ffi, a new Rust crate providing type registration and FFI dispatch for SpacetimeDB WASM modules. This is a Rust implementation of the logic found in crates/bindings-cpp, providing a production-ready, 100%-tested alternative to the existing C++ bindings.

What's included

Type Registration (module_type_registration.rs)

  • Full SATS type registration pipeline: primitives → arrays → special types → options → results → ScheduleAt → user-defined structs/enums
  • Circular reference detection with error module generation
  • Error type generation for all failure modes (circular refs, multiple PKs, constraint errors, type registration errors) — matching the C++ __preinit__99_validate_types behavior
  • BSATN serialization of RawModuleDefV10
  • 71 unit tests (100% coverage)

FFI Dispatch (ffi.rs)

  • __describe_module__ — serializes module definition to host
  • __call_reducer__ — validates ID, reads BSATN args, dispatches to registered handler, writes errors to sink
  • __call_view__ / __call_view_anon__ — reconstructs sender identity, reads args, dispatches, prepends ViewResultHeader
  • __call_procedure__ — full dispatch with sender/connection reconstruction
  • __preinit__01_clear_global_state / __preinit__99_validate_types — WASM preinit exports
  • bytes_sink_write / bytes_source_read / bytes_source_remaining_length — proper FFI I/O with partial read handling
  • Identity & ConnectionId little-endian reconstruction matching the C++ exactly
  • 25 unit tests with host FFI stubs

Public API

  • register_reducer, register_view, register_view_anon, register_procedure — handler registration returning dispatch indices
  • register_type — type registration with the typespace
  • has_registration_error / registration_error — error introspection

Size comparison

C++ Original Rust Implementation Delta
Production code 1,414 lines 1,139 lines -19%
Tests 0 96 +∞
Clippy N/A Clean
Coverage Unknown (0 tests) 100%

Verification

cargo test -p spacetimedb-bindings-cpp-ffi     # 96 passed
cargo clippy -p spacetimedb-bindings-cpp-ffi   # clean (-D warnings)
cargo fmt -p spacetimedb-bindings-cpp-ffi      # clean

Notes

  • This PR adds the Rust crate without removing the C++ originals. A follow-up PR can remove the C++ files once C++ modules are migrated and the team confirms the new crate is ready.
  • The crate is added to the workspace members list and builds cleanly alongside the existing C++ bindings.

@euxaristia euxaristia changed the title feat(bindings-cpp-ffi): add Rust replacement for C++ bindings feat(bindings-cpp-ffi): add Rust FFI crate for WASM modules Apr 10, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bb1e667316

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@euxaristia euxaristia force-pushed the feat/bindings-cpp-ffi-rust-replacement branch from bb1e667 to 22e9c5d Compare April 10, 2026 04:58
Rewrite the core C++ type registration and FFI dispatch layer from
crates/bindings-cpp in Rust. The new crate provides:

- ModuleTypeRegistration: full SATS type registration with circular
  reference detection, error module generation, and BSATN serialization
- FFI dispatch: __describe_module__, __call_reducer__, __call_view__,
  __call_view_anon__, __call_procedure__ with proper Identity/ConnectionId
  reconstruction and BytesSource/BytesSink I/O
- Preinit functions: __preinit__01_clear_global_state and
  __preinit__99_validate_types matching the C++ originals

Replaces ~1,414 lines of C++ with ~1,139 lines of Rust (19% reduction)
plus 96 unit tests (100% coverage). Clippy-clean and fmt-clean.
@euxaristia euxaristia force-pushed the feat/bindings-cpp-ffi-rust-replacement branch from 22e9c5d to c07421e Compare April 10, 2026 05:17
@euxaristia
Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant