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(ui): bound ancestor traversal across cyclic hierarchy adapters

yhhcfeb5aed38c0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3c359b7f7..d2e0c02c2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **UI ancestor walks terminate on malformed hierarchy adapters.** Depth counting and parent callbacks stop before repeating an entity or following an ancestor invalidated by a callback. Shallow chains use bounded array scans, deeper chains switch to a set, and long valid chains remain iterative with early callback termination preserved.
+
- **Unavailable dropdowns retire their open lists.** Disabling, hiding, deactivating or removing a dropdown closes its popup even when the owner drops out of component queries. Keyboard input and stale row clicks cannot change an unavailable control; availability is checked again after popup teardown callbacks.
- **Dropdown labels and open rows follow live option edits.** Renames and label replacements repaint without a false selection event; changed option lists or row heights rebuild an open popup and keep the selection in range. Stale rows cannot choose a newly substituted option, popup teardown cannot resurrect a disposed owner, and change callbacks may remove later controls safely.
diff --git a/sdk/src/ui/util/helpers.ts b/sdk/src/ui/util/helpers.ts
index 808a5e3d3..74a4b6926 100644
--- a/sdk/src/ui/util/helpers.ts
+++ b/sdk/src/ui/util/helpers.ts
@@ -46,13 +46,24 @@ export function nextPowerOf2(n: number): number {
return p;
}
+// Bound shallow scans; use a set only after sixteen ancestors.
+const PARENT_CHAIN_SET_AT = 17;
+
export function getEntityDepth(world: World, entity: Entity): number {
+ if (!world.valid(entity) || !world.has(entity, Parent)) return 0;
let depth = 0;
let current = entity;
- while (world.has(current, Parent)) {
+ const visited = [entity];
+ let seen: Set<Entity> | undefined;
+ while (world.valid(current) && world.has(current, Parent)) {
const parentData = world.get(current, Parent) as ParentData;
const parentEntity = parentData.entity;
- if (!world.valid(parentEntity)) break;
+ if (!world.valid(parentEntity) || (seen ? seen.has(parentEntity) : visited.includes(parentEntity))) break;
+ if (seen) seen.add(parentEntity);
+ else {
+ visited.push(parentEntity);
+ if (visited.length === PARENT_CHAIN_SET_AT) seen = new Set(visited);
+ }
depth++;
current = parentEntity;
}
@@ -80,11 +91,19 @@ export function walkParentChain(
world: Pick<World, 'has' | 'get' | 'valid'>, entity: Entity,
callback: (ancestor: Entity) => boolean,
): void {
+ if (!world.valid(entity) || !world.has(entity, Parent)) return;
let current = entity;
- while (world.has(current, Parent)) {
+ const visited = [entity];
+ let seen: Set<Entity> | undefined;
+ while (world.valid(current) && world.has(current, Parent)) {
const parentData = world.get(current, Parent) as ParentData;
const parentEntity = parentData.entity;
- if (!world.valid(parentEntity)) break;
+ if (!world.valid(parentEntity) || (seen ? seen.has(parentEntity) : visited.includes(parentEntity))) break;
+ if (seen) seen.add(parentEntity);
+ else {
+ visited.push(parentEntity);
+ if (visited.length === PARENT_CHAIN_SET_AT) seen = new Set(visited);
+ }
if (callback(parentEntity)) return;
current = parentEntity;
}
diff --git a/sdk/tests/ui-util-helpers.test.ts b/sdk/tests/ui-util-helpers.test.ts
index 93cd05271..d55da8de7 100644
--- a/sdk/tests/ui-util-helpers.test.ts
+++ b/sdk/tests/ui-util-helpers.test.ts
@@ -10,8 +10,53 @@ import {
isWordChar,
nextPowerOf2,
EntityStateMap,
+ getEntityDepth,
+ walkParentChain,
} from '../src/ui/util/helpers';
import type { Entity } from '../src/types';
+import type { World } from '../src/ecs/world';
+import { Parent } from '../src/ecs/component';
+
+describe('ancestor walks', () => {
+ function graph(links: number[], limit = 100) {
+ let reads = 0;
+ const alive = new Set(links.map((_, i) => i));
+ const world = {
+ valid: (e: Entity) => alive.has(e),
+ has: (e: Entity, c: unknown) => c === Parent && links[e] >= 0,
+ get: (e: Entity) => {
+ if (++reads > limit) throw new Error('ancestor traversal did not terminate');
+ return { entity: links[e] as Entity };
+ },
+ };
+ return { world, alive };
+ }
+ it.each([[0], [1,0], [1,2,1]])('stops before revisiting a node in cyclic hierarchy %j', (...links) => {
+ const { world } = graph(links);
+ const visited: Entity[] = [];
+ walkParentChain(world as never, 0 as Entity, e => { visited.push(e); return false; });
+ expect(new Set(visited).size).toBe(visited.length);
+ expect(visited).not.toContain(0);
+ expect(getEntityDepth(world as unknown as World, 0 as Entity)).toBe(visited.length);
+ });
+ it('stops when a callback invalidates the current ancestor', () => {
+ const { world, alive } = graph([1,2,-1]); const visited: Entity[] = [];
+ walkParentChain(world as never, 0 as Entity, e => { visited.push(e); alive.delete(e); return false; });
+ expect(visited).toEqual([1]);
+ });
+ it('retains early callback termination', () => {
+ const { world } = graph([1,2,-1]); const visited: Entity[] = [];
+ walkParentChain(world as never, 0 as Entity, e => { visited.push(e); return true; });
+ expect(visited).toEqual([1]);
+ });
+ it('handles 10000 ancestors without recursion or repeated callbacks', () => {
+ const links = Array.from({ length: 10001 }, (_, i) => i === 10000 ? -1 : i + 1);
+ const { world } = graph(links, 20000); let count = 0;
+ walkParentChain(world as never, 0 as Entity, () => { count++; return false; });
+ expect(count).toBe(10000);
+ expect(getEntityDepth(world as unknown as World, 0 as Entity)).toBe(10000);
+ });
+});
describe('isWordChar', () => {
it('accepts letters, digits and underscore; rejects space and punctuation', () => {
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index d2f8efda6..02052111a 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(ui): bound ancestor traversal across cyclic hierarchy adapters':
+ { note: 'UI ancestor walks terminate on malformed hierarchy adapters.' },
'fix(ui): retire unavailable dropdown popups before selection':
{ note: 'Unavailable dropdowns retire their open lists.' },
'fix(ui): reconcile dropdown options and guard popup teardown':