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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(query): a write-back that failed was tried again on the way out
esengine3cd149c2e36c
diff --git a/sdk/src/ecs/query.ts b/sdk/src/ecs/query.tsindex a2f4049d6..6b024d133 100644--- a/sdk/src/ecs/query.ts+++ b/sdk/src/ecs/query.ts@@ -428,7 +428,14 @@ export class QueryInstance<C extends readonly QueryArg[]> implements Iterable<Qulet started = false;let done = false;- const writeMut = () => { self.writeMutBack_(prevEntity!); };+ // Takes the pending entity rather than reading it, so a write-back is+ // attempted exactly once. A failing one has still been tried, and finalize+ // must not retry it on the way out under the error it raised.+ const writeMut = () => {+ const entity = prevEntity!;+ prevEntity = null;+ self.writeMutBack_(entity);+ };// endIteration first, so the depth balances even if the write-back throws.const finalize = (errorInFlight = false) => {@@ -512,16 +519,21 @@ export class QueryInstance<C extends readonly QueryArg[]> implements Iterable<Quconst actualComponents = this.actualComponents_;world.beginIteration();+ // Non-null means one entity's Mut edits are still owed a write-back and+ // none has been attempted. flushMut takes it, so a failing write-back is+ // not tried a second time by the finally below.let prevEntity: Entity | null = null;- let completed = false;+ const flushMut = (): void => {+ const entity = prevEntity!;+ prevEntity = null;+ this.writeMutBack_(entity);+ };try {for (let idx = 0; idx < entities.length; idx++) {const entity = entities[idx];if (hasChangeFilters && !this.passesChangeFilters_(entity)) continue;- if (prevEntity !== null && hasMut) {- this.writeMutBack_(prevEntity);- }+ if (prevEntity !== null && hasMut) flushMut();result[0] = entity;for (let i = 0; i < compCount; i++) {@@ -548,16 +560,13 @@ export class QueryInstance<C extends readonly QueryArg[]> implements Iterable<Qu}}- if (prevEntity !== null && hasMut) {- this.writeMutBack_(prevEntity);- }- completed = true;+ if (prevEntity !== null && hasMut) flushMut();} finally {- if (!completed && prevEntity !== null && hasMut) {+ if (prevEntity !== null && hasMut) {// The callback threw partway. Edits it already finished still go// back, but a failure writing them must not replace the error on// its way out — that error is the one worth reading.- try { this.writeMutBack_(prevEntity); } catch { /* the original error wins */ }+ try { flushMut(); } catch { /* the original error wins */ }}world.endIteration();}diff --git a/sdk/tests/query-iteration-balance.test.ts b/sdk/tests/query-iteration-balance.test.tsindex 5ec41cc3f..d177689e4 100644--- a/sdk/tests/query-iteration-balance.test.ts+++ b/sdk/tests/query-iteration-balance.test.ts@@ -109,6 +109,36 @@ describe('iteration depth balances however iteration ends', () => {expect(world.isIterating()).toBe(false);});+ it('forEach: a failing write-back is attempted once, not retried on the way out', () => {+ const world = worldWithEntities(2);+ const q = new QueryInstance(world, Query(Mut(P)), -1);+ const attempts: number[] = [];+ (q as unknown as { writeMutBack_: (e: number) => void }).writeMutBack_ = (e) => {+ attempts.push(e);+ throw new Error('write-back');+ };+ // The callback is clean, so the throw is the write-back's own. Retrying it+ // in the finally would run a write that already failed a second time.+ expect(() => q.forEach(() => { /* no error of its own */ })).toThrow('write-back');+ expect(attempts).toHaveLength(1);+ expect(world.isIterating()).toBe(false);+ });++ it('for..of: a failing write-back is attempted once, not retried by finalize', () => {+ const world = worldWithEntities(3);+ const q = new QueryInstance(world, Query(Mut(P)), -1);+ const attempts: number[] = [];+ (q as unknown as { writeMutBack_: (e: number) => void }).writeMutBack_ = (e) => {+ attempts.push(e);+ throw new Error('write-back');+ };+ expect(() => {+ for (const _row of q) { /* the previous row's write-back throws */ }+ }).toThrow('write-back');+ expect(attempts).toHaveLength(1);+ expect(world.isIterating()).toBe(false);+ });+it('a write-back failure on a clean run is not swallowed', () => {const world = worldWithEntities(2);const q = new QueryInstance(world, Query(Mut(P)), -1);