From 24286a4d71fb262a9aaab48ea5bc9d5efe61c171 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Tue, 31 Dec 2019 02:42:39 +0100 Subject: [PATCH] Add StartupData constructor (#156) --- src/binding.cc | 3 +-- src/isolate.rs | 2 +- src/snapshot.rs | 55 +++++++++++++++++++++++++++++++++++------------ tests/test_api.rs | 16 ++++++++++++++ 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 2ae7b4bc..d0798861 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -583,8 +583,7 @@ void v8__FunctionCallbackInfo__GetReturnValue( } v8::Value* v8__FunctionCallbackInfo__GetArgument( - const v8::FunctionCallbackInfo& self, - int i) { + const v8::FunctionCallbackInfo& self, int i) { return local_to_ptr(self[i]); } diff --git a/src/isolate.rs b/src/isolate.rs index 2a3d3a18..2302bf70 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -336,7 +336,7 @@ impl CreateParams { /// - The call will abort if the data is invalid. pub fn set_snapshot_blob(&mut self, snapshot_blob: &mut StartupData) { unsafe { - v8__Isolate__CreateParams__SET__snapshot_blob(self, &mut *snapshot_blob) + v8__Isolate__CreateParams__SET__snapshot_blob(self, snapshot_blob) }; } } diff --git a/src/snapshot.rs b/src/snapshot.rs index 0be5c93c..450c9b90 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -1,12 +1,15 @@ +use std::borrow::Borrow; +use std::marker::PhantomData; +use std::mem::MaybeUninit; +use std::ops::Deref; +use std::ops::DerefMut; + use crate::external_references::ExternalReferences; use crate::support::int; use crate::support::intptr_t; use crate::Context; use crate::Isolate; use crate::Local; -use std::mem::MaybeUninit; -use std::ops::Deref; -use std::ops::DerefMut; extern "C" { fn v8__SnapshotCreator__CONSTRUCT( @@ -20,7 +23,7 @@ extern "C" { fn v8__SnapshotCreator__CreateBlob( this: *mut SnapshotCreator, function_code_handling: FunctionCodeHandling, - ) -> StartupData; + ) -> OwnedStartupData; fn v8__SnapshotCreator__SetDefaultContext( this: &mut SnapshotCreator, context: *mut Context, @@ -28,29 +31,53 @@ extern "C" { fn v8__StartupData__DESTRUCT(this: &mut StartupData); } -#[derive(Debug)] #[repr(C)] -pub struct StartupData { - data: *mut u8, +pub struct StartupData<'a> { + data: *const u8, raw_size: int, + _phantom: PhantomData<&'a [u8]>, } -impl Deref for StartupData { +impl<'a> StartupData<'a> { + pub fn new(data: &'a D) -> Self + where + D: Borrow<[u8]>, + { + let data = data.borrow(); + Self { + data: data.as_ptr(), + raw_size: data.len() as int, + _phantom: PhantomData, + } + } +} + +impl<'a> Deref for StartupData<'a> { 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) } +#[repr(transparent)] +pub struct OwnedStartupData(StartupData<'static>); + +impl Deref for OwnedStartupData { + type Target = StartupData<'static>; + fn deref(&self) -> &Self::Target { + &self.0 } } -impl Drop for StartupData { +impl DerefMut for OwnedStartupData { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Drop for OwnedStartupData { fn drop(&mut self) { - unsafe { v8__StartupData__DESTRUCT(self) } + unsafe { v8__StartupData__DESTRUCT(&mut self.0) } } } @@ -103,7 +130,7 @@ impl SnapshotCreator { pub fn create_blob( &mut self, function_code_handling: FunctionCodeHandling, - ) -> Option { + ) -> Option { let blob = unsafe { v8__SnapshotCreator__CreateBlob(self, function_code_handling) }; if blob.data.is_null() { diff --git a/tests/test_api.rs b/tests/test_api.rs index 573b0d0b..73b12782 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1437,6 +1437,22 @@ fn external_references() { drop(g); } +#[test] +fn startup_data() { + let data1 = b"abcd"; + let sd1 = v8::StartupData::new(data1); + assert_eq!(&*sd1, data1); + + let data2 = b"defg"; + let vec2 = Vec::from(&data2[..]); + let sd2 = v8::StartupData::new(&vec2); + assert_eq!(&*sd2, data2); + + let data3 = b"hijk"; + let sd3 = Box::new(v8::StartupData::new(data3)); + assert_eq!(&**sd3, data3); +} + #[test] fn uint8_array() { let g = setup();