0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00
This commit is contained in:
Bartek Iwańczuk 2022-06-07 18:05:11 +02:00 committed by Bert Belder
parent d8c7abdbaf
commit bace807372
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
4 changed files with 93 additions and 26 deletions

View file

@ -1682,10 +1682,10 @@ void v8__Context__SetPromiseHooks(v8::Context& self, v8::Function& init_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,
size_t context_snapshot_index) { size_t context_snapshot_index) {
v8::MaybeLocal<v8::Context> maybe_local = v8::Context::FromSnapshot( v8::MaybeLocal<v8::Context> maybe_local =
isolate, context_snapshot_index); v8::Context::FromSnapshot(isolate, context_snapshot_index);
return maybe_local_to_ptr(maybe_local); return maybe_local_to_ptr(maybe_local);
} }
@ -2337,15 +2337,14 @@ void v8__Proxy__Revoke(const v8::Proxy& self) { ptr_to_local(&self)->Revoke(); }
void v8__SnapshotCreator__CONSTRUCT(uninit_t<v8::SnapshotCreator>* buf, void v8__SnapshotCreator__CONSTRUCT(uninit_t<v8::SnapshotCreator>* buf,
const intptr_t* external_references, const intptr_t* external_references,
v8::StartupData* existing_blob) { v8::StartupData* existing_blob) {
construct_in_place<v8::SnapshotCreator>(buf, external_references, existing_blob); construct_in_place<v8::SnapshotCreator>(buf, external_references,
existing_blob);
} }
void v8__SnapshotCreator__DESTRUCT(v8::SnapshotCreator* self) { void v8__SnapshotCreator__DESTRUCT(v8::SnapshotCreator* self) {
self->~SnapshotCreator(); self->~SnapshotCreator();
} }
void v8__StartupData__DESTRUCT(v8::StartupData* self) { delete[] self->data; }
v8::Isolate* v8__SnapshotCreator__GetIsolate(const v8::SnapshotCreator& self) { v8::Isolate* v8__SnapshotCreator__GetIsolate(const v8::SnapshotCreator& self) {
// `v8::SnapshotCreator::GetIsolate()` is not declared as a const method, but // `v8::SnapshotCreator::GetIsolate()` is not declared as a const method, but
// this appears to be a mistake. // this appears to be a mistake.
@ -3160,4 +3159,7 @@ const char* v8__CompiledWasmModule__SourceUrl(v8::CompiledWasmModule* self,
void v8__CompiledWasmModule__DELETE(v8::CompiledWasmModule* self) { void v8__CompiledWasmModule__DELETE(v8::CompiledWasmModule* self) {
delete self; delete self;
} }
void char__DELETE_ARRAY(const char* ptr[]) { delete[] ptr; }
} // extern "C" } // extern "C"

View file

@ -3,12 +3,15 @@ use crate::scope::data::ScopeData;
use crate::support::char; use crate::support::char;
use crate::support::int; use crate::support::int;
use crate::support::intptr_t; use crate::support::intptr_t;
use crate::support::Allocated;
use crate::support::CharArray;
use crate::Context; use crate::Context;
use crate::Data; use crate::Data;
use crate::Isolate; use crate::Isolate;
use crate::Local; use crate::Local;
use crate::OwnedIsolate; use crate::OwnedIsolate;
use std::any::Any;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
@ -45,7 +48,6 @@ extern "C" {
context: *const Context, context: *const Context,
data: *const Data, data: *const Data,
) -> usize; ) -> usize;
fn v8__StartupData__DESTRUCT(this: *mut StartupData);
} }
// TODO(piscisaureus): merge this struct with // TODO(piscisaureus): merge this struct with
@ -57,23 +59,18 @@ pub struct StartupData {
raw_size: int, raw_size: int,
} }
impl From<Box<[u8]>> for StartupData { impl StartupData {
fn from(slice: Box<[u8]>) -> Self { pub fn from_static(data: &'static [u8]) -> Self {
let mut data: Vec<u8> = vec![]; Self {
data.copy_from_slice(&slice);
StartupData {
data: data.as_ptr() as *const char, data: data.as_ptr() as *const char,
raw_size: data.len() as int, raw_size: i32::try_from(data.len()).unwrap(),
} }
} }
} }
impl From<&[u8]> for StartupData { impl From<&'static [u8]> for StartupData {
fn from(slice: &[u8]) -> Self { fn from(data: &'static [u8]) -> Self {
StartupData { Self::from_static(data)
data: slice.as_ptr() as *const char,
raw_size: slice.len() as int,
}
} }
} }
@ -98,9 +95,51 @@ impl Borrow<[u8]> for StartupData {
} }
} }
impl Drop for StartupData { /// A `StartupData` object that owns the data it references, which is kept in a
fn drop(&mut self) { /// char array allocated in C++. This is what V8 returns when calling
unsafe { v8__StartupData__DESTRUCT(self) } /// `SnapshotCreator::CreateBlob()`.
pub struct OwnedStartupData {
inner: StartupData,
_owner: Box<dyn Any>,
}
impl OwnedStartupData {
pub fn new<T>(owned_data: T) -> Self
where
T: Deref<Target = [u8]> + 'static,
{
let inner = {
let slice = owned_data.deref();
let data = slice.as_ptr() as *const char;
let raw_size = int::try_from(slice.len()).unwrap();
StartupData { data, raw_size }
};
Self {
inner,
_owner: Box::new(owned_data),
}
}
fn from_raw_char_array(raw: StartupData) -> Self {
let char_array = unsafe { CharArray::from_ptr(raw.data) };
Self {
inner: raw,
_owner: Box::new(char_array),
}
}
}
impl Deref for OwnedStartupData {
type Target = StartupData;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl Borrow<[u8]> for OwnedStartupData {
fn borrow(&self) -> &[u8] {
&**self
} }
} }
@ -122,7 +161,7 @@ impl SnapshotCreator {
#[inline(always)] #[inline(always)]
pub fn new( pub fn new(
external_references: Option<&'static ExternalReferences>, external_references: Option<&'static ExternalReferences>,
existing_blob: Option<&StartupData>, existing_blob: Option<impl Allocated<[u8]>>,
) -> Self { ) -> Self {
let mut snapshot_creator: MaybeUninit<Self> = MaybeUninit::uninit(); let mut snapshot_creator: MaybeUninit<Self> = MaybeUninit::uninit();
let external_references_ptr = if let Some(er) = external_references { let external_references_ptr = if let Some(er) = external_references {
@ -205,7 +244,7 @@ impl SnapshotCreator {
pub fn create_blob( pub fn create_blob(
&mut self, &mut self,
function_code_handling: FunctionCodeHandling, function_code_handling: FunctionCodeHandling,
) -> Option<StartupData> { ) -> Option<OwnedStartupData> {
{ {
let isolate = unsafe { &mut *v8__SnapshotCreator__GetIsolate(self) }; let isolate = unsafe { &mut *v8__SnapshotCreator__GetIsolate(self) };
ScopeData::get_root_mut(isolate); ScopeData::get_root_mut(isolate);
@ -217,7 +256,10 @@ impl SnapshotCreator {
None None
} else { } else {
debug_assert!(blob.raw_size > 0); debug_assert!(blob.raw_size > 0);
Some(blob) // Wrap the returned blob in a `OwnedStartupData`, which has a drop
// handler that will release the heap allocated `char[]` buffer that holds
// the snapshot blob returned by `v8::SnapshotCreator::CreateBlob()`.
Some(OwnedStartupData::from_raw_char_array(blob))
} }
} }

View file

@ -365,6 +365,29 @@ fn assert_shared_ptr_use_count_eq<T: Shared>(
); );
} }
extern "C" {
fn char__DELETE_ARRAY(ptr: *const char);
}
/// Wrapper around a C++ heap allocated `const char*` array. This wrapper calls
/// `delete[]` in C++ when it is dropped.
#[repr(transparent)]
pub struct CharArray(*const char);
impl CharArray {
pub unsafe fn from_ptr(ptr: *const char) -> Self {
Self(ptr)
}
}
impl Drop for CharArray {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { char__DELETE_ARRAY(self.0) };
}
}
}
/// A trait for values with static lifetimes that are allocated at a fixed /// A trait for values with static lifetimes that are allocated at a fixed
/// address in memory. Practically speaking, that means they're either a /// address in memory. Practically speaking, that means they're either a
/// `&'static` reference, or they're heap-allocated in a `Arc`, `Box`, `Rc`, /// `&'static` reference, or they're heap-allocated in a `Arc`, `Box`, `Rc`,

View file

@ -5225,7 +5225,7 @@ fn module_snapshot() {
let result = eval(scope, "a === 3").unwrap(); let result = eval(scope, "a === 3").unwrap();
assert!(result.same_value(true_val)); assert!(result.same_value(true_val));
let result = eval(scope, "a === 42").unwrap(); let result = eval(scope, "b === 42").unwrap();
assert!(result.same_value(true_val)); assert!(result.same_value(true_val));
} }
} }