mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-21 15:04:33 -05:00
feat: v8::Context::set_promise_hooks accepts None (#1178)
Co-authored-by: Bert Belder <bertbelder@gmail.com>
This commit is contained in:
parent
72216372d5
commit
623c6cd81d
3 changed files with 95 additions and 15 deletions
|
@ -1709,13 +1709,14 @@ const v8::Object* v8__Context__GetExtrasBindingObject(v8::Context& self) {
|
||||||
return local_to_ptr(ptr_to_local(&self)->GetExtrasBindingObject());
|
return local_to_ptr(ptr_to_local(&self)->GetExtrasBindingObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
void v8__Context__SetPromiseHooks(v8::Context& self, v8::Function& init_hook,
|
void v8__Context__SetPromiseHooks(v8::Context& self,
|
||||||
v8::Function& before_hook,
|
const v8::Function* init_hook,
|
||||||
v8::Function& after_hook,
|
const v8::Function* before_hook,
|
||||||
v8::Function& resolve_hook) {
|
const v8::Function* after_hook,
|
||||||
|
const v8::Function* resolve_hook) {
|
||||||
ptr_to_local(&self)->SetPromiseHooks(
|
ptr_to_local(&self)->SetPromiseHooks(
|
||||||
ptr_to_local(&init_hook), ptr_to_local(&before_hook),
|
ptr_to_local(init_hook), ptr_to_local(before_hook),
|
||||||
ptr_to_local(&after_hook), ptr_to_local(&resolve_hook));
|
ptr_to_local(after_hook), ptr_to_local(resolve_hook));
|
||||||
}
|
}
|
||||||
|
|
||||||
const v8::Context* v8__Context__FromSnapshot(v8::Isolate* isolate,
|
const v8::Context* v8__Context__FromSnapshot(v8::Isolate* isolate,
|
||||||
|
|
|
@ -110,18 +110,18 @@ impl Context {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_promise_hooks(
|
pub fn set_promise_hooks(
|
||||||
&self,
|
&self,
|
||||||
init_hook: Local<Function>,
|
init_hook: Option<Local<Function>>,
|
||||||
before_hook: Local<Function>,
|
before_hook: Option<Local<Function>>,
|
||||||
after_hook: Local<Function>,
|
after_hook: Option<Local<Function>>,
|
||||||
resolve_hook: Local<Function>,
|
resolve_hook: Option<Local<Function>>,
|
||||||
) {
|
) {
|
||||||
unsafe {
|
unsafe {
|
||||||
v8__Context__SetPromiseHooks(
|
v8__Context__SetPromiseHooks(
|
||||||
self,
|
self,
|
||||||
&*init_hook,
|
init_hook.map_or_else(null, |v| &*v),
|
||||||
&*before_hook,
|
before_hook.map_or_else(null, |v| &*v),
|
||||||
&*after_hook,
|
after_hook.map_or_else(null, |v| &*v),
|
||||||
&*resolve_hook,
|
resolve_hook.map_or_else(null, |v| &*v),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3269,7 +3269,12 @@ fn context_promise_hooks() {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
context.set_promise_hooks(init_hook, before_hook, after_hook, resolve_hook);
|
context.set_promise_hooks(
|
||||||
|
Some(init_hook),
|
||||||
|
Some(before_hook),
|
||||||
|
Some(after_hook),
|
||||||
|
Some(resolve_hook),
|
||||||
|
);
|
||||||
|
|
||||||
let source = r#"
|
let source = r#"
|
||||||
function expect(expected, actual = promises.size) {
|
function expect(expected, actual = promises.size) {
|
||||||
|
@ -3307,6 +3312,80 @@ fn context_promise_hooks() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn context_promise_hooks_partial() {
|
||||||
|
let _setup_guard = setup::parallel_test();
|
||||||
|
let isolate = &mut v8::Isolate::new(Default::default());
|
||||||
|
{
|
||||||
|
let scope = &mut v8::HandleScope::new(isolate);
|
||||||
|
let context = v8::Context::new(scope);
|
||||||
|
let scope = &mut v8::ContextScope::new(scope, context);
|
||||||
|
let init_hook = v8::Local::<v8::Function>::try_from(
|
||||||
|
eval(
|
||||||
|
scope,
|
||||||
|
r#"
|
||||||
|
globalThis.promises = new Set();
|
||||||
|
function initHook(promise) {
|
||||||
|
promises.add(promise);
|
||||||
|
}
|
||||||
|
initHook;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let before_hook = v8::Local::<v8::Function>::try_from(
|
||||||
|
eval(
|
||||||
|
scope,
|
||||||
|
r#"
|
||||||
|
globalThis.promiseStack = [];
|
||||||
|
function beforeHook(promise) {
|
||||||
|
promiseStack.push(promise);
|
||||||
|
}
|
||||||
|
beforeHook;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
context.set_promise_hooks(Some(init_hook), Some(before_hook), None, None);
|
||||||
|
|
||||||
|
let source = r#"
|
||||||
|
function expect(expected, actual = promises.size) {
|
||||||
|
if (actual !== expected) throw `expected ${expected}, actual ${actual}`;
|
||||||
|
}
|
||||||
|
expect(0);
|
||||||
|
var p = new Promise(resolve => {
|
||||||
|
expect(1);
|
||||||
|
resolve();
|
||||||
|
expect(1);
|
||||||
|
});
|
||||||
|
expect(1);
|
||||||
|
new Promise(() => {});
|
||||||
|
expect(2);
|
||||||
|
|
||||||
|
expect(0, promiseStack.length);
|
||||||
|
p.then(() => {
|
||||||
|
expect(1, promiseStack.length);
|
||||||
|
});
|
||||||
|
promises.values().next().value
|
||||||
|
"#;
|
||||||
|
let promise = eval(scope, source).unwrap();
|
||||||
|
let promise = v8::Local::<v8::Promise>::try_from(promise).unwrap();
|
||||||
|
assert!(promise.has_handler());
|
||||||
|
assert_eq!(promise.state(), v8::PromiseState::Fulfilled);
|
||||||
|
|
||||||
|
scope.perform_microtask_checkpoint();
|
||||||
|
let _ = eval(
|
||||||
|
scope,
|
||||||
|
r#"
|
||||||
|
expect(1, promiseStack.length);
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn allow_atomics_wait() {
|
fn allow_atomics_wait() {
|
||||||
let _setup_guard = setup::parallel_test();
|
let _setup_guard = setup::parallel_test();
|
||||||
|
|
Loading…
Reference in a new issue