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(sdk): a schedule that is not one says which system, and which value

yhh8bd241000642
diff --git a/sdk/src/app/app.ts b/sdk/src/app/app.ts
index 43fa2572f..af337de36 100644
--- a/sdk/src/app/app.ts
+++ b/sdk/src/app/app.ts
@@ -70,6 +70,21 @@ interface SystemEntry {
fromBundle?: boolean;
}
+/**
+ * How a rejected schedule reads back to whoever wrote it. The two mistakes that
+ * get here look nothing alike in the source and identically at runtime, so the
+ * message has to tell them apart: a member that does not exist arrives as
+ * `undefined`, and swapped arguments arrive as the system itself.
+ */
+function describeSchedule(value: unknown): string {
+ if (value === undefined) return 'undefined (no such Schedule member?)';
+ if (value === null) return 'null';
+ if (typeof value === 'number' || typeof value === 'string') return JSON.stringify(value);
+ const name = (value as { _name?: unknown } | null)?._name;
+ if (typeof name === 'string') return `the system "${name}" (arguments the wrong way round?)`;
+ return Object.prototype.toString.call(value);
+}
+
// =============================================================================
// App
// =============================================================================
@@ -241,6 +256,35 @@ export class App {
// Systems
// =========================================================================
+ /**
+ * The entry list for `schedule`, or a refusal that says what was wrong.
+ *
+ * Every valid schedule gets its list in the constructor, so a miss means the
+ * caller passed something that is not a Schedule — a member that does not
+ * exist (`Schedule.LateUpdate` evaluates to `undefined`), or the two
+ * arguments the wrong way round. Neither is caught before it runs: the
+ * editor and the mini-game exporters bundle project code with esbuild, which
+ * strips types without checking them, so a registration TypeScript would
+ * have rejected reaches the App intact.
+ *
+ * It used to be `systems_.get(schedule)!`, which turned both into "Cannot
+ * read properties of undefined (reading 'push')" thrown from inside a
+ * minified bundle, naming neither the system nor the value.
+ */
+ private scheduleBucket_(schedule: Schedule, systemName: string): SystemEntry[] {
+ const bucket = this.systems_.get(schedule);
+ if (bucket) return bucket;
+ const valid = Object.entries(Schedule)
+ .filter(([, v]) => typeof v === 'number')
+ .map(([k, v]) => `${k} (${String(v)})`)
+ .join(', ');
+ throw new Error(
+ `addSystemToSchedule("${systemName}"): ${describeSchedule(schedule)} is not a schedule. `
+ + `Expected one of: ${valid}. `
+ + `Check the Schedule member exists, and that the schedule comes before the system.`,
+ );
+ }
+
/**
* Register `system` onto `schedule`. Ordering given here is added to
* whatever the definition already declared, so a system can carry its own
@@ -262,7 +306,7 @@ export class App {
};
this.templateToRuntime_.set(system._id, scoped._id);
- this.systems_.get(schedule)!.push({
+ this.scheduleBucket_(schedule, name).push({
system: scoped,
runBefore: mergeOrderingEdges(system._runBefore, options?.runBefore),
runAfter: mergeOrderingEdges(system._runAfter, options?.runAfter),
@@ -404,7 +448,7 @@ export class App {
};
this.templateToRuntime_.set(sys._id, scoped._id);
- this.systems_.get(schedule)!.push({
+ this.scheduleBucket_(schedule, name).push({
system: scoped,
runBefore: mergedRunBefore,
runAfter: mergedRunAfter,
diff --git a/sdk/tests/hot-swap-systems.test.ts b/sdk/tests/hot-swap-systems.test.ts
index 4bcdd838d..c505ced5c 100644
--- a/sdk/tests/hot-swap-systems.test.ts
+++ b/sdk/tests/hot-swap-systems.test.ts
@@ -155,3 +155,41 @@ describe('probeRegistrations', () => {
expect(getUserComponents().has('Probe')).toBe(false);
});
});
+
+/**
+ * A registration TypeScript would have rejected still reaches the App: the
+ * editor and the mini-game exporters bundle project code with esbuild, which
+ * strips types without checking them. What it hits has to say what is wrong —
+ * it used to be `undefined.push` from inside a minified bundle.
+ */
+describe('a schedule that is not one', () => {
+ const sys = () => defineSystem([], () => {}, { name: 'Spin' });
+
+ it('names the system and the value when the member does not exist', () => {
+ // `Schedule.LateUpdate` — no such member, so the call site passes undefined.
+ expect(() => App.new().addSystemToSchedule(undefined as unknown as Schedule, sys()))
+ .toThrow(/addSystemToSchedule\("Spin"\).*undefined \(no such Schedule member\?\)/s);
+ });
+
+ it('recognises the arguments being the wrong way round', () => {
+ const system = sys();
+ expect(() => App.new().addSystemToSchedule(system as unknown as Schedule, system))
+ .toThrow(/the system "Spin" \(arguments the wrong way round\?\)/);
+ });
+
+ it('lists what a schedule can be', () => {
+ expect(() => App.new().addSystemToSchedule(99 as Schedule, sys()))
+ .toThrow(/Expected one of: .*Update \(3\).*FixedUpdate \(11\)/s);
+ });
+
+ it('refuses a bundle drain the same way, rather than half-registering it', () => {
+ const app = App.new();
+ expect(() => app.addBundleSystems([
+ { schedule: Schedule.Update, system: defineSystem([], () => {}, { name: 'Good' }) },
+ { schedule: undefined as unknown as number, system: sys() },
+ ])).toThrow(/Spin/);
+ // And the flag the drain sets is cleared, so a later good registration
+ // is not mistaken for one of the bundle's.
+ expect((app as unknown as { addingBundleSystems_: boolean }).addingBundleSystems_).toBe(false);
+ });
+});