Skip to content

Fix: Create child environment for IR script execution - #552

Closed
rogeralsing with Copilot wants to merge 5 commits into
mainfrom
copilot/create-child-environment-for-ir
Closed

Fix: Create child environment for IR script execution#552
rogeralsing with Copilot wants to merge 5 commits into
mainfrom
copilot/create-child-environment-for-ir

Conversation

Copilot AI commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

Problem

IR script execution was overwriting GlobalEnvironment's slot 0 (Symbol.This → GlobalObject) with synthetic slots for loop iterators, causing global identifier resolution to fail with ReferenceError instead of the expected TypeError.

Solution

Create a child environment for IR synthetic slots when the parent environment already has slots:

public static JsValue RunScript(ExecutionPlan plan, JsEnvironment environment, EvaluationContext context)
{
    var irEnvironment = environment;
    if (plan.SlotCount > 0)
    {
        if (environment.HasSlots)
        {
            // Isolate IR slots from GlobalEnvironment
            irEnvironment = new JsEnvironment(environment, false, false, null, "ir-script-wrapper");
        }
        
        irEnvironment.InitializeSlots(plan.SlotCount, plan.RootScopeId);
        irEnvironment.PopulateSyntheticSlotNames(plan.SlotSymbols);
    }

    var runner = new ExecutionPlanRunner(plan, irEnvironment, context, 0);
    return runner.RunScriptInternal();
}

Benefits over offset-based approach:

  • No runtime offset calculations
  • Clean scope isolation matching JavaScript semantics
  • Works with any pre-existing slots

Changes:

  • Modified TypedAstEvaluator.ExecutionPlanRunner.Core.cs: Child environment creation logic
  • Enabled 8 previously skipped tests in DebugScopeTests.cs
Original prompt

This section details on the original issue you should resolve

<issue_title>Fix: Create child environment for IR script execution</issue_title>
<issue_description>Parent: #481

Goal

Implement Option C from the root cause analysis - create a child environment for IR slots.

Root Cause (from #481)

PopulateSyntheticSlotNames() overwrites slot 0 (Symbol.This → GlobalObject) with synthetic slots.

Solution

public static JsValue RunScript(ExecutionPlan plan, JsEnvironment environment, EvaluationContext context)
{
    var irEnvironment = environment;
    if (plan.SlotCount > 0 && environment.HasSlots)
    {
        // Create child environment for IR slots to avoid clobbering GlobalEnvironment
        irEnvironment = new JsEnvironment(environment, false, false, null, "ir-script-wrapper");
        irEnvironment.InitializeSlots(plan.SlotCount, plan.RootScopeId);
        irEnvironment.PopulateSyntheticSlotNames(plan.SlotSymbols);
    }
    else if (plan.SlotCount > 0)
    {
        irEnvironment.InitializeSlots(plan.SlotCount, plan.RootScopeId);
        irEnvironment.PopulateSyntheticSlotNames(plan.SlotSymbols);
    }

    var runner = new ExecutionPlanRunner(plan, irEnvironment, context, 0);
    return runner.RunScriptInternal();
}

Files

  • src/Asynkron.JsEngine/Ast/TypedAstEvaluator.ExecutionPlanRunner.Core.cs

Complexity: Simple

  • Clear fix path from analysis
  • Localized change</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai

coderabbitai Bot commented Jan 9, 2026

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI and others added 3 commits January 9, 2026 23:05
Co-authored-by: rogeralsing <647031+rogeralsing@users.noreply.github.com>
Co-authored-by: rogeralsing <647031+rogeralsing@users.noreply.github.com>
Co-authored-by: rogeralsing <647031+rogeralsing@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix create child environment for IR script execution Fix: Create child environment for IR script execution Jan 9, 2026
Copilot AI requested a review from rogeralsing January 9, 2026 23:15
@rogeralsing

Copy link
Copy Markdown
Contributor

@copilot build fails

@rogeralsing rogeralsing closed this Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: Create child environment for IR script execution

2 participants