Skip to content

Commit

Permalink
WIP segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 7, 2019
1 parent cf09d26 commit c820e73
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
26 changes: 18 additions & 8 deletions core/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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");
}
})();
"#,
Expand Down
14 changes: 11 additions & 3 deletions core/libdeno/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ v8::MaybeLocal<v8::Promise> 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);

Expand All @@ -536,9 +538,15 @@ v8::MaybeLocal<v8::Promise> 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) {
Expand Down
8 changes: 8 additions & 0 deletions core/libdeno/modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ 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);
v8::HandleScope handle_scope(isolate);
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.
Expand All @@ -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"

0 comments on commit c820e73

Please sign in to comment.