Skip to content

Implement core disposal logic for explicit resource management#5079

Open
abhinavs1920 wants to merge 13 commits intoboa-dev:mainfrom
abhinavs1920:feat/res2
Open

Implement core disposal logic for explicit resource management#5079
abhinavs1920 wants to merge 13 commits intoboa-dev:mainfrom
abhinavs1920:feat/res2

Conversation

@abhinavs1920
Copy link
Contributor

This Pull Request fixes/closes #4445 .

Make using actually dispose ( continued work )

Follow-up to my last PR (#4649) that added parsing.

Now when a block ends, resources get cleaned up properly.

  • New bytecodes: AddDisposableResource + DisposeResources + PushDisposalScope
  • VM now has a disposal stack + scope tracking
  • Bytecompiler adds resources after each using and cleans up at end of blocks

Works like:

{
  using file = openFile();
} // ← auto disposed here

{
  using a = one();
  using b = two();
} // disposes b then a

{
  using outer = ...;
  { using inner = ...; } // inner first
} // then outer

using x = null; // just skips, no crash

Signed-off-by: Abhinav Sharma <abhinavs1920bpl@gmail.com>
@github-actions github-actions bot added C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics Waiting On Review Waiting on reviews from the maintainers C-VM Issues and PRs related to the Boa Virtual Machine. labels Mar 14, 2026
@github-actions github-actions bot added this to the v1.0.0 milestone Mar 14, 2026
@jedel1043 jedel1043 added Waiting On Author Waiting on PR changes from the author and removed Waiting On Review Waiting on reviews from the maintainers labels Mar 15, 2026
@github-actions github-actions bot added the Waiting On Review Waiting on reviews from the maintainers label Mar 15, 2026
@github-actions
Copy link

github-actions bot commented Mar 15, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 50,073 50,073 0
Ignored 2,072 2,072 0
Failed 818 818 0
Panics 0 0 0
Conformance 94.54% 94.54% 0.00%

Tested main commit: 50f0103d9188d33862efbc38397a81e497973632
Tested PR commit: a684036e5e486d0e3a29ab0ac5f19a58dba664b2
Compare commits: 50f0103...a684036

@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 63.63636% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.46%. Comparing base (6ddc2b4) to head (a684036).
⚠️ Report is 886 commits behind head on main.

Files with missing lines Patch % Lines
...engine/src/vm/opcode/disposal/dispose_resources.rs 56.25% 7 Missing ⚠️
core/engine/src/vm/flowgraph/mod.rs 0.00% 6 Missing ⚠️
core/engine/src/bytecompiler/mod.rs 16.66% 5 Missing ⚠️
core/engine/src/vm/code_block.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5079       +/-   ##
===========================================
+ Coverage   47.24%   59.46%   +12.21%     
===========================================
  Files         476      582      +106     
  Lines       46892    63236    +16344     
===========================================
+ Hits        22154    37603    +15449     
- Misses      24738    25633      +895     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Abhinav Sharma <abhinavs1920bpl@gmail.com>
Signed-off-by: Abhinav Sharma <abhinavs1920bpl@gmail.com>
@abhinavs1920 abhinavs1920 marked this pull request as ready for review March 16, 2026 09:07
@abhinavs1920 abhinavs1920 requested a review from a team as a code owner March 16, 2026 09:07
abhinavs1920 and others added 2 commits March 17, 2026 03:29
…es opcode, removing runtime scope depth tracking

Signed-off-by: Abhinav Sharma <abhinavs1920bpl@gmail.com>
@jedel1043 jedel1043 removed the Waiting On Author Waiting on PR changes from the author label Mar 17, 2026
Copy link
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, I really like the staged approach you're doing to implement the feature.

One thing though is that we need to gate everything behind the experimental feature while the proposal is not in stage 4. The parser can stay as it is since that usually doesn't get gated, but the bytecompiler should throw an error instruction instead of pushing a resource to the resources stack if the feature is not enabled.

@jedel1043 jedel1043 added Waiting On Author Waiting on PR changes from the author and removed Waiting On Review Waiting on reviews from the maintainers labels Mar 18, 2026
@abhinavs1920
Copy link
Contributor Author

Done, pushed the fix. Makes sense to keep unfinished proposals gated, I should've caught that myself looking at how import.defer was handled :)

Quick question while I'm here: for the next PR I'm planning to wire up disposal into the exception handling path (try-finally integration + SuppressedError). Is there anything specific about how the current handler/finally mechanism works that I should be aware of before diving in, or is the existing try.rs in the bytecompiler a good enough starting point?

Signed-off-by: Abhinav Sharma <abhinavs1920bpl@gmail.com>
@jedel1043
Copy link
Member

Yes, try.rs is half of the solution but the other half lives in https://github.com/boa-dev/boa/blob/main/core/engine/src/bytecompiler/jump_control.rs, which is our jump handler tracking code that ensures finally statements are always executed even when returning from functions or breaking from loops.

I imagine you can reuse the code that we have for try-finally to integrate using statements, since those are very similar in concept

@abhinavs1920
Copy link
Contributor Author

Just went through both files. The picture is clearer now, jump_control.rs is basically the glue that makes sure any break, continue, or return inside a try block doesn't just jump past the finally. It does this by collecting JumpRecords and routing them through a JumpTable at the end of the finally block so each one ends up at the right destination after disposal runs.

For using, I think the approach would be: push a JumpControlInfo with TRY_WITH_FINALLY semantics when entering a block that has using declarations, wrap the block body in a handler (like push_handler does for try), and on exit emit DisposeResources in the same spot compile_finally_stmt runs today. That way breaks/returns/exceptions all naturally route through disposal before leaving the scope.

Does that sound like the right direction, or is there a cleaner way you had in mind :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Builtins PRs and Issues related to builtins/intrinsics C-Tests Issues and PRs related to the tests. C-VM Issues and PRs related to the Boa Virtual Machine. Waiting On Author Waiting on PR changes from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement explicit resource management proposal

2 participants