Skip to content

Commit c16c1f8

Browse files
committed
getNativeArg: return a TypeError if js value is not an object
1 parent a97749e commit c16c1f8

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/engines/v8/generate.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const TPL = @import("v8.zig").TPL;
2222
// Utils functions
2323
// ---------------
2424

25+
const JSError = error{
26+
InvalidArgument,
27+
};
28+
2529
fn throwBasicError(msg: []const u8, isolate: v8.Isolate) v8.Value {
2630
const except_msg = v8.String.initUtf8(isolate, msg);
2731
const exception = v8.Exception.initError(except_msg);
@@ -36,6 +40,11 @@ fn throwError(
3640
err: anyerror,
3741
isolate: v8.Isolate,
3842
) v8.Value {
43+
// well known error.
44+
switch (err) {
45+
JSError.InvalidArgument => return throwTypeError("invalid argument", isolate),
46+
else => {},
47+
}
3948
const ret = func.return_type;
4049

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

142-
if (!js_value.isObject()) unreachable; // TODO: throw js exception
150+
if (!js_value.isObject()) return JSError.InvalidArgument;
143151

144152
// JS object
145153
const ptr = getNativeObject(

src/tests/types_native_test.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ pub fn exec(
293293
// not.
294294
// https://github.com/lightpanda-io/jsruntime-lib/issues/185
295295
var bug_native_obj = [_]tests.Case{
296-
.{ .src = "let car = new Car();", .ex = "undefined" },
297-
.{ .src = "car.changeBrand('foo');", .ex = "true" },
296+
.{ .src = "try { car.changeBrand('foo'); false; } catch(e) { e instanceof TypeError; }", .ex = "true" },
298297
};
299298
try tests.checkCases(js_env, &bug_native_obj);
300299
}

0 commit comments

Comments
 (0)