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.

fix(aot): the differentials compile the C the way a build does

yhh16d036e5da7a
diff --git a/sdk/tests/aot-conformance.test.ts b/sdk/tests/aot-conformance.test.ts
index 93f91078e..355c29bfa 100644
--- a/sdk/tests/aot-conformance.test.ts
+++ b/sdk/tests/aot-conformance.test.ts
@@ -15,16 +15,14 @@
* agrees with itself.
*/
import { describe, it, expect } from 'vitest';
-import { spawnSync } from 'node:child_process';
-import { mkdtempSync, mkdirSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
-import { tmpdir } from 'node:os';
+import { mkdirSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { lowerProgram, brokenPromises } from '../../compiler/src/frontend';
import { builtinShapes } from '../../compiler/src/builtins';
import { inlineSystem } from '../../compiler/src/inline';
import { packLayout } from '../../compiler/src/abi';
-import { emitC, moduleDeclarations, WASM_LINK_FLAGS } from '../../compiler/src/codegen';
+import { emitC, moduleDeclarations } from '../../compiler/src/codegen';
import { emccPath } from '../../build-tools/utils/emscripten.js';
import { App } from '../src/app/app';
@@ -32,7 +30,7 @@ import { AppContext, setDefaultContext } from '../src/ecs/context';
import { setEditorMode, setPlayMode } from '../src/ecs/env';
import { createMockModule } from './mocks/wasm';
import { FakeEngine } from './fakeEngine';
-import { useBytesPlatform } from './helpers/aotFade';
+import { buildAotModule, useBytesPlatform } from './helpers/aotFade';
import { Schedule } from '../src/ecs/system';
import { Time } from '../src/ecs/resource';
import type { AotManifest } from '../src/ecs/aot/AotSystems';
@@ -61,21 +59,15 @@ function compileFixture(emcc: string): { wasm: Uint8Array; manifest: AotManifest
const inlined = lowered.module.systems.map((s) => inlineSystem(s, lowered.module.fns));
const c = emitC(lowered.module, packLayout(lowered.module.comps), inlined, 4);
- const dir = mkdtempSync(path.join(tmpdir(), 'estella-conf-'));
- mkdirSync(dir, { recursive: true });
- writeFileSync(path.join(dir, 'estella_abi.h'), c.header);
- writeFileSync(path.join(dir, 'estella_offsets.h'), c.offsets);
- writeFileSync(path.join(dir, 'sys.c'), c.source);
- const out = path.join(dir, 'sys.wasm');
- const built = spawnSync(emcc, [
- '-std=c11', '-O2', '-ffp-contract=off', '-Wall', '-Wextra',
- ...WASM_LINK_FLAGS, `-sEXPORTED_FUNCTIONS=${c.symbols.map((s) => `_${s}`).join(',')}`,
- '-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}`);
+ // Through the one spelling of "how the emitted C is compiled", so this road
+ // is built the way a shipped project's is rather than nearly that way.
+ const wasm = buildAotModule(emcc, c.source, c.symbols, {
+ 'estella_abi.h': c.header,
+ 'estella_offsets.h': c.offsets,
+ });
return {
- wasm: readFileSync(out),
+ wasm,
manifest: {
engineAbi: c.handshake.engineAbi,
projectShapes: c.handshake.projectShapes,
diff --git a/sdk/tests/aot-context.test.ts b/sdk/tests/aot-context.test.ts
index a81403db0..7da4d8ef9 100644
--- a/sdk/tests/aot-context.test.ts
+++ b/sdk/tests/aot-context.test.ts
@@ -17,9 +17,6 @@
* than a missing one.
*/
import { describe, it, expect } from 'vitest';
-import { spawnSync } from 'node:child_process';
-import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
-import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -30,7 +27,7 @@ import { AotResources } from '../src/ecs/aot/AotResources';
import type { Entity } from '../src/types';
import { emccPath } from '../../build-tools/utils/emscripten.js';
import { FakeEngine } from './fakeEngine';
-import { WASM_LINK_FLAGS } from '../../compiler/src/codegen';
+import { buildAotModule } from './helpers/aotFade';
const e = (n: number): Entity => n as unknown as Entity;
const N = 16;
@@ -104,19 +101,7 @@ void es_sys_Decay(es_addr_t ctx) {
}
`;
-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',
- ...WASM_LINK_FLAGS, '-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 buildModule = (): Uint8Array => buildAotModule(EMCC!, SYSTEM_C, 'es_sys_Decay');
const DEFAULTS = { remaining: 0, rate: 1 };
diff --git a/sdk/tests/aot-dispatch.test.ts b/sdk/tests/aot-dispatch.test.ts
index 95798c05f..2e9f08cd2 100644
--- a/sdk/tests/aot-dispatch.test.ts
+++ b/sdk/tests/aot-dispatch.test.ts
@@ -13,9 +13,6 @@
* pixels: same project, two runs, and nothing may differ.
*/
import { describe, it, expect } from 'vitest';
-import { spawnSync } from 'node:child_process';
-import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
-import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -34,7 +31,7 @@ import { engineAbiDigest, projectShapeDigest } from '../src/ecs/aot/abiDigest';
import type { Entity } from '../src/types';
import { emccPath } from '../../build-tools/utils/emscripten.js';
import { FakeEngine } from './fakeEngine';
-import { WASM_LINK_FLAGS } from '../../compiler/src/codegen';
+import { buildAotModule } from './helpers/aotFade';
const N = 20;
const FRAMES = 10;
@@ -76,19 +73,7 @@ void es_sys_Decay(es_addr_t ctx) {
}
`;
-function buildModule(): Uint8Array {
- const dir = mkdtempSync(path.join(tmpdir(), 'estella-dispatch-'));
- 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',
- ...WASM_LINK_FLAGS, '-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 buildModule = (): Uint8Array => buildAotModule(EMCC!, SYSTEM_C, 'es_sys_Decay');
/**
* The system the author wrote. The twin is held against THIS, so the C above is
diff --git a/sdk/tests/aot-install.test.ts b/sdk/tests/aot-install.test.ts
index 3894883e4..301649e01 100644
--- a/sdk/tests/aot-install.test.ts
+++ b/sdk/tests/aot-install.test.ts
@@ -18,9 +18,7 @@
* Loud skip without emsdk — the module here is really compiled.
*/
import { describe, it, expect } from 'vitest';
-import { spawnSync } from 'node:child_process';
-import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
-import { tmpdir } from 'node:os';
+import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -38,9 +36,10 @@ import type { AnyComponentDef } from '../src/ecs/component';
import type { WasmHeap } from '../src/ecs/WasmPoolMemory';
import type { Entity } from '../src/types';
import { emccPath } from '../../build-tools/utils/emscripten.js';
-import { WASM_LINK_FLAGS } from '../../compiler/src/codegen';
import { FakeEngine } from './fakeEngine';
-import { buildFadeModule, FADE_FIELDS, fadeManifest } from './helpers/aotFade';
+import {
+ buildAotModuleFile, buildFadeModule, FADE_FIELDS, fadeManifest, type AotModuleFile,
+} from './helpers/aotFade';
const N = 16;
@@ -93,19 +92,7 @@ function moduleFor(kind: keyof typeof HOSTS, built: { bytes: Uint8Array; path: s
return kind === 'a host that takes only a path' ? built.path : built.bytes;
}
-function buildModule(): { bytes: Uint8Array; path: string } {
- const dir = mkdtempSync(path.join(tmpdir(), 'estella-install-'));
- 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',
- ...WASM_LINK_FLAGS, '-sEXPORTED_FUNCTIONS=_es_sys_Fade',
- '-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 { bytes: readFileSync(out), path: out };
-}
+const buildModule = (): AotModuleFile => buildAotModuleFile(EMCC!, SYSTEM_C, 'es_sys_Fade');
const FADE_FIELDS = ['alpha', 'step'];
diff --git a/sdk/tests/helpers/aotFade.ts b/sdk/tests/helpers/aotFade.ts
index a317ddc27..a8ecf5f9f 100644
--- a/sdk/tests/helpers/aotFade.ts
+++ b/sdk/tests/helpers/aotFade.ts
@@ -13,7 +13,7 @@ import { mkdtempSync, mkdirSync, writeFileSync, readFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
-import { WASM_LINK_FLAGS } from '../../../compiler/src/codegen';
+import { CFLAGS, WASM_LINK_FLAGS } from '../../../compiler/src/codegen';
import { engineAbiDigest, projectShapeDigest } from '../../src/ecs/aot/abiDigest';
import { resourceMethodBit } from '../../src/ecs/resourceShapes';
import type { AotManifest } from '../../src/ecs/aot/AotSystems';
@@ -56,18 +56,53 @@ export const FADE_PROBE_ALPHA = -1;
export const FADE_FIELDS = ['alpha', 'step'];
-export function buildAotModule(emcc: string, source: string, symbol: string): Uint8Array {
+/**
+ * How the emitted C is compiled here: the BUILD's flags, plus a test's warnings.
+ *
+ * Four copies of this list had drifted from `CFLAGS`, all four missing
+ * `-fexcess-precision=standard`: a differential that compiles the C differently
+ * from a shipped project is a differential about another binary.
+ */
+const TEST_CFLAGS: readonly string[] = [...CFLAGS, '-Wall', '-Wextra'];
+
+/** A built module and where it landed, for a host that takes only a path. */
+export interface AotModuleFile {
+ bytes: Uint8Array;
+ path: string;
+}
+
+/**
+ * Compile one C source into a side module. `headers` are written beside it, for
+ * a source the compiler emitted with `#include`s of its own.
+ */
+export function buildAotModuleFile(
+ emcc: string,
+ source: string,
+ symbols: string | readonly string[],
+ headers: Readonly<Record<string, string>> = {},
+): AotModuleFile {
const dir = mkdtempSync(path.join(tmpdir(), 'estella-aot-'));
mkdirSync(dir, { recursive: true });
+ for (const [name, text] of Object.entries(headers)) writeFileSync(path.join(dir, name), text);
writeFileSync(path.join(dir, 'sys.c'), source);
const out = path.join(dir, 'sys.wasm');
+ const exported = (typeof symbols === 'string' ? [symbols] : symbols).map((s) => `_${s}`);
const built = spawnSync(emcc, [
- '-std=c11', '-O2', '-ffp-contract=off', '-Wall', '-Wextra',
- ...WASM_LINK_FLAGS, `-sEXPORTED_FUNCTIONS=_${symbol}`,
+ ...TEST_CFLAGS,
+ ...WASM_LINK_FLAGS, `-sEXPORTED_FUNCTIONS=${exported.join(',')}`,
'-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);
+ return { bytes: readFileSync(out), path: out };
+}
+
+export function buildAotModule(
+ emcc: string,
+ source: string,
+ symbols: string | readonly string[],
+ headers: Readonly<Record<string, string>> = {},
+): Uint8Array {
+ return buildAotModuleFile(emcc, source, symbols, headers).bytes;
}
export function buildFadeModule(emcc: string, source: string = FADE_C): Uint8Array {