Home / Function/ lowerContextAccess() — react Function Reference

lowerContextAccess() — react Function Reference

Architecture documentation for the lowerContextAccess() function in LowerContextAccess.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  d3225c91_4858_fca4_72c5_c74a4f05f299["lowerContextAccess()"]
  ed770299_f9db_61d4_407c_b8ead52e24ec["LowerContextAccess.ts"]
  d3225c91_4858_fca4_72c5_c74a4f05f299 -->|defined in| ed770299_f9db_61d4_407c_b8ead52e24ec
  c3bc3875_256f_8f5e_7800_2f9c5bae65eb["runWithEnvironment()"]
  c3bc3875_256f_8f5e_7800_2f9c5bae65eb -->|calls| d3225c91_4858_fca4_72c5_c74a4f05f299
  d865d73c_e43b_cca8_9f60_c83bc41deeb8["getContextKeys()"]
  d3225c91_4858_fca4_72c5_c74a4f05f299 -->|calls| d865d73c_e43b_cca8_9f60_c83bc41deeb8
  39b89946_330a_d058_a291_b850d7d6fc5f["emitLoadLoweredContextCallee()"]
  d3225c91_4858_fca4_72c5_c74a4f05f299 -->|calls| 39b89946_330a_d058_a291_b850d7d6fc5f
  53244187_914c_cc90_5880_7bfc1fc9c0bb["push()"]
  d3225c91_4858_fca4_72c5_c74a4f05f299 -->|calls| 53244187_914c_cc90_5880_7bfc1fc9c0bb
  c059451e_3ed5_62a9_6995_a4adbca6d62c["emitSelectorFn()"]
  d3225c91_4858_fca4_72c5_c74a4f05f299 -->|calls| c059451e_3ed5_62a9_6995_a4adbca6d62c
  0c09df5a_6c07_11e5_1a6b_9253007463b8["markInstructionIds()"]
  d3225c91_4858_fca4_72c5_c74a4f05f299 -->|calls| 0c09df5a_6c07_11e5_1a6b_9253007463b8
  style d3225c91_4858_fca4_72c5_c74a4f05f299 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts lines 36–132

export function lowerContextAccess(
  fn: HIRFunction,
  loweredContextCalleeConfig: ExternalFunction,
): void {
  const contextAccess: Map<IdentifierId, CallExpression> = new Map();
  const contextKeys: Map<IdentifierId, Array<string>> = new Map();

  // collect context access and keys
  for (const [, block] of fn.body.blocks) {
    for (const instr of block.instructions) {
      const {value, lvalue} = instr;

      if (
        value.kind === 'CallExpression' &&
        isUseContextHookType(value.callee.identifier)
      ) {
        contextAccess.set(lvalue.identifier.id, value);
        continue;
      }

      if (value.kind !== 'Destructure') {
        continue;
      }

      const destructureId = value.value.identifier.id;
      if (!contextAccess.has(destructureId)) {
        continue;
      }

      const keys = getContextKeys(value);
      if (keys === null) {
        return;
      }

      if (contextKeys.has(destructureId)) {
        /*
         * TODO(gsn): Add support for accessing context over multiple
         * statements.
         */
        return;
      } else {
        contextKeys.set(destructureId, keys);
      }
    }
  }

  let importLoweredContextCallee: NonLocalImportSpecifier | null = null;

  if (contextAccess.size > 0 && contextKeys.size > 0) {
    for (const [, block] of fn.body.blocks) {
      let nextInstructions: Array<Instruction> | null = null;

      for (let i = 0; i < block.instructions.length; i++) {
        const instr = block.instructions[i];
        const {lvalue, value} = instr;
        if (
          value.kind === 'CallExpression' &&
          isUseContextHookType(value.callee.identifier) &&
          contextKeys.has(lvalue.identifier.id)
        ) {
          importLoweredContextCallee ??=
            fn.env.programContext.addImportSpecifier(
              loweredContextCalleeConfig,
            );
          const loweredContextCalleeInstr = emitLoadLoweredContextCallee(
            fn.env,
            importLoweredContextCallee,
          );

          if (nextInstructions === null) {
            nextInstructions = block.instructions.slice(0, i);
          }
          nextInstructions.push(loweredContextCalleeInstr);

          const keys = contextKeys.get(lvalue.identifier.id)!;
          const selectorFnInstr = emitSelectorFn(fn.env, keys);
          nextInstructions.push(selectorFnInstr);

          const lowerContextCallId = loweredContextCalleeInstr.lvalue;
          value.callee = lowerContextCallId;

Domain

Subdomains

Frequently Asked Questions

What does lowerContextAccess() do?
lowerContextAccess() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts.
Where is lowerContextAccess() defined?
lowerContextAccess() is defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/LowerContextAccess.ts at line 36.
What does lowerContextAccess() call?
lowerContextAccess() calls 5 function(s): emitLoadLoweredContextCallee, emitSelectorFn, getContextKeys, markInstructionIds, push.
What calls lowerContextAccess()?
lowerContextAccess() is called by 1 function(s): runWithEnvironment.

Analyze Your Own Codebase

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

Try Supermodel Free