0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-27 01:29:19 -05:00

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 <bertbelder@gmail.com>
This commit is contained in:
Bartek Iwańczuk 2023-02-09 13:34:28 +01:00 committed by GitHub
parent 83b54692c1
commit 3b6d79c0e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 0 deletions

View file

@ -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());
}

View file

@ -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<Value>,
) {
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<HandleScope>,

View file

@ -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();