aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/api/bun.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-26 20:35:26 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-26 20:35:26 -0700
commit5875d1419b49b97a78dfeeb9d6f1bd9f00d2eaeb (patch)
tree46fd8978a7508e7d2f03bc24a8c1119544b7d80c /src/bun.js/api/bun.zig
parent24a9bc23b7e1c7911cb2e146be199d940b9729e6 (diff)
downloadbun-5875d1419b49b97a78dfeeb9d6f1bd9f00d2eaeb.tar.gz
bun-5875d1419b49b97a78dfeeb9d6f1bd9f00d2eaeb.tar.zst
bun-5875d1419b49b97a78dfeeb9d6f1bd9f00d2eaeb.zip
Make `Bun.spawn`, FileSink and FileBlobLoader a little more reliable
Diffstat (limited to 'src/bun.js/api/bun.zig')
-rw-r--r--src/bun.js/api/bun.zig46
1 files changed, 27 insertions, 19 deletions
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index d62b1e9b8..0b4f62c39 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -3384,7 +3384,8 @@ pub const Subprocess = struct {
stderr: Readable,
killed: bool = false,
- has_ref: bool = false,
+ reffer: JSC.Ref = JSC.Ref.init(),
+ poll_ref: JSC.PollRef = JSC.PollRef.init(),
exit_promise: JSValue = JSValue.zero,
this_jsvalue: JSValue = JSValue.zero,
@@ -3402,6 +3403,16 @@ pub const Subprocess = struct {
globalThis: *JSC.JSGlobalObject,
+ pub fn ref(this: *Subprocess) void {
+ this.reffer.ref(this.globalThis.bunVM());
+ this.poll_ref.ref(this.globalThis.bunVM());
+ }
+
+ pub fn unref(this: *Subprocess) void {
+ this.reffer.unref(this.globalThis.bunVM());
+ this.poll_ref.unref(this.globalThis.bunVM());
+ }
+
pub fn constructor(
_: *JSC.JSGlobalObject,
_: *JSC.CallFrame,
@@ -3425,8 +3436,9 @@ pub const Subprocess = struct {
defer blob.detach();
var stream = JSC.WebCore.ReadableStream.fromBlob(globalThis, &blob, 0);
-
- break :brk Readable{ .pipe = JSC.WebCore.ReadableStream.fromJS(stream, globalThis).? };
+ var out = JSC.WebCore.ReadableStream.fromJS(stream, globalThis).?;
+ out.ptr.File.stored_global_this_ = globalThis;
+ break :brk Readable{ .pipe = out };
},
.callback, .fd, .path, .blob => Readable{ .fd = @intCast(JSC.Node.FileDescriptor, fd) },
};
@@ -3559,20 +3571,6 @@ pub const Subprocess = struct {
this.stderr.close();
}
- pub fn unref(this: *Subprocess) void {
- if (!this.has_ref)
- return;
- this.has_ref = false;
- this.globalThis.bunVM().active_tasks -= 1;
- }
-
- pub fn ref(this: *Subprocess) void {
- if (this.has_ref)
- return;
- this.has_ref = true;
- this.globalThis.bunVM().active_tasks += 1;
- }
-
pub fn doRef(this: *Subprocess, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSValue {
this.ref();
return JSC.JSValue.jsUndefined();
@@ -3608,7 +3606,7 @@ pub const Subprocess = struct {
.path, .pipe, .callback => {
var sink = try globalThis.bunVM().allocator.create(JSC.WebCore.FileSink);
sink.* = .{
- .opened_fd = fd,
+ .fd = fd,
.buffer = bun.ByteList.init(&.{}),
.allocator = globalThis.bunVM().allocator,
};
@@ -3659,7 +3657,7 @@ pub const Subprocess = struct {
bun.default_allocator.destroy(this);
}
- pub fn getExitStatus(
+ pub fn getExited(
this: *Subprocess,
globalThis: *JSGlobalObject,
) callconv(.C) JSValue {
@@ -3674,6 +3672,16 @@ pub const Subprocess = struct {
return this.exit_promise;
}
+ pub fn getExitCode(
+ this: *Subprocess,
+ _: *JSGlobalObject,
+ ) callconv(.C) JSValue {
+ if (this.exit_code) |code| {
+ return JSC.JSValue.jsNumber(code);
+ }
+ return JSC.JSValue.jsNull();
+ }
+
pub fn spawn(globalThis: *JSC.JSGlobalObject, args: JSValue) JSValue {
var arena = std.heap.ArenaAllocator.init(bun.default_allocator);
defer arena.deinit();