From c820e7393c5f68696f489d08d43549e9884f1c42 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 7 Jun 2019 18:06:07 -0400 Subject: [PATCH] WIP segfault --- core/isolate.rs | 26 ++++++++++++++++++-------- core/libdeno/binding.cc | 14 +++++++++++--- core/libdeno/modules.cc | 8 ++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/core/isolate.rs b/core/isolate.rs index c9b281a618703a..02ce8c67603368 100644 --- a/core/isolate.rs +++ b/core/isolate.rs @@ -217,11 +217,16 @@ impl Isolate { referrer: *const c_char, id: deno_dyn_import_id, ) { + let isolate = unsafe { Isolate::from_raw_ptr(user_data) }; + println!( + "isolate.config.dyn_import {}", + isolate.config.dyn_import.is_some() + ); + let specifier = unsafe { CStr::from_ptr(specifier).to_str().unwrap() }; let referrer = unsafe { CStr::from_ptr(referrer).to_str().unwrap() }; - debug!("dyn_import specifier {} referrer {} ", specifier, referrer); + println!("dyn_import specifier {} referrer {} ", specifier, referrer); - let isolate = unsafe { Isolate::from_raw_ptr(user_data) }; if let Some(ref f) = isolate.config.dyn_import { let inner = f(specifier, referrer); let fut = DynImport { inner, id }; @@ -511,7 +516,7 @@ impl Future for Isolate { loop { // If there are any pending dyn_import futures, do those first. - if !self.pending_dyn_imports.is_empty(){ + if !self.pending_dyn_imports.is_empty() { match self.pending_dyn_imports.poll() { Err(()) => panic!("unexpected dyn_import error"), Ok(Ready(None)) => break, @@ -921,16 +926,16 @@ pub mod tests { let mod_b2 = mod_b.clone(); let mut config = Config::default(); - config.dyn_import(move |specifier, referrer| { + config.dyn_import(move |_specifier, referrer| { count_.fetch_add(1, Ordering::Relaxed); - assert_eq!(specifier, "foo.js"); + // assert_eq!(specifier, "foo.js"); assert_eq!(referrer, "dyn_import3.js"); let mod_id = mod_b2.lock().unwrap(); - println!("dyn_import callback mod_id = {}", *mod_id); Box::new(futures::future::ok(*mod_id)) }); let mut isolate = Isolate::new(StartupData::None, config); + // Instantiate mod_b { let mut mod_id = mod_b.lock().unwrap(); @@ -947,9 +952,14 @@ pub mod tests { "dyn_import3.js", r#" (async () => { - let mod = await import("foo.js"); + let mod = await import("foo1.js"); + if (mod.b() !== 'b') { + throw Error("bad1"); + } + // And again! + mod = await import("foo2.js"); if (mod.b() !== 'b') { - throw Error("bad"); + throw Error("bad2"); } })(); "#, diff --git a/core/libdeno/binding.cc b/core/libdeno/binding.cc index 4aeb6003afcd3f..7d91b088d23617 100644 --- a/core/libdeno/binding.cc +++ b/core/libdeno/binding.cc @@ -515,6 +515,8 @@ v8::MaybeLocal HostImportModuleDynamicallyCallback( auto* isolate = context->GetIsolate(); DenoIsolate* d = DenoIsolate::FromIsolate(isolate); v8::Isolate::Scope isolate_scope(isolate); + v8::Context::Scope context_scope(context); + v8::EscapableHandleScope handle_scope(isolate); v8::String::Utf8Value specifier_str(isolate, specifier); @@ -536,9 +538,15 @@ v8::MaybeLocal HostImportModuleDynamicallyCallback( std::make_tuple(import_id), std::make_tuple(d->isolate_, resolver)); - d->dyn_import_cb_(d->user_data_, *specifier_str, *referrer_name_str, - import_id); - return resolver->GetPromise(); + printf("BEFORE dyn_import_cb_ %s \n", *specifier_str); + { + // deno::UserDataScope user_data_scope(d, d->user_data_); + d->dyn_import_cb_(d->user_data_, *specifier_str, *referrer_name_str, + import_id); + } + printf("AFTER dyn_import_cb_ %s \n", *specifier_str); + auto promise = resolver->GetPromise(); + return handle_scope.Escape(promise); } void DenoIsolate::AddIsolate(v8::Isolate* isolate) { diff --git a/core/libdeno/modules.cc b/core/libdeno/modules.cc index 2a89ae8fbb2cdc..26edc43078b0f0 100644 --- a/core/libdeno/modules.cc +++ b/core/libdeno/modules.cc @@ -153,6 +153,8 @@ void deno_mod_evaluate(Deno* d_, void* user_data, deno_mod id) { void deno_dyn_import(Deno* d_, deno_dyn_import_id import_id, deno_mod mod_id) { auto* d = unwrap(d_); + // deno::UserDataScope user_data_scope(d, user_data); + auto* isolate = d->isolate_; v8::Isolate::Scope isolate_scope(isolate); v8::Locker locker(isolate); @@ -160,6 +162,8 @@ void deno_dyn_import(Deno* d_, deno_dyn_import_id import_id, deno_mod mod_id) { auto context = d->context_.Get(d->isolate_); v8::Context::Scope context_scope(context); + v8::TryCatch try_catch(isolate); + auto it = d->dyn_import_map_.find(import_id); if (it == d->dyn_import_map_.end()) { CHECK(false); // TODO(ry) error on bad import_id. @@ -184,6 +188,10 @@ void deno_dyn_import(Deno* d_, deno_dyn_import_id import_id, deno_mod mod_id) { persistent_promise->Reset(); d->dyn_import_map_.erase(it); + + if (try_catch.HasCaught()) { + HandleException(context, try_catch.Exception()); + } } } // extern "C"