Skip to content

Commit ae61e06

Browse files
committed
ensure the js object has a internal field
1 parent 78b36c2 commit ae61e06

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/engines/v8/generate.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ fn getNativeObject(
723723
obj_ptr.* = T{};
724724
} else {
725725
// retrieve the zig object from it's javascript counterpart
726+
727+
// check if the js object has a least one internal field.
728+
if (js_obj.internalFieldCount() == 0) return JSError.InvalidArgument;
729+
730+
// TODO ensure the js object corresponds to the expected native type.
731+
726732
const ext = js_obj.getInternalField(0).castTo(v8.External).get().?;
727733
if (comptime T_refl.is_mem_guarantied()) {
728734
// memory is fixed

src/tests/types_native_test.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,15 @@ pub fn exec(
289289
};
290290
try tests.checkCases(js_env, &separate_cases);
291291

292-
// Test for the bug #185: native func expects a object but the js value is
293-
// not.
294-
// https://github.com/lightpanda-io/jsruntime-lib/issues/185
295292
var bug_native_obj = [_]tests.Case{
293+
// Test for the bug #185: native func expects a object but the js value is
294+
// not.
295+
// https://github.com/lightpanda-io/jsruntime-lib/issues/185
296296
.{ .src = "try { car.changeBrand('foo'); false; } catch(e) { e instanceof TypeError; }", .ex = "true" },
297+
// Test for the bug #187: native func expects a native object but the js value is
298+
// not.
299+
// https://github.com/lightpanda-io/jsruntime-lib/issues/187
300+
.{ .src = "try { car.changeBrand({'foo': 'bar'}); false; } catch(e) { e instanceof TypeError; }", .ex = "true" },
297301
};
298302
try tests.checkCases(js_env, &bug_native_obj);
299303
}

0 commit comments

Comments
 (0)