Files
clockwork-labs-bot a97c69c582 feat: Improve module backtraces for C# and C++ bindings
Mirror the backtrace improvements from PR #577 (Rust/TS) to the C# and
C++ module bindings so that stack traces captured by the host show
actual reducer/procedure/view function names instead of anonymous
wrappers.

C# changes:
- Add [MethodImpl(NoInlining)] to all generated Invoke() methods in
  reducer, procedure, and view dispatcher classes. This prevents the
  JIT/AOT from inlining these frames away, ensuring they appear as
  named WASM functions in wasmtime-captured backtraces.
- Add Log.Error() in the reducer error handler for consistency with
  procedures and views, which already log full exceptions.

C++ changes:
- Add __attribute__((noinline)) to user function definitions in all
  macro variants: SPACETIMEDB_REDUCER, SPACETIMEDB_REDUCER_NAMED,
  SPACETIMEDB_INIT, SPACETIMEDB_CLIENT_CONNECTED,
  SPACETIMEDB_CLIENT_DISCONNECTED, SPACETIMEDB_VIEW,
  SPACETIMEDB_VIEW_NAMED, SPACETIMEDB_PROCEDURE, and
  SPACETIMEDB_PROCEDURE_NAMED. This ensures the actual user function
  name appears as a distinct frame in WASM backtraces rather than
  being optimized into the surrounding lambda wrappers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 19:17:06 -04:00
..
2023-11-30 14:54:26 +00:00
2026-03-20 21:53:51 +00:00

SpacetimeDB.Runtime

This project contains the core SpacetimeDB SATS typesystem, attributes for the codegen as well as runtime bindings for SpacetimeDB WebAssembly modules.

The runtime bindings are currently implementing via Wasi.Sdk package, which is a .NET implementation of the WASI standard. This is likely to change in the future.

While not really documented, it allows to build raw WebAssembly modules with custom bindings as well, which is what we're using here. The process is somewhat complicated, but here are the steps:

  • bindings.c declares raw C bindings to the SpacetimeDB FFI imports and marks them with attributes like __attribute__((import_module("spacetime"), import_name("_insert"))) that make them WebAssembly imports. (unfortunately, function name duplication is currently unavoidable)
  • bindings.c implements a bunch of Mono-compatible wrappers that convert between Mono types and raw types expected by the SpacetimeDB FFI and invoke corresponding raw bindings.
  • Runtime.cs declares corresponding functions with compatible signatures for Mono-compatible wrappers to attach to. It marks them all with [MethodImpl(MethodImplOptions.InternalCall)].
  • bindings.c attaches all those Mono-compatible wrappers to their C# declarations in a mono_stdb_attach_bindings function.
  • bindings.c adds FFI-compatible exports that search for a method by assembly name, namespace, class name and a method name in the Mono runtime and invoke it. Those exports are marked with attributes like __attribute__((export_name("__call_reducer__"))) so that they're exported from Wasm by the linker.
  • Finally, bindings.c implements no-op shims for all the WASI APIs so that they're linked internally and not attempted to be imported from the runtime itself.

The result is a WebAssembly module FFI-compatible with SpacetimeDB and with no WASI imports, which is what we need.

Regenerating RawModuleDef

To regenenerate the Autogen folder, run:

cargo run -p spacetimedb-codegen --example regen-csharp-moduledef