2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-07-18 16:44:09 -04:00
|
|
|
use libc::c_char;
|
|
|
|
use libc::c_int;
|
|
|
|
use libc::c_void;
|
2018-12-04 16:21:02 -05:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::ptr::null;
|
2018-07-18 16:44:09 -04:00
|
|
|
|
2018-12-04 16:21:02 -05:00
|
|
|
// TODO(F001): change this definition to `extern { pub type isolate; }`
|
|
|
|
// After RFC 1861 is stablized. See https://github.com/rust-lang/rust/issues/43467.
|
2018-07-18 16:44:09 -04:00
|
|
|
#[repr(C)]
|
2018-09-22 08:47:44 -04:00
|
|
|
pub struct isolate {
|
2018-07-19 21:14:32 -04:00
|
|
|
_unused: [u8; 0],
|
2018-07-18 16:44:09 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:21:02 -05:00
|
|
|
/// If "alloc_ptr" is not null, this type represents a buffer which is created
|
|
|
|
/// in C side, and then passed to Rust side by `DenoRecvCb`. Finally it should
|
|
|
|
/// be moved back to C side by `deno_respond`. If it is not passed to
|
|
|
|
/// `deno_respond` in the end, it will be leaked.
|
|
|
|
///
|
|
|
|
/// If "alloc_ptr" is null, this type represents a borrowed slice.
|
2018-07-18 16:44:09 -04:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct deno_buf {
|
2018-12-04 16:21:02 -05:00
|
|
|
alloc_ptr: *const u8,
|
|
|
|
alloc_len: usize,
|
|
|
|
data_ptr: *const u8,
|
|
|
|
data_len: usize,
|
2018-07-18 16:44:09 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:21:02 -05:00
|
|
|
/// `deno_buf` can not clone, and there is no interior mutability.
|
|
|
|
/// This type satisfies Send bound.
|
|
|
|
unsafe impl Send for deno_buf {}
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
impl deno_buf {
|
|
|
|
pub fn empty() -> Self {
|
2018-11-30 03:30:49 -05:00
|
|
|
Self {
|
2018-12-04 16:21:02 -05:00
|
|
|
alloc_ptr: null(),
|
2018-11-29 22:03:00 -05:00
|
|
|
alloc_len: 0,
|
2018-12-04 16:21:02 -05:00
|
|
|
data_ptr: null(),
|
2018-11-29 22:03:00 -05:00
|
|
|
data_len: 0,
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 16:21:02 -05:00
|
|
|
|
|
|
|
pub unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
alloc_ptr: null(),
|
|
|
|
alloc_len: 0,
|
|
|
|
data_ptr: ptr,
|
|
|
|
data_len: len,
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 22:03:00 -05:00
|
|
|
}
|
|
|
|
|
2018-12-03 22:07:34 -05:00
|
|
|
/// Converts Rust &Buf to libdeno `deno_buf`.
|
|
|
|
impl<'a> From<&'a [u8]> for deno_buf {
|
|
|
|
fn from(x: &'a [u8]) -> Self {
|
|
|
|
Self {
|
2018-12-04 16:21:02 -05:00
|
|
|
alloc_ptr: null(),
|
2018-12-03 22:07:34 -05:00
|
|
|
alloc_len: 0,
|
2018-12-04 16:21:02 -05:00
|
|
|
data_ptr: x.as_ref().as_ptr(),
|
2018-12-03 22:07:34 -05:00
|
|
|
data_len: x.len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:21:02 -05:00
|
|
|
impl Deref for deno_buf {
|
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &[u8] {
|
|
|
|
unsafe { std::slice::from_raw_parts(self.data_ptr, self.data_len) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for deno_buf {
|
|
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
|
|
|
unsafe {
|
|
|
|
std::slice::from_raw_parts_mut(self.data_ptr as *mut u8, self.data_len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for deno_buf {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsMut<[u8]> for deno_buf {
|
|
|
|
fn as_mut(&mut self) -> &mut [u8] {
|
|
|
|
&mut *self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
type DenoRecvCb = unsafe extern "C" fn(
|
2018-10-08 05:04:33 -04:00
|
|
|
user_data: *mut c_void,
|
2018-09-27 17:33:10 -04:00
|
|
|
req_id: i32,
|
|
|
|
buf: deno_buf,
|
|
|
|
data_buf: deno_buf,
|
|
|
|
);
|
2018-07-18 16:44:09 -04:00
|
|
|
|
2018-12-04 18:06:20 -05:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct deno_config {
|
|
|
|
pub shared: deno_buf,
|
|
|
|
pub recv_cb: DenoRecvCb,
|
|
|
|
}
|
|
|
|
|
2018-07-18 16:44:09 -04:00
|
|
|
extern "C" {
|
2018-07-19 21:14:32 -04:00
|
|
|
pub fn deno_init();
|
|
|
|
pub fn deno_v8_version() -> *const c_char;
|
2018-09-22 08:47:44 -04:00
|
|
|
pub fn deno_set_v8_flags(argc: *mut c_int, argv: *mut *mut c_char);
|
2018-12-04 18:06:20 -05:00
|
|
|
pub fn deno_new(snapshot: deno_buf, config: deno_config) -> *const isolate;
|
2018-09-22 08:47:44 -04:00
|
|
|
pub fn deno_delete(i: *const isolate);
|
|
|
|
pub fn deno_last_exception(i: *const isolate) -> *const c_char;
|
2018-10-12 14:22:52 -04:00
|
|
|
pub fn deno_check_promise_errors(i: *const isolate);
|
2018-10-07 17:48:06 -04:00
|
|
|
pub fn deno_respond(
|
|
|
|
i: *const isolate,
|
|
|
|
user_data: *mut c_void,
|
|
|
|
req_id: i32,
|
|
|
|
buf: deno_buf,
|
|
|
|
);
|
2018-07-19 21:14:32 -04:00
|
|
|
pub fn deno_execute(
|
2018-09-22 08:47:44 -04:00
|
|
|
i: *const isolate,
|
2018-10-07 17:48:06 -04:00
|
|
|
user_data: *mut c_void,
|
2018-07-19 21:14:32 -04:00
|
|
|
js_filename: *const c_char,
|
|
|
|
js_source: *const c_char,
|
|
|
|
) -> c_int;
|
2018-07-18 16:44:09 -04:00
|
|
|
}
|