Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw returned errors on wrapWithHasContainer #2832

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2736,8 +2736,10 @@ pub fn wrapWithHasContainer(
exception: js.ExceptionRef,
) js.JSObjectRef {
var iter = JSC.Node.ArgumentsSlice.from(ctx.bunVM(), arguments);
defer iter.deinit();
var args: Args = undefined;

comptime var passed_exception_ref = false;
comptime var i: usize = 0;
inline while (i < FunctionTypeInfo.params.len) : (i += 1) {
const ArgType = comptime FunctionTypeInfo.params[i].type.?;
Expand All @@ -2752,12 +2754,10 @@ pub fn wrapWithHasContainer(
JSC.Node.StringOrBuffer => {
const arg = iter.nextEat() orelse {
exception.* = JSC.toInvalidArguments("expected string or buffer", .{}, ctx).asObjectRef();
iter.deinit();
return null;
};
args[i] = JSC.Node.StringOrBuffer.fromJS(ctx.ptr(), iter.arena.allocator(), arg, exception) orelse {
exception.* = JSC.toInvalidArguments("expected string or buffer", .{}, ctx).asObjectRef();
iter.deinit();
return null;
};
},
Expand All @@ -2766,7 +2766,6 @@ pub fn wrapWithHasContainer(
if (!arg.isEmptyOrUndefinedOrNull()) {
args[i] = JSC.Node.StringOrBuffer.fromJS(ctx.ptr(), iter.arena.allocator(), arg, exception) orelse {
exception.* = JSC.toInvalidArguments("expected string or buffer", .{}, ctx).asObjectRef();
iter.deinit();
return null;
};
} else {
Expand All @@ -2781,7 +2780,6 @@ pub fn wrapWithHasContainer(
if (!arg.isEmptyOrUndefinedOrNull()) {
args[i] = JSC.Node.SliceOrBuffer.fromJS(ctx.ptr(), iter.arena.allocator(), arg, exception) orelse {
exception.* = JSC.toInvalidArguments("expected string or buffer", .{}, ctx).asObjectRef();
iter.deinit();
return null;
};
} else {
Expand All @@ -2795,12 +2793,10 @@ pub fn wrapWithHasContainer(
if (iter.nextEat()) |arg| {
args[i] = arg.asArrayBuffer(ctx.ptr()) orelse {
exception.* = JSC.toInvalidArguments("expected TypedArray", .{}, ctx).asObjectRef();
iter.deinit();
return null;
};
} else {
exception.* = JSC.toInvalidArguments("expected TypedArray", .{}, ctx).asObjectRef();
iter.deinit();
return null;
}
},
Expand All @@ -2809,7 +2805,6 @@ pub fn wrapWithHasContainer(
if (!arg.isEmptyOrUndefinedOrNull()) {
args[i] = arg.asArrayBuffer(ctx.ptr()) orelse {
exception.* = JSC.toInvalidArguments("expected TypedArray", .{}, ctx).asObjectRef();
iter.deinit();
return null;
};
} else {
Expand All @@ -2822,13 +2817,11 @@ pub fn wrapWithHasContainer(
ZigString => {
var string_value = eater(&iter) orelse {
JSC.throwInvalidArguments("Missing argument", .{}, ctx, exception);
iter.deinit();
return null;
};

if (string_value.isUndefinedOrNull()) {
JSC.throwInvalidArguments("Expected string", .{}, ctx, exception);
iter.deinit();
return null;
}

Expand Down Expand Up @@ -2857,29 +2850,26 @@ pub fn wrapWithHasContainer(
*Request => {
args[i] = (eater(&iter) orelse {
JSC.throwInvalidArguments("Missing Request object", .{}, ctx, exception);
iter.deinit();
return null;
}).as(Request) orelse {
JSC.throwInvalidArguments("Expected Request object", .{}, ctx, exception);
iter.deinit();
return null;
};
},
js.JSObjectRef => {
args[i] = thisObject;
if (!JSValue.fromRef(thisObject).isCell() or !JSValue.fromRef(thisObject).isObject()) {
JSC.throwInvalidArguments("Expected object", .{}, ctx, exception);
iter.deinit();
return null;
}
},
js.ExceptionRef => {
args[i] = exception;
passed_exception_ref = true;
},
JSValue => {
const val = eater(&iter) orelse {
JSC.throwInvalidArguments("Missing argument", .{}, ctx, exception);
iter.deinit();
return null;
};
args[i] = val;
Expand All @@ -2892,9 +2882,15 @@ pub fn wrapWithHasContainer(
}

var result: JSValue = @call(.auto, @field(Container, name), args);
if (exception.* != null) {
iter.deinit();
return null;
if (comptime passed_exception_ref) {
if (exception.* != null) {
return null;
}
} else {
if (result.isError()) {
exception.* = result.asObjectRef();
return null;
}
}

if (comptime maybe_async) {
Expand All @@ -2905,8 +2901,6 @@ pub fn wrapWithHasContainer(
}
}

iter.deinit();

if (result == .zero) {
return null;
}
Expand Down
10 changes: 10 additions & 0 deletions test/js/bun/ffi/ffi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,13 @@ if (ok) {
} else {
it.skip("run ffi", () => {});
}

it("dlopen throws an error instead of returning it", () => {
let err;
try {
dlopen("nonexistent", { x: {} });
} catch (error) {
err = error;
}
expect(err).toBeTruthy();
});