Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

refactor(sdk): six hand-copied enums collapse to re-exports of their generated twins

yhh9b44382159d5
diff --git a/sdk/src/physics/PhysicsComponents.ts b/sdk/src/physics/PhysicsComponents.ts
index a5ca014cf..741082f3d 100644
--- a/sdk/src/physics/PhysicsComponents.ts
+++ b/sdk/src/physics/PhysicsComponents.ts
@@ -103,16 +103,12 @@ export interface ChainColliderData {
}
// =============================================================================
-// Body Type Enum (matches C++ BodyType)
+// Body Type Enum
// =============================================================================
-export const BodyType = {
- Static: 0,
- Kinematic: 1,
- Dynamic: 2
-} as const;
-
-export type BodyType = (typeof BodyType)[keyof typeof BodyType];
+// Single-sourced from the C++ BodyType ES_ENUM via the generated module — the
+// hand-restated `as const` copy this replaced could drift from C++.
+export { BodyType } from '../wasm.generated';
// =============================================================================
// Builtin Component Instances
diff --git a/sdk/src/ui/core/ui-node.ts b/sdk/src/ui/core/ui-node.ts
index de411aead..51b851684 100644
--- a/sdk/src/ui/core/ui-node.ts
+++ b/sdk/src/ui/core/ui-node.ts
@@ -16,25 +16,11 @@
import { defineBuiltin } from '../../component';
import { auto, px, type Dimension } from './dimension';
-/** Positioning scheme (mirrors the C++ UIPositionType enum). */
-export const UIPositionType = {
- /** In flex flow (default). */
- Relative: 0,
- /** Out of flow; placed by `inset` against the parent box — covers
- * anchor/stretch (the old RectTransform cases). */
- Absolute: 1,
-} as const;
-export type UIPositionType = (typeof UIPositionType)[keyof typeof UIPositionType];
-
-/** Per-item cross-axis alignment override (mirrors the C++ AlignSelf enum). */
-export const AlignSelf = {
- Auto: 0,
- Start: 1,
- Center: 2,
- End: 3,
- Stretch: 4,
-} as const;
-export type AlignSelf = (typeof AlignSelf)[keyof typeof AlignSelf];
+// Positioning scheme (Relative = in flex flow; Absolute = out of flow, placed by
+// `inset` against the parent box — covers the old RectTransform anchor/stretch
+// cases) and per-item cross-axis alignment override. Both single-sourced from
+// the C++ ES_ENUMs via the generated module.
+export { UIPositionType, AlignSelf } from '../../wasm.generated';
export interface UINodeData {
/** Relative (flex flow) or Absolute (placed by inset). */
diff --git a/sdk/src/ui/core/ui-visual.ts b/sdk/src/ui/core/ui-visual.ts
index cabfc9f6e..11994eba4 100644
--- a/sdk/src/ui/core/ui-visual.ts
+++ b/sdk/src/ui/core/ui-visual.ts
@@ -15,46 +15,19 @@
import { defineBuiltin } from '../../component';
import type { Color, Vec2, Vec4 } from '../../types';
-/** Draw mode (mirrors the C++ UIVisualType enum). */
-export const UIVisualType = {
- /** Invisible — present only for render-order/hit participation. */
- None: 0,
- /** Tinted white quad (no texture). */
- SolidColor: 1,
- /** Textured quad; uvOffset/uvScale select a sprite sub-region. */
- Image: 2,
- /** 9-slice via sliceBorder. */
- NineSlice: 3,
- /** Texture repeated every tileSize px of the box. */
- Tiled: 4,
- /** Texture cropped to fillAmount along fillMethod/fillOrigin. */
- Filled: 5,
-} as const;
-export type UIVisualType = (typeof UIVisualType)[keyof typeof UIVisualType];
-
-/** Fill mode for Filled visuals (mirrors the C++ UIFillMethod enum). */
-export const FillMethod = {
- /** Linear crop along X. */
- Horizontal: 0,
- /** Linear crop along Y. */
- Vertical: 1,
- /** Clockwise sweep of the full circle (cooldown / ring gauge). */
- Radial360: 2,
- /** Clockwise sweep of a 90° arc from the origin edge (corner meter). */
- Radial90: 3,
- /** Clockwise sweep of a 180° arc from the origin edge (speedometer). */
- Radial180: 4,
-} as const;
-export type FillMethod = (typeof FillMethod)[keyof typeof FillMethod];
-
-/** Fill anchor edge for Filled visuals (mirrors the C++ UIFillOrigin enum). */
-export const FillOrigin = {
- Left: 0,
- Right: 1,
- Bottom: 2,
- Top: 3,
-} as const;
-export type FillOrigin = (typeof FillOrigin)[keyof typeof FillOrigin];
+// Draw mode (None = invisible but render-order/hit-participating; SolidColor;
+// Image with a uvOffset/uvScale sub-region; NineSlice via sliceBorder; Tiled
+// every tileSize px; Filled = cropped to fillAmount along fillMethod/fillOrigin),
+// the Filled fill mode (linear X/Y crops; clockwise Radial360/90/180 sweeps) and
+// its anchor edge. All single-sourced from the C++ ES_ENUMs via the generated
+// module; FillMethod/FillOrigin keep their public names as aliases of the C++
+// UIFillMethod/UIFillOrigin.
+import {
+ UIVisualType,
+ UIFillMethod as FillMethod,
+ UIFillOrigin as FillOrigin,
+} from '../../wasm.generated';
+export { UIVisualType, FillMethod, FillOrigin };
export interface UIVisualData {
visualType: UIVisualType;
diff --git a/sdk/tests/cpp-contract.test.ts b/sdk/tests/cpp-contract.test.ts
index 1fdec638e..7841e0919 100644
--- a/sdk/tests/cpp-contract.test.ts
+++ b/sdk/tests/cpp-contract.test.ts
@@ -29,8 +29,8 @@ import { ENTITY_INDEX_BITS, ENTITY_GEN_BITS } from '../src/types';
import { TILE_ID_MASK, TILE_FLIP_H, TILE_FLIP_V, TILE_FLIP_D } from '../src/tilemap/tileBits';
import { CHUNK_SIZE } from '../src/tilemap/chunkCodec';
-// EHT-generated twins (the authoritative C++ ES_ENUM values) + the hand-copied
-// mirrors that still restate them in the UI/physics modules.
+// EHT-generated twins (the authoritative C++ ES_ENUM values) + the module-local
+// names the UI/physics modules publish them under.
import * as gen from '../src/wasm.generated';
import { BodyType } from '../src/physics/PhysicsComponents';
import { UIPositionType, AlignSelf } from '../src/ui/core/ui-node';
@@ -183,14 +183,12 @@ describe('C++ contract: UI base layer (renderer/plugins/UIElementPlugin.hpp)', (
});
});
-describe('C++ contract: hand-copied enum mirrors == their EHT-generated twin', () => {
- // These enums ARE generated from C++ ES_ENUM into wasm.generated.ts, but the
- // UI/physics modules also hand-restate them as `as const` objects (predating
- // the EHT enum generation). The generated copy is authoritative; the hand copy
- // is the drift risk. Guard the member map (name -> value) so a C++/EHT change
- // that updates the generated twin but not the hand copy goes RED here. (Full
- // collapse to a re-export is an API-shape change tracked separately.)
- const cases: Array<[string, Record<string, number>, Record<string, string | number>]> = [
+describe('C++ contract: module-published enums ARE their EHT-generated twin', () => {
+ // These used to be hand-restated `as const` copies in the UI/physics modules;
+ // they are now re-exports of the generated enums. Pin the object IDENTITY so a
+ // future edit can't silently reintroduce a copy — same-values-different-object
+ // is exactly the drift vector the re-export eliminated.
+ const cases: Array<[string, unknown, unknown]> = [
['BodyType', BodyType, gen.BodyType],
['UIPositionType', UIPositionType, gen.UIPositionType],
['AlignSelf', AlignSelf, gen.AlignSelf],
@@ -198,10 +196,9 @@ describe('C++ contract: hand-copied enum mirrors == their EHT-generated twin', (
['FillMethod', FillMethod, gen.UIFillMethod],
['FillOrigin', FillOrigin, gen.UIFillOrigin],
];
- for (const [label, hand, generated] of cases) {
- it(`${label} matches wasm.generated`, () => {
- expect(sortedEntries(new Map(Object.entries(hand))))
- .toEqual(sortedEntries(enumForward(generated)));
+ for (const [label, published, generated] of cases) {
+ it(`${label} is a re-export of the generated enum`, () => {
+ expect(published).toBe(generated);
});
}
});