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(contract): every esengine subpath has one meaning in a native game

yhh165db34732b6
diff --git a/tools/check-native-script-modules.mjs b/tools/check-native-script-modules.mjs
new file mode 100644
index 000000000..2cea33e68
--- /dev/null
+++ b/tools/check-native-script-modules.mjs
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file check-native-script-modules.mjs — the SDK's module vocabulary and the
+ * native exporter's answer for it are one list, checked both ways.
+ *
+ * `sdk/package.json`'s `exports` is what a project may import. Every one of them
+ * needs a disposition in tools/nativeScriptModules.mjs, and every disposition
+ * needs an export still standing behind it. Either half alone rots: a list of
+ * excuses nobody prunes, or a rule that silently stops covering what it names.
+ *
+ * The failure this exists to prevent is specific. The exporter matched
+ * `/^esengine(\/.*)?$/` and handed every match the core global, so a subpath
+ * meant "esengine" and its own exports were undefined — found on a phone, four
+ * frames into a packaged game, as `cannot read property '_id' of undefined`.
+ * A new `./navmesh` would have been swallowed exactly the same way.
+ *
+ * node tools/check-native-script-modules.mjs
+ */
+import { readFileSync, existsSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { MODULES, DISPOSITIONS, NATIVE_MODULE_REGISTRY, subpathOf, specifierOf, nativeSubpaths }
+ from './nativeScriptModules.mjs';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const PKG = path.join(ROOT, 'sdk', 'package.json');
+const NATIVE_ENTRY = path.join(ROOT, 'sdk', 'src', 'index.native.ts');
+
+const problems = [];
+const pkg = JSON.parse(readFileSync(PKG, 'utf8'));
+const published = Object.keys(pkg.exports ?? {});
+
+// 1. Every published subpath has an answer. No default: a new export is RED
+// until somebody decides what it means to a packaged native game.
+for (const subpath of published) {
+ const specifier = specifierOf(subpath);
+ if (!MODULES[specifier]) {
+ problems.push(
+ `sdk/package.json publishes "${subpath}" and tools/nativeScriptModules.mjs has no `
+ + `disposition for "${specifier}".\n`
+ + ` Say what a natively packaged game script gets when it imports it — `
+ + `${Object.keys(DISPOSITIONS).join(', ')}.`);
+ }
+}
+
+// 2. Every disposition still names something the SDK publishes.
+for (const specifier of Object.keys(MODULES)) {
+ const subpath = subpathOf(specifier);
+ if (!published.includes(subpath)) {
+ problems.push(
+ `"${specifier}" has a disposition but sdk/package.json no longer publishes `
+ + `"${subpath}" — the rule outlived the module it was written for.`);
+ }
+}
+
+// 3. A class that owes a reason gives one.
+for (const [specifier, m] of Object.entries(MODULES)) {
+ const klass = DISPOSITIONS[m.disposition];
+ if (!klass) {
+ problems.push(`"${specifier}" claims unknown disposition "${m.disposition}".`);
+ continue;
+ }
+ if (m.disposition === 'forbidden-native-script' && !m.why) {
+ problems.push(
+ `"${specifier}" is forbidden to a native game script and says no why. `
+ + `A refusal without a reason is the kind of rule the next person deletes.`);
+ }
+}
+
+// 5. Every native-subpath has a namespace in the registry the native entry
+// installs. Skipped with a word — not silently — while that entry is being
+// written, since a check that cannot see its subject must say so.
+const subs = nativeSubpaths();
+if (existsSync(NATIVE_ENTRY)) {
+ const entry = readFileSync(NATIVE_ENTRY, 'utf8');
+ if (!entry.includes(NATIVE_MODULE_REGISTRY)) {
+ console.log(`check-native-script-modules: sdk/src/index.native.ts does not install `
+ + `${NATIVE_MODULE_REGISTRY} yet — ${subs.length} native-subpath namespace(s) unchecked.`);
+ } else {
+ for (const specifier of subs) {
+ if (!entry.includes(`'${specifier}'`) && !entry.includes(`"${specifier}"`)) {
+ problems.push(
+ `"${specifier}" is a native-subpath and ${NATIVE_MODULE_REGISTRY} does not `
+ + `publish a namespace for it — a game importing it would resolve to nothing.`);
+ }
+ }
+ }
+}
+
+if (problems.length) {
+ console.error('check-native-script-modules: the module vocabulary and its native answer disagree.\n');
+ for (const p of problems) console.error(` ✗ ${p}\n`);
+ process.exit(1);
+}
+
+const counts = {};
+for (const m of Object.values(MODULES)) counts[m.disposition] = (counts[m.disposition] ?? 0) + 1;
+console.log(`check-native-script-modules: ${published.length} published specifier(s), each with a `
+ + `disposition — ${Object.entries(counts).map(([k, v]) => `${v} ${k}`).join(', ')}.`);
diff --git a/tools/gates.mjs b/tools/gates.mjs
index bb31a7326..e5ef15f81 100644
--- a/tools/gates.mjs
+++ b/tools/gates.mjs
@@ -183,6 +183,9 @@ export const GATES = [
// …and that whoever mints one can say how it survives a device generation:
// recovery only ever holds a number, so the answer has to exist before it runs.
{ id: 'mesh-provenance', run: 'node tools/check-mesh-provenance.mjs' },
+ // Every `esengine/*` specifier the SDK publishes has one answer for a script
+ // inside a packaged native game, and a subpath never means the bare specifier.
+ { id: 'native-script-modules', run: 'node tools/check-native-script-modules.mjs' },
{ id: 'shader-blocks', run: 'node tools/check-shader-blocks.mjs' },
{ id: 'wgsl-twin', run: 'node tools/check-wgsl-twin.mjs' },
{
diff --git a/tools/nativeScriptModules.mjs b/tools/nativeScriptModules.mjs
new file mode 100644
index 000000000..8805f935c
--- /dev/null
+++ b/tools/nativeScriptModules.mjs
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file nativeScriptModules.mjs — what each `esengine/*` specifier MEANS to a
+ * script inside a natively packaged game.
+ *
+ * A native build has no module loader, so the exporter rewrites every `esengine`
+ * import to something the host already evaluated. It did that with one rule:
+ *
+ * /^esengine(\/.*)?$/ -> globalThis.ESEngine
+ *
+ * which reads "esengine, and also anything under it" — four distinct modules
+ * collapsed onto the core namespace. The core does not carry their exports, so
+ * `import { Physics3D } from 'esengine/physics3d'` produced `undefined`, `Res`
+ * stored it without looking (a descriptor holds the reference; nothing reads
+ * `_id` until a frame resolves it), and the game threw
+ * `cannot read property '_id' of undefined` every frame it ran on a device.
+ * 49 exports across the four subpaths are unreachable that way; `Physics3D` is
+ * merely the one that crashed instead of being quietly undefined.
+ *
+ * So a specifier is not matched, it is LOOKED UP. Each one carries a class, and
+ * each class owes an obligation a gate can check. There is deliberately NO
+ * default: an export added to sdk/package.json with no disposition here fails
+ * the build rather than inheriting a guess, which is the whole failure this
+ * file exists to make impossible.
+ */
+
+/**
+ * The registry a native game script resolves a subpath through — installed as a
+ * non-enumerable global by the native entry, holding namespaces from the
+ * ALREADY-RUNNING graph. A second copy would mint a second `Physics3D`, and
+ * `Res` is keyed by identity: the runtime installs one, the game asks another.
+ */
+export const NATIVE_MODULE_REGISTRY = '__ESTELLA_NATIVE_MODULES__';
+
+export const DISPOSITIONS = {
+ 'core-global': {
+ means: 'the host has already evaluated the SDK and installed globalThis.ESEngine; this binds to that instance',
+ resolvesTo: 'globalThis.ESEngine',
+ needsNamespace: false,
+ },
+ 'native-subpath': {
+ means: 'a public plugin module whose namespace the native entry publishes from its own graph',
+ resolvesTo: `globalThis.${NATIVE_MODULE_REGISTRY}[<specifier>]`,
+ needsNamespace: true,
+ },
+ 'forbidden-native-script': {
+ means: 'a real SDK entry that a natively packaged game script must not import; packaging fails and says why',
+ resolvesTo: null,
+ needsNamespace: false,
+ },
+};
+
+/**
+ * Every specifier the SDK's `exports` map publishes, and what it means here.
+ *
+ * `why` is owed by anything forbidden: "no" without a reason is the kind of
+ * rule that gets deleted by whoever hits it next.
+ */
+export const MODULES = {
+ 'esengine': { disposition: 'core-global' },
+
+ 'esengine/physics': { disposition: 'native-subpath' },
+ 'esengine/physics3d': { disposition: 'native-subpath' },
+ 'esengine/spine': { disposition: 'native-subpath' },
+ 'esengine/dragonbones': { disposition: 'native-subpath' },
+
+ 'esengine/native': {
+ disposition: 'forbidden-native-script',
+ why: 'the SDK entry the host itself evaluated — a game script importing it is asking for a second copy of the thing already running',
+ },
+ 'esengine/wasm': {
+ disposition: 'forbidden-native-script',
+ why: 'the emscripten module surface; a native build embeds Dawn and QuickJS and has no such module',
+ },
+ 'esengine/node': {
+ disposition: 'forbidden-native-script',
+ why: 'the Node entry, for servers and tools (examples/multiplayer-arena/server) — it reaches for node: builtins a packaged game has none of',
+ },
+ 'esengine/wechat': {
+ disposition: 'forbidden-native-script',
+ why: 'the WeChat mini-game entry, a different host with a different platform adapter',
+ },
+ 'esengine/minigame': {
+ disposition: 'forbidden-native-script',
+ why: 'the mini-game entry, as above',
+ },
+};
+
+/** The `exports` subpath ("." / "./physics3d") a specifier corresponds to. */
+export function subpathOf(specifier) {
+ return specifier === 'esengine' ? '.' : `.${specifier.slice('esengine'.length)}`;
+}
+
+/** The specifier an `exports` subpath corresponds to — the inverse of the above. */
+export function specifierOf(subpath) {
+ return subpath === '.' ? 'esengine' : `esengine${subpath.slice(1)}`;
+}
+
+/** Every specifier that must have a namespace in the native registry. */
+export function nativeSubpaths() {
+ return Object.entries(MODULES)
+ .filter(([, m]) => DISPOSITIONS[m.disposition]?.needsNamespace)
+ .map(([specifier]) => specifier);
+}