Unverified Commit 0af39705 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Fix a bug where our `global` rewrite could introduce a TDZ issue (#82659)

Change our `global` rewrite to use a non-shadowable property on `__turbopack_context__`

The previous rewrite to `globalThis` would break if there was a local declaration of `let globalThis` which could either trigger a TDZ error or simply evaluate to the wrong value. 


Closes PACK-5256
Fixes #82632
parent 50524de0
...@@ -137,4 +137,5 @@ interface TurbopackBaseContext<M> { ...@@ -137,4 +137,5 @@ interface TurbopackBaseContext<M> {
x: ExternalRequire x: ExternalRequire
y: ExternalImport y: ExternalImport
z: CommonJsRequire z: CommonJsRequire
g: typeof globalThis
} }
...@@ -686,6 +686,9 @@ function requireStub(_moduleId: ModuleId): never { ...@@ -686,6 +686,9 @@ function requireStub(_moduleId: ModuleId): never {
} }
contextPrototype.z = requireStub contextPrototype.z = requireStub
// Make `globalThis` available to the module in a way that cannot be shadowed by a local variable.
contextPrototype.g = globalThis
type ContextConstructor<M> = { type ContextConstructor<M> = {
new (module: Module, exports: Exports): TurbopackBaseContext<M> new (module: Module, exports: Exports): TurbopackBaseContext<M>
} }
......
...@@ -159,7 +159,7 @@ use crate::{ ...@@ -159,7 +159,7 @@ use crate::{
util::InlineSourceMap, util::InlineSourceMap,
}, },
runtime_functions::{ runtime_functions::{
TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_EXPORT_VALUE, TURBOPACK_EXPORTS, TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_EXPORT_VALUE, TURBOPACK_EXPORTS, TURBOPACK_GLOBAL,
TURBOPACK_REQUIRE_REAL, TURBOPACK_REQUIRE_STUB, TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS, TURBOPACK_REQUIRE_REAL, TURBOPACK_REQUIRE_STUB, TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS,
}, },
tree_shake::{find_turbopack_part_id_in_asserts, part_of_module, split}, tree_shake::{find_turbopack_part_id_in_asserts, part_of_module, split},
...@@ -1506,7 +1506,8 @@ async fn compile_time_info_for_module_type( ...@@ -1506,7 +1506,8 @@ async fn compile_time_info_for_module_type(
InputRelativeConstant::FileName, InputRelativeConstant::FileName,
)); ));
// Compiletime rewrite the nodejs `global` to `globalThis` // Compiletime rewrite the nodejs `global` to `__turbopack_context_.g` which is a shortcut for
// `globalThis` that cannot be shadowed by a local variable.
let global = rcstr!("global"); let global = rcstr!("global");
free_var_references free_var_references
.entry(vec![ .entry(vec![
...@@ -1516,7 +1517,7 @@ async fn compile_time_info_for_module_type( ...@@ -1516,7 +1517,7 @@ async fn compile_time_info_for_module_type(
.or_insert(rcstr!("object").into()); .or_insert(rcstr!("object").into());
free_var_references free_var_references
.entry(vec![DefinableNameSegment::Name(global)]) .entry(vec![DefinableNameSegment::Name(global)])
.or_insert(FreeVarReference::Ident(rcstr!("globalThis"))); .or_insert(TURBOPACK_GLOBAL.into());
free_var_references.extend(TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS.into_iter().map( free_var_references.extend(TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS.into_iter().map(
|(name, shortcut)| { |(name, shortcut)| {
......
...@@ -53,60 +53,45 @@ impl<'l> From<&'l TurbopackRuntimeFunctionShortcut> for &'l str { ...@@ -53,60 +53,45 @@ impl<'l> From<&'l TurbopackRuntimeFunctionShortcut> for &'l str {
} }
} }
pub const TURBOPACK_EXPORTS: &TurbopackRuntimeFunctionShortcut = macro_rules! make_shortcut {
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.e", "e"); ($shortcut:expr) => {
pub const TURBOPACK_MODULE: &TurbopackRuntimeFunctionShortcut = const {
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.m", "m"); &TurbopackRuntimeFunctionShortcut::new(
pub const TURBOPACK_REQUIRE: &TurbopackRuntimeFunctionShortcut = concat!("__turbopack_context__.", $shortcut),
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.r", "r"); $shortcut,
pub const TURBOPACK_ASYNC_LOADER: &TurbopackRuntimeFunctionShortcut = )
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.A", "A"); }
pub const TURBOPACK_MODULE_CONTEXT: &TurbopackRuntimeFunctionShortcut = };
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.f", "f"); }
pub const TURBOPACK_IMPORT: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.i", "i"); pub const TURBOPACK_EXPORTS: &TurbopackRuntimeFunctionShortcut = make_shortcut!("e");
pub const TURBOPACK_ESM: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("m");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.s", "s"); pub const TURBOPACK_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("r");
pub const TURBOPACK_EXPORT_VALUE: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_ASYNC_LOADER: &TurbopackRuntimeFunctionShortcut = make_shortcut!("A");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.v", "v"); pub const TURBOPACK_MODULE_CONTEXT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("f");
pub const TURBOPACK_EXPORT_NAMESPACE: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("i");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.n", "n"); pub const TURBOPACK_ESM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("s");
pub const TURBOPACK_CACHE: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_EXPORT_VALUE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("v");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.c", "c"); pub const TURBOPACK_EXPORT_NAMESPACE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("n");
pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("c");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.M", "M"); pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut = make_shortcut!("M");
pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut = make_shortcut!("l");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.l", "l"); pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("L");
pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("C");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.L", "L"); pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut = make_shortcut!("j");
pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("P");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.C", "C"); pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("U");
pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("R");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.j", "j"); pub const TURBOPACK_WORKER_BLOB_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("b");
pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("a");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.P", "P"); pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("x");
pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("y");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.U", "U"); pub const TURBOPACK_REFRESH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("k");
pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut = make_shortcut!("z");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.R", "R"); pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("t");
pub const TURBOPACK_WORKER_BLOB_URL: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("w");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.b", "b"); pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("u");
pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut = pub const TURBOPACK_GLOBAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("g");
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.a", "a");
pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.x", "x");
pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.y", "y");
pub const TURBOPACK_REFRESH: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.k", "k");
pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.z", "z");
pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.t", "t");
pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.w", "w");
pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut =
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.u", "u");
/// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called /// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called
/// from user code (by inserting a replacement into free_var_references) /// from user code (by inserting a replacement into free_var_references)
......
console.log(global)
let globalThis = 2
it('should rewrite global', () => {
expect(typeof global).toBe('object')
expect(typeof globalThis).toBe('number')
})
...@@ -460,6 +460,8 @@ contextPrototype.U = relativeURL; ...@@ -460,6 +460,8 @@ contextPrototype.U = relativeURL;
throw new Error('dynamic usage of require is not supported'); throw new Error('dynamic usage of require is not supported');
} }
contextPrototype.z = requireStub; contextPrototype.z = requireStub;
// Make `globalThis` available to the module in a way that cannot be shadowed by a local variable.
contextPrototype.g = globalThis;
function applyModuleFactoryName(factory) { function applyModuleFactoryName(factory) {
// Give the module factory a nice name to improve stack traces. // Give the module factory a nice name to improve stack traces.
Object.defineProperty(factory, 'name', { Object.defineProperty(factory, 'name', {
......
...@@ -470,6 +470,8 @@ contextPrototype.U = relativeURL; ...@@ -470,6 +470,8 @@ contextPrototype.U = relativeURL;
throw new Error('dynamic usage of require is not supported'); throw new Error('dynamic usage of require is not supported');
} }
contextPrototype.z = requireStub; contextPrototype.z = requireStub;
// Make `globalThis` available to the module in a way that cannot be shadowed by a local variable.
contextPrototype.g = globalThis;
function applyModuleFactoryName(factory) { function applyModuleFactoryName(factory) {
// Give the module factory a nice name to improve stack traces. // Give the module factory a nice name to improve stack traces.
Object.defineProperty(factory, 'name', { Object.defineProperty(factory, 'name', {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment