diff options
author | F001 <changchun.fan@qq.com> | 2018-12-05 12:36:10 +0800 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-12-05 00:14:53 -0800 |
commit | 60c008d23b2bdad333711b43148a5053e83a62cc (patch) | |
tree | f14a975d28387ab8d4906580b871958b3907a511 /src/libdeno.rs | |
parent | 45320beca82518e998614d2cbb4e733a47b7fef9 (diff) |
Isolate::from_raw_ptr and other cleanups.
`Isolate::from_void_ptr` is renamed to `from_raw_ptr`, to keep
consistency with std libs.
It is changed to `unsafe` function, because it can't guarantee that the
input is valid. This guarantee should be provided by the caller.
Its return type is changed to `&Isolate`, because `&mut Isolate` type
requires that no other aliases co-exist in this period of time, this
does not seem true. So I changed most of the methods to accept shared
reference `&Isolate`. It is easier to reason about the correctness of
`unsafe` blocks. As long as these shared references are in the same
thread, these `unsafe` codes are probably correct.
Diffstat (limited to 'src/libdeno.rs')
-rw-r--r-- | src/libdeno.rs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/libdeno.rs b/src/libdeno.rs index 3e41c7d60..cbc074f1b 100644 --- a/src/libdeno.rs +++ b/src/libdeno.rs @@ -31,6 +31,7 @@ pub struct deno_buf { unsafe impl Send for deno_buf {} impl deno_buf { + #[inline] pub fn empty() -> Self { Self { alloc_ptr: null(), @@ -40,6 +41,7 @@ impl deno_buf { } } + #[inline] pub unsafe fn from_raw_parts(ptr: *const u8, len: usize) -> Self { Self { alloc_ptr: null(), @@ -52,6 +54,7 @@ impl deno_buf { /// 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(), @@ -64,27 +67,37 @@ impl<'a> From<&'a [u8]> for deno_buf { 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 } } @@ -112,13 +125,13 @@ extern "C" { pub fn deno_check_promise_errors(i: *const isolate); pub fn deno_respond( i: *const isolate, - user_data: *mut c_void, + user_data: *const c_void, req_id: i32, buf: deno_buf, ); pub fn deno_execute( i: *const isolate, - user_data: *mut c_void, + user_data: *const c_void, js_filename: *const c_char, js_source: *const c_char, ) -> c_int; |