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
fix(aot): a compiled system that reads an event could never be loaded
esenginede7f37233161
diff --git a/compiler/src/abi.ts b/compiler/src/abi.tsindex f62f6470a..6042cdb98 100644--- a/compiler/src/abi.ts+++ b/compiler/src/abi.ts@@ -504,7 +504,16 @@ export function abiHandshake(// Scoped to the components the compiled systems NAME: adding an unrelated// component to a project must not invalidate a module that never reads it.const named = new Set<string>();- for (const p of plans) for (const q of p.queries) for (const a of q) named.add(a.comp);+ for (const p of plans) {+ // A reader's slot queries the EVENT, which `defineEvent<T>` erases, so+ // the other half of this digest cannot recompute one — counted here, it+ // refuses every module with a reader. The payload layout owes its own.+ const payloads = new Set((p.readers ?? []).map((r) => r.slot));+ p.queries.forEach((q, k) => {+ if (payloads.has(k)) return;+ for (const a of q) named.add(a.comp);+ });+ }// A project's own RESOURCE is a project shape; the ENGINE's is not, being// covered by `engineAbi`. Counting one twice puts it in a digest the// runtime computes without it.diff --git a/pipeline/tests/build-compiled-systems.test.ts b/pipeline/tests/build-compiled-systems.test.tsindex 8efa8591b..a92a619b6 100644--- a/pipeline/tests/build-compiled-systems.test.ts+++ b/pipeline/tests/build-compiled-systems.test.ts@@ -16,6 +16,7 @@ import { tmpdir } from 'node:os';import path from 'node:path';import { fileURLToPath, pathToFileURL } from 'node:url';import { buildCompiledSystems, readCompiledManifest } from '../src/bundle/buildCompiledSystems';+import { projectShapeDigest } from '../../sdk/src/ecs/aot/abiDigest';import { emccPath } from '../../build-tools/utils/emscripten.js';import { findHostCC, nativeModuleExt } from '../../compiler/src/hostCC';@@ -441,3 +442,36 @@ export const moveSystem = defineSystem(.toBe(a.manifest!.moduleContract);});});+++/**+ * A system that READS an event, built for real and handed to the runtime that+ * has to load it. Every test of a reader until now wrote its own manifest, and+ * wrote the projectShapes a runtime would compute rather than the one a build+ * produces — so a real reader module had never been loaded by anything.+ */+describe('a compiled system that reads an event', () => {+ const READER = `import { defineComponent, defineEvent, defineSystem, Query, Mut, EventReader } from 'esengine';+export const Mover = defineComponent('Mover', { speed: 100 });+export const Hit = defineEvent<{ amount: number }>('Hit');+/** @compiled */+export const absorb = defineSystem(+ [EventReader(Hit), Query(Mut(Mover))],+ (hits, query) => { for (const h of hits) { for (const [, m] of query) { m.speed += h.amount; } } },+ { name: 'Absorb' },+);+`;++ it.skipIf(!HOST_CC)('builds a manifest the runtime can recompute', async () => {+ const out = await buildCompiledSystems(project({ 'src/systems.ts': READER }),+ { mode: 'release', target: 'native', cc: HOST_CC, run });+ expect(out.errors).toEqual([]);+ expect(out.manifest!.systems[0]!.readers).toHaveLength(1);++ // Exactly what a runtime holding this project computes: its components, and+ // nothing that answers to an event's name. Counting `Hit` here refused every+ // reader module ever built, blaming a component that had not moved.+ expect(out.manifest!.projectShapes)+ .toBe(projectShapeDigest([{ name: 'Mover', fields: ['speed'] }]));+ });+});diff --git a/sdk/src/ecs/aot/AotSystems.ts b/sdk/src/ecs/aot/AotSystems.tsindex 998bce267..65441a133 100644--- a/sdk/src/ecs/aot/AotSystems.ts+++ b/sdk/src/ecs/aot/AotSystems.ts@@ -212,7 +212,12 @@ function scriptShapes(// what says whether that order is still the one here.out.push({ name: r.name, fields: [...(resourceFields(r.name) ?? [])] });}- for (const query of decl.queries) {+ // A reader's slot queries the EVENT it reads. Skipped on PURPOSE and+ // not by failing to resolve: both halves of a digest must range over+ // the same set. The payload layout still owes a mechanism of its own.+ const payloads = new Set((decl.readers ?? []).map((r) => r.slot));+ for (const [k, query] of decl.queries.entries()) {+ if (payloads.has(k)) continue;for (const arg of query) {if (seen.has(arg.comp)) continue;seen.add(arg.comp);diff --git a/sdk/tests/aot-native-install.test.ts b/sdk/tests/aot-native-install.test.tsindex a27125245..e593210dd 100644--- a/sdk/tests/aot-native-install.test.ts+++ b/sdk/tests/aot-native-install.test.ts@@ -21,7 +21,7 @@*/import { describe, it, expect } from 'vitest';import { World } from '../src/ecs/world';-import { defineComponent } from '../src/ecs/component';+import { defineComponent, getComponent } from '../src/ecs/component';import { defineSystem, SystemRunner } from '../src/ecs/system';import { defineEvent, EventReader, EventWriter, EventRegistry } from '../src/ecs/event';import { Query, Mut, Changed } from '../src/ecs/query';@@ -29,6 +29,7 @@ import { ResourceStorage } from '../src/ecs/resource';import { engineAbiDigest, projectShapeDigest } from '../src/ecs/aot/abiDigest';import type { AotManifest } from '../src/ecs/aot/AotSystems';import { installNativeAot, type NativeAotBindings } from '../src/ecs/aot/installNativeAot';+import { AotSystems } from '../src/ecs/aot/AotSystems';import type { WasmHeap } from '../src/ecs/WasmPoolMemory';/** Declared per test, not at module scope: the suite's setup resets the@@ -380,3 +381,35 @@ describe('a module and the sidecar written beside it', () => {expect(install(paired(PAIRED), undefined)).not.toThrow();});});+++/**+ * Both halves of `projectShapes` must range over the same set. A reader's slot+ * queries the EVENT it reads, which `defineEvent<T>` erases — left in on either+ * side alone, every module with a reader is refused, blaming a component that+ * never moved.+ */+describe('an event a reader names is not a component either half counts', () => {+ /** The event shares a real component's NAME, which is what makes this a test+ * and not a restatement: without the slot rule this side would resolve it+ * and count a shape the compiler did not. */+ function manifest(): AotManifest {+ return {+ engineAbi: engineAbiDigest(8),+ projectShapes: projectShapeDigest([{ name: 'Drift', fields: ['x'] }]),+ systems: [{+ name: 'AbsorbSystem', symbol: 'es_sys_Absorb', resources: [],+ queries: [[{ comp: 'Named', mut: false }], [{ comp: 'Drift', mut: true }]],+ readers: [{ slot: 0, event: 'Named', fields: ['amount'] }],+ }],+ };+ }++ it('is left out even where a component answers to its name', () => {+ declareDrift();+ defineComponent('Named', { amount: 0 });+ const m = manifest();+ expect(() => new AotSystems().install(+ m, { es_sys_Absorb: () => {} }, (name) => getComponent(name), () => [], 8)).not.toThrow();+ });+});