Skip to main content

run_chunk_in_global_scope

Function run_chunk_in_global_scope 

Source
pub fn run_chunk_in_global_scope(chunk: Chunk) -> Result<Value, String>
Expand description

Run chunk in the GLOBAL scope instead of the caller’s.

run_chunk_on executes on whatever frame is current, so a nested run sees — and can shadow — the calling function’s locals. That is right for a direct eval, and wrong for every other runtime-source construct: a new Function body, an indirect eval and vm.runInThisContext are all specified to run in the global scope (ECMA-262 19.2.1.1 PerformEval with a null strictCaller/direct pair; FunctionBody is instantiated with the global environment, 20.2.1.1.1 step 26). Measured against node v26.7.0, function outer(){ let loc = 42; return vm.runInThisContext('typeof loc'); } is "undefined" there and was "number" here.

A var the chunk itself declares lands in the top-level scope and persists, so successive vm.runInThisContext calls share it.