diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-04-08 10:12:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-08 10:12:43 -0400 |
commit | f7fdb90fd51e340ea598c055bb3573d3cdfbdaa8 (patch) | |
tree | 40db117b3a9fd2ac70e3b5551195e21eef464138 /core/libdeno.rs | |
parent | cdb72afd8d91978573f0fa897844aee853983b44 (diff) |
core: snapshot improvements (#2052)
* Moves how snapshots are supplied to the Isolate. Previously they were
given by Behavior::startup_data() but it was only called once at
startup. It makes more sense (and simplifies Behavior) to pass it to the
constructor of Isolate.
* Adds new libdeno type deno_snapshot instead of overloading
deno_buf.
* Adds new libdeno method to delete snapshot deno_snapshot_delete().
* Renames deno_get_snapshot() to deno_snapshot_new().
* Makes StartupData hold references to snapshots. This was implicit when
it previously held a deno_buf but is made explicit now. Note that
include_bytes!() returns a &'static [u8] and we want to avoid
copying that.
Diffstat (limited to 'core/libdeno.rs')
-rw-r--r-- | core/libdeno.rs | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/core/libdeno.rs b/core/libdeno.rs index 1a80330de..e6445f299 100644 --- a/core/libdeno.rs +++ b/core/libdeno.rs @@ -4,6 +4,7 @@ use libc::c_char; use libc::c_int; use libc::c_void; use libc::size_t; +use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::ptr::null; @@ -108,6 +109,56 @@ impl AsMut<[u8]> for deno_buf { } } +#[repr(C)] +pub struct deno_snapshot<'a> { + pub data_ptr: *const u8, + pub data_len: usize, + _marker: PhantomData<&'a [u8]>, +} + +/// `deno_snapshot` can not clone, and there is no interior mutability. +/// This type satisfies Send bound. +unsafe impl Send for deno_snapshot<'_> {} + +// TODO(ry) Snapshot1 and Snapshot2 are not very good names and need to be +// reconsidered. The entire snapshotting interface is still under construction. + +/// The type returned from deno_snapshot_new. Needs to be dropped. +pub type Snapshot1<'a> = deno_snapshot<'a>; + +// TODO Does this make sense? +impl Drop for Snapshot1<'_> { + fn drop(&mut self) { + unsafe { deno_snapshot_delete(self) } + } +} + +/// The type created from slice. Used for loading. +pub type Snapshot2<'a> = deno_snapshot<'a>; + +/// Converts Rust &Buf to libdeno `deno_buf`. +impl<'a> From<&'a [u8]> for Snapshot2<'a> { + #[inline] + fn from(x: &'a [u8]) -> Self { + Self { + data_ptr: x.as_ref().as_ptr(), + data_len: x.len(), + _marker: PhantomData, + } + } +} + +impl Snapshot2<'_> { + #[inline] + pub fn empty() -> Self { + Self { + data_ptr: null(), + data_len: 0, + _marker: PhantomData, + } + } +} + #[allow(non_camel_case_types)] type deno_recv_cb = unsafe extern "C" fn( user_data: *mut c_void, @@ -126,9 +177,9 @@ type deno_resolve_cb = unsafe extern "C" fn( ) -> deno_mod; #[repr(C)] -pub struct deno_config { +pub struct deno_config<'a> { pub will_snapshot: c_int, - pub load_snapshot: deno_buf, + pub load_snapshot: Snapshot2<'a>, pub shared: deno_buf, pub recv_cb: deno_recv_cb, } @@ -214,4 +265,9 @@ extern "C" { user_data: *const c_void, id: deno_mod, ); + + pub fn deno_snapshot_new(i: *const isolate) -> Snapshot1<'static>; + + #[allow(dead_code)] + pub fn deno_snapshot_delete(s: &mut deno_snapshot); } |