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(ecs): world.set reads a vector written before it grew an axis

yhh4409fe831e21
diff --git a/sdk/src/ecs/world.ts b/sdk/src/ecs/world.ts
index 2d655057c..6fcf14ab4 100644
--- a/sdk/src/ecs/world.ts
+++ b/sdk/src/ecs/world.ts
@@ -7,7 +7,7 @@
import { WasmModuleAborted } from '../wasm/moduleHealth';
import { Entity, entityGeneration, entityIndex, makeEntity, INVALID_ENTITY } from '../types';
-import { AnyComponentDef, ComponentDef, ComponentData, BuiltinComponentDef, isBuiltinComponent, builtinComponentRegistry, extraBuiltinComponentRegistry, getUserComponents, getComponent, Disabled, Name, Parent, Children, type ParentData, type ChildrenData } from './component';
+import { AnyComponentDef, ComponentDef, ComponentData, BuiltinComponentDef, isBuiltinComponent, mergeIntoDefaults, builtinComponentRegistry, extraBuiltinComponentRegistry, getUserComponents, getComponent, Disabled, Name, Parent, Children, type ParentData, type ChildrenData } from './component';
import type { CppRegistry, ESEngineModule } from '../wasm';
import { handleWasmError } from '../wasm/wasmError';
import { BuiltinBridge, builtinFromWasm, numberListFieldsOf, convertForWasm, type BridgeConnectOptions,
@@ -876,6 +876,11 @@ export class World {
delete wasmData[k];
}
}
+ // Merged into the defaults one level in, as an add is: a field
+ // that grew an axis (ParticleEmitter.gravity went vec2 -> vec3)
+ // still reads the {x, y} a document wrote before it did, where
+ // embind refused the whole value for its missing `z`.
+ wasmData = mergeIntoDefaults(defaults, wasmData);
// Through the bridge's own write, so a replace marshals the
// way an add does: a list field crosses as the vector embind
// registered, never as the JS array a document holds.
diff --git a/sdk/tests/builtin-set-default-merge-wasm.test.ts b/sdk/tests/builtin-set-default-merge-wasm.test.ts
new file mode 100644
index 000000000..494fb7631
--- /dev/null
+++ b/sdk/tests/builtin-set-default-merge-wasm.test.ts
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file Replacing a builtin with data written before a field grew reads it as that field.
+ *
+ * `insert` merges data into the defaults one level in (component-default-merge
+ * test), so a gravity stored as {x, y} still reads after it became a vec3. The
+ * REPLACE path, `world.set`, handed the document's value to embind as it was, and
+ * embind refuses a value_object with a missing field: the whole ParticleEmitter
+ * write failed with `Missing field: "z"` and the emitter kept whatever it had.
+ * celestial-heights' three scenes carry such a gravity, so opening them in the
+ * editor logged the error and their dust fell at the wrong rate.
+ */
+import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
+import { App } from '../src/app/app';
+import { Transform, ParticleEmitter } from '../src/ecs/component';
+import type { ESEngineModule } from '../src/wasm';
+import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';
+
+let engine: ESEngineModule;
+const apps: App[] = [];
+afterEach(() => { for (const app of apps.splice(0)) app.quit(); vi.restoreAllMocks(); });
+
+describe.skipIf(!HAS_WASM)('world.set on a builtin with an older, shorter vector', () => {
+ beforeAll(async () => { engine = await loadWasmModule(); });
+
+ it('fills the axis the data lacks from the default and keeps the rest', () => {
+ const app = App.new();
+ apps.push(app);
+ app.connectCpp(new engine.Registry(), engine, { ownedRegistry: true });
+ const e = app.world.spawn();
+ app.world.insert(e, Transform, {});
+ app.world.insert(e, ParticleEmitter, { rate: 3 });
+
+ const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
+ const stored = { ...app.world.get(e, ParticleEmitter), gravity: { x: 0, y: -260 } };
+ app.world.set(e, ParticleEmitter, stored as never);
+
+ const read = app.world.get(e, ParticleEmitter);
+ expect(read.gravity).toEqual({ x: 0, y: -260, z: 0 });
+ expect(read.rate).toBe(3);
+ expect(errors.mock.calls.map((c) => c.join(' ')).filter((s) => /Missing field/.test(s))).toEqual([]);
+ });
+});