1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use libc::c_char;
use libc::c_int;
use libc::c_void;
use libc::size_t;
use std::ops::{Deref, DerefMut};
use std::ptr::null;
// TODO(F001): change this definition to `extern { pub type isolate; }`
// After RFC 1861 is stablized. See https://github.com/rust-lang/rust/issues/43467.
#[repr(C)]
pub struct isolate {
_unused: [u8; 0],
}
/// If "alloc_ptr" is not null, this type represents a buffer which is created
/// in C side, and then passed to Rust side by `deno_recv_cb`. 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.
#[repr(C)]
pub struct deno_buf {
alloc_ptr: *const u8,
alloc_len: usize,
data_ptr: *const u8,
data_len: usize,
}
/// `deno_buf` can not clone, and there is no interior mutability.
/// This type satisfies Send bound.
unsafe impl Send for deno_buf {}
impl deno_buf {
#[inline]
pub fn empty() -> Self {
Self {
alloc_ptr: null(),
alloc_len: 0,
data_ptr: null(),
data_len: 0,
}
}
#[inline]
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,
}
}
}
/// Converts Rust &Buf to libdeno `deno_buf`.
impl<'a> From<&'a [u8]> for deno_buf {
#[inline]
fn from(x: &'a [u8]) -> Self {
Self {
alloc_ptr: null(),
alloc_len: 0,
data_ptr: x.as_ref().as_ptr(),
data_len: x.len(),
}
}
}
impl Deref for deno_buf {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.data_ptr, self.data_len) }
}
}
impl DerefMut for deno_buf {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
if self.alloc_ptr.is_null() {
panic!("Can't modify the buf");
}
std::slice::from_raw_parts_mut(self.data_ptr as *mut u8, self.data_len)
}
}
}
impl AsRef<[u8]> for deno_buf {
#[inline]
fn as_ref(&self) -> &[u8] {
&*self
}
}
impl AsMut<[u8]> for deno_buf {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
if self.alloc_ptr.is_null() {
panic!("Can't modify the buf");
}
&mut *self
}
}
#[allow(non_camel_case_types)]
type deno_recv_cb = unsafe extern "C" fn(
user_data: *mut c_void,
req_id: i32,
buf: deno_buf,
data_buf: deno_buf,
);
#[allow(non_camel_case_types)]
pub type deno_mod = i32;
#[allow(non_camel_case_types)]
type deno_resolve_cb = unsafe extern "C" fn(
user_data: *mut c_void,
specifier: *const c_char,
referrer: deno_mod,
) -> deno_mod;
#[repr(C)]
pub struct deno_config {
pub will_snapshot: c_int,
pub load_snapshot: deno_buf,
pub shared: deno_buf,
pub recv_cb: deno_recv_cb,
}
extern "C" {
pub fn deno_init();
pub fn deno_v8_version() -> *const c_char;
pub fn deno_set_v8_flags(argc: *mut c_int, argv: *mut *mut c_char);
pub fn deno_new(config: deno_config) -> *const isolate;
pub fn deno_delete(i: *const isolate);
pub fn deno_last_exception(i: *const isolate) -> *const c_char;
pub fn deno_check_promise_errors(i: *const isolate);
pub fn deno_respond(
i: *const isolate,
user_data: *const c_void,
req_id: i32,
buf: deno_buf,
);
pub fn deno_execute(
i: *const isolate,
user_data: *const c_void,
js_filename: *const c_char,
js_source: *const c_char,
);
// Modules
pub fn deno_mod_new(
i: *const isolate,
name: *const c_char,
source: *const c_char,
) -> deno_mod;
pub fn deno_mod_imports_len(i: *const isolate, id: deno_mod) -> size_t;
pub fn deno_mod_imports_get(
i: *const isolate,
id: deno_mod,
index: size_t,
) -> *const c_char;
pub fn deno_mod_instantiate(
i: *const isolate,
user_data: *const c_void,
id: deno_mod,
resolve_cb: deno_resolve_cb,
);
pub fn deno_mod_evaluate(
i: *const isolate,
user_data: *const c_void,
id: deno_mod,
);
}
|