Home / Class/ Context Class — react Architecture

Context Class — react Architecture

Architecture documentation for the Context class in BuildReactiveFunction.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  4f68940d_e29b_1b55_8b23_bd29e8900efa["Context"]
  3e304366_7140_93cb_47c8_0d400140265a["BuildReactiveFunction.ts"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|defined in| 3e304366_7140_93cb_47c8_0d400140265a
  311fed6d_b376_8177_0ed3_a3f46713b178["constructor()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 311fed6d_b376_8177_0ed3_a3f46713b178
  5f0bcc60_3bbd_2d14_7b2f_e247f599d89f["block()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 5f0bcc60_3bbd_2d14_7b2f_e247f599d89f
  d8d8e921_affe_9002_b63a_cc0606d3457f["scheduleCatchHandler()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| d8d8e921_affe_9002_b63a_cc0606d3457f
  1f49a9d5_b964_3a45_69a0_1b11206d9167["reachable()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 1f49a9d5_b964_3a45_69a0_1b11206d9167
  216a1a94_c1bc_9d34_ed30_b8a6c02b0687["schedule()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 216a1a94_c1bc_9d34_ed30_b8a6c02b0687
  98473280_e771_cfd6_8eda_8978bff6b1fc["scheduleLoop()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 98473280_e771_cfd6_8eda_8978bff6b1fc
  5c45e136_a311_949a_7cc2_60a908bc7a50["unschedule()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 5c45e136_a311_949a_7cc2_60a908bc7a50
  f51ff363_bceb_1d79_f026_ed2ca9638f21["unscheduleAll()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| f51ff363_bceb_1d79_f026_ed2ca9638f21
  3211b3cf_7008_8b9d_08fc_eac57eabd5b2["isScheduled()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 3211b3cf_7008_8b9d_08fc_eac57eabd5b2
  c9d5ef77_d0cd_11c8_8414_09d6d6ce703e["getBreakTarget()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| c9d5ef77_d0cd_11c8_8414_09d6d6ce703e
  f7a2da57_16b4_88af_75a7_54ec356e2b1d["getContinueTarget()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| f7a2da57_16b4_88af_75a7_54ec356e2b1d
  552805e3_e1bf_e86a_ba3c_06118a8afc3a["debugBreakTargets()"]
  4f68940d_e29b_1b55_8b23_bd29e8900efa -->|method| 552805e3_e1bf_e86a_ba3c_06118a8afc3a

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts lines 1247–1473

class Context {
  ir: HIR;
  #nextScheduleId: number = 0;

  /*
   * Used to track which blocks *have been* generated already in order to
   * abort if a block is generated a second time. This is an error catching
   * mechanism for debugging purposes, and is not used by the codegen algorithm
   * to drive decisions about how to emit blocks.
   */
  emitted: Set<BlockId> = new Set();

  scopeFallthroughs: Set<BlockId> = new Set();
  /*
   * A set of blocks that are already scheduled to be emitted by eg a parent.
   * This allows child nodes to avoid re-emitting the same block and emit eg
   * a break instead.
   */
  #scheduled: Set<BlockId> = new Set();

  #catchHandlers: Set<BlockId> = new Set();

  /*
   * Represents which control flow operations are currently in scope, with the innermost
   * scope last. Roughly speaking, the last ControlFlowTarget on the stack indicates where
   * control will implicitly transfer, such that gotos to that block can be elided. Gotos
   * targeting items higher up the stack may need labeled break or continue; see
   * getBreakTarget() and getContinueTarget() for more details.
   */
  #controlFlowStack: Array<ControlFlowTarget> = [];

  constructor(ir: HIR) {
    this.ir = ir;
  }

  block(id: BlockId): BasicBlock {
    return this.ir.blocks.get(id)!;
  }

  scheduleCatchHandler(block: BlockId): void {
    this.#catchHandlers.add(block);
  }

  reachable(id: BlockId): boolean {
    const block = this.ir.blocks.get(id)!;
    return block.terminal.kind !== 'unreachable';
  }

  /*
   * Record that the given block will be emitted (eg by the codegen of a parent node)
   * so that child nodes can avoid re-emitting it.
   */
  schedule(block: BlockId, type: 'if' | 'switch' | 'case'): number {
    const id = this.#nextScheduleId++;
    CompilerError.invariant(!this.#scheduled.has(block), {
      reason: `Break block is already scheduled: bb${block}`,
      loc: GeneratedSource,
    });
    this.#scheduled.add(block);
    this.#controlFlowStack.push({block, id, type});
    return id;
  }

  scheduleLoop(
    fallthroughBlock: BlockId,
    continueBlock: BlockId,
    loopBlock: BlockId | null,
  ): number {
    const id = this.#nextScheduleId++;
    const ownsBlock = !this.#scheduled.has(fallthroughBlock);
    this.#scheduled.add(fallthroughBlock);
    CompilerError.invariant(!this.#scheduled.has(continueBlock), {
      reason: `Continue block is already scheduled: bb${continueBlock}`,
      loc: GeneratedSource,
    });
    this.#scheduled.add(continueBlock);
    let ownsLoop = false;
    if (loopBlock !== null) {
      ownsLoop = !this.#scheduled.has(loopBlock);
      this.#scheduled.add(loopBlock);
    }

Domain

Frequently Asked Questions

What is the Context class?
Context is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts.
Where is Context defined?
Context is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts at line 1247.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free