2019-12-24 14:03:32 +01:00
|
|
|
use crate::support::int;
|
|
|
|
use crate::Context;
|
|
|
|
use crate::Isolate;
|
|
|
|
use crate::Local;
|
|
|
|
use std::mem::MaybeUninit;
|
2019-12-25 08:14:55 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2019-12-24 14:03:32 +01:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn v8__SnapshotCreator__CONSTRUCT(buf: &mut MaybeUninit<SnapshotCreator>);
|
|
|
|
fn v8__SnapshotCreator__DESTRUCT(this: &mut SnapshotCreator);
|
|
|
|
fn v8__SnapshotCreator__GetIsolate(
|
|
|
|
this: &mut SnapshotCreator,
|
|
|
|
) -> &mut Isolate;
|
|
|
|
fn v8__SnapshotCreator__CreateBlob(
|
|
|
|
this: *mut SnapshotCreator,
|
|
|
|
function_code_handling: FunctionCodeHandling,
|
|
|
|
) -> StartupData;
|
|
|
|
fn v8__SnapshotCreator__SetDefaultContext(
|
|
|
|
this: &mut SnapshotCreator,
|
|
|
|
context: *mut Context,
|
|
|
|
);
|
2019-12-25 08:14:55 -05:00
|
|
|
fn v8__StartupData__DESTRUCT(this: &mut StartupData);
|
2019-12-24 14:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct StartupData {
|
2019-12-25 08:14:55 -05:00
|
|
|
data: *mut u8,
|
|
|
|
raw_size: int,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for StartupData {
|
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
unsafe { std::slice::from_raw_parts(self.data, self.raw_size as usize) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for StartupData {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
unsafe { std::slice::from_raw_parts_mut(self.data, self.raw_size as usize) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for StartupData {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { v8__StartupData__DESTRUCT(self) }
|
|
|
|
}
|
2019-12-24 14:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum FunctionCodeHandling {
|
|
|
|
Clear,
|
|
|
|
Keep,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper class to create a snapshot data blob.
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct SnapshotCreator([usize; 1]);
|
|
|
|
|
|
|
|
impl Default for SnapshotCreator {
|
|
|
|
/// Create and enter an isolate, and set it up for serialization.
|
|
|
|
/// The isolate is created from scratch.
|
|
|
|
fn default() -> Self {
|
|
|
|
let mut snapshot_creator: MaybeUninit<Self> = MaybeUninit::uninit();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
v8__SnapshotCreator__CONSTRUCT(&mut snapshot_creator);
|
|
|
|
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.
|
|
|
|
pub fn set_default_context<'sc>(&mut self, mut context: Local<'sc, Context>) {
|
|
|
|
unsafe { v8__SnapshotCreator__SetDefaultContext(self, &mut *context) };
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2019-12-25 08:14:55 -05:00
|
|
|
) -> Option<StartupData> {
|
|
|
|
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 14:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the isolate prepared by the snapshot creator.
|
|
|
|
pub fn get_isolate(&mut self) -> &Isolate {
|
|
|
|
unsafe { v8__SnapshotCreator__GetIsolate(self) }
|
|
|
|
}
|
|
|
|
}
|