From 36cf16b5947637bad2a39b3871bcc8caae8bd776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 9 Feb 2023 20:38:25 +0100 Subject: [PATCH] refactor: Move set_promise_hooks API to HandleScope (#1186) --- src/context.rs | 28 ---------------------------- src/scope.rs | 28 ++++++++++++++++++++++++++++ tests/test_api.rs | 11 ++--------- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/context.rs b/src/context.rs index 2437b3a6..3ad3614c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -5,7 +5,6 @@ use crate::isolate::Isolate; use crate::isolate::RawSlot; use crate::support::int; use crate::Context; -use crate::Function; use crate::HandleScope; use crate::Local; use crate::Object; @@ -27,13 +26,6 @@ extern "C" { fn v8__Context__Global(this: *const Context) -> *const Object; fn v8__Context__GetExtrasBindingObject(this: *const Context) -> *const Object; - fn v8__Context__SetPromiseHooks( - this: *const Context, - init_hook: *const Function, - before_hook: *const Function, - after_hook: *const Function, - resolve_hook: *const Function, - ); fn v8__Context__GetNumberOfEmbedderDataFields(this: *const Context) -> u32; fn v8__Context__GetAlignedPointerFromEmbedderData( this: *const Context, @@ -107,26 +99,6 @@ impl Context { unsafe { scope.cast_local(|_| v8__Context__Global(self)) }.unwrap() } - #[inline(always)] - pub fn set_promise_hooks<'s>( - &self, - _scope: &mut HandleScope<'s, ()>, - init_hook: Option>, - before_hook: Option>, - after_hook: Option>, - resolve_hook: Option>, - ) { - unsafe { - v8__Context__SetPromiseHooks( - self, - init_hook.map_or_else(null, |v| &*v), - before_hook.map_or_else(null, |v| &*v), - after_hook.map_or_else(null, |v| &*v), - resolve_hook.map_or_else(null, |v| &*v), - ) - } - } - #[inline] fn get_annex_mut<'a>( &'a self, diff --git a/src/scope.rs b/src/scope.rs index 92b2e129..04542db4 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -96,6 +96,7 @@ use crate::function::PropertyCallbackInfo; use crate::Context; use crate::Data; use crate::DataError; +use crate::Function; use crate::Handle; use crate::Isolate; use crate::Local; @@ -287,6 +288,26 @@ impl<'s> HandleScope<'s> { } } + #[inline(always)] + pub fn set_promise_hooks( + &mut self, + init_hook: Option>, + before_hook: Option>, + after_hook: Option>, + resolve_hook: Option>, + ) { + unsafe { + let sd = data::ScopeData::get_mut(self); + raw::v8__Context__SetPromiseHooks( + sd.get_current_context(), + init_hook.map_or_else(std::ptr::null, |v| &*v), + before_hook.map_or_else(std::ptr::null, |v| &*v), + after_hook.map_or_else(std::ptr::null, |v| &*v), + resolve_hook.map_or_else(std::ptr::null, |v| &*v), + ); + } + } + #[inline(always)] pub fn set_continuation_preserved_embedder_data( &mut self, @@ -1719,6 +1740,13 @@ mod raw { this: *const Context, index: usize, ) -> *const Data; + pub(super) fn v8__Context__SetPromiseHooks( + this: *const Context, + init_hook: *const Function, + before_hook: *const Function, + after_hook: *const Function, + resolve_hook: *const Function, + ); pub(super) fn v8__Context__SetContinuationPreservedEmbedderData( this: *const Context, value: *const Value, diff --git a/tests/test_api.rs b/tests/test_api.rs index cab1dff1..2c274042 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -3269,8 +3269,7 @@ fn context_promise_hooks() { .unwrap(), ) .unwrap(); - context.set_promise_hooks( - scope, + scope.set_promise_hooks( Some(init_hook), Some(before_hook), Some(after_hook), @@ -3349,13 +3348,7 @@ fn context_promise_hooks_partial() { .unwrap(), ) .unwrap(); - context.set_promise_hooks( - scope, - Some(init_hook), - Some(before_hook), - None, - None, - ); + scope.set_promise_hooks(Some(init_hook), Some(before_hook), None, None); let source = r#" function expect(expected, actual = promises.size) {