Lots of stuff (#7027)

* Use debug mode by default

* Enable build with assertions enabled

* Update cli.zig

* Update bun-linux-build.yml

* Fixes

* Fix `ASSERT_ENABLED`

* try this

* Update Dockerfile

* mimalloc debug

* Update CMakeLists.txt

* `Bun.deepMatch` - fix assertion failures

cc @dylan-conway, looks like we need to use `putDirectMayBeIndex` and check for `isCell` more carefully.

* Object.create support in code generator and callbacks wrapper

* Remove unused file

* zig upgrade

* zls

* Fix various errors

* Support `BuiltinAccessor` in create_hash_table script

* Fix assertion failure in `process.mainModule`

* Fix assertion failure in `onerror`

* Fix assertion failure when creating a Worker

* Fix asssertion failure when loading lots of files in bun test

* Fix assertion failure when termating a `Worker`

* Add helper for converting BunString to a WTFString

* Fix assertion failure in notifyNeedTermination

* Add more debug logs in `bun test`

* Fix compiler warning in usockets

* Fix assertion failure with `Worker` termination (another)

* Fix assertion failure in `coerceToInt64`

* Fix assertion failure in `BroadcastChannel`

* Fix assertion failure in `Headers.prototype.getAll`

* Fixes #7067

* Add heap analyzer label for CommonJS modules

* Fix assertion failure in module.require && module.require.resolve

* Remove unused code

* Fix assertion failure in debugger

* Fix crash in debugger

* Fix assertion failures in bun:sqlite

* Bump zig

* Bump WebKit

* Fix assertion failure in JSPromise::reject && JSInternalPromise::reject

* Fix assertion failure in ReadableStream::cancel

* Fix assertion failure in AsyncContextFrame::create

* Fix assertion failure in bun:sqlite

* Fix assertion failure in mocks

* Fix assertion failure in ServerWebSocket.close

* Fix assertion failure in N-API with subclasses

* [napi] Make promises cheaper

* undo

* Don't check for exceptions in ObjectInitializationScope

* Add separate entry point for test runner that doesn't generate code

* Don't deref builtin code

* Fix preload test

* Fix assertion failure in memoryUsage()

* Fix preload test, part 2

* Ensure that the env map for a Worker is empty after it is used

* The pointer for the Arena allocator used in parsing should not change

* Terminate thread on exit

* Start to implement scriptExecutionStatus

* Update worker.test.ts

* Fix Dirent.name setter

* Update settings.json

* Fix assertion failure in node:http

* Use correct value for `JSFinalObject::maxInlineCapacity`

* JSFinalObject::maxInlineCapacity x2

* Don't strip when assertions are enabled

* Make `m_wasTerminated` atomic

* Preserve directives in the transpiler

cc @ctjlewis

* Workaround assertion failure in ServerWebSocket.sendBinary and ServerWebSocket.sendText

* windows

* Buffer lockfile serialization in-memory

* PR feedback

* PR feedback

* PR feedback

* Windows

* quotes

* Update CMakeLists.txt

* Update bun-linux-build.yml

* Update bun-linux-build.yml

* Move this code to BunString.cpp

* Update BunString.cpp

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2023-11-14 07:10:09 +01:00
committed by GitHub
parent 228bfbd24a
commit 7a4e0158d6
94 changed files with 1580 additions and 1214 deletions

View File

@@ -582,7 +582,7 @@ pub const VirtualMachine = struct {
modules: ModuleLoader.AsyncModule.Queue = .{},
aggressive_garbage_collection: GCLevel = GCLevel.none,
parser_arena: ?@import("root").bun.ArenaAllocator = null,
parser_arena: ?*@import("root").bun.ArenaAllocator = null,
gc_controller: JSC.GarbageCollectionController = .{},
worker: ?*JSC.WebWorker = null,
@@ -590,6 +590,7 @@ pub const VirtualMachine = struct {
debugger: ?Debugger = null,
has_started_debugger: bool = false,
has_terminated: bool = false,
pub const OnUnhandledRejection = fn (*VirtualMachine, globalObject: *JSC.JSGlobalObject, JSC.JSValue) void;
@@ -875,6 +876,20 @@ pub const VirtualMachine = struct {
}
}
pub fn scriptExecutionStatus(this: *VirtualMachine) callconv(.C) JSC.ScriptExecutionStatus {
if (this.worker) |worker| {
if (worker.requested_terminate) {
return .stopped;
}
}
return .running;
}
comptime {
@export(scriptExecutionStatus, .{ .name = "Bun__VM__scriptExecutionStatus" });
}
pub fn onExit(this: *VirtualMachine) void {
this.exit_handler.dispatchOnExit();
@@ -1160,7 +1175,7 @@ pub const VirtualMachine = struct {
.ref_strings_mutex = Lock.init(),
.file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator),
.standalone_module_graph = opts.graph.?,
.parser_arena = @import("root").bun.ArenaAllocator.init(allocator),
.parser_arena = null,
};
vm.source_mappings = .{ .map = &vm.saved_source_map_table };
vm.regular_event_loop.tasks = EventLoop.Queue.init(
@@ -1269,7 +1284,7 @@ pub const VirtualMachine = struct {
.ref_strings = JSC.RefString.Map.init(allocator),
.ref_strings_mutex = Lock.init(),
.file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator),
.parser_arena = @import("root").bun.ArenaAllocator.init(allocator),
.parser_arena = null,
};
vm.source_mappings = .{ .map = &vm.saved_source_map_table };
vm.regular_event_loop.tasks = EventLoop.Queue.init(
@@ -1406,7 +1421,7 @@ pub const VirtualMachine = struct {
.ref_strings = JSC.RefString.Map.init(allocator),
.ref_strings_mutex = Lock.init(),
.file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator),
.parser_arena = @import("root").bun.ArenaAllocator.init(allocator),
.parser_arena = null,
.standalone_module_graph = worker.parent.standalone_module_graph,
.worker = worker,
};
@@ -1451,7 +1466,7 @@ pub const VirtualMachine = struct {
vm.regular_event_loop.global = vm.global;
vm.regular_event_loop.virtual_machine = vm;
vm.jsc = vm.global.vm();
vm.bundler.setAllocator(allocator);
if (source_code_printer == null) {
var writer = try js_printer.BufferWriter.init(allocator);
source_code_printer = allocator.create(js_printer.BufferPrinter) catch unreachable;
@@ -2024,6 +2039,7 @@ pub const VirtualMachine = struct {
// TODO:
pub fn deinit(this: *VirtualMachine) void {
this.source_mappings.deinit();
this.has_terminated = true;
}
pub const ExceptionList = std.ArrayList(Api.JsException);
@@ -2082,6 +2098,82 @@ pub const VirtualMachine = struct {
this.global.deleteModuleRegistryEntry(&str);
}
fn loadPreloads(this: *VirtualMachine) !?*JSInternalPromise {
this.is_in_preload = true;
defer this.is_in_preload = false;
for (this.preload) |preload| {
var result = switch (this.bundler.resolver.resolveAndAutoInstall(
this.bundler.fs.top_level_dir,
normalizeSource(preload),
.stmt,
.read_only,
)) {
.success => |r| r,
.failure => |e| {
this.log.addErrorFmt(
null,
logger.Loc.Empty,
this.allocator,
"{s} resolving preload {any}",
.{
@errorName(e),
js_printer.formatJSONString(preload),
},
) catch unreachable;
return e;
},
.pending, .not_found => {
this.log.addErrorFmt(
null,
logger.Loc.Empty,
this.allocator,
"preload not found {any}",
.{
js_printer.formatJSONString(preload),
},
) catch unreachable;
return error.ModuleNotFound;
},
};
var promise = JSModuleLoader.import(this.global, &String.fromBytes(result.path().?.text));
this.pending_internal_promise = promise;
JSValue.fromCell(promise).protect();
defer JSValue.fromCell(promise).unprotect();
// pending_internal_promise can change if hot module reloading is enabled
if (this.isWatcherEnabled()) {
this.eventLoop().performGC();
switch (this.pending_internal_promise.status(this.global.vm())) {
JSC.JSPromise.Status.Pending => {
while (this.pending_internal_promise.status(this.global.vm()) == .Pending) {
this.eventLoop().tick();
if (this.pending_internal_promise.status(this.global.vm()) == .Pending) {
this.eventLoop().autoTick();
}
}
},
else => {},
}
} else {
this.eventLoop().performGC();
this.waitForPromise(JSC.AnyPromise{
.Internal = promise,
});
}
if (promise.status(this.global.vm()) == .Rejected)
return promise;
}
// only load preloads once
this.preload.len = 0;
return null;
}
pub fn reloadEntryPoint(this: *VirtualMachine, entry_path: []const u8) !*JSInternalPromise {
this.has_loaded = false;
this.main = entry_path;
@@ -2095,95 +2187,56 @@ pub const VirtualMachine = struct {
);
this.eventLoop().ensureWaker();
var promise: *JSInternalPromise = undefined;
if (this.debugger != null) {
try Debugger.create(this, this.global);
}
if (!this.bundler.options.disable_transpilation) {
if (try this.loadPreloads()) |promise| {
JSC.JSValue.fromCell(promise).ensureStillAlive();
JSC.JSValue.fromCell(promise).protect();
this.pending_internal_promise = promise;
return promise;
}
var promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(main_file_name)) orelse return error.JSError;
this.pending_internal_promise = promise;
JSC.JSValue.fromCell(promise).ensureStillAlive();
return promise;
} else {
var promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(this.main)) orelse return error.JSError;
this.pending_internal_promise = promise;
JSC.JSValue.fromCell(promise).ensureStillAlive();
return promise;
}
}
pub fn reloadEntryPointForTestRunner(this: *VirtualMachine, entry_path: []const u8) !*JSInternalPromise {
this.has_loaded = false;
this.main = entry_path;
this.main_hash = bun.JSC.Watcher.getHash(entry_path);
this.eventLoop().ensureWaker();
if (this.debugger != null) {
try Debugger.create(this, this.global);
}
if (!this.bundler.options.disable_transpilation) {
{
this.is_in_preload = true;
defer this.is_in_preload = false;
for (this.preload) |preload| {
var result = switch (this.bundler.resolver.resolveAndAutoInstall(
this.bundler.fs.top_level_dir,
normalizeSource(preload),
.stmt,
.read_only,
)) {
.success => |r| r,
.failure => |e| {
this.log.addErrorFmt(
null,
logger.Loc.Empty,
this.allocator,
"{s} resolving preload {any}",
.{
@errorName(e),
js_printer.formatJSONString(preload),
},
) catch unreachable;
return e;
},
.pending, .not_found => {
this.log.addErrorFmt(
null,
logger.Loc.Empty,
this.allocator,
"preload not found {any}",
.{
js_printer.formatJSONString(preload),
},
) catch unreachable;
return error.ModuleNotFound;
},
};
promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.fromBytes(result.path().?.text));
if (try this.loadPreloads()) |promise| {
JSC.JSValue.fromCell(promise).ensureStillAlive();
this.pending_internal_promise = promise;
JSC.JSValue.fromCell(promise).protect();
this.pending_internal_promise = promise;
JSValue.fromCell(promise).protect();
defer JSValue.fromCell(promise).unprotect();
// pending_internal_promise can change if hot module reloading is enabled
if (this.isWatcherEnabled()) {
this.eventLoop().performGC();
switch (this.pending_internal_promise.status(this.global.vm())) {
JSC.JSPromise.Status.Pending => {
while (this.pending_internal_promise.status(this.global.vm()) == .Pending) {
this.eventLoop().tick();
if (this.pending_internal_promise.status(this.global.vm()) == .Pending) {
this.eventLoop().autoTick();
}
}
},
else => {},
}
} else {
this.eventLoop().performGC();
this.waitForPromise(JSC.AnyPromise{
.Internal = promise,
});
}
if (promise.status(this.global.vm()) == .Rejected)
return promise;
}
return promise;
}
// only load preloads once
this.preload.len = 0;
promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(main_file_name));
this.pending_internal_promise = promise;
JSC.JSValue.fromCell(promise).ensureStillAlive();
} else {
promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(this.main));
this.pending_internal_promise = promise;
JSC.JSValue.fromCell(promise).ensureStillAlive();
}
var promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.fromBytes(this.main)) orelse return error.JSError;
this.pending_internal_promise = promise;
JSC.JSValue.fromCell(promise).ensureStillAlive();
return promise;
}
@@ -2191,9 +2244,48 @@ pub const VirtualMachine = struct {
pub fn loadEntryPointForWebWorker(this: *VirtualMachine, entry_path: string) anyerror!*JSInternalPromise {
var promise = try this.reloadEntryPoint(entry_path);
this.eventLoop().performGC();
this.waitForPromise(JSC.AnyPromise{
this.eventLoop().waitForPromiseWithTermination(JSC.AnyPromise{
.Internal = promise,
});
if (this.worker) |worker| {
if (worker.requested_terminate) {
return error.WorkerTerminated;
}
}
return this.pending_internal_promise;
}
pub fn loadEntryPointForTestRunner(this: *VirtualMachine, entry_path: string) anyerror!*JSInternalPromise {
var promise = try this.reloadEntryPointForTestRunner(entry_path);
// pending_internal_promise can change if hot module reloading is enabled
if (this.isWatcherEnabled()) {
this.eventLoop().performGC();
switch (this.pending_internal_promise.status(this.global.vm())) {
JSC.JSPromise.Status.Pending => {
while (this.pending_internal_promise.status(this.global.vm()) == .Pending) {
this.eventLoop().tick();
if (this.pending_internal_promise.status(this.global.vm()) == .Pending) {
this.eventLoop().autoTick();
}
}
},
else => {},
}
} else {
if (promise.status(this.global.vm()) == .Rejected) {
return promise;
}
this.eventLoop().performGC();
this.waitForPromise(JSC.AnyPromise{
.Internal = promise,
});
}
this.eventLoop().autoTick();
return this.pending_internal_promise;
}
@@ -2216,6 +2308,10 @@ pub const VirtualMachine = struct {
else => {},
}
} else {
if (promise.status(this.global.vm()) == .Rejected) {
return promise;
}
this.eventLoop().performGC();
this.waitForPromise(JSC.AnyPromise{
.Internal = promise,
@@ -2257,7 +2353,7 @@ pub const VirtualMachine = struct {
};
this.runWithAPILock(MacroEntryPointLoader, &loader, MacroEntryPointLoader.load);
return loader.promise;
return loader.promise orelse return error.JSError;
}
/// A subtlelty of JavaScriptCore:
@@ -2271,16 +2367,16 @@ pub const VirtualMachine = struct {
const MacroEntryPointLoader = struct {
path: string,
promise: *JSInternalPromise = undefined,
promise: ?*JSInternalPromise = null,
pub fn load(this: *MacroEntryPointLoader) void {
this.promise = VirtualMachine.get()._loadMacroEntryPoint(this.path);
}
};
pub inline fn _loadMacroEntryPoint(this: *VirtualMachine, entry_path: string) *JSInternalPromise {
pub inline fn _loadMacroEntryPoint(this: *VirtualMachine, entry_path: string) ?*JSInternalPromise {
var promise: *JSInternalPromise = undefined;
promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(entry_path));
promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(entry_path)) orelse return null;
this.waitForPromise(JSC.AnyPromise{
.Internal = promise,
});