aboutsummaryrefslogtreecommitdiff
path: root/src/bun_js.zig
blob: fce2108bdc98091d64e6f3c87b1324007f13bcf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const bun = @import("bun");
const string = bun.string;
const Output = bun.Output;
const Global = bun.Global;
const Environment = bun.Environment;
const strings = bun.strings;
const MutableString = bun.MutableString;
const stringZ = bun.stringZ;
const default_allocator = bun.default_allocator;
const C = bun.C;
const std = @import("std");

const lex = @import("js_lexer.zig");
const logger = @import("bun").logger;
const options = @import("options.zig");
const js_parser = @import("js_parser.zig");
const json_parser = @import("json_parser.zig");
const js_printer = @import("js_printer.zig");
const js_ast = @import("js_ast.zig");
const linker = @import("linker.zig");

const sync = @import("./sync.zig");
const Api = @import("api/schema.zig").Api;
const resolve_path = @import("./resolver/resolve_path.zig");
const configureTransformOptionsForBun = @import("./bun.js/config.zig").configureTransformOptionsForBun;
const Command = @import("cli.zig").Command;
const bundler = @import("bundler.zig");
const NodeModuleBundle = @import("node_module_bundle.zig").NodeModuleBundle;
const DotEnv = @import("env_loader.zig");
const which = @import("which.zig").which;
const JSC = @import("bun").JSC;
const AsyncHTTP = @import("bun").HTTP.AsyncHTTP;
const Arena = @import("./mimalloc_arena.zig").Arena;

const OpaqueWrap = JSC.OpaqueWrap;
const VirtualMachine = JSC.VirtualMachine;

pub const Run = struct {
    file: std.fs.File,
    ctx: Command.Context,
    vm: *VirtualMachine,
    entry_path: string,
    arena: Arena = undefined,

    pub fn boot(ctx: Command.Context, file: std.fs.File, entry_path: string) !void {
        JSC.markBinding(@src());
        @import("bun.js/javascript_core_c_api.zig").JSCInitialize();

        js_ast.Expr.Data.Store.create(default_allocator);
        js_ast.Stmt.Data.Store.create(default_allocator);
        var arena = try Arena.init();

        var run = Run{
            .vm = try VirtualMachine.init(arena.allocator(), ctx.args, null, ctx.log, null),
            .file = file,
            .arena = arena,
            .ctx = ctx,
            .entry_path = entry_path,
        };
        run.vm.argv = ctx.passthrough;
        run.vm.arena = &run.arena;
        run.vm.allocator = arena.allocator();

        run.vm.bundler.options.install = ctx.install;
        run.vm.bundler.resolver.opts.install = ctx.install;
        run.vm.bundler.resolver.opts.global_cache = ctx.debug.global_cache;
        run.vm.bundler.resolver.opts.prefer_offline_install = (ctx.debug.offline_mode_setting orelse .online) == .offline;
        run.vm.bundler.resolver.opts.prefer_latest_install = (ctx.debug.offline_mode_setting orelse .online) == .latest;
        run.vm.bundler.options.global_cache = run.vm.bundler.resolver.opts.global_cache;
        run.vm.bundler.options.prefer_offline_install = run.vm.bundler.resolver.opts.prefer_offline_install;
        run.vm.bundler.options.prefer_latest_install = run.vm.bundler.resolver.opts.prefer_latest_install;
        run.vm.bundler.resolver.env_loader = run.vm.bundler.env;

        if (ctx.debug.macros) |macros| {
            run.vm.bundler.options.macro_remap = macros;
        }

        run.vm.bundler.configureRouter(false) catch {
            if (Output.enable_ansi_colors_stderr) {
                run.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
            } else {
                run.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
            }
            Output.prettyErrorln("\n", .{});
            Global.exit(1);
        };
        run.vm.bundler.configureDefines() catch {
            if (Output.enable_ansi_colors_stderr) {
                run.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
            } else {
                run.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
            }
            Output.prettyErrorln("\n", .{});
            Global.exit(1);
        };
        AsyncHTTP.max_simultaneous_requests = 255;

        if (run.vm.bundler.env.map.get("BUN_CONFIG_MAX_HTTP_REQUESTS")) |max_http_requests| {
            load: {
                AsyncHTTP.max_simultaneous_requests = std.fmt.parseInt(u16, max_http_requests, 10) catch {
                    run.vm.log.addErrorFmt(
                        null,
                        logger.Loc.Empty,
                        run.vm.allocator,
                        "BUN_CONFIG_MAX_HTTP_REQUESTS value \"{s}\" is not a valid integer between 1 and 65535",
                        .{max_http_requests},
                    ) catch unreachable;
                    break :load;
                };

                if (AsyncHTTP.max_simultaneous_requests == 0) {
                    run.vm.log.addWarningFmt(
                        null,
                        logger.Loc.Empty,
                        run.vm.allocator,
                        "BUN_CONFIG_MAX_HTTP_REQUESTS value must be a number between 1 and 65535",
                        .{},
                    ) catch unreachable;
                    AsyncHTTP.max_simultaneous_requests = 255;
                }
            }
        }

        run.vm.loadExtraEnv();
        run.vm.is_main_thread = true;
        JSC.VirtualMachine.is_main_thread_vm = true;

        var callback = OpaqueWrap(Run, Run.start);
        run.vm.global.vm().holdAPILock(&run, callback);
    }

    pub fn start(this: *Run) void {
        if (this.ctx.debug.hot_reload) {
            JSC.HotReloader.enableHotModuleReloading(this.vm);
        }
        var promise = this.vm.loadEntryPoint(this.entry_path) catch return;

        if (promise.status(this.vm.global.vm()) == .Rejected) {
            this.vm.runErrorHandler(promise.result(this.vm.global.vm()), null);
            Global.exit(1);
        }

        _ = promise.result(this.vm.global.vm());

        if (this.vm.log.msgs.items.len > 0) {
            if (Output.enable_ansi_colors) {
                this.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
            } else {
                this.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
            }
            Output.prettyErrorln("\n", .{});
            Output.flush();
        }

        // don't run the GC if we don't actually need to
        if (this.vm.eventLoop().tasks.count > 0 or this.vm.active_tasks > 0 or
            this.vm.uws_event_loop.?.active > 0 or
            this.vm.eventLoop().tickConcurrentWithCount() > 0)
        {
            this.vm.global.vm().releaseWeakRefs();
            _ = this.vm.arena.gc(false);
            _ = this.vm.global.vm().runGC(false);
            this.vm.tick();
        }

        {
            while (this.vm.eventLoop().tasks.count > 0 or this.vm.active_tasks > 0 or this.vm.uws_event_loop.?.active > 0) {
                this.vm.tick();
                this.vm.eventLoop().autoTickActive();
            }

            if (this.vm.log.msgs.items.len > 0) {
                if (Output.enable_ansi_colors) {
                    this.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
                } else {
                    this.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
                }
                Output.prettyErrorln("\n", .{});
                Output.flush();
            }
        }

        this.vm.onExit();

        if (!JSC.is_bindgen) JSC.napi.fixDeadCodeElimination();

        Global.exit(0);
    }
};