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

@ -1684,8 +1684,8 @@ void v8__Context__SetPromiseHooks(v8::Context& self, v8::Function& init_hook,
const v8::Context* v8__Context__FromSnapshot(v8::Isolate* isolate,
size_t context_snapshot_index) {
v8::MaybeLocal<v8::Context> maybe_local = v8::Context::FromSnapshot(
isolate, context_snapshot_index);
v8::MaybeLocal<v8::Context> maybe_local =
v8::Context::FromSnapshot(isolate, context_snapshot_index);
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,
const intptr_t* external_references,
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) {
self->~SnapshotCreator();
}
void v8__StartupData__DESTRUCT(v8::StartupData* self) { delete[] self->data; }
v8::Isolate* v8__SnapshotCreator__GetIsolate(const v8::SnapshotCreator& self) {
// `v8::SnapshotCreator::GetIsolate()` is not declared as a const method, but
// 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) {
delete self;
}
void char__DELETE_ARRAY(const char* ptr[]) { delete[] ptr; }
} // extern "C"

View file

@ -3,12 +3,15 @@ use crate::scope::data::ScopeData;
use crate::support::char;
use crate::support::int;
use crate::support::intptr_t;
use crate::support::Allocated;
use crate::support::CharArray;
use crate::Context;
use crate::Data;
use crate::Isolate;
use crate::Local;
use crate::OwnedIsolate;
use std::any::Any;
use std::borrow::Borrow;
use std::convert::TryFrom;
use std::mem::MaybeUninit;
@ -45,7 +48,6 @@ extern "C" {
context: *const Context,
data: *const Data,
) -> usize;
fn v8__StartupData__DESTRUCT(this: *mut StartupData);
}
// TODO(piscisaureus): merge this struct with
@ -57,23 +59,18 @@ pub struct StartupData {
raw_size: int,
}
impl From<Box<[u8]>> for StartupData {
fn from(slice: Box<[u8]>) -> Self {
let mut data: Vec<u8> = vec![];
data.copy_from_slice(&slice);
StartupData {
impl StartupData {
pub fn from_static(data: &'static [u8]) -> Self {
Self {
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 {
fn from(slice: &[u8]) -> Self {
StartupData {
data: slice.as_ptr() as *const char,
raw_size: slice.len() as int,
}
impl From<&'static [u8]> for StartupData {
fn from(data: &'static [u8]) -> Self {
Self::from_static(data)
}
}
@ -98,9 +95,51 @@ impl Borrow<[u8]> for StartupData {
}
}
impl Drop for StartupData {
fn drop(&mut self) {
unsafe { v8__StartupData__DESTRUCT(self) }
/// A `StartupData` object that owns the data it references, which is kept in a
/// char array allocated in C++. This is what V8 returns when calling
/// `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)]
pub fn new(
external_references: Option<&'static ExternalReferences>,
existing_blob: Option<&StartupData>,
existing_blob: Option<impl Allocated<[u8]>>,
) -> Self {
let mut snapshot_creator: MaybeUninit<Self> = MaybeUninit::uninit();
let external_references_ptr = if let Some(er) = external_references {
@ -205,7 +244,7 @@ impl SnapshotCreator {
pub fn create_blob(
&mut self,
function_code_handling: FunctionCodeHandling,
) -> Option<StartupData> {
) -> Option<OwnedStartupData> {
{
let isolate = unsafe { &mut *v8__SnapshotCreator__GetIsolate(self) };
ScopeData::get_root_mut(isolate);
@ -217,7 +256,10 @@ impl SnapshotCreator {
None
} else {
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
/// address in memory. Practically speaking, that means they're either a
/// `&'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();
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));
}
}