diff --git a/src/engines/v8/generate.zig b/src/engines/v8/generate.zig index c122b98..3dd500a 100644 --- a/src/engines/v8/generate.zig +++ b/src/engines/v8/generate.zig @@ -723,6 +723,12 @@ fn getNativeObject( obj_ptr.* = T{}; } else { // retrieve the zig object from it's javascript counterpart + + // check if the js object has a least one internal field. + if (js_obj.internalFieldCount() == 0) return JSError.InvalidArgument; + + // TODO ensure the js object corresponds to the expected native type. + const ext = js_obj.getInternalField(0).castTo(v8.External).get().?; if (comptime T_refl.is_mem_guarantied()) { // memory is fixed diff --git a/src/tests/types_native_test.zig b/src/tests/types_native_test.zig index 8e31440..140c26c 100644 --- a/src/tests/types_native_test.zig +++ b/src/tests/types_native_test.zig @@ -289,11 +289,15 @@ pub fn exec( }; try tests.checkCases(js_env, &separate_cases); - // Test for the bug #185: native func expects a object but the js value is - // not. - // https://github.com/lightpanda-io/jsruntime-lib/issues/185 var bug_native_obj = [_]tests.Case{ + // Test for the bug #185: native func expects a object but the js value is + // not. + // https://github.com/lightpanda-io/jsruntime-lib/issues/185 .{ .src = "try { car.changeBrand('foo'); false; } catch(e) { e instanceof TypeError; }", .ex = "true" }, + // Test for the bug #187: native func expects a native object but the js value is + // not. + // https://github.com/lightpanda-io/jsruntime-lib/issues/187 + .{ .src = "try { car.changeBrand({'foo': 'bar'}); false; } catch(e) { e instanceof TypeError; }", .ex = "true" }, }; try tests.checkCases(js_env, &bug_native_obj); }