diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig index b2148f23a187c..9765b074a864a 100644 --- a/src/bun.js/api/bun.zig +++ b/src/bun.js/api/bun.zig @@ -1333,11 +1333,11 @@ pub fn getPublicPathJS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFr } fn fs(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue { - var module = globalObject.allocator().create(JSC.Node.NodeJSFS) catch unreachable; - module.* = .{}; - const vm = globalObject.bunVM(); - if (vm.standalone_module_graph != null) - module.node_fs.vm = vm; + var module = JSC.Node.NodeJSFS.new(.{ + .node_fs = .{ + .vm = globalObject.bunVM(), + }, + }); return module.toJS(globalObject); } diff --git a/src/bun.js/node/node.classes.ts b/src/bun.js/node/node.classes.ts index 286ae543b1e5a..e3eaeebcf4740 100644 --- a/src/bun.js/node/node.classes.ts +++ b/src/bun.js/node/node.classes.ts @@ -435,7 +435,7 @@ export default [ name: "NodeJSFS", construct: true, noConstructor: true, - finalize: false, + finalize: true, klass: {}, proto: { diff --git a/src/bun.js/node/node_fs_binding.zig b/src/bun.js/node/node_fs_binding.zig index 24d07670ec285..9ad02e6033175 100644 --- a/src/bun.js/node/node_fs_binding.zig +++ b/src/bun.js/node/node_fs_binding.zig @@ -1,3 +1,4 @@ +const bun = @import("root").bun; const JSC = @import("root").bun.JSC; const std = @import("std"); const Flavor = JSC.Node.Flavor; @@ -138,15 +139,26 @@ fn call(comptime FunctionEnum: NodeFSFunctionEnum) NodeFSFunction { } pub const NodeJSFS = struct { - node_fs: JSC.Node.NodeFS = undefined, + node_fs: JSC.Node.NodeFS = .{}, pub usingnamespace JSC.Codegen.JSNodeJSFS; + pub usingnamespace bun.New(@This()); pub fn constructor(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) ?*@This() { globalObject.throw("Not a constructor", .{}); return null; } + pub fn finalize(this: *JSC.Node.NodeJSFS) callconv(.C) void { + if (this.node_fs.vm) |vm| { + if (vm.node_fs == this) { + return; + } + } + + this.destroy(); + } + pub const access = call(.access); pub const appendFile = call(.appendFile); pub const close = call(.close);