Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "RawModuleDefV10.g.h"
#include "RawModuleDefV8.g.h"
#include "RawModuleDefV9.g.h"
#include "RawModuleDefV10.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
#include "RawTableDefV10.g.h"
#include "RawRowLevelSecurityDefV9.g.h"
#include "RawHttpRouteDefV10.g.h"
#include "RawModuleMountV10.g.h"

namespace SpacetimeDB::Internal {

SPACETIMEDB_INTERNAL_TAGGED_ENUM(RawModuleDefV10Section, SpacetimeDB::Internal::Typespace, std::vector<SpacetimeDB::Internal::RawTypeDefV10>, std::vector<SpacetimeDB::Internal::RawTableDefV10>, std::vector<SpacetimeDB::Internal::RawReducerDefV10>, std::vector<SpacetimeDB::Internal::RawProcedureDefV10>, std::vector<SpacetimeDB::Internal::RawViewDefV10>, std::vector<SpacetimeDB::Internal::RawScheduleDefV10>, std::vector<SpacetimeDB::Internal::RawLifeCycleReducerDefV10>, std::vector<SpacetimeDB::Internal::RawRowLevelSecurityDefV9>, SpacetimeDB::Internal::CaseConversionPolicy, SpacetimeDB::Internal::ExplicitNames, std::vector<SpacetimeDB::Internal::RawHttpHandlerDefV10>, std::vector<SpacetimeDB::Internal::RawHttpRouteDefV10>)
SPACETIMEDB_INTERNAL_TAGGED_ENUM(RawModuleDefV10Section, SpacetimeDB::Internal::Typespace, std::vector<SpacetimeDB::Internal::RawTypeDefV10>, std::vector<SpacetimeDB::Internal::RawTableDefV10>, std::vector<SpacetimeDB::Internal::RawReducerDefV10>, std::vector<SpacetimeDB::Internal::RawProcedureDefV10>, std::vector<SpacetimeDB::Internal::RawViewDefV10>, std::vector<SpacetimeDB::Internal::RawScheduleDefV10>, std::vector<SpacetimeDB::Internal::RawLifeCycleReducerDefV10>, std::vector<SpacetimeDB::Internal::RawRowLevelSecurityDefV9>, SpacetimeDB::Internal::CaseConversionPolicy, SpacetimeDB::Internal::ExplicitNames, std::vector<SpacetimeDB::Internal::RawHttpHandlerDefV10>, std::vector<SpacetimeDB::Internal::RawHttpRouteDefV10>, std::vector<SpacetimeDB::Internal::RawModuleMountV10>)
} // namespace SpacetimeDB::Internal
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

// This was generated using spacetimedb codegen.

#pragma once

#include <cstdint>
#include <string>
#include <vector>
#include <optional>
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"

// Forward declaration breaks the circular include chain:
// RawModuleMountV10 -> RawModuleDefV10 -> RawModuleDefV10Section -> RawModuleMountV10
namespace SpacetimeDB::Internal { struct RawModuleDefV10; }

namespace SpacetimeDB::Internal {

SPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawModuleMountV10) {
std::string namespace_; // renamed: 'namespace' is a C++ keyword
std::shared_ptr<SpacetimeDB::Internal::RawModuleDefV10> module; // shared_ptr breaks infinite-size recursion

void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {
::SpacetimeDB::bsatn::serialize(writer, namespace_);
if (module) ::SpacetimeDB::bsatn::serialize(writer, *module);
}
bool operator==(const RawModuleMountV10& o) const noexcept {
if (namespace_ != o.namespace_) return false;
if (module && o.module) return *module == *o.module;
return !module && !o.module;
}
bool operator!=(const RawModuleMountV10& o) const noexcept { return !(*this == o); }
};
} // namespace SpacetimeDB::Internal
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "Lifecycle.g.h"
#include "ProductType.g.h"
#include "Lifecycle.g.h"

namespace SpacetimeDB::Internal {

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 51 additions & 18 deletions crates/bindings-typescript/src/lib/autogen/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 38 additions & 1 deletion crates/bindings-typescript/src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import {
} from './algebraic_type';
import type {
CaseConversionPolicy,
ExplicitNames,
RawHttpHandlerDefV10,
RawHttpRouteDefV10,
RawLifeCycleReducerDefV10,
RawModuleMountV10,
RawModuleDefV10,
RawModuleDefV10Section,
RawProcedureDefV10,
RawReducerDefV10,
RawRowLevelSecurityDefV9,
RawScheduleDefV10,
RawScopedTypeNameV10,
RawTableDefV10,
RawTypeDefV10,
RawViewDefV10,
Typespace,
} from './autogen/types';
import type { UntypedIndex } from './indexes';
import type { UntypedTableDef } from './table';
Expand Down Expand Up @@ -42,6 +54,7 @@ export type TableNamesOf<S extends UntypedSchemaDef> = Values<
*/
export type UntypedSchemaDef = {
tables: Record<string, UntypedTableDef>;
namespaces?: Record<string, UntypedSchemaDef>;
};

/**
Expand Down Expand Up @@ -174,7 +187,20 @@ type CompoundTypeCache = Map<
>;

export type ModuleDef = {
[S in RawModuleDefV10Section as Uncapitalize<S['tag']>]: S['value'];
typespace: Typespace;
types: RawTypeDefV10[];
tables: RawTableDefV10[];
reducers: RawReducerDefV10[];
procedures: RawProcedureDefV10[];
views: RawViewDefV10[];
schedules: RawScheduleDefV10[];
lifeCycleReducers: RawLifeCycleReducerDefV10[];
httpHandlers: RawHttpHandlerDefV10[];
httpRoutes: RawHttpRouteDefV10[];
rowLevelSecurity: RawRowLevelSecurityDefV9[];
caseConversionPolicy: CaseConversionPolicy;
explicitNames: ExplicitNames;
mounts: RawModuleMountV10[];
};

type Section = RawModuleDefV10Section;
Expand All @@ -201,6 +227,7 @@ export class ModuleContext {
explicitNames: {
entries: [],
},
mounts: [],
};

get moduleDef(): ModuleDef {
Expand Down Expand Up @@ -259,9 +286,19 @@ export class ModuleContext {
value: module.caseConversionPolicy,
}
);
push(
module.mounts && {
tag: 'Mounts',
value: module.mounts,
}
);
return { sections };
}

addMount(mount: RawModuleMountV10) {
this.#moduleDef.mounts.push(mount);
}

/**
* Set the case conversion policy for this module.
* Called by the settings mechanism.
Expand Down
58 changes: 58 additions & 0 deletions crates/codegen/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,53 @@ impl<'opts> Cpp<'opts> {
writeln!(output, "}};").unwrap();
}

fn generate_raw_module_mount_v10_special(&self) -> String {
// RawModuleMountV10 is special for two reasons:
// 1. Its `namespace` field is a C++ keyword, renamed to `namespace_`.
// 2. It contains `RawModuleDefV10` which creates a circular include chain:
// RawModuleMountV10 → RawModuleDefV10 → RawModuleDefV10Section → RawModuleMountV10
// We break this with a forward declaration and shared_ptr (which only needs a declaration).
r#"// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.

// This was generated using spacetimedb codegen.

#pragma once

#include <cstdint>
#include <string>
#include <vector>
#include <optional>
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"

// Forward declaration breaks the circular include chain:
// RawModuleMountV10 -> RawModuleDefV10 -> RawModuleDefV10Section -> RawModuleMountV10
namespace SpacetimeDB::Internal { struct RawModuleDefV10; }

namespace SpacetimeDB::Internal {

SPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawModuleMountV10) {
std::string namespace_; // renamed: 'namespace' is a C++ keyword
std::shared_ptr<SpacetimeDB::Internal::RawModuleDefV10> module; // shared_ptr breaks infinite-size recursion

void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {
::SpacetimeDB::bsatn::serialize(writer, namespace_);
if (module) ::SpacetimeDB::bsatn::serialize(writer, *module);
}
bool operator==(const RawModuleMountV10& o) const noexcept {
if (namespace_ != o.namespace_) return false;
if (module && o.module) return *module == *o.module;
return !module && !o.module;
}
Comment on lines +269 to +273
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This fails compilation due to RawModuleDefV10 incomplete at this stage and dereferencing the pointer needs the complete type.

To get through this today we can do a light touch and just check the pointer, it's not as solid as the Rust/TypeScript that does a deeper check but this will compile and we'll have to do the C++ implementation in a future PR anyhow:

Suggested change
bool operator==(const RawModuleMountV10& o) const noexcept {
if (namespace_ != o.namespace_) return false;
if (module && o.module) return *module == *o.module;
return !module && !o.module;
}
bool operator==(const RawModuleMountV10& o) const noexcept {
return namespace_ == o.namespace_ && module == o.module;
}

bool operator!=(const RawModuleMountV10& o) const noexcept { return !(*this == o); }
};
} // namespace SpacetimeDB::Internal
"#
.to_string()
}

// Generate minimal sum type (TaggedEnum only)
fn write_sum_type(&self, output: &mut String, module: &ModuleDef, type_name: &str, sum: &SumTypeDef) {
// Special case: Generate proper tagged enum for RawIndexAlgorithm with data variants
Expand Down Expand Up @@ -529,6 +576,17 @@ impl Lang for Cpp<'_> {
}];
}

// Special handling for RawModuleMountV10:
// (1) its `namespace` field is a C++ keyword; (2) its `module` field creates a
// circular include chain through RawModuleDefV10 → RawModuleDefV10Section.
// We break both with a forward declaration and shared_ptr.
if name.to_string() == "RawModuleMountV10" {
return vec![OutputFile {
filename: format!("{name}.g.h"),
code: self.generate_raw_module_mount_v10_special(),
}];
}

self.write_standard_includes(&mut output);

// Add includes for dependencies
Expand Down
Loading
Loading