diff options
author | 2021-07-28 10:56:36 -0700 | |
---|---|---|
committer | 2021-07-28 10:56:36 -0700 | |
commit | 68fa7ec2d0bc3549b398929ad9794783be451b7c (patch) | |
tree | feb1331ea93b6f69905f250269f72452304b0c6c /src/javascript/jsc/javascript.zig | |
parent | d18ff76912f6c4522132adfb7c0af1db7c5b6726 (diff) | |
download | bun-68fa7ec2d0bc3549b398929ad9794783be451b7c.tar.gz bun-68fa7ec2d0bc3549b398929ad9794783be451b7c.tar.zst bun-68fa7ec2d0bc3549b398929ad9794783be451b7c.zip |
WIP
Former-commit-id: 60cc85dc0652b34e9c7ec409f32ba635cc4b2e51
Diffstat (limited to 'src/javascript/jsc/javascript.zig')
-rw-r--r-- | src/javascript/jsc/javascript.zig | 106 |
1 files changed, 1 insertions, 105 deletions
diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 04e7a9d80..c8ce409af 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -15,111 +15,7 @@ const http = @import("../../http.zig"); usingnamespace @import("./node_env_buf_map.zig"); usingnamespace @import("./base.zig"); usingnamespace @import("./webcore/response.zig"); - -const DefaultSpeedyDefines = struct { - pub const Keys = struct { - const window = "window"; - }; - pub const Values = struct { - const window = "undefined"; - }; -}; - -pub fn configureTransformOptionsForSpeedy(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions { - var args = _args; - - args.platform = Api.Platform.speedy; - args.serve = false; - args.write = false; - args.resolve = Api.ResolveMode.lazy; - args.generate_node_module_bundle = false; - - // We inline process.env.* at bundle time but process.env is a proxy object which will otherwise return undefined. - - var env_map = try getNodeEnvMap(allocator); - var env_count = env_map.count(); - - if (args.define) |def| { - for (def.keys) |key| { - env_count += @boolToInt((env_map.get(key) == null)); - } - } - var needs_node_env = env_map.get("NODE_ENV") == null; - var needs_window_undefined = true; - - var needs_regenerate = args.define == null and env_count > 0; - if (args.define) |def| { - if (def.keys.len != env_count) { - needs_regenerate = true; - } - for (def.keys) |key| { - if (strings.eql(key, "process.env.NODE_ENV")) { - needs_node_env = false; - } else if (strings.eql(key, "window")) { - needs_window_undefined = false; - } - } - } - - var extras_count = @intCast(usize, @boolToInt(needs_node_env)) + @intCast(usize, @boolToInt(needs_window_undefined)); - - if (needs_regenerate) { - var new_list = try allocator.alloc([]const u8, env_count * 2 + extras_count * 2); - var keys = new_list[0 .. new_list.len / 2]; - var values = new_list[keys.len..]; - var new_map = Api.StringMap{ - .keys = keys, - .values = values, - }; - var iter = env_map.iterator(); - - var last: usize = 0; - while (iter.next()) |entry| { - keys[last] = entry.key_ptr.*; - var value = entry.value_ptr.*; - - if (value.len == 0 or value[0] != '"' or value[value.len - 1] != '"') { - value = try std.fmt.allocPrint(allocator, "\"{s}\"", .{value}); - } - values[last] = value; - last += 1; - } - - if (args.define) |def| { - var from_env = keys[0..last]; - - for (def.keys) |pre, i| { - if (env_map.get(pre) != null) { - for (from_env) |key, j| { - if (strings.eql(key, pre)) { - values[j] = def.values[i]; - } - } - } else { - keys[last] = pre; - values[last] = def.values[i]; - last += 1; - } - } - } - - if (needs_node_env) { - keys[last] = options.DefaultUserDefines.NodeEnv.Key; - values[last] = options.DefaultUserDefines.NodeEnv.Value; - last += 1; - } - - if (needs_window_undefined) { - keys[last] = DefaultSpeedyDefines.Keys.window; - values[last] = DefaultSpeedyDefines.Values.window; - last += 1; - } - - args.define = new_map; - } - - return args; -} +usingnamespace @import("./config.zig"); // If you read JavascriptCore/API/JSVirtualMachine.mm - https://github.com/WebKit/WebKit/blob/acff93fb303baa670c055cb24c2bad08691a01a0/Source/JavaScriptCore/API/JSVirtualMachine.mm#L101 // We can see that it's sort of like std.mem.Allocator but for JSGlobalContextRef, to support Automatic Reference Counting |