diff options
author | 2021-09-17 14:24:42 -0700 | |
---|---|---|
committer | 2021-09-17 14:24:42 -0700 | |
commit | 98e9b93b7ef0ad5c99461cd58c58e7cccece73e8 (patch) | |
tree | 8e4246c4cc878eee1c0ce0f5cc9a282de59ce5ee /src | |
parent | 3350e63004007d30eacb03ec2da5ab02138e0943 (diff) | |
download | bun-98e9b93b7ef0ad5c99461cd58c58e7cccece73e8.tar.gz bun-98e9b93b7ef0ad5c99461cd58c58e7cccece73e8.tar.zst bun-98e9b93b7ef0ad5c99461cd58c58e7cccece73e8.zip |
global -> globalThis
Diffstat (limited to '')
-rw-r--r-- | src/defines.zig | 43 | ||||
-rw-r--r-- | src/runtime.version | 2 |
2 files changed, 30 insertions, 15 deletions
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 |