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(physics): a wasm older than the code that drives it says so once, not every frame

yhhf0d3f158473e
diff --git a/sdk/src/physics/PhysicsPlugin.ts b/sdk/src/physics/PhysicsPlugin.ts
index 2d76f4791..cd1365bbb 100644
--- a/sdk/src/physics/PhysicsPlugin.ts
+++ b/sdk/src/physics/PhysicsPlugin.ts
@@ -73,6 +73,43 @@ function resolveConfig(config: PhysicsPluginConfig): ResolvedPhysicsConfig {
// Plugin
// =============================================================================
+/**
+ * What the frame loop calls every step. A module missing any of it is a wasm
+ * built before the JS that drives it.
+ *
+ * `PhysicsWasmModule` is a TypeScript interface, so it is gone at run time and
+ * nothing was checking that the binary answered to it. A `physics.wasm` built
+ * hours before the commit that added pose interpolation therefore installed
+ * happily, and `PhysicsStepSystem` and `PhysicsInterpolateSystem` then threw
+ * `_physics_capturePoses is not a function` TWICE A FRAME, for the length of
+ * the session, at anyone who dropped a RigidBody into a scene and pressed Play.
+ * Two dogfood runs read that as "physics is broken" and turned it off.
+ *
+ * Not the whole interface — the per-frame contract, whose absence is the one
+ * that fails on every tick rather than once.
+ */
+const REQUIRED_EXPORTS = [
+ '_physics_init', '_physics_setWorldConfig', '_physics_step',
+ '_physics_capturePoses', '_physics_getInterpolatedCount', '_physics_getInterpolatedTransforms',
+ '_physics_collectEvents', '_physics_getDynamicBodyCount', '_physics_getDynamicBodyTransforms',
+ '_physics_setBodyTransform',
+] as const;
+
+function assertModuleContract(module: PhysicsWasmModule): void {
+ const missing = REQUIRED_EXPORTS.filter(
+ (name) => typeof (module as unknown as Record<string, unknown>)[name] !== 'function',
+ );
+ if (missing.length === 0) return;
+ // Thrown, so the plugin's own catch marks the subsystem in error and no
+ // system is registered: the game runs WITHOUT physics and says why once,
+ // rather than running with physics that cannot step and saying why forever.
+ throw new Error(
+ `physics.wasm is out of date — it does not export ${missing.join(', ')}. `
+ + 'It was built before the engine code that calls it; rebuild the physics module '
+ + '(the WASM build is a separate step from the editor build). Physics is not installed.',
+ );
+}
+
export class PhysicsPlugin implements Plugin {
name = 'physics';
private config_: ResolvedPhysicsConfig;
@@ -113,6 +150,8 @@ export class PhysicsPlugin implements Plugin {
// guarded module has the same type, so every downstream call
// site (PhysicsSystem closures, the PhysicsAPI API wrapper) gains
// abort safety without changing a single call.
+ assertModuleContract(loaded);
+
this.bridge_.connect(loaded);
const module = this.bridge_.module;
this.module_ = module;
diff --git a/sdk/tests/physics-module-contract.test.ts b/sdk/tests/physics-module-contract.test.ts
new file mode 100644
index 000000000..2d0c0a5ff
--- /dev/null
+++ b/sdk/tests/physics-module-contract.test.ts
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file The physics plugin checks that the wasm answers to the interface.
+ *
+ * `PhysicsWasmModule` is a TypeScript interface and therefore gone at run time.
+ * A `physics.wasm` built before the JS that drives it installed happily and then
+ * threw `_physics_capturePoses is not a function` twice a frame for the length
+ * of the session — for anyone who put a RigidBody in a scene and pressed Play.
+ */
+import { describe, it, expect, vi } from 'vitest';
+import { PhysicsPlugin } from '../src/physics/PhysicsPlugin';
+import type { App } from '../src/app/app';
+
+/** Everything the plugin touches before it would reach the module. */
+function fakeApp(): App & { errors: string[] } {
+ const errors: string[] = [];
+ const resources = new Map<unknown, unknown>();
+ return {
+ errors,
+ insertResource: (def: unknown, value: unknown) => { resources.set(def, value); },
+ getResource: (def: unknown) => resources.get(def) ?? {},
+ subsystems: {
+ transition: () => {},
+ markError: (_name: string, message: string) => { errors.push(message); },
+ },
+ setFixedTimestep: () => {},
+ world: { getAllEntities: () => [] },
+ addSystem: () => {},
+ } as unknown as App & { errors: string[] };
+}
+
+const EVERY_EXPORT = [
+ '_physics_init', '_physics_setWorldConfig', '_physics_step',
+ '_physics_capturePoses', '_physics_getInterpolatedCount', '_physics_getInterpolatedTransforms',
+ '_physics_collectEvents', '_physics_getDynamicBodyCount', '_physics_getDynamicBodyTransforms',
+ '_physics_setBodyTransform',
+];
+
+const moduleWith = (names: readonly string[]): Record<string, unknown> =>
+ Object.fromEntries(names.map((n) => [n, vi.fn()]));
+
+/** Let the plugin's init promise settle. */
+const settle = () => new Promise((r) => { setTimeout(r, 0); });
+
+describe('the physics plugin against a stale wasm', () => {
+ it('refuses a module missing the frame loop it will call, and names what is missing', async () => {
+ const stale = moduleWith(EVERY_EXPORT.filter((n) => !n.includes('Interpolated') && n !== '_physics_capturePoses'));
+ const app = fakeApp();
+ new PhysicsPlugin('', {}, async () => stale as never).build(app);
+ await settle();
+
+ expect(app.errors).toHaveLength(1);
+ expect(app.errors[0]).toContain('_physics_capturePoses');
+ expect(app.errors[0]).toContain('out of date');
+ // The point of throwing rather than warning: nothing was initialised, so
+ // no system runs that would throw again on every tick.
+ expect(stale._physics_init).not.toHaveBeenCalled();
+ });
+
+ it('lets a module that answers to the whole contract through', async () => {
+ const good = moduleWith(EVERY_EXPORT);
+ const app = fakeApp();
+ new PhysicsPlugin('', {}, async () => good as never).build(app);
+ await settle();
+
+ expect(app.errors.filter((e) => e.includes('out of date'))).toHaveLength(0);
+ expect(good._physics_init).toHaveBeenCalled();
+ });
+});