2020-01-07 06:45:44 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
// This module provides higher level implementation of CoreIsolate that
|
2020-01-08 09:06:04 -05:00
|
|
|
// supports asynchronous loading and executution of ES Modules.
|
|
|
|
// The isolate.rs should never depend on this module.
|
2020-01-07 06:45:44 -05:00
|
|
|
|
|
|
|
use rusty_v8 as v8;
|
|
|
|
|
|
|
|
use crate::bindings;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::errors::ErrBox;
|
|
|
|
use crate::errors::ErrWithV8Handle;
|
2020-02-03 18:08:44 -05:00
|
|
|
use crate::futures::FutureExt;
|
2020-01-07 06:45:44 -05:00
|
|
|
use futures::ready;
|
|
|
|
use futures::stream::FuturesUnordered;
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use futures::stream::StreamFuture;
|
|
|
|
use futures::task::AtomicWaker;
|
2020-02-03 18:08:44 -05:00
|
|
|
use futures::Future;
|
2020-05-29 17:41:39 -04:00
|
|
|
use std::cell::RefCell;
|
2020-01-07 06:45:44 -05:00
|
|
|
use std::collections::HashMap;
|
2020-04-27 11:25:26 -04:00
|
|
|
use std::convert::TryFrom;
|
2020-01-07 06:45:44 -05:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::option::Option;
|
|
|
|
use std::pin::Pin;
|
2020-02-14 19:18:36 -05:00
|
|
|
use std::rc::Rc;
|
2020-01-07 06:45:44 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
|
|
|
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::core_isolate::exception_to_err_result;
|
|
|
|
use crate::errors::attach_handle_to_error;
|
2020-01-08 09:06:04 -05:00
|
|
|
use crate::module_specifier::ModuleSpecifier;
|
|
|
|
use crate::modules::LoadState;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::modules::ModuleId;
|
|
|
|
use crate::modules::ModuleLoadId;
|
2020-03-02 13:12:49 -05:00
|
|
|
use crate::modules::ModuleLoader;
|
|
|
|
use crate::modules::ModuleSource;
|
2020-01-08 09:06:04 -05:00
|
|
|
use crate::modules::Modules;
|
2020-04-30 08:37:06 -04:00
|
|
|
use crate::modules::PrepareLoadFuture;
|
2020-01-08 09:06:04 -05:00
|
|
|
use crate::modules::RecursiveModuleLoad;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::CoreIsolate;
|
|
|
|
use crate::StartupData;
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
/// More specialized version of `CoreIsolate` that provides loading
|
2020-01-11 04:49:16 -05:00
|
|
|
/// and execution of ES Modules.
|
2020-01-07 06:45:44 -05:00
|
|
|
///
|
2020-01-11 04:49:16 -05:00
|
|
|
/// Creating `EsIsolate` requires to pass `loader` argument
|
2020-03-02 13:12:49 -05:00
|
|
|
/// that implements `ModuleLoader` trait - that way actual resolution and
|
2020-01-11 04:49:16 -05:00
|
|
|
/// loading of modules can be customized by the implementor.
|
2020-05-29 17:41:39 -04:00
|
|
|
pub struct EsIsolate(CoreIsolate);
|
|
|
|
|
|
|
|
pub struct EsIsolateState {
|
2020-03-02 13:12:49 -05:00
|
|
|
loader: Rc<dyn ModuleLoader>,
|
2020-01-08 09:06:04 -05:00
|
|
|
pub modules: Modules,
|
2020-01-07 06:45:44 -05:00
|
|
|
pub(crate) dyn_import_map:
|
2020-04-26 13:00:10 -04:00
|
|
|
HashMap<ModuleLoadId, v8::Global<v8::PromiseResolver>>,
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-04-30 08:37:06 -04:00
|
|
|
preparing_dyn_imports: FuturesUnordered<Pin<Box<PrepareLoadFuture>>>,
|
2020-01-08 09:06:04 -05:00
|
|
|
pending_dyn_imports: FuturesUnordered<StreamFuture<RecursiveModuleLoad>>,
|
2020-01-07 06:45:44 -05:00
|
|
|
waker: AtomicWaker,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for EsIsolate {
|
2020-04-23 05:51:07 -04:00
|
|
|
type Target = CoreIsolate;
|
2020-01-07 06:45:44 -05:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2020-05-29 17:41:39 -04:00
|
|
|
&self.0
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for EsIsolate {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
2020-05-29 17:41:39 -04:00
|
|
|
&mut self.0
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EsIsolate {
|
2020-01-08 09:06:04 -05:00
|
|
|
pub fn new(
|
2020-03-02 13:12:49 -05:00
|
|
|
loader: Rc<dyn ModuleLoader>,
|
2020-01-08 09:06:04 -05:00
|
|
|
startup_data: StartupData,
|
|
|
|
will_snapshot: bool,
|
2020-05-29 17:41:39 -04:00
|
|
|
) -> Self {
|
2020-04-23 05:51:07 -04:00
|
|
|
let mut core_isolate = CoreIsolate::new(startup_data, will_snapshot);
|
2020-01-07 06:45:44 -05:00
|
|
|
{
|
2020-05-29 17:41:39 -04:00
|
|
|
core_isolate.set_host_initialize_import_meta_object_callback(
|
2020-01-07 06:45:44 -05:00
|
|
|
bindings::host_initialize_import_meta_object_callback,
|
|
|
|
);
|
2020-05-29 17:41:39 -04:00
|
|
|
core_isolate.set_host_import_module_dynamically_callback(
|
2020-01-07 06:45:44 -05:00
|
|
|
bindings::host_import_module_dynamically_callback,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
core_isolate.set_slot(Rc::new(RefCell::new(EsIsolateState {
|
2020-01-08 09:06:04 -05:00
|
|
|
modules: Modules::new(),
|
2020-02-14 19:18:36 -05:00
|
|
|
loader,
|
2020-01-07 06:45:44 -05:00
|
|
|
dyn_import_map: HashMap::new(),
|
2020-04-30 08:37:06 -04:00
|
|
|
preparing_dyn_imports: FuturesUnordered::new(),
|
2020-01-07 06:45:44 -05:00
|
|
|
pending_dyn_imports: FuturesUnordered::new(),
|
|
|
|
waker: AtomicWaker::new(),
|
2020-05-29 17:41:39 -04:00
|
|
|
})));
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
EsIsolate(core_isolate)
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-25 08:31:42 -05:00
|
|
|
/// Low-level module creation.
|
|
|
|
///
|
|
|
|
/// Called during module loading or dynamic import loading.
|
|
|
|
fn mod_new(
|
|
|
|
&mut self,
|
|
|
|
main: bool,
|
|
|
|
name: &str,
|
|
|
|
source: &str,
|
|
|
|
) -> Result<ModuleId, ErrBox> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
|
|
|
let core_state_rc = CoreIsolate::state(self);
|
2020-07-18 16:32:11 -04:00
|
|
|
let scope = &mut v8::HandleScope::with_context(
|
|
|
|
&mut *self.0,
|
|
|
|
core_state_rc.borrow().global_context.as_ref().unwrap(),
|
|
|
|
);
|
2020-01-07 06:45:44 -05:00
|
|
|
|
|
|
|
let name_str = v8::String::new(scope, name).unwrap();
|
|
|
|
let source_str = v8::String::new(scope, source).unwrap();
|
|
|
|
|
|
|
|
let origin = bindings::module_origin(scope, name_str);
|
|
|
|
let source = v8::script_compiler::Source::new(source_str, &origin);
|
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
let maybe_module = v8::script_compiler::compile_module(tc_scope, source);
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
if tc_scope.has_caught() {
|
2020-01-07 06:45:44 -05:00
|
|
|
assert!(maybe_module.is_none());
|
2020-06-20 07:18:08 -04:00
|
|
|
let e = tc_scope.exception().unwrap();
|
|
|
|
return exception_to_err_result(tc_scope, e);
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
2020-01-25 08:31:42 -05:00
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
let module = maybe_module.unwrap();
|
|
|
|
let id = module.get_identity_hash();
|
|
|
|
|
2020-01-25 12:53:16 -05:00
|
|
|
let mut import_specifiers: Vec<ModuleSpecifier> = vec![];
|
2020-01-07 06:45:44 -05:00
|
|
|
for i in 0..module.get_module_requests_length() {
|
2020-01-25 12:53:16 -05:00
|
|
|
let import_specifier =
|
2020-06-20 07:18:08 -04:00
|
|
|
module.get_module_request(i).to_rust_string_lossy(tc_scope);
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = state_rc.borrow();
|
2020-01-25 12:53:16 -05:00
|
|
|
let module_specifier =
|
2020-05-29 17:41:39 -04:00
|
|
|
state.loader.resolve(&import_specifier, name, false)?;
|
2020-01-25 12:53:16 -05:00
|
|
|
import_specifiers.push(module_specifier);
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
state_rc.borrow_mut().modules.register(
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
main,
|
|
|
|
v8::Global::<v8::Module>::new(tc_scope, module),
|
|
|
|
import_specifiers,
|
|
|
|
);
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-01-25 08:31:42 -05:00
|
|
|
Ok(id)
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-25 08:31:42 -05:00
|
|
|
/// Instantiates a ES module
|
2020-01-11 04:49:16 -05:00
|
|
|
///
|
2020-01-25 08:31:42 -05:00
|
|
|
/// ErrBox can be downcast to a type that exposes additional information about
|
2020-03-02 17:20:16 -05:00
|
|
|
/// the V8 exception. By default this type is JSError, however it may be a
|
2020-04-23 05:51:07 -04:00
|
|
|
/// different type if CoreIsolate::set_js_error_create_fn() has been used.
|
2020-01-25 08:31:42 -05:00
|
|
|
fn mod_instantiate(&mut self, id: ModuleId) -> Result<(), ErrBox> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let core_state_rc = CoreIsolate::state(self);
|
|
|
|
let core_state = core_state_rc.borrow();
|
2020-07-18 16:32:11 -04:00
|
|
|
let scope = &mut v8::HandleScope::with_context(
|
|
|
|
&mut *self.0,
|
|
|
|
core_state.global_context.as_ref().unwrap(),
|
|
|
|
);
|
2020-06-20 07:18:08 -04:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let module = match Self::state(tc_scope).borrow().modules.get_info(id) {
|
|
|
|
Some(info) => v8::Local::new(tc_scope, &info.handle),
|
2020-01-25 08:31:42 -05:00
|
|
|
None if id == 0 => return Ok(()),
|
|
|
|
_ => panic!("module id {} not found in module table", id),
|
|
|
|
};
|
2020-01-07 06:45:44 -05:00
|
|
|
|
|
|
|
if module.get_status() == v8::ModuleStatus::Errored {
|
2020-06-20 07:18:08 -04:00
|
|
|
exception_to_err_result(tc_scope, module.get_exception())?
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-25 08:31:42 -05:00
|
|
|
let result =
|
2020-06-20 07:18:08 -04:00
|
|
|
module.instantiate_module(tc_scope, bindings::module_resolve_callback);
|
2020-01-25 08:31:42 -05:00
|
|
|
match result {
|
|
|
|
Some(_) => Ok(()),
|
|
|
|
None => {
|
2020-06-20 07:18:08 -04:00
|
|
|
let exception = tc_scope.exception().unwrap();
|
|
|
|
exception_to_err_result(tc_scope, exception)
|
2020-01-25 08:31:42 -05:00
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
|
2020-01-25 08:31:42 -05:00
|
|
|
/// Evaluates an already instantiated ES module.
|
2020-01-07 06:45:44 -05:00
|
|
|
///
|
|
|
|
/// ErrBox can be downcast to a type that exposes additional information about
|
2020-03-02 17:20:16 -05:00
|
|
|
/// the V8 exception. By default this type is JSError, however it may be a
|
2020-04-23 05:51:07 -04:00
|
|
|
/// different type if CoreIsolate::set_js_error_create_fn() has been used.
|
2020-01-25 08:31:42 -05:00
|
|
|
pub fn mod_evaluate(&mut self, id: ModuleId) -> Result<(), ErrBox> {
|
2020-06-05 18:36:54 -04:00
|
|
|
self.shared_init();
|
2020-05-29 17:41:39 -04:00
|
|
|
|
|
|
|
let core_state_rc = CoreIsolate::state(self);
|
2020-02-24 18:53:29 -05:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let scope = &mut v8::HandleScope::with_context(
|
|
|
|
&mut *self.0,
|
|
|
|
core_state_rc.borrow().global_context.as_ref().unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let module = Self::state(scope)
|
|
|
|
.borrow()
|
|
|
|
.modules
|
|
|
|
.get_info(id)
|
|
|
|
.map(|info| v8::Local::new(scope, &info.handle))
|
|
|
|
.expect("ModuleInfo not found");
|
2020-01-07 06:45:44 -05:00
|
|
|
let mut status = module.get_status();
|
2020-07-18 16:32:11 -04:00
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
if status == v8::ModuleStatus::Instantiated {
|
2020-04-27 11:25:26 -04:00
|
|
|
// IMPORTANT: Top-level-await is enabled, which means that return value
|
|
|
|
// of module evaluation is a promise.
|
|
|
|
//
|
|
|
|
// Because that promise is created internally by V8, when error occurs during
|
|
|
|
// module evaluation the promise is rejected, and since the promise has no rejection
|
|
|
|
// handler it will result in call to `bindings::promise_reject_callback` adding
|
|
|
|
// the promise to pending promise rejection table - meaning Isolate will return
|
|
|
|
// error on next poll().
|
|
|
|
//
|
|
|
|
// This situation is not desirable as we want to manually return error at the
|
|
|
|
// end of this function to handle it further. It means we need to manually
|
|
|
|
// remove this promise from pending promise rejection table.
|
|
|
|
//
|
|
|
|
// For more details see:
|
|
|
|
// https://github.com/denoland/deno/issues/4908
|
|
|
|
// https://v8.dev/features/top-level-await#module-execution-order
|
2020-06-20 07:18:08 -04:00
|
|
|
let maybe_value = module.evaluate(scope);
|
2020-04-27 11:25:26 -04:00
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
// Update status after evaluating.
|
|
|
|
status = module.get_status();
|
2020-04-27 11:25:26 -04:00
|
|
|
|
|
|
|
if let Some(value) = maybe_value {
|
2020-01-07 06:45:44 -05:00
|
|
|
assert!(
|
|
|
|
status == v8::ModuleStatus::Evaluated
|
|
|
|
|| status == v8::ModuleStatus::Errored
|
|
|
|
);
|
2020-04-27 11:25:26 -04:00
|
|
|
let promise = v8::Local::<v8::Promise>::try_from(value)
|
|
|
|
.expect("Expected to get promise as module evaluation result");
|
|
|
|
let promise_id = promise.get_identity_hash();
|
2020-05-29 17:41:39 -04:00
|
|
|
let mut core_state = core_state_rc.borrow_mut();
|
2020-07-18 16:32:11 -04:00
|
|
|
core_state.pending_promise_exceptions.remove(&promise_id);
|
2020-01-07 06:45:44 -05:00
|
|
|
} else {
|
|
|
|
assert!(status == v8::ModuleStatus::Errored);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match status {
|
2020-01-25 08:31:42 -05:00
|
|
|
v8::ModuleStatus::Evaluated => Ok(()),
|
2020-01-07 06:45:44 -05:00
|
|
|
v8::ModuleStatus::Errored => {
|
2020-01-25 08:31:42 -05:00
|
|
|
let exception = module.get_exception();
|
2020-05-29 17:41:39 -04:00
|
|
|
exception_to_err_result(scope, exception)
|
2020-02-27 19:29:02 -05:00
|
|
|
.map_err(|err| attach_handle_to_error(scope, err, exception))
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
other => panic!("Unexpected module status {:?}", other),
|
2020-01-25 08:31:42 -05:00
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-11 04:49:16 -05:00
|
|
|
fn dyn_import_error(
|
2020-01-07 06:45:44 -05:00
|
|
|
&mut self,
|
2020-04-26 13:00:10 -04:00
|
|
|
id: ModuleLoadId,
|
2020-01-25 08:31:42 -05:00
|
|
|
err: ErrBox,
|
2020-01-07 06:45:44 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
|
|
|
let core_state_rc = CoreIsolate::state(self);
|
2020-01-21 14:24:31 -05:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let scope = &mut v8::HandleScope::with_context(
|
|
|
|
&mut *self.0,
|
|
|
|
core_state_rc.borrow().global_context.as_ref().unwrap(),
|
|
|
|
);
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let resolver_handle = state_rc
|
|
|
|
.borrow_mut()
|
2020-01-11 04:49:16 -05:00
|
|
|
.dyn_import_map
|
|
|
|
.remove(&id)
|
|
|
|
.expect("Invalid dyn import id");
|
2020-07-18 16:32:11 -04:00
|
|
|
let resolver = resolver_handle.get(scope);
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-03-02 15:30:46 -05:00
|
|
|
let exception = err
|
|
|
|
.downcast_ref::<ErrWithV8Handle>()
|
2020-07-18 16:32:11 -04:00
|
|
|
.map(|err| err.get_handle(scope))
|
2020-03-02 15:30:46 -05:00
|
|
|
.unwrap_or_else(|| {
|
2020-01-25 08:31:42 -05:00
|
|
|
let message = err.to_string();
|
|
|
|
let message = v8::String::new(scope, &message).unwrap();
|
|
|
|
v8::Exception::type_error(scope, message)
|
2020-03-02 15:30:46 -05:00
|
|
|
});
|
2020-01-25 08:31:42 -05:00
|
|
|
|
2020-06-20 07:18:08 -04:00
|
|
|
resolver.reject(scope, exception).unwrap();
|
2020-08-29 07:21:22 -04:00
|
|
|
scope.perform_microtask_checkpoint();
|
2020-01-25 08:31:42 -05:00
|
|
|
Ok(())
|
2020-01-11 04:49:16 -05:00
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-01-11 04:49:16 -05:00
|
|
|
fn dyn_import_done(
|
|
|
|
&mut self,
|
2020-04-26 13:00:10 -04:00
|
|
|
id: ModuleLoadId,
|
2020-01-11 04:49:16 -05:00
|
|
|
mod_id: ModuleId,
|
|
|
|
) -> Result<(), ErrBox> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
|
|
|
|
|
|
|
let core_state_rc = CoreIsolate::state(self);
|
|
|
|
|
2020-01-11 04:49:16 -05:00
|
|
|
debug!("dyn_import_done {} {:?}", id, mod_id);
|
|
|
|
assert!(mod_id != 0);
|
2020-07-18 16:32:11 -04:00
|
|
|
let scope = &mut v8::HandleScope::with_context(
|
|
|
|
&mut *self.0,
|
|
|
|
core_state_rc.borrow().global_context.as_ref().unwrap(),
|
|
|
|
);
|
2020-01-21 14:24:31 -05:00
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
let resolver_handle = state_rc
|
|
|
|
.borrow_mut()
|
|
|
|
.dyn_import_map
|
|
|
|
.remove(&id)
|
|
|
|
.expect("Invalid dyn import id");
|
|
|
|
let resolver = resolver_handle.get(scope);
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-06-03 12:19:21 -04:00
|
|
|
let module = {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = state_rc.borrow();
|
2020-07-18 16:32:11 -04:00
|
|
|
state
|
2020-05-29 17:41:39 -04:00
|
|
|
.modules
|
|
|
|
.get_info(mod_id)
|
2020-07-18 16:32:11 -04:00
|
|
|
.map(|info| v8::Local::new(scope, &info.handle))
|
|
|
|
.expect("Dyn import module info not found")
|
2020-05-29 17:41:39 -04:00
|
|
|
};
|
2020-07-18 16:32:11 -04:00
|
|
|
// Resolution success
|
2020-01-11 04:49:16 -05:00
|
|
|
assert_eq!(module.get_status(), v8::ModuleStatus::Evaluated);
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-01-11 04:49:16 -05:00
|
|
|
let module_namespace = module.get_module_namespace();
|
2020-06-20 07:18:08 -04:00
|
|
|
resolver.resolve(scope, module_namespace).unwrap();
|
2020-08-29 07:21:22 -04:00
|
|
|
scope.perform_microtask_checkpoint();
|
2020-01-25 08:31:42 -05:00
|
|
|
Ok(())
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-04-30 08:37:06 -04:00
|
|
|
fn prepare_dyn_imports(
|
|
|
|
&mut self,
|
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Result<(), ErrBox>> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
|
|
|
|
2020-04-30 08:37:06 -04:00
|
|
|
loop {
|
2020-05-29 17:41:39 -04:00
|
|
|
let r = {
|
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
state.preparing_dyn_imports.poll_next_unpin(cx)
|
|
|
|
};
|
|
|
|
match r {
|
2020-04-30 08:37:06 -04:00
|
|
|
Poll::Pending | Poll::Ready(None) => {
|
|
|
|
// There are no active dynamic import loaders, or none are ready.
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
Poll::Ready(Some(prepare_poll)) => {
|
|
|
|
let dyn_import_id = prepare_poll.0;
|
|
|
|
let prepare_result = prepare_poll.1;
|
|
|
|
|
|
|
|
match prepare_result {
|
|
|
|
Ok(load) => {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
state.pending_dyn_imports.push(load.into_future());
|
2020-04-30 08:37:06 -04:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
self.dyn_import_error(dyn_import_id, err)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
fn poll_dyn_imports(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
2020-01-07 06:45:44 -05:00
|
|
|
loop {
|
2020-05-29 17:41:39 -04:00
|
|
|
let poll_result = {
|
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
state.pending_dyn_imports.poll_next_unpin(cx)
|
|
|
|
};
|
|
|
|
|
|
|
|
match poll_result {
|
2020-01-07 06:45:44 -05:00
|
|
|
Poll::Pending | Poll::Ready(None) => {
|
|
|
|
// There are no active dynamic import loaders, or none are ready.
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
Poll::Ready(Some(load_stream_poll)) => {
|
|
|
|
let maybe_result = load_stream_poll.0;
|
|
|
|
let mut load = load_stream_poll.1;
|
2020-04-26 13:00:10 -04:00
|
|
|
let dyn_import_id = load.id;
|
2020-01-08 09:06:04 -05:00
|
|
|
|
|
|
|
if let Some(load_stream_result) = maybe_result {
|
|
|
|
match load_stream_result {
|
|
|
|
Ok(info) => {
|
|
|
|
// A module (not necessarily the one dynamically imported) has been
|
|
|
|
// fetched. Create and register it, and if successful, poll for the
|
|
|
|
// next recursive-load event related to this dynamic import.
|
|
|
|
match self.register_during_load(info, &mut load) {
|
|
|
|
Ok(()) => {
|
|
|
|
// Keep importing until it's fully drained
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = state_rc.borrow_mut();
|
|
|
|
state.pending_dyn_imports.push(load.into_future());
|
2020-01-08 09:06:04 -05:00
|
|
|
}
|
2020-01-25 08:31:42 -05:00
|
|
|
Err(err) => self.dyn_import_error(dyn_import_id, err)?,
|
2020-01-08 09:06:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// A non-javascript error occurred; this could be due to a an invalid
|
|
|
|
// module specifier, or a problem with the source map, or a failure
|
|
|
|
// to fetch the module source code.
|
2020-01-25 08:31:42 -05:00
|
|
|
self.dyn_import_error(dyn_import_id, err)?
|
2020-01-08 09:06:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The top-level module from a dynamic import has been instantiated.
|
|
|
|
// Load is done.
|
|
|
|
let module_id = load.root_module_id.unwrap();
|
|
|
|
self.mod_instantiate(module_id)?;
|
2020-02-27 19:29:02 -05:00
|
|
|
match self.mod_evaluate(module_id) {
|
2020-01-25 08:31:42 -05:00
|
|
|
Ok(()) => self.dyn_import_done(dyn_import_id, module_id)?,
|
|
|
|
Err(err) => self.dyn_import_error(dyn_import_id, err)?,
|
|
|
|
};
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
|
|
|
|
fn register_during_load(
|
|
|
|
&mut self,
|
2020-03-02 13:12:49 -05:00
|
|
|
info: ModuleSource,
|
2020-01-08 09:06:04 -05:00
|
|
|
load: &mut RecursiveModuleLoad,
|
|
|
|
) -> Result<(), ErrBox> {
|
2020-03-02 13:12:49 -05:00
|
|
|
let ModuleSource {
|
2020-01-08 09:06:04 -05:00
|
|
|
code,
|
|
|
|
module_url_specified,
|
|
|
|
module_url_found,
|
|
|
|
} = info;
|
|
|
|
|
|
|
|
let is_main =
|
|
|
|
load.state == LoadState::LoadingRoot && !load.is_dynamic_import();
|
|
|
|
let referrer_specifier =
|
2020-04-03 22:41:16 -04:00
|
|
|
ModuleSpecifier::resolve_url(&module_url_found).unwrap();
|
2020-01-08 09:06:04 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = Self::state(self);
|
2020-01-08 09:06:04 -05:00
|
|
|
// #A There are 3 cases to handle at this moment:
|
|
|
|
// 1. Source code resolved result have the same module name as requested
|
|
|
|
// and is not yet registered
|
|
|
|
// -> register
|
|
|
|
// 2. Source code resolved result have a different name as requested:
|
|
|
|
// 2a. The module with resolved module name has been registered
|
|
|
|
// -> alias
|
2020-05-14 00:38:42 -04:00
|
|
|
// 2b. The module with resolved module name has not yet been registered
|
2020-01-08 09:06:04 -05:00
|
|
|
// -> register & alias
|
|
|
|
|
|
|
|
// If necessary, register an alias.
|
|
|
|
if module_url_specified != module_url_found {
|
2020-05-29 17:41:39 -04:00
|
|
|
let mut state = state_rc.borrow_mut();
|
|
|
|
state
|
|
|
|
.modules
|
|
|
|
.alias(&module_url_specified, &module_url_found);
|
2020-01-08 09:06:04 -05:00
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let maybe_mod_id = {
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
state.modules.get_id(&module_url_found)
|
|
|
|
};
|
|
|
|
|
|
|
|
let module_id = match maybe_mod_id {
|
2020-01-08 09:06:04 -05:00
|
|
|
Some(id) => {
|
|
|
|
// Module has already been registered.
|
|
|
|
debug!(
|
|
|
|
"Already-registered module fetched again: {}",
|
|
|
|
module_url_found
|
|
|
|
);
|
|
|
|
id
|
|
|
|
}
|
|
|
|
// Module not registered yet, do it now.
|
|
|
|
None => self.mod_new(is_main, &module_url_found, &code)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Now we must iterate over all imports of the module and load them.
|
2020-05-29 17:41:39 -04:00
|
|
|
let imports = {
|
|
|
|
let state_rc = Self::state(self);
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
state.modules.get_children(module_id).unwrap().clone()
|
|
|
|
};
|
2020-01-25 12:53:16 -05:00
|
|
|
|
|
|
|
for module_specifier in imports {
|
2020-05-29 17:41:39 -04:00
|
|
|
let is_registered = {
|
|
|
|
let state_rc = Self::state(self);
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
state.modules.is_registered(&module_specifier)
|
|
|
|
};
|
|
|
|
if !is_registered {
|
2020-01-25 12:53:16 -05:00
|
|
|
load
|
|
|
|
.add_import(module_specifier.to_owned(), referrer_specifier.clone());
|
2020-01-08 09:06:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we just finished loading the root module, store the root module id.
|
|
|
|
if load.state == LoadState::LoadingRoot {
|
|
|
|
load.root_module_id = Some(module_id);
|
|
|
|
load.state = LoadState::LoadingImports;
|
|
|
|
}
|
|
|
|
|
|
|
|
if load.pending.is_empty() {
|
|
|
|
load.state = LoadState::Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-11 04:49:16 -05:00
|
|
|
/// Asynchronously load specified module and all of it's dependencies
|
|
|
|
///
|
|
|
|
/// User must call `Isolate::mod_evaluate` with returned `ModuleId`
|
|
|
|
/// manually after load is finished.
|
2020-01-08 09:06:04 -05:00
|
|
|
pub async fn load_module(
|
|
|
|
&mut self,
|
2020-02-18 10:08:18 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2020-01-08 09:06:04 -05:00
|
|
|
code: Option<String>,
|
|
|
|
) -> Result<ModuleId, ErrBox> {
|
2020-06-05 18:36:54 -04:00
|
|
|
self.shared_init();
|
2020-05-29 17:41:39 -04:00
|
|
|
let loader = {
|
|
|
|
let state_rc = Self::state(self);
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
state.loader.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
let load = RecursiveModuleLoad::main(&specifier.to_string(), code, loader);
|
2020-04-30 08:37:06 -04:00
|
|
|
let (_load_id, prepare_result) = load.prepare().await;
|
|
|
|
|
|
|
|
let mut load = prepare_result?;
|
2020-01-08 09:06:04 -05:00
|
|
|
|
|
|
|
while let Some(info_result) = load.next().await {
|
|
|
|
let info = info_result?;
|
|
|
|
self.register_during_load(info, &mut load)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let root_id = load.root_module_id.expect("Root module id empty");
|
|
|
|
self.mod_instantiate(root_id).map(|_| root_id)
|
|
|
|
}
|
2020-05-29 17:41:39 -04:00
|
|
|
|
2020-06-05 18:36:54 -04:00
|
|
|
pub fn snapshot(&mut self) -> v8::StartupData {
|
|
|
|
let state_rc = Self::state(self);
|
|
|
|
std::mem::take(&mut state_rc.borrow_mut().modules);
|
|
|
|
CoreIsolate::snapshot(self)
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
pub fn state(isolate: &v8::Isolate) -> Rc<RefCell<EsIsolateState>> {
|
|
|
|
let s = isolate.get_slot::<Rc<RefCell<EsIsolateState>>>().unwrap();
|
|
|
|
s.clone()
|
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for EsIsolate {
|
|
|
|
type Output = Result<(), ErrBox>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
2020-05-29 17:41:39 -04:00
|
|
|
let es_isolate = self.get_mut();
|
|
|
|
|
|
|
|
let state_rc = Self::state(es_isolate);
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
{
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
state.waker.register(cx.waker());
|
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let has_preparing = {
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
!state.preparing_dyn_imports.is_empty()
|
|
|
|
};
|
|
|
|
if has_preparing {
|
|
|
|
let poll_imports = es_isolate.prepare_dyn_imports(cx)?;
|
2020-04-30 08:37:06 -04:00
|
|
|
assert!(poll_imports.is_ready());
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let has_pending = {
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
!state.pending_dyn_imports.is_empty()
|
|
|
|
};
|
|
|
|
if has_pending {
|
|
|
|
let poll_imports = es_isolate.poll_dyn_imports(cx)?;
|
2020-01-07 06:45:44 -05:00
|
|
|
assert!(poll_imports.is_ready());
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
match ready!(es_isolate.0.poll_unpin(cx)) {
|
2020-01-07 06:45:44 -05:00
|
|
|
Ok(()) => {
|
2020-05-29 17:41:39 -04:00
|
|
|
let state = state_rc.borrow();
|
|
|
|
if state.pending_dyn_imports.is_empty()
|
|
|
|
&& state.preparing_dyn_imports.is_empty()
|
2020-04-30 08:37:06 -04:00
|
|
|
{
|
2020-01-07 06:45:44 -05:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Poll::Ready(Err(e)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
impl EsIsolateState {
|
|
|
|
// Called by V8 during `Isolate::mod_instantiate`.
|
|
|
|
pub fn module_resolve_cb(
|
|
|
|
&mut self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer_id: ModuleId,
|
|
|
|
) -> ModuleId {
|
|
|
|
let referrer = self.modules.get_name(referrer_id).unwrap();
|
|
|
|
let specifier = self
|
|
|
|
.loader
|
|
|
|
.resolve(specifier, referrer, false)
|
|
|
|
.expect("Module should have been already resolved");
|
|
|
|
self.modules.get_id(specifier.as_str()).unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by V8 during `Isolate::mod_instantiate`.
|
|
|
|
pub fn dyn_import_cb(
|
|
|
|
&mut self,
|
|
|
|
resolver_handle: v8::Global<v8::PromiseResolver>,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
) {
|
|
|
|
debug!("dyn_import specifier {} referrer {} ", specifier, referrer);
|
|
|
|
|
|
|
|
let load = RecursiveModuleLoad::dynamic_import(
|
|
|
|
specifier,
|
|
|
|
referrer,
|
|
|
|
self.loader.clone(),
|
|
|
|
);
|
|
|
|
self.dyn_import_map.insert(load.id, resolver_handle);
|
|
|
|
self.waker.wake();
|
|
|
|
let fut = load.prepare().boxed_local();
|
|
|
|
self.preparing_dyn_imports.push(fut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
|
|
|
use super::*;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::core_isolate::tests::run_in_task;
|
2020-05-29 17:41:39 -04:00
|
|
|
use crate::core_isolate::CoreIsolateState;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::js_check;
|
2020-03-02 13:12:49 -05:00
|
|
|
use crate::modules::ModuleSourceFuture;
|
2020-01-07 06:45:44 -05:00
|
|
|
use crate::ops::*;
|
2020-05-28 13:36:43 -04:00
|
|
|
use crate::ZeroCopyBuf;
|
2020-01-07 06:45:44 -05:00
|
|
|
use std::io;
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2020-02-14 19:18:36 -05:00
|
|
|
use std::sync::Arc;
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mods() {
|
2020-06-05 18:36:54 -04:00
|
|
|
#[derive(Default)]
|
2020-01-08 09:06:04 -05:00
|
|
|
struct ModsLoader {
|
|
|
|
pub count: Arc<AtomicUsize>,
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:12:49 -05:00
|
|
|
impl ModuleLoader for ModsLoader {
|
2020-01-08 09:06:04 -05:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
_is_main: bool,
|
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
|
|
|
self.count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
assert_eq!(specifier, "./b.js");
|
|
|
|
assert_eq!(referrer, "file:///a.js");
|
|
|
|
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap();
|
|
|
|
Ok(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
_module_specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-25 12:53:16 -05:00
|
|
|
_is_dyn_import: bool,
|
2020-03-02 13:12:49 -05:00
|
|
|
) -> Pin<Box<ModuleSourceFuture>> {
|
2020-01-08 09:06:04 -05:00
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:18:36 -05:00
|
|
|
let loader = Rc::new(ModsLoader::default());
|
2020-01-08 09:06:04 -05:00
|
|
|
let resolve_count = loader.count.clone();
|
2020-01-07 06:45:44 -05:00
|
|
|
let dispatch_count = Arc::new(AtomicUsize::new(0));
|
|
|
|
let dispatch_count_ = dispatch_count.clone();
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let dispatcher = move |_state: &mut CoreIsolateState,
|
2020-07-08 11:23:50 -04:00
|
|
|
zero_copy: &mut [ZeroCopyBuf]|
|
2020-04-19 23:54:46 -04:00
|
|
|
-> Op {
|
|
|
|
dispatch_count_.fetch_add(1, Ordering::Relaxed);
|
2020-07-08 11:23:50 -04:00
|
|
|
assert_eq!(zero_copy.len(), 1);
|
|
|
|
assert_eq!(zero_copy[0].len(), 1);
|
|
|
|
assert_eq!(zero_copy[0][0], 42);
|
2020-04-19 23:54:46 -04:00
|
|
|
let buf = vec![43u8, 0, 0, 0].into_boxed_slice();
|
|
|
|
Op::Async(futures::future::ready(buf).boxed())
|
|
|
|
};
|
2020-01-07 06:45:44 -05:00
|
|
|
|
|
|
|
isolate.register_op("test", dispatcher);
|
|
|
|
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"setup.js",
|
|
|
|
r#"
|
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
));
|
2020-01-08 09:06:04 -05:00
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
let specifier_a = "file:///a.js".to_string();
|
2020-01-07 06:45:44 -05:00
|
|
|
let mod_a = isolate
|
|
|
|
.mod_new(
|
|
|
|
true,
|
2020-01-08 09:06:04 -05:00
|
|
|
&specifier_a,
|
2020-01-07 06:45:44 -05:00
|
|
|
r#"
|
2020-01-08 09:06:04 -05:00
|
|
|
import { b } from './b.js'
|
2020-01-07 06:45:44 -05:00
|
|
|
if (b() != 'b') throw Error();
|
|
|
|
let control = new Uint8Array([42]);
|
|
|
|
Deno.core.send(1, control);
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let state_rc = EsIsolate::state(&isolate);
|
|
|
|
{
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
let imports = state.modules.get_children(mod_a);
|
|
|
|
assert_eq!(
|
|
|
|
imports,
|
|
|
|
Some(&vec![ModuleSpecifier::resolve_url("file:///b.js").unwrap()])
|
|
|
|
);
|
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
let mod_b = isolate
|
2020-01-08 09:06:04 -05:00
|
|
|
.mod_new(false, "file:///b.js", "export function b() { return 'b' }")
|
2020-01-07 06:45:44 -05:00
|
|
|
.unwrap();
|
2020-05-29 17:41:39 -04:00
|
|
|
{
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
let imports = state.modules.get_children(mod_b).unwrap();
|
|
|
|
assert_eq!(imports.len(), 0);
|
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
js_check(isolate.mod_instantiate(mod_b));
|
2020-01-07 06:45:44 -05:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);
|
2020-01-25 12:53:16 -05:00
|
|
|
assert_eq!(resolve_count.load(Ordering::SeqCst), 1);
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
js_check(isolate.mod_instantiate(mod_a));
|
2020-01-07 06:45:44 -05:00
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 0);
|
|
|
|
|
|
|
|
js_check(isolate.mod_evaluate(mod_a));
|
|
|
|
assert_eq!(dispatch_count.load(Ordering::Relaxed), 1);
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
#[test]
|
|
|
|
fn dyn_import_err() {
|
|
|
|
#[derive(Clone, Default)]
|
|
|
|
struct DynImportErrLoader {
|
|
|
|
pub count: Arc<AtomicUsize>,
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 13:12:49 -05:00
|
|
|
impl ModuleLoader for DynImportErrLoader {
|
2020-01-08 09:06:04 -05:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
_is_main: bool,
|
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
|
|
|
self.count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
assert_eq!(specifier, "/foo.js");
|
|
|
|
assert_eq!(referrer, "file:///dyn_import2.js");
|
|
|
|
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap();
|
|
|
|
Ok(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
_module_specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-25 12:53:16 -05:00
|
|
|
_is_dyn_import: bool,
|
2020-03-02 13:12:49 -05:00
|
|
|
) -> Pin<Box<ModuleSourceFuture>> {
|
2020-08-25 18:22:15 -04:00
|
|
|
async { Err(io::Error::from(io::ErrorKind::NotFound).into()) }.boxed()
|
2020-01-08 09:06:04 -05:00
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test an erroneous dynamic import where the specified module isn't found.
|
|
|
|
run_in_task(|cx| {
|
2020-02-14 19:18:36 -05:00
|
|
|
let loader = Rc::new(DynImportErrLoader::default());
|
2020-01-08 09:06:04 -05:00
|
|
|
let count = loader.count.clone();
|
|
|
|
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
js_check(isolate.execute(
|
2020-01-08 09:06:04 -05:00
|
|
|
"file:///dyn_import2.js",
|
2020-01-07 06:45:44 -05:00
|
|
|
r#"
|
|
|
|
(async () => {
|
2020-01-08 09:06:04 -05:00
|
|
|
await import("/foo.js");
|
2020-01-07 06:45:44 -05:00
|
|
|
})();
|
|
|
|
"#,
|
|
|
|
));
|
|
|
|
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(count.load(Ordering::Relaxed), 0);
|
2020-01-07 06:45:44 -05:00
|
|
|
// We should get an error here.
|
|
|
|
let result = isolate.poll_unpin(cx);
|
|
|
|
if let Poll::Ready(Ok(_)) = result {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2020-04-30 08:37:06 -04:00
|
|
|
assert_eq!(count.load(Ordering::Relaxed), 2);
|
2020-01-07 06:45:44 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:18:29 -04:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
struct DynImportOkLoader {
|
|
|
|
pub prepare_load_count: Arc<AtomicUsize>,
|
|
|
|
pub resolve_count: Arc<AtomicUsize>,
|
|
|
|
pub load_count: Arc<AtomicUsize>,
|
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-06-03 12:18:29 -04:00
|
|
|
impl ModuleLoader for DynImportOkLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
_is_main: bool,
|
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
|
|
|
let c = self.resolve_count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
assert!(c < 4);
|
|
|
|
assert_eq!(specifier, "./b.js");
|
|
|
|
assert_eq!(referrer, "file:///dyn_import3.js");
|
|
|
|
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap();
|
|
|
|
Ok(s)
|
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
|
2020-06-03 12:18:29 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
|
|
|
_is_dyn_import: bool,
|
|
|
|
) -> Pin<Box<ModuleSourceFuture>> {
|
|
|
|
self.load_count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
let info = ModuleSource {
|
|
|
|
module_url_specified: specifier.to_string(),
|
|
|
|
module_url_found: specifier.to_string(),
|
|
|
|
code: "export function b() { return 'b' }".to_owned(),
|
|
|
|
};
|
|
|
|
async move { Ok(info) }.boxed()
|
|
|
|
}
|
2020-04-30 08:37:06 -04:00
|
|
|
|
2020-06-03 12:18:29 -04:00
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
|
|
|
_load_id: ModuleLoadId,
|
|
|
|
_module_specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<String>,
|
|
|
|
_is_dyn_import: bool,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), ErrBox>>>> {
|
|
|
|
self.prepare_load_count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
async { Ok(()) }.boxed_local()
|
2020-01-08 09:06:04 -05:00
|
|
|
}
|
2020-06-03 12:18:29 -04:00
|
|
|
}
|
2020-01-08 09:06:04 -05:00
|
|
|
|
2020-06-03 12:18:29 -04:00
|
|
|
#[test]
|
|
|
|
fn dyn_import_ok() {
|
2020-01-08 09:06:04 -05:00
|
|
|
run_in_task(|cx| {
|
2020-02-14 19:18:36 -05:00
|
|
|
let loader = Rc::new(DynImportOkLoader::default());
|
2020-04-30 08:37:06 -04:00
|
|
|
let prepare_load_count = loader.prepare_load_count.clone();
|
2020-01-08 09:06:04 -05:00
|
|
|
let resolve_count = loader.resolve_count.clone();
|
|
|
|
let load_count = loader.load_count.clone();
|
|
|
|
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
|
|
|
|
2020-01-07 06:45:44 -05:00
|
|
|
// Dynamically import mod_b
|
|
|
|
js_check(isolate.execute(
|
2020-01-08 09:06:04 -05:00
|
|
|
"file:///dyn_import3.js",
|
2020-01-07 06:45:44 -05:00
|
|
|
r#"
|
|
|
|
(async () => {
|
2020-01-08 09:06:04 -05:00
|
|
|
let mod = await import("./b.js");
|
2020-01-07 06:45:44 -05:00
|
|
|
if (mod.b() !== 'b') {
|
|
|
|
throw Error("bad1");
|
|
|
|
}
|
|
|
|
// And again!
|
2020-01-08 09:06:04 -05:00
|
|
|
mod = await import("./b.js");
|
2020-01-07 06:45:44 -05:00
|
|
|
if (mod.b() !== 'b') {
|
|
|
|
throw Error("bad2");
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
"#,
|
|
|
|
));
|
|
|
|
|
2020-04-30 08:37:06 -04:00
|
|
|
// First poll runs `prepare_load` hook.
|
2020-08-13 20:15:12 -04:00
|
|
|
assert!(matches!(isolate.poll_unpin(cx), Poll::Pending));
|
2020-04-30 08:37:06 -04:00
|
|
|
assert_eq!(prepare_load_count.load(Ordering::Relaxed), 1);
|
|
|
|
|
|
|
|
// Second poll actually loads modules into the isolate.
|
2020-08-13 20:15:12 -04:00
|
|
|
assert!(matches!(isolate.poll_unpin(cx), Poll::Ready(Ok(_))));
|
2020-04-30 08:37:06 -04:00
|
|
|
assert_eq!(resolve_count.load(Ordering::Relaxed), 4);
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(load_count.load(Ordering::Relaxed), 2);
|
2020-08-13 20:15:12 -04:00
|
|
|
assert!(matches!(isolate.poll_unpin(cx), Poll::Ready(Ok(_))));
|
2020-04-30 08:37:06 -04:00
|
|
|
assert_eq!(resolve_count.load(Ordering::Relaxed), 4);
|
2020-01-08 09:06:04 -05:00
|
|
|
assert_eq!(load_count.load(Ordering::Relaxed), 2);
|
2020-01-07 06:45:44 -05:00
|
|
|
})
|
|
|
|
}
|
2020-06-03 12:18:29 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dyn_import_borrow_mut_error() {
|
|
|
|
// https://github.com/denoland/deno/issues/6054
|
|
|
|
run_in_task(|cx| {
|
|
|
|
let loader = Rc::new(DynImportOkLoader::default());
|
|
|
|
let prepare_load_count = loader.prepare_load_count.clone();
|
|
|
|
let mut isolate = EsIsolate::new(loader, StartupData::None, false);
|
|
|
|
js_check(isolate.execute(
|
|
|
|
"file:///dyn_import3.js",
|
|
|
|
r#"
|
|
|
|
(async () => {
|
|
|
|
let mod = await import("./b.js");
|
|
|
|
if (mod.b() !== 'b') {
|
|
|
|
throw Error("bad");
|
|
|
|
}
|
|
|
|
// Now do any op
|
|
|
|
Deno.core.ops();
|
|
|
|
})();
|
|
|
|
"#,
|
|
|
|
));
|
|
|
|
// First poll runs `prepare_load` hook.
|
|
|
|
let _ = isolate.poll_unpin(cx);
|
|
|
|
assert_eq!(prepare_load_count.load(Ordering::Relaxed), 1);
|
|
|
|
// Second poll triggers error
|
|
|
|
let _ = isolate.poll_unpin(cx);
|
|
|
|
})
|
|
|
|
}
|
2020-06-05 18:36:54 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn es_snapshot() {
|
|
|
|
#[derive(Default)]
|
|
|
|
struct ModsLoader;
|
|
|
|
|
|
|
|
impl ModuleLoader for ModsLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
_is_main: bool,
|
|
|
|
) -> Result<ModuleSpecifier, ErrBox> {
|
|
|
|
assert_eq!(specifier, "file:///main.js");
|
|
|
|
assert_eq!(referrer, ".");
|
|
|
|
let s = ModuleSpecifier::resolve_import(specifier, referrer).unwrap();
|
|
|
|
Ok(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
_module_specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
|
|
|
_is_dyn_import: bool,
|
|
|
|
) -> Pin<Box<ModuleSourceFuture>> {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let loader = std::rc::Rc::new(ModsLoader::default());
|
|
|
|
let mut runtime_isolate = EsIsolate::new(loader, StartupData::None, true);
|
|
|
|
|
|
|
|
let specifier = ModuleSpecifier::resolve_url("file:///main.js").unwrap();
|
|
|
|
let source_code = "Deno.core.print('hello\\n')".to_string();
|
|
|
|
|
|
|
|
let module_id = futures::executor::block_on(
|
|
|
|
runtime_isolate.load_module(&specifier, Some(source_code)),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
js_check(runtime_isolate.mod_evaluate(module_id));
|
|
|
|
|
|
|
|
let _snapshot = runtime_isolate.snapshot();
|
|
|
|
}
|
2020-01-07 06:45:44 -05:00
|
|
|
}
|