Skip to content

vm: allow global proxy reuse across contexts - #65477

Closed
koal44 wants to merge 2 commits into
nodejs:mainfrom
koal44:vm-global-proxy-reuse
Closed

koal44 wants to merge 2 commits into
nodejs:mainfrom
koal44:vm-global-proxy-reuse

Conversation

@koal44

@koal44 koal44 commented Aug 22, 2026 •

Copy link
Copy Markdown

Cross-document navigation in a browsing context creates a new Window and realm while preserving the identity of its WindowProxy/globalThis. Modeling this behavior with node:vm is currently not possible, as discussed in #855 and #31807.

This PR adds a reuseGlobalProxy option to vm.createContext(). When supplied a global proxy produced with vm.constants.DONT_CONTEXTIFY, Node.js detaches it from the old context and attaches it to a new one.

The implementation was inspired by Chromium’s approach: detaching the old context, then passing the preserved global proxy into the replacement context.

Conceptually:

old_context->DetachGlobal();
new_context = Context::New(/* ... */, old_global_proxy);

Signed-off-by: koal44 <random.var.ny@gmail.com>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Aug 22, 2026
Signed-off-by: koal44 <random.var.ny@gmail.com>
@koal44

koal44 commented Sep 3, 2026 •

Copy link
Copy Markdown
Author

I’m closing this version while I continue exploring what an HTML embedder needs from Node's vm API. Using this in my own project showed that global-proxy reuse needs a clearer context lifecycle. I now favor something more like:

const first = vm.createContextHandle();
const windowProxy = first.globalProxy;

first.detachGlobal();

const second = vm.createContextHandle({
  reuseGlobalProxyFrom: first,
});

console.log(second.globalProxy === windowProxy); // true

I'll return once I have a better WPT-backed story for the API.

@koal44 koal44 closed this Sep 3, 2026
@koal44
koal44 deleted the vm-global-proxy-reuse branch September 5, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants