aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/javascript.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/javascript.zig')
-rw-r--r--src/bun.js/javascript.zig48
1 files changed, 45 insertions, 3 deletions
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index fe559fac8..24633eea7 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -314,6 +314,7 @@ pub export fn Bun__handleRejectedPromise(global: *JSGlobalObject, promise: *JSC.
const result = promise.result(global.vm());
var jsc_vm = global.bunVM();
jsc_vm.onUnhandledError(global, result);
+ jsc_vm.autoGarbageCollect();
}
pub export fn Bun__onDidAppendPlugin(jsc_vm: *VirtualMachine, globalObject: *JSGlobalObject) void {
@@ -428,6 +429,13 @@ pub const VirtualMachine = struct {
unhandled_error_counter: usize = 0,
modules: ModuleLoader.AsyncModule.Queue = .{},
+ aggressive_garbage_collection: GCLevel = GCLevel.none,
+
+ pub const GCLevel = enum {
+ none,
+ mild,
+ aggressive,
+ };
pub threadlocal var is_main_thread_vm: bool = false;
@@ -435,6 +443,27 @@ pub const VirtualMachine = struct {
this.onUnhandledRejection = defaultOnUnhandledRejection;
}
+ pub fn loadExtraEnv(this: *VirtualMachine) void {
+ var map = this.bundler.env.map;
+
+ if (map.get("BUN_SHOW_BUN_STACKFRAMES") != null)
+ this.hide_bun_stackframes = false;
+
+ if (map.get("BUN_OVERRIDE_MODULE_PATH")) |override_path| {
+ if (override_path.len > 0) {
+ this.load_builtins_from_path = override_path;
+ }
+ }
+
+ if (map.get("BUN_GARBAGE_COLLECTOR_LEVEL")) |gc_level| {
+ if (strings.eqlComptime(gc_level, "1")) {
+ this.aggressive_garbage_collection = .mild;
+ } else if (strings.eqlComptime(gc_level, "2")) {
+ this.aggressive_garbage_collection = .aggressive;
+ }
+ }
+ }
+
pub fn onUnhandledError(this: *JSC.VirtualMachine, globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) void {
this.unhandled_error_counter += 1;
this.onUnhandledRejection(this, globalObject, value);
@@ -448,6 +477,22 @@ pub const VirtualMachine = struct {
return this.bundler.getPackageManager();
}
+ pub fn garbageCollect(this: *const VirtualMachine, sync: bool) JSValue {
+ @setCold(true);
+ Global.mimalloc_cleanup(false);
+ if (sync)
+ return this.global.vm().runGC(true);
+
+ this.global.vm().collectAsync();
+ return JSValue.jsNumber(this.global.vm().heapSize());
+ }
+
+ pub inline fn autoGarbageCollect(this: *const VirtualMachine) void {
+ if (this.aggressive_garbage_collection != .none) {
+ _ = this.garbageCollect(this.aggressive_garbage_collection == .aggressive);
+ }
+ }
+
pub fn reload(this: *VirtualMachine) void {
Output.debug("Reloading...", .{});
this.global.reload();
@@ -677,9 +722,6 @@ pub const VirtualMachine = struct {
VirtualMachine.vm.bundler.configureLinker();
try VirtualMachine.vm.bundler.configureFramework(false);
- if (VirtualMachine.vm.bundler.env.get("BUN_SHOW_BUN_STACKFRAMES") != null)
- VirtualMachine.vm.hide_bun_stackframes = false;
-
vm.bundler.macro_context = js_ast.Macro.MacroContext.init(&vm.bundler);
if (_args.serve orelse false) {