From 3b6d79c0e61aefa07a93fa574bc073b27f9e60b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 9 Feb 2023 13:34:28 +0100 Subject: [PATCH] feat: Add bindings for continuation embedder data (#1184) Adds bindings for: - v8::Context::GetContinuationPreservedEmbedderData - v8::Context::SetContinuationPreservedEmbedderData These APIs are available on the "HandleScope". --------- Co-authored-by: Bert Belder --- src/binding.cc | 12 ++++++++++++ src/scope.rs | 36 ++++++++++++++++++++++++++++++++++++ tests/test_api.rs | 24 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 81edebe1..fd738626 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1726,6 +1726,18 @@ const v8::Context* v8__Context__FromSnapshot(v8::Isolate* isolate, return maybe_local_to_ptr(maybe_local); } +void v8__Context__SetContinuationPreservedEmbedderData(v8::Context& context, + const v8::Value* data) { + auto c = ptr_to_local(&context); + c->SetContinuationPreservedEmbedderData(ptr_to_local(data)); +} + +const v8::Value* v8__Context__GetContinuationPreservedEmbedderData( + const v8::Context& context) { + auto value = ptr_to_local(&context)->GetContinuationPreservedEmbedderData(); + return local_to_ptr(value); +} + const v8::String* v8__Message__Get(const v8::Message& self) { return local_to_ptr(self.Get()); } diff --git a/src/scope.rs b/src/scope.rs index 85710457..92b2e129 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -286,6 +286,35 @@ impl<'s> HandleScope<'s> { .and_then(|data| data.try_into()) } } + + #[inline(always)] + pub fn set_continuation_preserved_embedder_data( + &mut self, + data: Local, + ) { + unsafe { + let sd = data::ScopeData::get_mut(self); + raw::v8__Context__SetContinuationPreservedEmbedderData( + sd.get_current_context(), + &*data, + ); + } + } + + #[inline(always)] + pub fn get_continuation_preserved_embedder_data( + &mut self, + ) -> Local<'s, Value> { + unsafe { + self + .cast_local(|sd| { + raw::v8__Context__GetContinuationPreservedEmbedderData( + sd.get_current_context(), + ) + }) + .unwrap() + } + } } /// A HandleScope which first allocates a handle in the current scope @@ -1690,6 +1719,13 @@ mod raw { this: *const Context, index: usize, ) -> *const Data; + pub(super) fn v8__Context__SetContinuationPreservedEmbedderData( + this: *const Context, + value: *const Value, + ); + pub(super) fn v8__Context__GetContinuationPreservedEmbedderData( + this: *const Context, + ) -> *const Value; pub(super) fn v8__HandleScope__CONSTRUCT( buf: *mut MaybeUninit, diff --git a/tests/test_api.rs b/tests/test_api.rs index 7165a50c..cab1dff1 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -4011,6 +4011,30 @@ fn array_buffer_view() { } } +#[test] +fn continuation_preserved_embedder_data() { + 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 data = scope.get_continuation_preserved_embedder_data(); + assert!(data.is_undefined()); + + let value = v8::String::new(scope, "hello").unwrap(); + scope.set_continuation_preserved_embedder_data(value.into()); + let data = scope.get_continuation_preserved_embedder_data(); + assert!(data.is_string()); + assert_eq!(data.to_rust_string_lossy(scope), "hello"); + + eval(scope, "b = 2 + 3").unwrap(); + let data = scope.get_continuation_preserved_embedder_data(); + assert!(data.is_string()); + assert_eq!(data.to_rust_string_lossy(scope), "hello"); + } +} + #[test] fn snapshot_creator() { let _setup_guard = setup::sequential_test();