Skip to content

Commit

Permalink
refactor err.err and protect it
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Aug 22, 2023
1 parent 70d8e69 commit 4d07c98
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/bun.js/api/bun/subprocess.zig
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ pub const Subprocess = struct {
return;
},
.err => |err| {
if (err == .err) {
this.status = .{ .err = err.err };
if (err == .Error) {
this.status = .{ .err = err.Error };
} else {
this.status = .{ .err = JSC.Node.Syscall.Error.fromCode(.CANCELED, .read) };
}
Expand Down
11 changes: 7 additions & 4 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2925,10 +2925,13 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
var emptied = this.request_body_buf;
this.request_body_buf = .{};
return .{
.owned = .{ .list = emptied.toManaged(this.allocator), .size_hint = if (emptied.capacity < max_request_body_preallocate_length)
emptied.capacity
else
0 },
.owned = .{
.list = emptied.toManaged(this.allocator),
.size_hint = if (emptied.capacity < max_request_body_preallocate_length)
emptied.capacity
else
0,
},
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/webcore/response.zig
Original file line number Diff line number Diff line change
Expand Up @@ -738,15 +738,15 @@ pub const Fetch = struct {

if (!success) {
const err = this.onReject();

err.ensureStillAlive();
if (this.response.get()) |response_js| {
if (response_js.as(Response)) |response| {
const body = response.body;
if (body.value == .Locked) {
if (body.value.Locked.readable) |readable| {
readable.ptr.Bytes.onData(
.{
.err = .{ .js_err = err },
.err = .{ .JSValue = err },
},
bun.default_allocator,
);
Expand Down
52 changes: 32 additions & 20 deletions src/bun.js/webcore/streams.zig
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,13 @@ pub const StreamResult = union(Tag) {
into_array_and_done: IntoArray,
pending: *Pending,

err: union(Err) {
err: Syscall.Error,
js_err: JSC.JSValue,
},
err: union(Err) { Error: Syscall.Error, JSValue: JSC.JSValue },

done: void,

pub const Err = enum {
err,
js_err,
Error,
JSValue,
};

pub const Tag = enum {
Expand Down Expand Up @@ -767,10 +764,13 @@ pub const StreamResult = union(Tag) {
promise.asValue(globalThis).unprotect();
switch (result) {
.err => |err| {
if (err == .err) {
promise.reject(globalThis, err.err.toJSC(globalThis));
if (err == .Error) {
promise.reject(globalThis, err.Error.toJSC(globalThis));
} else {
promise.reject(globalThis, err.js_err);
const js_err = err.JSValue;
js_err.ensureStillAlive();
js_err.unprotect();
promise.reject(globalThis, js_err);
}
},
.done => {
Expand Down Expand Up @@ -817,10 +817,13 @@ pub const StreamResult = union(Tag) {
},

.err => |err| {
if (err == .err) {
return JSC.JSPromise.rejectedPromise(globalThis, JSValue.c(err.err.toJS(globalThis))).asValue(globalThis);
if (err == .Error) {
return JSC.JSPromise.rejectedPromise(globalThis, JSValue.c(err.Error.toJS(globalThis))).asValue(globalThis);
}
return JSC.JSPromise.rejectedPromise(globalThis, err.js_err).asValue(globalThis);
const js_err = err.JSValue;
js_err.ensureStillAlive();
js_err.unprotect();
return JSC.JSPromise.rejectedPromise(globalThis, js_err).asValue(globalThis);
},

// false == controller.close()
Expand Down Expand Up @@ -3006,10 +3009,13 @@ pub fn ReadableStreamSource(
pub fn processResult(globalThis: *JSGlobalObject, flags: JSValue, result: StreamResult) JSC.JSValue {
switch (result) {
.err => |err| {
if (err == .err) {
globalThis.vm().throwError(globalThis, err.err.toJSC(globalThis));
if (err == .Error) {
globalThis.vm().throwError(globalThis, err.Error.toJSC(globalThis));
} else {
globalThis.vm().throwError(globalThis, err.js_err);
const js_err = err.JSValue;
js_err.ensureStillAlive();
js_err.unprotect();
globalThis.vm().throwError(globalThis, js_err);
}
return JSValue.jsUndefined();
},
Expand Down Expand Up @@ -3328,7 +3334,13 @@ pub const ByteStream = struct {

if (to_copy.len == 0) {
if (stream == .err) {
this.pending.result = stream;
if (stream.err == .Error) {
this.pending.result = .{ .err = .{ .Error = stream.err.Error } };
}
const js_err = stream.err.JSValue;
js_err.ensureStillAlive();
js_err.protect();
this.pending.result = .{ .err = .{ .JSValue = js_err } };
} else {
this.pending.result = .{
.done = {},
Expand Down Expand Up @@ -3523,7 +3535,7 @@ pub const ReadResult = union(enum) {
pub fn toStreamWithIsDone(this: ReadResult, pending: *StreamResult.Pending, buf: []u8, view: JSValue, close_on_empty: bool, is_done: bool) StreamResult {
return switch (this) {
.pending => .{ .pending = pending },
.err => .{ .err = .{ .err = this.err } },
.err => .{ .err = .{ .Error = this.err } },
.done => .{ .done = {} },
.read => |slice| brk: {
const owned = slice.ptr != buf.ptr;
Expand Down Expand Up @@ -4100,7 +4112,7 @@ pub const File = struct {
if (@hasField(HTTPClient.NetworkThread.Completion, "result")) {
this.pending.result = .{
.err = .{
.err = Syscall.Error{
.Error = Syscall.Error{
.errno = @as(Syscall.Error.Int, @intCast(-completion.result)),
.syscall = .read,
},
Expand All @@ -4109,7 +4121,7 @@ pub const File = struct {
} else {
this.pending.result = .{
.err = .{
.err = Syscall.Error{
.Error = Syscall.Error{
// this is too hacky
.errno = @as(Syscall.Error.Int, @truncate(@as(u16, @intCast(@max(1, @intFromError(err)))))),
.syscall = .read,
Expand Down Expand Up @@ -4140,7 +4152,7 @@ pub const File = struct {
else => {},
}

this.pending.result = .{ .err = .{ .err = err } };
this.pending.result = .{ .err = .{ .Error = err } };
scheduleMainThreadTask(this);
return;
},
Expand Down

0 comments on commit 4d07c98

Please sign in to comment.