Hone logo
Hone
Problems

Escape Analysis in JavaScript: Identifying Escaped Variables

Escape analysis is a crucial optimization technique in compilers and interpreters. It determines whether a variable's value can "escape" the scope in which it's defined, potentially being used outside that scope. This challenge asks you to implement a simplified version of escape analysis in JavaScript to identify variables that escape their immediate scope. Understanding escape analysis helps in optimizing memory management and potentially enabling more aggressive inlining of functions.

Problem Description

You are tasked with building a function analyzeEscape(code) that takes a JavaScript code string as input and returns an object indicating which variables escape their scope. The function should parse the code (you can assume a simplified, well-formed JavaScript syntax for this challenge – no complex parsing is required, simple string searching is sufficient) and identify variables that are assigned to a global variable or passed as an argument to a function call. The analysis should focus on identifying variables defined within a function that are then used outside that function's scope.

Key Requirements:

  • Scope Detection: Identify the scope of each variable (currently, just functions).
  • Global Assignment: Detect if a variable is assigned to a global variable (e.g., window.myVar = localVar;).
  • Function Argument Passing: Detect if a variable is passed as an argument to a function call (e.g., myFunction(localVar);).
  • Return Value: The function should return an object where keys are variable names and values are booleans. true indicates the variable escapes its scope, and false indicates it does not.
  • Simplified Syntax: Assume the input code is relatively simple and well-formed. You don't need to handle complex JavaScript features like closures, this, or eval. Focus on direct assignments and function arguments.

Expected Behavior:

The analyzeEscape function should accurately identify variables that escape their scope based on the defined criteria. It should handle nested functions and multiple function calls.

Edge Cases to Consider:

  • Variables declared with var, let, or const within a function.
  • Function calls with multiple arguments.
  • Nested functions – escape analysis should consider the inner scope.
  • Variables used only within the same function scope (should not be flagged as escaping).
  • Global variables declared outside of any function (should not be flagged).

Examples

Example 1:

Input: `function foo() { let x = 1; window.y = x; }`
Output: { x: false, y: true }
Explanation: `x` is defined and used only within `foo`. `y` is assigned a value from `x` and assigned to the global `window` object, so it escapes.

Example 2:

Input: `function foo(a, b) { let x = a + b; return x; } function bar() { let result = foo(1, 2); console.log(result); }`
Output: { a: true, b: true, x: true, result: true }
Explanation: `a` and `b` are arguments to `foo` and therefore escape. `x` is defined within `foo` and its value is returned, so it escapes. `result` is assigned the return value of `foo`, so it escapes.

Example 3:

Input: `function outer() { function inner() { let z = 10; } outer(); }`
Output: { z: false }
Explanation: `z` is defined and used only within `inner`, so it does not escape.

Constraints

  • Input String Length: The input code string will be no longer than 500 characters.
  • Variable Names: Variable names will consist of alphanumeric characters and underscores (e.g., my_variable, count).
  • Function Calls: Function calls will be in the format functionName(arg1, arg2, ...).
  • Performance: The solution should execute in a reasonable time (under 1 second) for the given input size. Optimization is not the primary focus, but avoid excessively inefficient algorithms.
  • No External Libraries: You are not allowed to use external JavaScript libraries for parsing or analysis.

Notes

  • You can use regular expressions to simplify the parsing process.
  • Consider using a simple state machine or recursive approach to track variable scopes.
  • Focus on identifying the assignment of a variable to a global or passing it as a function argument. The actual usage of the variable after that point is not relevant for this simplified analysis.
  • The code provided is a simplified representation of JavaScript. You don't need to handle all possible JavaScript syntax. Focus on the core concepts of escape analysis within the given constraints.
  • Assume that function names are valid JavaScript identifiers.
Loading editor...
javascript