Skip to content

Commit

Permalink
ensure the js object has a internal field
Browse files Browse the repository at this point in the history
  • Loading branch information
krichprollsch committed Jan 19, 2024
1 parent 78b36c2 commit ae61e06
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/engines/v8/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/tests/types_native_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit ae61e06

Please sign in to comment.