2019-12-27 09:12:16 -05:00
|
|
|
use crate::external_references::ExternalReferences;
|
2020-06-03 01:38:34 -04:00
|
|
|
use crate::scope::data::ScopeData;
|
2020-04-20 15:18:03 -04:00
|
|
|
use crate::support::char;
|
2019-12-24 08:03:32 -05:00
|
|
|
use crate::support::int;
|
2019-12-27 09:12:16 -05:00
|
|
|
use crate::support::intptr_t;
|
2019-12-24 08:03:32 -05:00
|
|
|
use crate::Context;
|
|
|
|
use crate::Isolate;
|
|
|
|
use crate::Local;
|
2020-01-03 16:52:05 -05:00
|
|
|
use crate::OwnedIsolate;
|
2020-04-20 15:18:03 -04:00
|
|
|
|
2020-01-03 16:52:05 -05:00
|
|
|
use std::borrow::Borrow;
|
2020-04-20 15:18:03 -04:00
|
|
|
use std::convert::TryFrom;
|
2020-01-03 16:52:05 -05:00
|
|
|
use std::mem::MaybeUninit;
|
|
|
|
use std::ops::Deref;
|
2019-12-24 08:03:32 -05:00
|
|
|
|
|
|
|
extern "C" {
|
2019-12-27 09:12:16 -05:00
|
|
|
fn v8__SnapshotCreator__CONSTRUCT(
|
2020-04-13 08:43:56 -04:00
|
|
|
buf: *mut MaybeUninit<SnapshotCreator>,
|
2019-12-27 09:12:16 -05:00
|
|
|
external_references: *const intptr_t,
|
|
|
|
);
|
2020-04-13 08:43:56 -04:00
|
|
|
fn v8__SnapshotCreator__DESTRUCT(this: *mut SnapshotCreator);
|
|
|
|
fn v8__SnapshotCreator__GetIsolate(
|
|
|
|
this: *mut SnapshotCreator,
|
|
|
|
) -> *mut Isolate;
|
2019-12-24 08:03:32 -05:00
|
|
|
fn v8__SnapshotCreator__CreateBlob(
|
|
|
|
this: *mut SnapshotCreator,
|
|
|
|
function_code_handling: FunctionCodeHandling,
|
2020-04-20 15:18:03 -04:00
|
|
|
) -> StartupData;
|
2019-12-24 08:03:32 -05:00
|
|
|
fn v8__SnapshotCreator__SetDefaultContext(
|
2020-04-13 08:43:56 -04:00
|
|
|
this: *mut SnapshotCreator,
|
|
|
|
context: *const Context,
|
2019-12-24 08:03:32 -05:00
|
|
|
);
|
2020-04-13 08:43:56 -04:00
|
|
|
fn v8__StartupData__DESTRUCT(this: *mut StartupData);
|
2019-12-24 08:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2020-04-20 15:18:03 -04:00
|
|
|
pub struct StartupData {
|
|
|
|
data: *const char,
|
2019-12-25 08:14:55 -05:00
|
|
|
raw_size: int,
|
2019-12-30 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:18:03 -04:00
|
|
|
impl Deref for StartupData {
|
2019-12-25 08:14:55 -05:00
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2020-04-20 15:18:03 -04:00
|
|
|
let data = self.data as *const u8;
|
|
|
|
let len = usize::try_from(self.raw_size).unwrap();
|
|
|
|
unsafe { std::slice::from_raw_parts(data, len) }
|
2019-12-25 08:14:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:18:03 -04:00
|
|
|
impl AsRef<[u8]> for StartupData {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&**self
|
2019-12-30 20:42:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:18:03 -04:00
|
|
|
impl Borrow<[u8]> for StartupData {
|
|
|
|
fn borrow(&self) -> &[u8] {
|
|
|
|
&**self
|
2019-12-25 08:14:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:18:03 -04:00
|
|
|
impl Drop for StartupData {
|
2019-12-25 08:14:55 -05:00
|
|
|
fn drop(&mut self) {
|
2020-04-20 15:18:03 -04:00
|
|
|
unsafe { v8__StartupData__DESTRUCT(self) }
|
2019-12-25 08:14:55 -05:00
|
|
|
}
|
2019-12-24 08:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum FunctionCodeHandling {
|
|
|
|
Clear,
|
|
|
|
Keep,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper class to create a snapshot data blob.
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct SnapshotCreator([usize; 1]);
|
|
|
|
|
2019-12-27 09:12:16 -05:00
|
|
|
impl SnapshotCreator {
|
2019-12-24 08:03:32 -05:00
|
|
|
/// Create and enter an isolate, and set it up for serialization.
|
|
|
|
/// The isolate is created from scratch.
|
2019-12-27 09:12:16 -05:00
|
|
|
pub fn new(external_references: Option<&'static ExternalReferences>) -> Self {
|
2019-12-24 08:03:32 -05:00
|
|
|
let mut snapshot_creator: MaybeUninit<Self> = MaybeUninit::uninit();
|
2019-12-27 09:12:16 -05:00
|
|
|
let external_references_ptr = if let Some(er) = external_references {
|
|
|
|
er.as_ptr()
|
|
|
|
} else {
|
|
|
|
std::ptr::null()
|
|
|
|
};
|
2019-12-24 08:03:32 -05:00
|
|
|
unsafe {
|
2019-12-27 09:12:16 -05:00
|
|
|
v8__SnapshotCreator__CONSTRUCT(
|
|
|
|
&mut snapshot_creator,
|
|
|
|
external_references_ptr,
|
|
|
|
);
|
2019-12-24 08:03:32 -05:00
|
|
|
snapshot_creator.assume_init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for SnapshotCreator {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { v8__SnapshotCreator__DESTRUCT(self) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SnapshotCreator {
|
|
|
|
/// Set the default context to be included in the snapshot blob.
|
|
|
|
/// The snapshot will not contain the global proxy, and we expect one or a
|
|
|
|
/// global object template to create one, to be provided upon deserialization.
|
2020-06-03 01:38:34 -04:00
|
|
|
pub fn set_default_context<'s>(&mut self, context: Local<'s, Context>) {
|
2020-04-13 08:43:56 -04:00
|
|
|
unsafe { v8__SnapshotCreator__SetDefaultContext(self, &*context) };
|
2019-12-24 08:03:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a snapshot data blob.
|
|
|
|
/// This must not be called from within a handle scope.
|
|
|
|
pub fn create_blob(
|
|
|
|
&mut self,
|
|
|
|
function_code_handling: FunctionCodeHandling,
|
2020-04-20 15:18:03 -04:00
|
|
|
) -> Option<StartupData> {
|
2020-06-03 01:38:34 -04:00
|
|
|
{
|
|
|
|
let isolate = unsafe { &mut *v8__SnapshotCreator__GetIsolate(self) };
|
|
|
|
ScopeData::get_root_mut(isolate);
|
|
|
|
}
|
2019-12-25 08:14:55 -05:00
|
|
|
let blob =
|
|
|
|
unsafe { v8__SnapshotCreator__CreateBlob(self, function_code_handling) };
|
|
|
|
if blob.data.is_null() {
|
|
|
|
debug_assert!(blob.raw_size == 0);
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
debug_assert!(blob.raw_size > 0);
|
|
|
|
Some(blob)
|
|
|
|
}
|
2019-12-24 08:03:32 -05:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:05:01 -04:00
|
|
|
/// This is marked unsafe because it should be called at most once per
|
|
|
|
/// snapshot creator.
|
2020-01-03 16:52:05 -05:00
|
|
|
// TODO Because the SnapshotCreator creates its own isolate, we need a way to
|
|
|
|
// get an owned handle to it. This is a questionable design which ought to be
|
|
|
|
// revisited after the libdeno integration is complete.
|
|
|
|
pub unsafe fn get_owned_isolate(&mut self) -> OwnedIsolate {
|
|
|
|
let isolate_ptr = v8__SnapshotCreator__GetIsolate(self);
|
2020-05-06 13:26:23 -04:00
|
|
|
let mut owned_isolate = OwnedIsolate::new(isolate_ptr);
|
2020-06-03 01:38:34 -04:00
|
|
|
ScopeData::new_root(&mut owned_isolate);
|
2020-05-06 13:26:23 -04:00
|
|
|
owned_isolate.create_annex(Box::new(()));
|
|
|
|
owned_isolate
|
2020-01-03 16:52:05 -05:00
|
|
|
}
|
2019-12-24 08:03:32 -05:00
|
|
|
}
|