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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
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 = bun.js_lexer;
const logger = @import("bun").logger;
const options = @import("options.zig");
const js_parser = bun.js_parser;
const json_parser = bun.JSON;
const js_printer = bun.js_printer;
const js_ast = bun.JSAst;
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 = bun.bundler;
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;
var run: Run = undefined;
pub const Run = struct {
file: std.fs.File,
ctx: Command.Context,
vm: *VirtualMachine,
entry_path: string,
arena: Arena = undefined,
any_unhandled: bool = false,
pub fn boot(ctx_: Command.Context, file: std.fs.File, entry_path: string) !void {
var ctx = ctx_;
JSC.markBinding(@src());
bun.JSC.initialize();
js_ast.Expr.Data.Store.create(default_allocator);
js_ast.Stmt.Data.Store.create(default_allocator);
var arena = try Arena.init();
if (!ctx.debug.loaded_bunfig) {
try bun.CLI.Arguments.loadConfigPath(ctx.allocator, true, "bunfig.toml", &ctx, .RunCommand);
}
run = .{
.vm = try VirtualMachine.init(arena.allocator(), ctx.args, null, ctx.log, null),
.file = file,
.arena = arena,
.ctx = ctx,
.entry_path = entry_path,
};
var vm = run.vm;
var b = &vm.bundler;
vm.preload = ctx.preloads;
vm.argv = ctx.passthrough;
vm.arena = &run.arena;
vm.allocator = arena.allocator();
b.options.install = ctx.install;
b.resolver.opts.install = ctx.install;
b.resolver.opts.global_cache = ctx.debug.global_cache;
b.resolver.opts.prefer_offline_install = (ctx.debug.offline_mode_setting orelse .online) == .offline;
b.resolver.opts.prefer_latest_install = (ctx.debug.offline_mode_setting orelse .online) == .latest;
b.options.global_cache = b.resolver.opts.global_cache;
b.options.prefer_offline_install = b.resolver.opts.prefer_offline_install;
b.options.prefer_latest_install = b.resolver.opts.prefer_latest_install;
b.resolver.env_loader = b.env;
if (ctx.debug.macros) |macros| {
b.options.macro_remap = macros;
}
b.configureRouter(false) catch {
if (Output.enable_ansi_colors_stderr) {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
Output.prettyErrorln("\n", .{});
Global.exit(1);
};
b.configureDefines() catch {
if (Output.enable_ansi_colors_stderr) {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
Output.prettyErrorln("\n", .{});
Global.exit(1);
};
AsyncHTTP.loadEnv(vm.allocator, vm.log, b.env);
vm.loadExtraEnv();
vm.is_main_thread = true;
JSC.VirtualMachine.is_main_thread_vm = true;
var callback = OpaqueWrap(Run, Run.start);
vm.global.vm().holdAPILock(&run, callback);
}
fn onUnhandledRejectionBeforeClose(this: *JSC.VirtualMachine, _: *JSC.JSGlobalObject, value: JSC.JSValue) void {
this.runErrorHandler(value, null);
run.any_unhandled = true;
}
pub fn start(this: *Run) void {
var vm = this.vm;
vm.hot_reload = this.ctx.debug.hot_reload;
if (this.ctx.debug.hot_reload != .none) {
JSC.HotReloader.enableHotModuleReloading(vm);
}
if (vm.loadEntryPoint(this.entry_path)) |promise| {
if (promise.status(vm.global.vm()) == .Rejected) {
vm.runErrorHandler(promise.result(vm.global.vm()), null);
if (vm.hot_reload != .none) {
vm.eventLoop().tick();
vm.eventLoop().tickPossiblyForever();
} else {
Global.exit(1);
}
}
_ = promise.result(vm.global.vm());
if (vm.log.msgs.items.len > 0) {
if (Output.enable_ansi_colors) {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
Output.prettyErrorln("\n", .{});
Output.flush();
}
} else |err| {
if (vm.log.msgs.items.len > 0) {
if (Output.enable_ansi_colors) {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
Output.flush();
} else {
Output.prettyErrorln("Error occurred loading entry point: {s}", .{@errorName(err)});
}
if (vm.hot_reload != .none) {
vm.eventLoop().tick();
vm.eventLoop().tickPossiblyForever();
} else {
Global.exit(1);
}
}
// don't run the GC if we don't actually need to
if (vm.eventLoop().tasks.count > 0 or vm.active_tasks > 0 or
vm.uws_event_loop.?.active > 0 or
vm.eventLoop().tickConcurrentWithCount() > 0)
{
vm.global.vm().releaseWeakRefs();
_ = vm.arena.gc(false);
_ = vm.global.vm().runGC(false);
vm.tick();
}
{
if (this.vm.isWatcherEnabled()) {
var prev_promise = this.vm.pending_internal_promise;
if (prev_promise.status(vm.global.vm()) == .Rejected) {
vm.onUnhandledError(this.vm.global, this.vm.pending_internal_promise.result(vm.global.vm()));
}
while (true) {
while (vm.eventLoop().tasks.count > 0 or vm.active_tasks > 0 or vm.uws_event_loop.?.active > 0) {
vm.tick();
// Report exceptions in hot-reloaded modules
if (this.vm.pending_internal_promise.status(vm.global.vm()) == .Rejected and prev_promise != this.vm.pending_internal_promise) {
prev_promise = this.vm.pending_internal_promise;
vm.onUnhandledError(this.vm.global, this.vm.pending_internal_promise.result(vm.global.vm()));
continue;
}
vm.eventLoop().autoTickActive();
}
if (this.vm.pending_internal_promise.status(vm.global.vm()) == .Rejected and prev_promise != this.vm.pending_internal_promise) {
prev_promise = this.vm.pending_internal_promise;
vm.onUnhandledError(this.vm.global, this.vm.pending_internal_promise.result(vm.global.vm()));
}
vm.eventLoop().tickPossiblyForever();
}
if (this.vm.pending_internal_promise.status(vm.global.vm()) == .Rejected and prev_promise != this.vm.pending_internal_promise) {
prev_promise = this.vm.pending_internal_promise;
vm.onUnhandledError(this.vm.global, this.vm.pending_internal_promise.result(vm.global.vm()));
}
} else {
while (vm.eventLoop().tasks.count > 0 or vm.active_tasks > 0 or vm.uws_event_loop.?.active > 0) {
vm.tick();
vm.eventLoop().autoTickActive();
}
}
if (vm.log.msgs.items.len > 0) {
if (Output.enable_ansi_colors) {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
Output.prettyErrorln("\n", .{});
Output.flush();
}
}
vm.onUnhandledRejection = &onUnhandledRejectionBeforeClose;
vm.global.handleRejectedPromises();
vm.onExit();
if (!JSC.is_bindgen) JSC.napi.fixDeadCodeElimination();
Global.exit(@boolToInt(this.any_unhandled));
}
};
|