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.

feat(ecs): the SDK lays out a SysCtx and calls a compiled system

yhh3b0c3048c649
diff --git a/compiler/tests/pool-layout.test.ts b/compiler/tests/pool-layout.test.ts
index 7ea554fc4..d82b43f9b 100644
--- a/compiler/tests/pool-layout.test.ts
+++ b/compiler/tests/pool-layout.test.ts
@@ -23,6 +23,8 @@ import { builtinShapes } from '../src/builtins';
import { packLayout } from '../src/abi';
import { encBytes } from '../src/eir';
import { ScriptPool, poolShape, POOL_SLOT_BYTES } from '../../sdk/src/ecs/ScriptPool';
+import * as sdkAbi from '../../sdk/src/ecs/aot/AotContext';
+import { CMD_DESPAWN, CMD_REMOVE, CMD_WORDS, QUERYROWS_WORDS, SYSCTX_WORDS } from '../src/abi';
const FIXTURE = resolve(fileURLToPath(new URL('./fixtures/in-subset.ts', import.meta.url)));
@@ -69,3 +71,21 @@ describe('a script component has one layout, not two', () => {
}
});
});
+
+/**
+ * The SDK cannot import a build tool, so it keeps its own copy of the three
+ * struct sizes — and a contract with two authors is a contract with none. This
+ * is the seam that keeps them one number each.
+ */
+describe('the SDK and the compiler count the same words', () => {
+ it('SysCtx, QueryRows and Cmd', () => {
+ expect(sdkAbi.SYSCTX_WORDS).toBe(SYSCTX_WORDS);
+ expect(sdkAbi.QUERYROWS_WORDS).toBe(QUERYROWS_WORDS);
+ expect(sdkAbi.CMD_WORDS).toBe(CMD_WORDS);
+ });
+
+ it('and the command kinds, which are what a flush switches on', () => {
+ expect(sdkAbi.CMD_DESPAWN).toBe(CMD_DESPAWN);
+ expect(sdkAbi.CMD_REMOVE).toBe(CMD_REMOVE);
+ });
+});
diff --git a/sdk/src/ecs/aot/AotContext.ts b/sdk/src/ecs/aot/AotContext.ts
new file mode 100644
index 000000000..08899ae0c
--- /dev/null
+++ b/sdk/src/ecs/aot/AotContext.ts
@@ -0,0 +1,157 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file AotContext.ts
+ * @brief The host half of the ABI, in the language that owns the maps.
+ *
+ * @details docs/REARCH_AOT_ABI.md §2.1 gives the host four jobs: pack the rows,
+ * resolve the resource addresses, ready the command buffer, and fill a
+ * SysCtx. On web that host is TypeScript rather than C++, and not by
+ * preference — the query's candidate set and the script pools' slot
+ * maps are both here, so anything else would have to call back across
+ * the boundary once per entity per frame, which is the cost AOT exists
+ * to remove.
+ *
+ * Everything it writes goes into the ENGINE's linear memory, because
+ * that is where the compiled code reads. The arena is rebuilt per call:
+ * §2.2 gives a row array a one-call lifetime, and reusing the block
+ * rather than the contents is what keeps that free.
+ */
+
+import type { PoolBlock, PoolMemory } from '../ScriptPool';
+
+/**
+ * Sizes of the three structs, in 32-bit words: wasm32 is the only place this
+ * host runs, so an address is four bytes. The compiler is the author
+ * (`compiler/src/abi.ts`) and this is the SDK's copy, because a shipped engine
+ * may not import a build tool — `pool-layout.test.ts` holds the two together.
+ */
+export const SYSCTX_WORDS = 6;
+export const QUERYROWS_WORDS = 2;
+export const CMD_WORDS = 4;
+export const CMD_DESPAWN = 1;
+export const CMD_REMOVE = 2;
+
+/** One record the compiled code appended (§2.3). */
+export interface AotCommand {
+ readonly kind: number;
+ readonly a: number;
+}
+
+/** A row: the entity, then one address per component the query names. */
+export type AotRow = readonly number[];
+
+/**
+ * The per-call scratch, in engine memory. One instance per system is enough —
+ * a call is over before the next begins, and §2.2 says nothing may outlive it.
+ */
+export class AotContext {
+ private block: PoolBlock | null = null;
+ private words: Uint32Array = EMPTY;
+ private cmdCap: number;
+
+ constructor(private readonly memory: PoolMemory, cmdCap = 256) {
+ this.cmdCap = cmdCap;
+ }
+
+ /** Hand the block back; the arena is dead after this. */
+ dispose(): void {
+ if (this.block) this.memory.release(this.block);
+ this.block = null;
+ this.words = EMPTY;
+ }
+
+ /**
+ * Lay out one call and answer the ctx address.
+ *
+ * `queries[k]` is the rows of the k-th declared Query, in the order the
+ * system will walk them; `resources[j]` is the address of the j-th declared
+ * Res. Both are the caller's because both come from maps the caller owns.
+ */
+ build(queries: readonly (readonly AotRow[])[], resources: readonly number[]): number {
+ const rowWords = queries.reduce(
+ (n, rows) => n + rows.reduce((m, r) => m + r.length, 0), 0);
+ const need = SYSCTX_WORDS
+ + queries.length * QUERYROWS_WORDS
+ + rowWords
+ + resources.length
+ + 1 // the count, which the code writes back
+ + this.cmdCap * CMD_WORDS;
+ this.reserve_(need);
+
+ const base = this.block!.byteOffset;
+ const w = this.words;
+ // Laid out in one pass, each table after the last, so the addresses are
+ // known before anything that points at them is written.
+ let at = SYSCTX_WORDS;
+ const queryTable = at;
+ at += queries.length * QUERYROWS_WORDS;
+ const rowsAt: number[] = [];
+ for (const rows of queries) {
+ rowsAt.push(at);
+ for (const row of rows) {
+ for (const v of row) w[at++] = v;
+ }
+ }
+ const resTable = at;
+ for (const r of resources) w[at++] = r;
+ const countAt = at++;
+ w[countAt] = 0;
+ const cmdBuf = at;
+
+ queries.forEach((rows, k) => {
+ const slot = queryTable + k * QUERYROWS_WORDS;
+ w[slot] = base + rowsAt[k]! * 4;
+ w[slot + 1] = rows.length;
+ });
+ w[0] = base + queryTable * 4;
+ w[1] = base + resTable * 4;
+ w[2] = base + cmdBuf * 4;
+ w[3] = this.cmdCap;
+ w[4] = base + countAt * 4;
+ w[5] = 0; // events: no channel carries one yet
+ this.countAt_ = countAt;
+ this.cmdAt_ = cmdBuf;
+ return base;
+ }
+
+ /** What the call appended. Valid until the next `build`. */
+ commands(): AotCommand[] {
+ if (this.block === null) return [];
+ const w = this.view_();
+ const n = w[this.countAt_]!;
+ const out: AotCommand[] = [];
+ for (let i = 0; i < n; i++) {
+ const at = this.cmdAt_ + i * CMD_WORDS;
+ out.push({ kind: w[at]!, a: w[at + 1]! });
+ }
+ return out;
+ }
+
+ private countAt_ = 0;
+ private cmdAt_ = 0;
+
+ private reserve_(words: number): void {
+ if (this.block !== null && words * 4 <= this.block.byteLength) {
+ this.words = this.view_();
+ return;
+ }
+ const want = Math.max(words * 4 * 2, 1024);
+ const block = this.memory.alloc(want);
+ // After the allocation, never before: growing the heap detaches every
+ // view of it, including the one this is about to replace.
+ const old = this.block;
+ this.block = block;
+ this.words = this.view_();
+ if (old) this.memory.release(old);
+ }
+
+ /** The words of this arena, over whatever buffer the heap is in now. */
+ private view_(): Uint32Array {
+ const block = this.block!;
+ const buffer = this.memory.current?.(block) ?? block.buffer;
+ return new Uint32Array(buffer, block.byteOffset, block.byteLength >> 2);
+ }
+}
+
+const EMPTY = new Uint32Array(0);
diff --git a/sdk/tests/aot-context.test.ts b/sdk/tests/aot-context.test.ts
new file mode 100644
index 000000000..ce9bb717e
--- /dev/null
+++ b/sdk/tests/aot-context.test.ts
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file aot-context.test.ts
+ * @brief A compiled system, called by the SDK, over the SDK's own rows.
+ *
+ * @details Everything before this tested one link. This runs the chain: emcc
+ * builds the module, `ScriptPool` holds the components in the same
+ * linear memory, `AotContext` lays out the SysCtx there, and the
+ * exported symbol is called with it. What comes back is compared to the
+ * same loop written in TypeScript.
+ *
+ * The module imports the engine's memory and nothing else, so the
+ * memory here stands in for the engine's exactly.
+ *
+ * Loud skip without emsdk: a gate that never saw its subject is worse
+ * than a missing one.
+ */
+import { describe, it, expect } from 'vitest';
+import { spawnSync } from 'node:child_process';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+import { ScriptPool, poolShape } from '../src/ecs/ScriptPool';
+import { WasmPoolMemory, type WasmHeap } from '../src/ecs/WasmPoolMemory';
+import { AotContext, CMD_DESPAWN } from '../src/ecs/aot/AotContext';
+import type { Entity } from '../src/types';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
+const e = (n: number): Entity => n as unknown as Entity;
+const N = 16;
+const FRAMES = 8;
+
+function findEmcc(): string | null {
+ const at = path.join(ROOT, 'tools/emsdk/upstream/emscripten',
+ process.platform === 'win32' ? 'emcc.bat' : 'emcc');
+ return existsSync(at) ? at : null;
+}
+const EMCC = findEmcc();
+
+/**
+ * The engine, as far as this test needs one: a linear memory and a bump
+ * allocator over it. A real module differs in size, not in kind.
+ */
+class Engine implements WasmHeap {
+ // The module declares the minimum it expects; a host has to meet it.
+ readonly memory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
+ HEAPU8 = new Uint8Array(this.memory.buffer);
+ private next = 1024;
+ _malloc(size: number): number {
+ const at = this.next;
+ this.next += (size + 15) & ~15;
+ while (this.next > this.HEAPU8.byteLength) {
+ this.memory.grow(1);
+ this.HEAPU8 = new Uint8Array(this.memory.buffer);
+ }
+ return at;
+ }
+ _free(): void { /* a bump allocator frees nothing */ }
+}
+
+/**
+ * A hand-written system in the shape the compiler emits. Written out rather than
+ * taken from the corpus so the test says what it is testing; the corpus version
+ * is held to the same bytes by compiler/tests.
+ */
+const SYSTEM_C = `#include <stdint.h>
+#include <string.h>
+
+typedef uint32_t es_addr_t;
+#define ES_PTR(a) ((unsigned char *)(a))
+
+typedef struct EsQueryRows { es_addr_t rows; es_addr_t count; } EsQueryRows;
+typedef struct EsCmd { uint32_t kind, a, b, c; } EsCmd;
+typedef struct EsSysCtx {
+ es_addr_t queries, resources, cmdBuf, cmdCap, cmdCount, events;
+} EsSysCtx;
+
+static double ld(const unsigned char *p) { double v; memcpy(&v, p, 8); return v; }
+static void st(unsigned char *p, double v) { memcpy(p, &v, 8); }
+
+/* One Mut(Mover) query and one Res(Time): remaining -= delta, and despawn at 0. */
+void es_sys_Decay(es_addr_t ctx) {
+ const EsSysCtx *c = (const EsSysCtx *)ES_PTR(ctx);
+ const EsQueryRows *q = (const EsQueryRows *)ES_PTR(c->queries);
+ const es_addr_t *res = (const es_addr_t *)ES_PTR(c->resources);
+ unsigned char *time = ES_PTR(res[0]);
+ EsCmd *cmds = (EsCmd *)ES_PTR(c->cmdBuf);
+ uint32_t *count = (uint32_t *)ES_PTR(c->cmdCount);
+ uint32_t n = *count;
+ const es_addr_t *rows = (const es_addr_t *)ES_PTR(q[0].rows);
+ for (es_addr_t i = 0; i < q[0].count; ++i) {
+ const es_addr_t *row = rows + i * 2u;
+ unsigned char *m = ES_PTR(row[1]);
+ st(m, ld(m) - ld(time)); /* remaining is field 0 */
+ if (ld(m) <= 0.0 && n < (uint32_t)c->cmdCap) {
+ cmds[n].kind = 1u; cmds[n].a = (uint32_t)row[0];
+ cmds[n].b = 0u; cmds[n].c = 0u;
+ n += 1u;
+ }
+ }
+ *count = n;
+}
+`;
+
+function buildModule(): Uint8Array {
+ const dir = mkdtempSync(path.join(tmpdir(), 'estella-sdk-aot-'));
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(path.join(dir, 'sys.c'), SYSTEM_C);
+ const out = path.join(dir, 'sys.wasm');
+ const built = spawnSync(EMCC!, [
+ '-std=c11', '-O2', '-ffp-contract=off', '-Wall', '-Wextra',
+ '--no-entry', '-sSTANDALONE_WASM', '-sIMPORTED_MEMORY',
+ '-sERROR_ON_UNDEFINED_SYMBOLS=1', '-sEXPORTED_FUNCTIONS=_es_sys_Decay',
+ '-o', out, path.join(dir, 'sys.c'),
+ ], { encoding: 'utf8', cwd: dir, shell: process.platform === 'win32' });
+ if (built.status !== 0) throw new Error(`emcc failed:\n${built.stderr}`);
+ return readFileSync(out);
+}
+
+const DEFAULTS = { remaining: 0, rate: 1 };
+
+describe('a compiled system, called by the SDK', () => {
+ it('reports whether this gate could run at all', () => {
+ if (EMCC) console.log('[aot-context] built and ran a real module');
+ else console.warn('[aot-context] NO EMSDK — the chain did NOT run (pnpm emsdk:setup).');
+ expect(true).toBe(true);
+ });
+
+ it.skipIf(!EMCC)('moves the SDK\'s own rows, exactly as TypeScript does', async () => {
+ const engine = new Engine();
+ const memory = new WasmPoolMemory(engine);
+ const instance = await WebAssembly.instantiate(
+ new WebAssembly.Module(buildModule() as unknown as BufferSource),
+ { env: { memory: engine.memory } });
+ const api = instance.exports as unknown as Record<string, unknown>;
+ (api['_initialize'] as (() => void) | undefined)?.();
+ const decay = api['es_sys_Decay'] as (ctx: number) => void;
+
+ // The world: rows in the engine's memory, and the same values in a plain
+ // object per entity for TypeScript to walk.
+ const pool = new ScriptPool(poolShape(DEFAULTS)!, 8, memory);
+ const byHand = new Map<number, { remaining: number }>();
+ const live: number[] = [];
+ for (let i = 1; i <= N; i++) {
+ pool.put(e(i), DEFAULTS, { remaining: i * 0.05 });
+ byHand.set(i, { remaining: i * 0.05 });
+ live.push(i);
+ }
+
+ // Time is a host record too, so it needs an address like anything else.
+ const timeBlock = memory.alloc(8);
+ const delta = 1 / 30;
+ new Float64Array(engine.memory.buffer, timeBlock.byteOffset, 1)[0] = delta;
+
+ const ctx = new AotContext(memory);
+ for (let f = 0; f < FRAMES; f++) {
+ const rows = live.map((id) => [id, pool.address(e(id))!] as const);
+ const at = ctx.build([rows], [timeBlock.byteOffset]);
+ decay(at);
+
+ // The same loop, in TypeScript, over the plain objects.
+ const dead: number[] = [];
+ for (const id of live) {
+ const row = byHand.get(id)!;
+ row.remaining -= delta;
+ if (row.remaining <= 0) dead.push(id);
+ }
+
+ const commands = ctx.commands();
+ expect(commands.map((c) => c.a).sort((a, b) => a - b), `frame ${f} despawns`)
+ .toEqual(dead.sort((a, b) => a - b));
+ for (const c of commands) expect(c.kind).toBe(CMD_DESPAWN);
+
+ for (const id of live) {
+ expect(pool.get(e(id))!['remaining'], `frame ${f} entity ${id}`)
+ .toBe(byHand.get(id)!.remaining);
+ }
+ // The host applies the commands; the compiled code only records them.
+ for (const id of dead) {
+ pool.delete(e(id));
+ byHand.delete(id);
+ live.splice(live.indexOf(id), 1);
+ }
+ }
+
+ expect(live.length, 'nothing despawned, so the commands proved nothing')
+ .toBeLessThan(N);
+ expect(live.length, 'everything despawned, so the reads proved nothing')
+ .toBeGreaterThan(0);
+ ctx.dispose();
+ });
+
+ it.skipIf(!EMCC)('a call with no rows is a call, not a crash', async () => {
+ const engine = new Engine();
+ const memory = new WasmPoolMemory(engine);
+ const instance = await WebAssembly.instantiate(
+ new WebAssembly.Module(buildModule() as unknown as BufferSource),
+ { env: { memory: engine.memory } });
+ const api = instance.exports as unknown as Record<string, unknown>;
+ (api['_initialize'] as (() => void) | undefined)?.();
+
+ const timeBlock = memory.alloc(8);
+ new Float64Array(engine.memory.buffer, timeBlock.byteOffset, 1)[0] = 1 / 30;
+ const ctx = new AotContext(memory);
+ // An empty query still needs a well-formed table: `count` is zero and
+ // `rows` must point somewhere the code will not read.
+ (api['es_sys_Decay'] as (c: number) => void)(ctx.build([[]], [timeBlock.byteOffset]));
+ expect(ctx.commands()).toEqual([]);
+ });
+});