Skip to content

Commit

Permalink
getNativeArg: return a TypeError if js value is not an object
Browse files Browse the repository at this point in the history
  • Loading branch information
krichprollsch committed Jan 18, 2024
1 parent a97749e commit c16c1f8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/engines/v8/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const TPL = @import("v8.zig").TPL;
// Utils functions
// ---------------

const JSError = error{
InvalidArgument,
};

fn throwBasicError(msg: []const u8, isolate: v8.Isolate) v8.Value {
const except_msg = v8.String.initUtf8(isolate, msg);
const exception = v8.Exception.initError(except_msg);
Expand All @@ -36,6 +40,11 @@ fn throwError(
err: anyerror,
isolate: v8.Isolate,
) v8.Value {
// well known error.
switch (err) {
JSError.InvalidArgument => return throwTypeError("invalid argument", isolate),
else => {},
}
const ret = func.return_type;

// Is the returned Type a custom Exception error?
Expand Down Expand Up @@ -136,10 +145,9 @@ fn getNativeArg(
if (comptime arg_T.underOpt() != null) {
return null;
}
// TODO: else return error "Argument x is not an object"
}

if (!js_value.isObject()) unreachable; // TODO: throw js exception
if (!js_value.isObject()) return JSError.InvalidArgument;

// JS object
const ptr = getNativeObject(
Expand Down
3 changes: 1 addition & 2 deletions src/tests/types_native_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ pub fn exec(
// not.
// https://github.com/lightpanda-io/jsruntime-lib/issues/185
var bug_native_obj = [_]tests.Case{
.{ .src = "let car = new Car();", .ex = "undefined" },
.{ .src = "car.changeBrand('foo');", .ex = "true" },
.{ .src = "try { car.changeBrand('foo'); false; } catch(e) { e instanceof TypeError; }", .ex = "true" },
};
try tests.checkCases(js_env, &bug_native_obj);
}

0 comments on commit c16c1f8

Please sign in to comment.