Skip to content

Commit

Permalink
ref and deinit (#5883)
Browse files Browse the repository at this point in the history
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
  • Loading branch information
dylan-conway and Jarred-Sumner committed Sep 22, 2023
1 parent 0502c13 commit 09d6e85
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ pub const Async = struct {
}

this.ref.unref(this.globalObject.bunVM());
this.args.deinit();
if (@hasDecl(ArgumentType, "deinitAndUnprotect")) {
this.args.deinitAndUnprotect();
} else {
this.args.deinit();
}
this.promise.strong.deinit();
bun.default_allocator.destroy(this);
}
Expand Down Expand Up @@ -1936,7 +1940,13 @@ pub const Arguments = struct {
position: ?ReadPosition = null,
encoding: Encoding = Encoding.buffer,

pub fn deinit(_: Write) void {}
pub fn deinit(this: *const @This()) void {
this.buffer.deinit();
}

pub fn deinitAndUnprotect(this: *@This()) void {
this.buffer.deinitAndUnprotect();
}

pub fn toThreadSafe(self: *@This()) void {
self.buffer.toThreadSafe();
Expand Down
21 changes: 21 additions & 0 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,32 @@ pub const SliceWithUnderlyingStringOrBuffer = union(enum) {
};
}

pub fn deinit(this: *const SliceWithUnderlyingStringOrBuffer) void {
switch (this.*) {
.SliceWithUnderlyingString => |*str| {
str.deinit();
},
else => {},
}
}

pub fn deinitAndUnprotect(this: *const SliceWithUnderlyingStringOrBuffer) void {
switch (this.*) {
.SliceWithUnderlyingString => |*str| {
str.deinit();
},
.buffer => |buffer| {
buffer.buffer.value.unprotect();
},
}
}

pub fn fromJS(global: *JSC.JSGlobalObject, allocator: std.mem.Allocator, value: JSC.JSValue, exception: JSC.C.ExceptionRef) ?SliceWithUnderlyingStringOrBuffer {
_ = exception;
return switch (value.jsType()) {
JSC.JSValue.JSType.String, JSC.JSValue.JSType.StringObject, JSC.JSValue.JSType.DerivedStringObject, JSC.JSValue.JSType.Object => {
var str = bun.String.tryFromJS(value, global) orelse return null;
str.ref();
return SliceWithUnderlyingStringOrBuffer{ .SliceWithUnderlyingString = str.toSlice(allocator) };
},

Expand Down

0 comments on commit 09d6e85

Please sign in to comment.