Internal Component._setActive can be accidentally overridden by user Script

Aperta
#3,103 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
54/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
typescript
Ambito
backend

Direzione di ricerca

Read packages/core/src/Component.ts and packages/core/src/Entity.ts, especially the lifecycle call sites linked in the issue; then find the core regression-test conventions. Add a focused test for a Script with a string-keyed _setActive helper, covering component and hierarchy activation plus onAwake and onEnable. Done means the helper is not invoked by Engine lifecycle calls, while the virtual hooks still work and public declarations remain unchanged.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug entity script

Describe the bug

A user Script can accidentally override the Engine's internal Component._setActive(value, activeChangeFlag) method with an unrelated helper of the same name. Entity currently invokes the method through normal JavaScript dynamic dispatch, so the user method receives Engine lifecycle arguments and can crash project startup.

This has occurred twice in independent scripts on @galacean/[email protected] (GameManager and UIController).

To Reproduce

import { Entity, Script } from "@galacean/engine";

export default class UIController extends Script {
  private _setActive(entity: Entity | null, active: boolean): void {
    if (entity) entity.isActive = active;
  }
}

entity.addComponent(UIController);

Entity.addComponent() calls:

component._setActive(true, ActiveChangeFlag.All);

Because TypeScript private is emitted as a normal string-keyed prototype method, the call dispatches to UIController._setActive. The boolean true is then treated as an Entity, producing:

TypeError: Cannot create property 'isActive' on boolean 'true'
    at UIController._setActive
    at Entity.addComponent

Current source locations:

Why TypeScript does not catch it

_setActive is marked @internal, and packages/core/tsconfig.json enables stripInternal. The published Component.d.ts therefore contains no _setActive member, so strict consumer typechecking sees the user helper as a new private method rather than an override.

Expected behavior

Internal non-virtual lifecycle operations must not be overrideable by consumer Script members. A user helper named _setActive should not corrupt component activation, while intentionally virtual Engine hooks such as _onAwake and _onEnable must keep their existing behavior.

Suggested ownership boundary

Give this non-virtual operation a package-internal unique Symbol identity and let Entity call it through that identity. The symbol can be imported directly between core modules without being re-exported from packages/core/src/index.ts; @internal + stripInternal can keep the public declarations unchanged. This encodes non-overridability once and does not reserve the string _setActive in user scripts.

A focused regression should add a Script with an unrelated string-keyed _setActive helper, then verify that addComponent, hierarchy activation, onAwake, and onEnable still work and the user helper is not invoked by the Engine.

Environment

  • Engine: 2.0.0-alpha.43
  • Branch/source checked: dev/2.0@490d2bc8ca1f1c187a995da3c913e59c56959a30
  • Browser: Chromium-based runtime
  • OS-specific: No

Screenshots

N/A — the failure occurs before window.engine is created.

Lingua principale
TypeScript
Stelle
5.9k
Fork
413
Merge medio
16h 17m
PR unite (30g)
41

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di galacean/engine

Tutte le issue di galacean/engine

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.