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(editor): undo of an omitted field keeps it in the inspector

esenginea400ec10796c
diff --git a/desktop/src/engine/Reconciler.ts b/desktop/src/engine/Reconciler.ts
index c971d2d68..548ff3aaa 100644
--- a/desktop/src/engine/Reconciler.ts
+++ b/desktop/src/engine/Reconciler.ts
@@ -258,10 +258,7 @@ export class ReconcilerImpl {
const defaults = componentDefaults(def);
const out: Record<string, unknown> = {};
for (const key of Object.keys(defaults)) {
- // A field that is absent OR explicitly undefined (e.g. undo restoring a
- // partial component's untouched field) falls back to the default — the wasm
- // bindings reject an undefined POD field (`'w' in undefined`).
- out[key] = data[key] !== undefined ? this.resolveRefs(data[key]) : defaults[key];
+ out[key] = key in data ? this.resolveRefs(data[key]) : defaults[key];
}
return out;
}
diff --git a/desktop/src/engine/SceneModel.ts b/desktop/src/engine/SceneModel.ts
index dd30f153d..11a255cc2 100644
--- a/desktop/src/engine/SceneModel.ts
+++ b/desktop/src/engine/SceneModel.ts
@@ -148,10 +148,15 @@ export class SceneModelImpl {
if (!e) return;
let comp = e.components.find((c) => c.type === compType);
if (!comp) {
+ if (value === undefined) return;
comp = { type: compType, data: {} } as SceneComponent;
e.components.push(comp);
}
- (comp.data as Record<string, unknown>)[key] = value;
+ const data = comp.data as Record<string, unknown>;
+ // Never store `undefined` (undo of an edit to an omitted field): a key present
+ // but undefined drops from the inspector and crashes the wasm projection.
+ if (value === undefined) delete data[key];
+ else data[key] = value;
this.emit({ kind: 'componentChanged', sourceId, type: compType });
}
diff --git a/desktop/tests/engine-model-sync.test.ts b/desktop/tests/engine-model-sync.test.ts
index 5772f9888..20f6671b0 100644
--- a/desktop/tests/engine-model-sync.test.ts
+++ b/desktop/tests/engine-model-sync.test.ts
@@ -27,6 +27,7 @@ vi.mock('@/engine/EngineHost', () => ({
}));
import { EditorSession } from '@/engine/EditorSession';
+import { inspectorFields } from '@/engine/schema';
function sceneWithUnknown(): SceneData {
return {
@@ -104,15 +105,19 @@ describe.skipIf(!HAS_WASM)('Model-authoritative projection + lossless save', ()
expect(t.position.x).toBe(0);
});
- it('undo of an edit to a field the Transform omitted restores the default, not undefined (was a wasm crash)', () => {
- // Authored scenes routinely omit rotation (it defaults to identity). Editing it
- // records before=undefined, so undo used to project rotation:undefined → the
- // wasm bindings threw "Cannot use 'in' operator to search for 'w' in undefined".
+ it('undo of an edit to a field the Transform omitted restores the default (no wasm crash, field stays in the inspector)', () => {
+ // A Transform that omits rotation (defaults to identity); editing it records
+ // before=undefined, which used to crash wasm + drop rotation from the inspector.
const src = S.model.addEntity('Partial', [{ type: 'Transform', data: { position: { x: 1, y: 2, z: 0 } } }] as never);
const rt = S.model.runtimeFor(src)!;
S.commands.setField(src, 'Transform', 'rotation', 'angle', 45);
+
expect(() => S.history.undo()).not.toThrow();
- expect((host.world.get(rt, Transform).rotation as { w: number }).w).toBe(1); // identity, not undefined
+ expect((host.world.get(rt, Transform).rotation as { w: number }).w).toBe(1);
+
+ const tData = S.model.entityBySource(src)!.components.find((c) => c.type === 'Transform')!.data as Record<string, unknown>;
+ expect('rotation' in tData).toBe(false); // absent, not present-and-undefined
+ expect(inspectorFields('Transform', tData).some((f) => f.key === 'rotation')).toBe(true);
});
it('addEntity / undo is reflected in the model', () => {