diff options
author | 2021-09-17 14:24:42 -0700 | |
---|---|---|
committer | 2021-09-17 14:24:42 -0700 | |
commit | 98e9b93b7ef0ad5c99461cd58c58e7cccece73e8 (patch) | |
tree | 8e4246c4cc878eee1c0ce0f5cc9a282de59ce5ee | |
parent | 3350e63004007d30eacb03ec2da5ab02138e0943 (diff) | |
download | bun-98e9b93b7ef0ad5c99461cd58c58e7cccece73e8.tar.gz bun-98e9b93b7ef0ad5c99461cd58c58e7cccece73e8.tar.zst bun-98e9b93b7ef0ad5c99461cd58c58e7cccece73e8.zip |
global -> globalThis
-rw-r--r-- | integration/scripts/browser.js | 1 | ||||
-rw-r--r-- | integration/snapshots-no-hmr/global-is-remapped-to-globalThis.js | 4 | ||||
-rw-r--r-- | integration/snapshots/global-is-remapped-to-globalThis.js | 26 | ||||
-rw-r--r-- | integration/snippets/global-is-remapped-to-globalThis.js | 4 | ||||
-rw-r--r-- | src/defines.zig | 43 | ||||
-rw-r--r-- | src/runtime.version | 2 |
6 files changed, 65 insertions, 15 deletions
diff --git a/integration/scripts/browser.js b/integration/scripts/browser.js index 7aa027d77..6ec075b11 100644 --- a/integration/scripts/browser.js +++ b/integration/scripts/browser.js @@ -90,6 +90,7 @@ async function main() { "/bundled-entry-point.js", "/export.js", "/type-only-imports.ts", + "/global-is-remapped-to-globalThis.js", ]; for (let test of tests) { diff --git a/integration/snapshots-no-hmr/global-is-remapped-to-globalThis.js b/integration/snapshots-no-hmr/global-is-remapped-to-globalThis.js new file mode 100644 index 000000000..9000d14b0 --- /dev/null +++ b/integration/snapshots-no-hmr/global-is-remapped-to-globalThis.js @@ -0,0 +1,4 @@ +export function test() { + console.assert(globalThis === globalThis); + return testDone(import.meta.url); +} diff --git a/integration/snapshots/global-is-remapped-to-globalThis.js b/integration/snapshots/global-is-remapped-to-globalThis.js new file mode 100644 index 000000000..dace8fa7a --- /dev/null +++ b/integration/snapshots/global-is-remapped-to-globalThis.js @@ -0,0 +1,26 @@ +import { +__HMRModule as HMR +} from "http://localhost:8080/__runtime.js"; +import { +__HMRClient as Bun +} from "http://localhost:8080/__runtime.js"; +Bun.activate(true); + +var hmr = new HMR(713665787, "global-is-remapped-to-globalThis.js"), exports = hmr.exports; +(hmr._load = function() { + function test() { + console.assert(globalThis === globalThis); + return testDone(import.meta.url); + } + hmr.exportAll({ + test: () => test + }); +})(); +var $$hmr_test = hmr.exports.test; +hmr._update = function(exports) { + $$hmr_test = exports.test; +}; + +export { + $$hmr_test as test +}; diff --git a/integration/snippets/global-is-remapped-to-globalThis.js b/integration/snippets/global-is-remapped-to-globalThis.js new file mode 100644 index 000000000..06b887925 --- /dev/null +++ b/integration/snippets/global-is-remapped-to-globalThis.js @@ -0,0 +1,4 @@ +export function test() { + console.assert(global === globalThis); + return testDone(import.meta.url); +} diff --git a/src/defines.zig b/src/defines.zig index 8bb96d41e..dc08e7ac8 100644 --- a/src/defines.zig +++ b/src/defines.zig @@ -28,6 +28,13 @@ const defines_path = fs.Path.initWithNamespace("defines.json", "internal"); pub const RawDefines = std.StringArrayHashMap(string); pub const UserDefines = std.StringHashMap(DefineData); +const Rewrites = struct { + pub const global = "global"; + pub const globalThis = "globalThis"; +}; + +var globalThisIdentifier = js_ast.E.Identifier{}; + pub const DefineData = struct { value: js_ast.Expr.Data, valueless: bool = false, @@ -202,7 +209,7 @@ pub const Define = struct { define.allocator = allocator; define.identifiers = std.StringHashMap(IdentifierDefine).init(allocator); define.dots = std.StringHashMap([]DotDefine).init(allocator); - try define.identifiers.ensureCapacity(641 + 2); + try define.identifiers.ensureCapacity(641 + 2 + 1); try define.dots.ensureCapacity(64); var val = js_ast.Expr.Data{ .e_undefined = .{} }; @@ -215,7 +222,7 @@ pub const Define = struct { if (global.len == 1) { // TODO: when https://github.com/ziglang/zig/pull/8596 is merged, switch to putAssumeCapacityNoClobber - define.identifiers.putAssumeCapacity(global[0], value_define); + define.identifiers.putAssumeCapacityNoClobber(global[0], value_define); } else { const key = global[global.len - 1]; // TODO: move this to comptime @@ -318,12 +325,20 @@ pub const Define = struct { try define.dots.put(tail, list.toOwnedSlice()); } } else { + // e.g. IS_BROWSER try define.identifiers.put(user_define_key, user_define.value_ptr.*); } } } + { + var global_entry = define.identifiers.getOrPutAssumeCapacity(Rewrites.global); + if (!global_entry.found_existing) { + global_entry.value_ptr.* = DefineData{ .value = js_ast.Expr.Data{ .e_identifier = &globalThisIdentifier }, .original_name = Rewrites.globalThis }; + } + } + return define; } }; @@ -337,13 +352,13 @@ test "UserDefines" { var log = logger.Log.init(alloc.dynamic); var data = try DefineData.from_input(orig, &log, alloc.dynamic); - expect(data.contains("process.env.NODE_ENV")); - expect(data.contains("globalThis")); + try expect(data.contains("process.env.NODE_ENV")); + try expect(data.contains("globalThis")); const globalThis = data.get("globalThis"); const val = data.get("process.env.NODE_ENV"); - expect(val != null); - expect(strings.utf16EqlString(val.?.value.e_string.value, "development")); - std.testing.expectEqualStrings(globalThis.?.original_name.?, "window"); + try expect(val != null); + try expect(strings.utf16EqlString(val.?.value.e_string.value, "development")); + try std.testing.expectEqualStrings(globalThis.?.original_name.?, "window"); } // 396,000ns was upper end of last time this was checked how long it took @@ -358,12 +373,12 @@ test "Defines" { var defines = try Define.init(alloc.dynamic, data); Output.print("Time: {d}", .{std.time.nanoTimestamp() - start}); const node_env_dots = defines.dots.get("NODE_ENV"); - expect(node_env_dots != null); - expect(node_env_dots.?.len > 0); + try expect(node_env_dots != null); + try expect(node_env_dots.?.len > 0); const node_env = node_env_dots.?[0]; - std.testing.expectEqual(node_env.parts.len, 2); - std.testing.expectEqualStrings("process", node_env.parts[0]); - std.testing.expectEqualStrings("env", node_env.parts[1]); - expect(node_env.data.original_name == null); - expect(strings.utf16EqlString(node_env.data.value.e_string.value, "development")); + try std.testing.expectEqual(node_env.parts.len, 2); + try std.testing.expectEqualStrings("process", node_env.parts[0]); + try std.testing.expectEqualStrings("env", node_env.parts[1]); + try expect(node_env.data.original_name == null); + try expect(strings.utf16EqlString(node_env.data.value.e_string.value, "development")); } diff --git a/src/runtime.version b/src/runtime.version index 92a07a77e..a1ae4839c 100644 --- a/src/runtime.version +++ b/src/runtime.version @@ -1 +1 @@ -4bdccb0aacc8c2f9
\ No newline at end of file +d60f3dbc38e60e11
\ No newline at end of file |