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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @file emscripten-wrapper.test.ts
* @brief Diagnostic: verify Object.entries / getter-setter / world.insert
* behaviour for Emscripten value-object reference wrappers, exercised
* through UINode's nested `Dimension` structs (the UIRect anchor
* structs this used to probe were retired in REARCH_GUI F3).
*/
import { describe, it, expect, beforeAll } from 'vitest';
import { App } from '../src/app/app';
import { UINode, type UINodeData } from '../src/ui/core/ui-node';
import type { ESEngineModule, CppRegistry } from '../src/wasm';
import { loadWasmModule, HAS_WASM } from './helpers/loadWasm';
/** px / auto Dimension literals (unit 0 = Px, 2 = Auto). */
const px = (v: number) => ({ value: v, unit: 0 });
const auto = () => ({ value: 0, unit: 2 });
/** A complete UINode value-object (embind requires every member present). */
function fullNode(over: Partial<UINodeData> = {}): UINodeData {
return {
position: 0,
display: 0,
opacity: 1,
pointerEvents: 0,
width: auto(), height: auto(),
minWidth: auto(), minHeight: auto(),
maxWidth: auto(), maxHeight: auto(),
flexGrow: 0, flexShrink: 1, flexBasis: auto(),
alignSelf: 0,
marginLeft: px(0), marginTop: px(0), marginRight: px(0), marginBottom: px(0),
insetLeft: auto(), insetTop: auto(), insetRight: auto(), insetBottom: auto(),
...over,
} as UINodeData;
}
describe.skipIf(!HAS_WASM)('Emscripten wrapper diagnostics', () => {
let module: ESEngineModule;
beforeAll(async () => {
module = await loadWasmModule();
});
it('Object.entries on Emscripten getUINode reference wrapper', () => {
const registry = new module.Registry() as unknown as CppRegistry;
const entity = registry.create();
registry.addUINode(entity, fullNode({ width: px(50), height: px(50) }) as never);
const ref = registry.getUINode(entity);
// The nested Dimension wrapper must read back via its getter.
expect(ref.width.value).toBeCloseTo(50, 5);
expect(ref.width.unit).toBe(0);
expect(ref.height.value).toBeCloseTo(50, 5);
// Object.* introspection on the embind reference wrapper must not throw.
expect(() => Object.entries(ref as unknown as object)).not.toThrow();
expect(() => Object.keys(ref as unknown as object)).not.toThrow();
expect(() => Object.getOwnPropertyNames(ref as unknown as object)).not.toThrow();
registry.destroy(entity);
(registry as unknown as { delete(): void }).delete();
});
it('world.insert with Emscripten wrapper preserves values', () => {
const app = App.new();
const registry = new module.Registry() as unknown as CppRegistry;
app.connectCpp(registry, module);
const world = app.world;
const entity = world.spawn();
world.insert(entity, UINode, fullNode());
const ref = world.get(entity, UINode) as UINodeData;
// Modify nested struct fields via the wrapper setters.
ref.width = px(120);
ref.height = px(64);
ref.position = 1;
expect(ref.width.value).toBeCloseTo(120, 5);
// Re-insert the wrapper and read back: values must survive the round-trip.
world.insert(entity, UINode, ref);
const after = world.get(entity, UINode) as UINodeData;
expect(after.width.value).toBeCloseTo(120, 5);
expect(after.width.unit).toBe(0);
expect(after.height.value).toBeCloseTo(64, 5);
expect(after.position).toBe(1);
world.despawn(entity);
world.disconnectCpp();
(registry as unknown as { delete(): void }).delete();
});
it('nested field modification on Emscripten wrapper', () => {
const registry = new module.Registry() as unknown as CppRegistry;
const entity = registry.create();
registry.addUINode(entity, fullNode({ width: px(200), height: px(200) }) as never);
const ref = registry.getUINode(entity);
// Mutating a nested member in place writes to a COPY, not C++ memory…
ref.width.value = 0.7;
// …whereas assigning a whole Dimension struct writes through.
ref.width = px(140);
expect(ref.width.value).toBeCloseTo(140, 5);
expect(ref.width.unit).toBe(0);
registry.destroy(entity);
(registry as unknown as { delete(): void }).delete();
});
});