mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-05-10 09:40:23 -04:00
14f79910ee
# Description of Changes
- Migrated the C++ module-definition assembly path to V10-first
internals:
- Added v10_builder and module_type_registration systems.
- Switched Module::__describe_module__ to serialize RawModuleDef with
V10 payload.
- Updated macro registration pipeline to register through V10
- Added explicit naming support across macro surface (*_NAMED variants
for reducer/procedure/
view and field/index macros).
- Reworked multi-column index macros (FIELD_MultiColumnIndex,
FIELD_MultiColumnIndex_NAMED) with
migration alias.
- Added SPACETIMEDB_SETTING_CASE_CONVERSION(...) to support case
conversion policy
- Error-path hardening by adding explicit constraint-registration error
tracking and preinit validation
- Codegen updates:
- Updated C++ moduledef regen to V10 builder types.
- Adjusted C++ codegen duplicate-variant wrapper generation to emit
proper product-type
wrappers.
- Test/harness updates:
- type-isolation-test runner now defaults to focused V10 regression
checks; --v9 runs broader
legacy/full suite.
- Added focused modules for positive/negative V10 checks:
- test_multicolumn_index_valid
- error_multicolumn_missing_field
- error_default_missing_field
- Re-enabled C++ paths in sdks/rust/tests/test.rs procedure/view/test
suites.
# API and ABI breaking changes
- Refactor of the underlying module definition
- New *_NAMED variant macros for explicit canonical naming
- FIELD_NamedMultiColumnIndex renamed to FIELD_MultiColumnIndex
# Expected complexity level and risk
3 - Large set of changes moving over to V10 with underlying changes to
make future updates a little easier
# Testing
- [x] Ran the type isolation test and expanded it
- [x] Ran the spacetimedb-sdk test framework to confirm no more drift
between C++ and other module languages
- [x] Ran Unreal test suite though not really applicable
- [x] New app creation with `spacetime init --template basic-cpp`
- [x] Ran describe module tests against Rust + C# matching with C++ on
the /modules/sdk-test* modules to find any possible mis-alignment
# Review
- [x] Another look at the new features with C++
- [x] Thoughts on *_NAMED macros, I couldn't come up with a better
solution with C++20
SpacetimeDB C++ Module Library Internal API
This directory contains the internal implementation of the SpacetimeDB C++ Module Library, matching the design of the C# bindings.
Overview
The Internal API provides:
- Module Definition Management: Automatic generation of module definitions using autogenerated types
- Type Registration: Type-safe registration system for tables and reducers
- FFI Wrappers: Safe wrappers around the SpacetimeDB ABI
- Table Operations: Type-safe CRUD operations on tables
- Reducer System: Framework for implementing reducers with proper serialization
Directory Structure
-
autogen/- Autogenerated types from Rust definitions (RawModuleDefV9, RawTableDefV9, etc.) -
Module.h/cpp- Main module management and registration -
FFI.h/cpp- Foreign Function Interface definitions
Key Components
1. Module Registration
The Module class is a singleton that manages:
- Table registration
- Reducer registration
- Type registration
- Module description generation
- Reducer invocation
2. Autogenerated Types
All module definition types are generated from the Rust codebase:
RawModuleDefV9- Main module definitionRawTableDefV9- Table definitionsRawReducerDefV9- Reducer definitionsTableAccess,TableType, etc. - Enums and supporting types
3. Table Implementation
Tables are implemented using the SPACETIMEDB_TABLE macro system:
- Define row types with field registration using
SPACETIMEDB_MODULE_STRUCT(modules) orSPACETIMEDB_STRUCT(clients) - Register tables with
SPACETIMEDB_TABLE(Type, "name", Access, ...constraints) - Use
ctx.db.table<T>("name")for table operations (insert, delete) - Tables are automatically registered during module initialization via
__preinit__functions
4. Reducer Implementation
Reducers are implemented using unified macro system:
SPACETIMEDB_REDUCER(name, ReducerContext ctx, ...params)- User-defined reducersSPACETIMEDB_INIT(name, ReducerContext ctx)- Module initializationSPACETIMEDB_CLIENT_CONNECTED(name, ReducerContext ctx)- Client connection handlerSPACETIMEDB_CLIENT_DISCONNECTED(name, ReducerContext ctx)- Client disconnection handler- The macros handle serialization, registration, and ABI integration automatically
Usage Example
// Define row type with field registration
struct Person {
uint32_t id;
std::string name;
uint8_t age;
};
SPACETIMEDB_MODULE_STRUCT(Person, id, name, age);
// Define table with constraints
SPACETIMEDB_TABLE(Person, "people", Public,
PrimaryKeyAutoInc(id),
Index(name)
);
// Define reducer with ReducerContext
SPACETIMEDB_REDUCER(add_person, ReducerContext ctx, std::string name, uint8_t age) {
// Access table and insert record
Person person{0, name, age}; // id will be auto-generated
ctx.db.table<Person>("people").insert(person);
LOG_INFO("Added person: " + name);
}
// Lifecycle reducers
SPACETIMEDB_INIT(init, ReducerContext ctx) {
LOG_INFO("Module initialized!");
return Ok();
}
SPACETIMEDB_CLIENT_CONNECTED(on_connect, ReducerContext ctx) {
LOG_INFO("Client connected!");
return Ok();
}
Migration from Old API
See MIGRATION_GUIDE.md for detailed steps on migrating from the manual module definition approach to this new Internal API.
Future Improvements
- Automatic BSATN serialization generation
- Macro-based table/reducer definitions
- Query builder API
- Index management