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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(bench): one way to build a component for embind, not three
esengine89977f9acddc
diff --git a/sdk/benchmarks/ui-layout.bench.ts b/sdk/benchmarks/ui-layout.bench.tsindex 28fa7f9e0..9086ad89c 100644--- a/sdk/benchmarks/ui-layout.bench.ts+++ b/sdk/benchmarks/ui-layout.bench.ts@@ -22,6 +22,7 @@ import type { ESEngineModule, Registry, Entity } from '../src/wasm/wasm.generateimport { WASM_DIR as WASM_DIR_SHARED } from '../tests/helpers/loadWasm';import { Transform, Canvas } from '../src/ecs/component';import { UINode } from '../src/ui/core/ui-node';+import { wasmData } from '../tests/helpers/wasmComponentData';let wasm: ESEngineModule;@@ -35,27 +36,10 @@ beforeAll(async () => {wasm = await engineMod.default({ locateFile: (p: string) => path.join(WASM_DIR, p) });});-/**- * A component in the shape embind takes, defaulted from the generated registry- * rather than spelled out here: embind rejects a partial struct, so a field added- * in C++ breaks this file at warmup unless the defaults come from one place.- * Colors are the one shape the two disagree on — authored r/g/b/a, bound as vec4.- */-function embindShape(def: Record<string, unknown>): Record<string, unknown> {- const out: Record<string, unknown> = {};- for (const [key, value] of Object.entries(def)) {- const c = value as { r?: number; g?: number; b?: number; a?: number };- out[key] = c && typeof c === 'object' && typeof c.r === 'number' && typeof c.a === 'number'- ? { x: c.r, y: c.g, z: c.b, w: c.a }- : value;- }- return out;-}--const TRANSFORM = embindShape(Transform._default);+const TRANSFORM = wasmData(Transform);const CANVAS = {- ...embindShape(Canvas._default),+ ...wasmData(Canvas),designResolution: { x: 800, y: 600 },pixelsPerUnit: 1, scaleMode: 0, matchWidthOrHeight: 0,};@@ -63,7 +47,7 @@ const CANVAS = {const px = (v: number) => ({ value: v, unit: 0 });const uiNode = (w: number, h: number) => ({- ...embindShape(UINode._default),+ ...wasmData(UINode),width: px(w), height: px(h),});diff --git a/sdk/benchmarks/wasm-boundary.bench.ts b/sdk/benchmarks/wasm-boundary.bench.tsindex 849aefdec..799a772f5 100644--- a/sdk/benchmarks/wasm-boundary.bench.ts+++ b/sdk/benchmarks/wasm-boundary.bench.ts@@ -5,7 +5,7 @@ import path from 'path';import { fileURLToPath } from 'url';import { Transform, Sprite } from '../src/ecs/component';import { UINode } from '../src/ui/core/ui-node';-import { convertForWasm } from '../src/ecs/bridge/BuiltinBridge';+import { wasmData } from '../tests/helpers/wasmComponentData';import { WASM_DIR as WASM_DIR_SHARED } from '../tests/helpers/loadWasm';let module: any;@@ -16,11 +16,6 @@ let Registry: any;// those is why these never found a wasm in CI.const WASM_DIR = WASM_DIR_SHARED;-// Build the embind value_object payloads from the authoritative component-// defaults (the same source BuiltinBridge inserts from), so these stay valid as-// the C++ structs gain fields instead of drifting into "missing field" errors.-const wasmData = (def: { _default: unknown; colorKeys: readonly string[] }) =>- convertForWasm({ ...(def._default as Record<string, unknown>) }, def.colorKeys);beforeAll(async () => {const jsPath = path.join(WASM_DIR, 'esengine.js');diff --git a/sdk/benchmarks/wasm-vs-world.bench.ts b/sdk/benchmarks/wasm-vs-world.bench.tsindex d40a088e1..604ed86ca 100644--- a/sdk/benchmarks/wasm-vs-world.bench.ts+++ b/sdk/benchmarks/wasm-vs-world.bench.ts@@ -4,7 +4,7 @@ import { describe, bench, beforeAll } from 'vitest';import { World } from '../src/ecs/world';import { defineComponent, Transform, Sprite, Camera } from '../src/ecs/component';import { RigidBody, BoxCollider, CircleCollider } from '../src/physics/PhysicsComponents';-import { convertForWasm } from '../src/ecs/bridge/BuiltinBridge';+import { wasmData } from '../tests/helpers/wasmComponentData';import type { CppRegistry, ESEngineModule } from '../src/wasm';import { loadWasmModule } from '../tests/helpers/loadWasm';@@ -14,12 +14,6 @@ beforeAll(async () => {wasmModule = await loadWasmModule();});-// The embind payloads are built from the components' own defaults — the same-// source BuiltinBridge inserts from. Hand-written copies of the C++ structs are-// what these used to be, and they had drifted: Camera's four viewport scalars-// became one Vec4 and every case touching it silently measured nothing.-const wasmData = (def: { _default: unknown; colorKeys: readonly string[] }) =>- convertForWasm({ ...(def._default as Record<string, unknown>) }, def.colorKeys);// The SDK-side shape (colors as r/g/b/a) is the component's default as authored;// the wasm one is that run through the same conversion the bridge uses.diff --git a/sdk/tests/helpers/wasmComponentData.ts b/sdk/tests/helpers/wasmComponentData.tsnew file mode 100644index 000000000..21fb9b0ff--- /dev/null+++ b/sdk/tests/helpers/wasmComponentData.ts@@ -0,0 +1,31 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file Component payloads for a direct embind call, built from the component's+ * own defaults.+ *+ * embind rejects a partial value_object, so anything calling `registry.addX`+ * needs every field the C++ struct declares. Writing them out by hand makes a+ * copy of the struct that nothing updates: the copy goes stale the next time a+ * field is added, `addX` throws at warmup, and vitest SKIPS a bench file that+ * throws while still exiting 0. That has now happened twice — Camera's four+ * viewport scalars becoming one Vec4, and Canvas gaining `layer` — and both+ * times the only thing that noticed was perf-guard's case count.+ *+ * So the defaults come from the registry and the conversion is+ * {@link convertForWasm}, the one BuiltinBridge itself inserts through. Which+ * fields are colors is read off the definition (`colorKeys`), not guessed from+ * the value's shape.+ */+import { convertForWasm } from '../../src/ecs/bridge/BuiltinBridge';++/** A component definition carrying the defaults and the color-field list. */+export interface WasmDataSource {+ readonly _default: unknown;+ readonly colorKeys: readonly string[];+}++/** The component's defaults in the shape embind takes. */+export function wasmData(def: WasmDataSource): Record<string, unknown> {+ return convertForWasm({ ...(def._default as Record<string, unknown>) }, def.colorKeys);+}