0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-11 08:34:01 -05:00

Add Isolate::set_allow_atomics_wait() (#500)

This commit is contained in:
Ben Noordhuis 2020-10-15 22:12:44 +02:00 committed by GitHub
parent 1c38b66093
commit 1988c98f3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View file

@ -249,6 +249,10 @@ void v8__Isolate__CancelTerminateExecution(v8::Isolate* isolate) {
isolate->CancelTerminateExecution();
}
void v8__Isolate__SetAllowAtomicsWait(v8::Isolate* isolate, bool allow) {
isolate->SetAllowAtomicsWait(allow);
}
void v8__Isolate__CreateParams__CONSTRUCT(
uninit_t<v8::Isolate::CreateParams>* buf) {
construct_in_place<v8::Isolate::CreateParams>(buf);

View file

@ -186,6 +186,7 @@ extern "C" {
isolate: *mut Isolate,
function: *const Function,
);
fn v8__Isolate__SetAllowAtomicsWait(isolate: *mut Isolate, allow: bool);
fn v8__HeapProfiler__TakeHeapSnapshot(
isolate: *mut Isolate,
@ -516,6 +517,13 @@ impl Isolate {
unsafe { v8__Isolate__EnqueueMicrotask(self, &*microtask) }
}
/// Set whether calling Atomics.wait (a function that may block) is allowed in
/// this isolate. This can also be configured via
/// CreateParams::allow_atomics_wait.
pub fn set_allow_atomics_wait(&mut self, allow: bool) {
unsafe { v8__Isolate__SetAllowAtomicsWait(self, allow) }
}
/// Disposes the isolate. The isolate must not be entered by any
/// thread to be disposable.
unsafe fn dispose(&mut self) {

View file

@ -1777,6 +1777,38 @@ fn promise_hook() {
}
}
#[test]
fn allow_atomics_wait() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
for allow in &[false, true, false] {
let allow = *allow;
isolate.set_allow_atomics_wait(allow);
{
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let source = r#"
const b = new SharedArrayBuffer(4);
const a = new Int32Array(b);
"timed-out" === Atomics.wait(a, 0, 0, 1);
"#;
let try_catch = &mut v8::TryCatch::new(scope);
let result = eval(try_catch, source);
if allow {
assert!(!try_catch.has_caught());
assert!(result.unwrap().is_true());
} else {
assert!(try_catch.has_caught());
let exc = try_catch.exception().unwrap();
let exc = exc.to_string(try_catch).unwrap();
let exc = exc.to_rust_string_lossy(try_catch);
assert!(exc.contains("Atomics.wait cannot be called in this context"));
}
}
}
}
fn mock_script_origin<'s>(
scope: &mut v8::HandleScope<'s>,
resource_name_: &str,