0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-12 17:09:28 -05:00

Add StartupData constructor (#156)

This commit is contained in:
Bert Belder 2019-12-31 02:42:39 +01:00
parent 7b28a18097
commit 24286a4d71
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
4 changed files with 59 additions and 17 deletions

View file

@ -583,8 +583,7 @@ void v8__FunctionCallbackInfo__GetReturnValue(
}
v8::Value* v8__FunctionCallbackInfo__GetArgument(
const v8::FunctionCallbackInfo<v8::Value>& self,
int i) {
const v8::FunctionCallbackInfo<v8::Value>& self, int i) {
return local_to_ptr(self[i]);
}

View file

@ -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)
};
}
}

View file

@ -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<D>(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<StartupData> {
) -> Option<OwnedStartupData> {
let blob =
unsafe { v8__SnapshotCreator__CreateBlob(self, function_code_handling) };
if blob.data.is_null() {

View file

@ -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();