Skip to content

Commit

Permalink
test(core): Test that sync ops return/throw objects in the right realm (
Browse files Browse the repository at this point in the history
denoland#14750)

This behavior was introduced in denoland#14019 but wasn't properly tested in
that PR.
  • Loading branch information
andreubotella committed May 30, 2022
1 parent 0cd8f53 commit 01e5bba
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3400,4 +3400,52 @@ assertEquals(1, notify_return_value);
let scope = &mut realm.handle_scope(&mut runtime);
assert_eq!(ret, serde_v8::to_v8(scope, "Test").unwrap());
}

#[test]
fn js_realm_sync_ops() {
// Test that returning a ZeroCopyBuf and throwing an exception from a sync
// op result in objects with prototypes from the right realm. Note that we
// don't test the result of returning structs, because they will be
// serialized to objects with null prototype.

#[op]
fn op_test(fail: bool) -> Result<ZeroCopyBuf, Error> {
if !fail {
Ok(ZeroCopyBuf::empty())
} else {
Err(crate::error::type_error("Test"))
}
}

let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![Extension::builder().ops(vec![op_test::decl()]).build()],
get_error_class_fn: Some(&|error| {
crate::error::get_custom_error_class(error).unwrap()
}),
..Default::default()
});
let new_realm = runtime.create_realm().unwrap();

// Test in both realms
for realm in [runtime.global_realm(), new_realm].into_iter() {
let ret = realm
.execute_script(
&mut runtime,
"",
r#"
const buf = Deno.core.opSync("op_test", false);
let err;
try {
Deno.core.opSync("op_test", true);
} catch(e) {
err = e;
}
buf instanceof Uint8Array && buf.byteLength === 0 &&
err instanceof TypeError && err.message === "Test"
"#,
)
.unwrap();
assert!(ret.open(runtime.v8_isolate()).is_true());
}
}
}

0 comments on commit 01e5bba

Please sign in to comment.