1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-10 08:09:06 -05:00

libdeno: don't pass pointers between core::Isolate and libdeno (#3602)

This commit is contained in:
Bartek Iwańczuk 2020-01-05 20:52:03 +01:00 committed by GitHub
parent c1d99ebeb9
commit a29eeaf326
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 147 deletions

View file

@ -164,8 +164,8 @@ type IsolateErrorHandleFn = dyn FnMut(ErrBox) -> Result<(), ErrBox>;
/// by implementing dispatcher function that takes control buffer and optional zero copy buffer /// by implementing dispatcher function that takes control buffer and optional zero copy buffer
/// as arguments. An async Op corresponds exactly to a Promise in JavaScript. /// as arguments. An async Op corresponds exactly to a Promise in JavaScript.
pub struct Isolate { pub struct Isolate {
libdeno_isolate: *mut libdeno::isolate, libdeno_isolate: Box<libdeno::DenoIsolate>,
shared_libdeno_isolate: Arc<Mutex<Option<*mut libdeno::isolate>>>, shared_libdeno_isolate: Arc<Mutex<Option<*mut libdeno::DenoIsolate>>>,
dyn_import: Option<Arc<DynImportFn>>, dyn_import: Option<Arc<DynImportFn>>,
js_error_create: Arc<JSErrorCreateFn>, js_error_create: Arc<JSErrorCreateFn>,
needs_init: bool, needs_init: bool,
@ -185,8 +185,6 @@ impl Drop for Isolate {
fn drop(&mut self) { fn drop(&mut self) {
// remove shared_libdeno_isolate reference // remove shared_libdeno_isolate reference
*self.shared_libdeno_isolate.lock().unwrap() = None; *self.shared_libdeno_isolate.lock().unwrap() = None;
unsafe { libdeno::deno_delete(self.libdeno_isolate) }
} }
} }
@ -229,10 +227,12 @@ impl Isolate {
}; };
let libdeno_isolate = unsafe { libdeno::deno_new(libdeno_config) }; let libdeno_isolate = unsafe { libdeno::deno_new(libdeno_config) };
let shared_libdeno_isolate = Arc::new(Mutex::new(Some(libdeno_isolate)));
let libdeno_isolate = unsafe { Box::from_raw(libdeno_isolate) };
Self { Self {
libdeno_isolate, libdeno_isolate,
shared_libdeno_isolate: Arc::new(Mutex::new(Some(libdeno_isolate))), shared_libdeno_isolate,
dyn_import: None, dyn_import: None,
js_error_create: Arc::new(CoreJSError::from_v8_exception), js_error_create: Arc::new(CoreJSError::from_v8_exception),
shared, shared,
@ -388,27 +388,21 @@ impl Isolate {
js_source: &str, js_source: &str,
) -> Result<(), ErrBox> { ) -> Result<(), ErrBox> {
self.shared_init(); self.shared_init();
unsafe { let core_i = self as *mut _ as *mut c_void;
libdeno::deno_execute( let libdeno_i = &mut self.libdeno_isolate;
self.libdeno_isolate, unsafe { libdeno::deno_execute(libdeno_i, core_i, js_filename, js_source) };
self as *mut _ as *mut c_void,
js_filename,
js_source,
)
};
self.check_last_exception() self.check_last_exception()
} }
fn check_last_exception(&mut self) -> Result<(), ErrBox> { fn check_last_exception(&mut self) -> Result<(), ErrBox> {
let maybe_err = let maybe_err = self.libdeno_isolate.last_exception_.clone();
unsafe { libdeno::deno_last_exception(self.libdeno_isolate) };
match maybe_err { match maybe_err {
None => Ok(()), None => Ok(()),
Some(json_str) => { Some(json_str) => {
let js_error_create = &*self.js_error_create; let js_error_create = &*self.js_error_create;
if self.error_handler.is_some() { if self.error_handler.is_some() {
// We need to clear last exception to avoid double handling. // We need to clear last exception to avoid double handling.
unsafe { libdeno::deno_clear_last_exception(self.libdeno_isolate) }; self.libdeno_isolate.last_exception_ = None;
let v8_exception = V8Exception::from_json(&json_str).unwrap(); let v8_exception = V8Exception::from_json(&json_str).unwrap();
let js_error = js_error_create(v8_exception); let js_error = js_error_create(v8_exception);
let handler = self.error_handler.as_mut().unwrap(); let handler = self.error_handler.as_mut().unwrap();
@ -422,15 +416,15 @@ impl Isolate {
} }
} }
fn check_promise_errors(&self) { fn check_promise_errors(&mut self) {
unsafe { unsafe {
libdeno::deno_check_promise_errors(self.libdeno_isolate); libdeno::deno_check_promise_errors(&mut self.libdeno_isolate);
} }
} }
fn throw_exception(&mut self, exception_text: &str) { fn throw_exception(&mut self, exception_text: &str) {
unsafe { unsafe {
libdeno::deno_throw_exception(self.libdeno_isolate, exception_text) libdeno::deno_throw_exception(&mut self.libdeno_isolate, exception_text)
} }
} }
@ -442,9 +436,9 @@ impl Isolate {
None => (0, deno_buf::empty()), None => (0, deno_buf::empty()),
Some((op_id, r)) => (op_id, deno_buf::from(r)), Some((op_id, r)) => (op_id, deno_buf::from(r)),
}; };
unsafe { let core_i = self.as_raw_ptr();
libdeno::deno_respond(self.libdeno_isolate, self.as_raw_ptr(), op_id, buf) let libdeno_i = &mut self.libdeno_isolate;
} unsafe { libdeno::deno_respond(libdeno_i, core_i, op_id, buf) }
self.check_last_exception() self.check_last_exception()
} }
@ -455,21 +449,17 @@ impl Isolate {
name: &str, name: &str,
source: &str, source: &str,
) -> Result<deno_mod, ErrBox> { ) -> Result<deno_mod, ErrBox> {
let id = unsafe { let id = self.libdeno_isolate.register_module(main, name, source);
libdeno::deno_mod_new(self.libdeno_isolate, main, name, source)
};
self.check_last_exception().map(|_| id) self.check_last_exception().map(|_| id)
} }
pub fn mod_get_imports(&self, id: deno_mod) -> Vec<String> { pub fn mod_get_imports(&self, id: deno_mod) -> Vec<String> {
let len = let info = self.libdeno_isolate.get_module_info(id).unwrap();
unsafe { libdeno::deno_mod_imports_len(self.libdeno_isolate, id) }; let len = info.import_specifiers.len();
let mut out = Vec::new(); let mut out = Vec::new();
for i in 0..len { for i in 0..len {
let specifier = let info = self.libdeno_isolate.get_module_info(id).unwrap();
unsafe { libdeno::deno_mod_imports_get(self.libdeno_isolate, id, i) } let specifier = info.import_specifiers.get(i).unwrap().to_string();
.unwrap();
out.push(specifier); out.push(specifier);
} }
out out
@ -482,7 +472,7 @@ impl Isolate {
/// the V8 exception. By default this type is CoreJSError, however it may be a /// the V8 exception. By default this type is CoreJSError, however it may be a
/// different type if Isolate::set_js_error_create() has been used. /// different type if Isolate::set_js_error_create() has been used.
pub fn snapshot(&mut self) -> Result<Snapshot1, ErrBox> { pub fn snapshot(&mut self) -> Result<Snapshot1, ErrBox> {
let snapshot = libdeno::deno_snapshot_new(self.libdeno_isolate); let snapshot = libdeno::deno_snapshot_new(&mut self.libdeno_isolate);
match self.check_last_exception() { match self.check_last_exception() {
Ok(..) => Ok(snapshot), Ok(..) => Ok(snapshot),
Err(err) => Err(err), Err(err) => Err(err),
@ -500,10 +490,13 @@ impl Isolate {
Err(None) => (0, None), Err(None) => (0, None),
Err(Some(err_str)) => (0, Some(err_str)), Err(Some(err_str)) => (0, Some(err_str)),
}; };
let core_i = self.as_raw_ptr();
let libdeno_i = &mut self.libdeno_isolate;
unsafe { unsafe {
libdeno::deno_dyn_import_done( libdeno::deno_dyn_import_done(
self.libdeno_isolate, libdeno_i,
self.as_raw_ptr(), core_i,
id, id,
mod_id, mod_id,
maybe_err_str, maybe_err_str,
@ -588,11 +581,10 @@ impl Isolate {
id: deno_mod, id: deno_mod,
resolve_fn: &mut ResolveFn, resolve_fn: &mut ResolveFn,
) -> Result<(), ErrBox> { ) -> Result<(), ErrBox> {
let libdeno_isolate = self.libdeno_isolate;
let mut ctx = ResolveContext { resolve_fn }; let mut ctx = ResolveContext { resolve_fn };
unsafe { unsafe {
libdeno::deno_mod_instantiate( libdeno::deno_mod_instantiate(
libdeno_isolate, &mut self.libdeno_isolate,
ctx.as_raw_ptr(), ctx.as_raw_ptr(),
id, id,
Self::resolve_cb, Self::resolve_cb,
@ -622,30 +614,13 @@ impl Isolate {
/// different type if Isolate::set_js_error_create() has been used. /// different type if Isolate::set_js_error_create() has been used.
pub fn mod_evaluate(&mut self, id: deno_mod) -> Result<(), ErrBox> { pub fn mod_evaluate(&mut self, id: deno_mod) -> Result<(), ErrBox> {
self.shared_init(); self.shared_init();
unsafe { let core_i = self.as_raw_ptr();
libdeno::deno_mod_evaluate(self.libdeno_isolate, self.as_raw_ptr(), id) let libdeno_i = &mut self.libdeno_isolate;
}; unsafe { libdeno::deno_mod_evaluate(libdeno_i, core_i, id) };
self.check_last_exception() self.check_last_exception()
} }
} }
struct LockerScope {
libdeno_isolate: *mut libdeno::isolate,
}
impl LockerScope {
fn new(libdeno_isolate: *mut libdeno::isolate) -> LockerScope {
unsafe { libdeno::deno_lock(libdeno_isolate) }
LockerScope { libdeno_isolate }
}
}
impl Drop for LockerScope {
fn drop(&mut self) {
unsafe { libdeno::deno_unlock(self.libdeno_isolate) }
}
}
impl Future for Isolate { impl Future for Isolate {
type Output = Result<(), ErrBox>; type Output = Result<(), ErrBox>;
@ -686,20 +661,14 @@ impl Future for Isolate {
} }
if inner.shared.size() > 0 { if inner.shared.size() > 0 {
// Lock the current thread for V8.
let locker = LockerScope::new(inner.libdeno_isolate);
inner.respond(None)?; inner.respond(None)?;
// The other side should have shifted off all the messages. // The other side should have shifted off all the messages.
assert_eq!(inner.shared.size(), 0); assert_eq!(inner.shared.size(), 0);
drop(locker);
} }
if overflow_response.is_some() { if overflow_response.is_some() {
// Lock the current thread for V8.
let locker = LockerScope::new(inner.libdeno_isolate);
let (op_id, buf) = overflow_response.take().unwrap(); let (op_id, buf) = overflow_response.take().unwrap();
inner.respond(Some((op_id, &buf)))?; inner.respond(Some((op_id, &buf)))?;
drop(locker);
} }
inner.check_promise_errors(); inner.check_promise_errors();
@ -720,7 +689,7 @@ impl Future for Isolate {
/// IsolateHandle is a thread safe handle on an Isolate. It exposed thread safe V8 functions. /// IsolateHandle is a thread safe handle on an Isolate. It exposed thread safe V8 functions.
#[derive(Clone)] #[derive(Clone)]
pub struct IsolateHandle { pub struct IsolateHandle {
shared_libdeno_isolate: Arc<Mutex<Option<*mut libdeno::isolate>>>, shared_libdeno_isolate: Arc<Mutex<Option<*mut libdeno::DenoIsolate>>>,
} }
unsafe impl Send for IsolateHandle {} unsafe impl Send for IsolateHandle {}
@ -730,10 +699,8 @@ impl IsolateHandle {
/// After terminating execution it is probably not wise to continue using /// After terminating execution it is probably not wise to continue using
/// the isolate. /// the isolate.
pub fn terminate_execution(&self) { pub fn terminate_execution(&self) {
unsafe { if let Some(isolate) = *self.shared_libdeno_isolate.lock().unwrap() {
if let Some(isolate) = *self.shared_libdeno_isolate.lock().unwrap() { unsafe { libdeno::deno_terminate_execution(isolate) }
libdeno::deno_terminate_execution(isolate)
}
} }
} }
} }

View file

@ -27,16 +27,16 @@ pub type OpId = u32;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub type isolate = DenoIsolate; pub type isolate = DenoIsolate;
struct ModuleInfo { pub struct ModuleInfo {
main: bool, pub main: bool,
name: String, pub name: String,
handle: v8::Global<v8::Module>, pub handle: v8::Global<v8::Module>,
import_specifiers: Vec<String>, pub import_specifiers: Vec<String>,
} }
pub struct DenoIsolate { pub struct DenoIsolate {
isolate_: Option<v8::OwnedIsolate>, isolate_: Option<v8::OwnedIsolate>,
last_exception_: Option<String>, pub last_exception_: Option<String>,
last_exception_handle_: v8::Global<v8::Value>, last_exception_handle_: v8::Global<v8::Value>,
context_: v8::Global<v8::Context>, context_: v8::Global<v8::Context>,
mods_: HashMap<deno_mod, ModuleInfo>, mods_: HashMap<deno_mod, ModuleInfo>,
@ -208,7 +208,7 @@ impl DenoIsolate {
id id
} }
fn get_module_info(&self, id: deno_mod) -> Option<&ModuleInfo> { pub fn get_module_info(&self, id: deno_mod) -> Option<&ModuleInfo> {
if id == 0 { if id == 0 {
return None; return None;
} }
@ -925,7 +925,7 @@ lazy_static! {
]); ]);
} }
pub unsafe fn deno_new_snapshotter(config: deno_config) -> *mut isolate { pub unsafe fn deno_new_snapshotter(config: deno_config) -> *mut DenoIsolate {
assert_ne!(config.will_snapshot, 0); assert_ne!(config.will_snapshot, 0);
// TODO(ry) Support loading snapshots before snapshotting. // TODO(ry) Support loading snapshots before snapshotting.
assert!(config.load_snapshot.is_none()); assert!(config.load_snapshot.is_none());
@ -1382,7 +1382,7 @@ fn initialize_context<'a>(
context.exit(); context.exit();
} }
pub unsafe fn deno_new(config: deno_config) -> *mut isolate { pub unsafe fn deno_new(config: deno_config) -> *mut DenoIsolate {
if config.will_snapshot != 0 { if config.will_snapshot != 0 {
return deno_new_snapshotter(config); return deno_new_snapshotter(config);
} }
@ -1416,21 +1416,7 @@ pub unsafe fn deno_new(config: deno_config) -> *mut isolate {
Box::into_raw(d) Box::into_raw(d)
} }
pub unsafe fn deno_delete(i: *mut DenoIsolate) { pub unsafe fn deno_check_promise_errors(i_mut: &mut DenoIsolate) {
let deno_isolate = unsafe { Box::from_raw(i as *mut DenoIsolate) };
drop(deno_isolate);
}
pub unsafe fn deno_last_exception(i: *mut DenoIsolate) -> Option<String> {
(*i).last_exception_.clone()
}
pub unsafe fn deno_clear_last_exception(i: *mut DenoIsolate) {
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
i_mut.last_exception_ = None;
}
pub unsafe fn deno_check_promise_errors(i: *mut DenoIsolate) {
/* /*
if (d->pending_promise_map_.size() > 0) { if (d->pending_promise_map_.size() > 0) {
auto* isolate = d->isolate_; auto* isolate = d->isolate_;
@ -1448,7 +1434,6 @@ pub unsafe fn deno_check_promise_errors(i: *mut DenoIsolate) {
} }
} }
*/ */
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
let isolate = i_mut.isolate_.as_ref().unwrap(); let isolate = i_mut.isolate_.as_ref().unwrap();
if i_mut.pending_promise_map_.is_empty() { if i_mut.pending_promise_map_.is_empty() {
@ -1473,20 +1458,7 @@ pub unsafe fn deno_check_promise_errors(i: *mut DenoIsolate) {
context.exit(); context.exit();
} }
pub unsafe fn deno_lock(i: *mut DenoIsolate) { pub unsafe fn deno_throw_exception(i_mut: &mut DenoIsolate, text: &str) {
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
assert!(i_mut.locker_.is_none());
let mut locker = v8::Locker::new(i_mut.isolate_.as_ref().unwrap());
i_mut.locker_ = Some(locker);
}
pub unsafe fn deno_unlock(i: *mut DenoIsolate) {
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
i_mut.locker_.take().unwrap();
}
pub unsafe fn deno_throw_exception(i: *mut DenoIsolate, text: &str) {
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
let isolate = i_mut.isolate_.as_ref().unwrap(); let isolate = i_mut.isolate_.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); let mut locker = v8::Locker::new(isolate);
let mut hs = v8::HandleScope::new(&mut locker); let mut hs = v8::HandleScope::new(&mut locker);
@ -1551,7 +1523,7 @@ pub unsafe fn deno_import_buf<'sc>(
} }
pub unsafe fn deno_respond( pub unsafe fn deno_respond(
i: *mut isolate, deno_isolate: &mut DenoIsolate,
core_isolate: *const c_void, core_isolate: *const c_void,
op_id: OpId, op_id: OpId,
buf: deno_buf, buf: deno_buf,
@ -1569,7 +1541,6 @@ pub unsafe fn deno_respond(
return; return;
} }
*/ */
let deno_isolate: &mut DenoIsolate = unsafe { &mut *i };
if !deno_isolate.current_args_.is_null() { if !deno_isolate.current_args_.is_null() {
// Synchronous response. // Synchronous response.
@ -1673,12 +1644,11 @@ pub unsafe fn deno_respond(
} }
pub unsafe fn deno_execute( pub unsafe fn deno_execute(
i: *mut DenoIsolate, i_mut: &mut DenoIsolate,
core_isolate: *mut c_void, core_isolate: *mut c_void,
js_filename: &str, js_filename: &str,
js_source: &str, js_source: &str,
) { ) {
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
i_mut.core_isolate_ = core_isolate; i_mut.core_isolate_ = core_isolate;
let isolate = i_mut.isolate_.as_ref().unwrap(); let isolate = i_mut.isolate_.as_ref().unwrap();
// println!("deno_execute -> Isolate ptr {:?}", isolate); // println!("deno_execute -> Isolate ptr {:?}", isolate);
@ -1737,35 +1707,6 @@ pub unsafe fn deno_run_microtasks(i: *mut isolate, core_isolate: *mut c_void) {
// Modules // Modules
pub unsafe fn deno_mod_new(
i: *mut DenoIsolate,
main: bool,
name: &str,
source: &str,
) -> deno_mod {
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
i_mut.register_module(main, name, source)
}
pub unsafe fn deno_mod_imports_len(i: *mut DenoIsolate, id: deno_mod) -> usize {
let info = (*i).get_module_info(id).unwrap();
info.import_specifiers.len()
}
pub unsafe fn deno_mod_imports_get(
i: *mut DenoIsolate,
id: deno_mod,
index: size_t,
) -> Option<String> {
match (*i).get_module_info(id) {
Some(info) => match info.import_specifiers.get(index) {
Some(specifier) => Some(specifier.to_string()),
None => None,
},
None => None,
}
}
fn resolve_callback( fn resolve_callback(
context: v8::Local<v8::Context>, context: v8::Local<v8::Context>,
specifier: v8::Local<v8::String>, specifier: v8::Local<v8::String>,
@ -1822,12 +1763,11 @@ fn resolve_callback(
} }
pub unsafe fn deno_mod_instantiate( pub unsafe fn deno_mod_instantiate(
i: *mut DenoIsolate, i_mut: &mut DenoIsolate,
resolve_context: *mut c_void, resolve_context: *mut c_void,
id: deno_mod, id: deno_mod,
resolve_cb: deno_resolve_cb, resolve_cb: deno_resolve_cb,
) { ) {
let i_mut: &mut DenoIsolate = unsafe { &mut *i };
i_mut.resolve_context_ = resolve_context; i_mut.resolve_context_ = resolve_context;
let isolate = i_mut.isolate_.as_ref().unwrap(); let isolate = i_mut.isolate_.as_ref().unwrap();
let mut locker = v8::Locker::new(isolate); let mut locker = v8::Locker::new(isolate);
@ -1868,11 +1808,10 @@ pub unsafe fn deno_mod_instantiate(
} }
pub unsafe fn deno_mod_evaluate( pub unsafe fn deno_mod_evaluate(
i: *mut DenoIsolate, deno_isolate: &mut DenoIsolate,
core_isolate: *const c_void, core_isolate: *const c_void,
id: deno_mod, id: deno_mod,
) { ) {
let deno_isolate: &mut DenoIsolate = unsafe { &mut *i };
let core_isolate: *mut c_void = unsafe { std::mem::transmute(core_isolate) }; let core_isolate: *mut c_void = unsafe { std::mem::transmute(core_isolate) };
deno_isolate.core_isolate_ = core_isolate; deno_isolate.core_isolate_ = core_isolate;
let isolate = deno_isolate.isolate_.as_ref().unwrap(); let isolate = deno_isolate.isolate_.as_ref().unwrap();
@ -1920,13 +1859,12 @@ pub unsafe fn deno_mod_evaluate(
/// Call exactly once for every deno_dyn_import_cb. /// Call exactly once for every deno_dyn_import_cb.
pub unsafe fn deno_dyn_import_done( pub unsafe fn deno_dyn_import_done(
i: *mut DenoIsolate, deno_isolate: &mut DenoIsolate,
core_isolate: *const c_void, core_isolate: *const c_void,
id: deno_dyn_import_id, id: deno_dyn_import_id,
mod_id: deno_mod, mod_id: deno_mod,
error_str: Option<String>, error_str: Option<String>,
) { ) {
let deno_isolate: &mut DenoIsolate = unsafe { &mut *i };
assert!( assert!(
(mod_id == 0 && error_str.is_some()) (mod_id == 0 && error_str.is_some())
|| (mod_id != 0 && error_str.is_none()) || (mod_id != 0 && error_str.is_none())
@ -1981,8 +1919,9 @@ pub unsafe fn deno_dyn_import_done(
deno_isolate.core_isolate_ = std::ptr::null_mut(); deno_isolate.core_isolate_ = std::ptr::null_mut();
} }
pub fn deno_snapshot_new(i: *mut DenoIsolate) -> v8::OwnedStartupData { pub fn deno_snapshot_new(
let deno_isolate: &mut DenoIsolate = unsafe { &mut *i }; deno_isolate: &mut DenoIsolate,
) -> v8::OwnedStartupData {
assert!(deno_isolate.snapshot_creator_.is_some()); assert!(deno_isolate.snapshot_creator_.is_some());
let isolate = deno_isolate.isolate_.as_ref().unwrap(); let isolate = deno_isolate.isolate_.as_ref().unwrap();