2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-04-05 00:04:06 -04:00
|
|
|
use crate::compiler::compile_async;
|
2019-02-18 10:42:15 -05:00
|
|
|
use crate::compiler::ModuleMetaData;
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::errors::DenoError;
|
2019-01-30 17:21:31 -05:00
|
|
|
use crate::errors::RustOrJsError;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::js_errors;
|
2019-04-04 05:33:32 -04:00
|
|
|
use crate::js_errors::JSErrorColor;
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::msg;
|
2019-04-09 13:11:25 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-03-20 11:38:43 -04:00
|
|
|
use crate::tokio_util;
|
2019-03-30 19:30:40 -04:00
|
|
|
use deno;
|
2019-04-23 18:58:00 -04:00
|
|
|
use deno::Config;
|
2019-03-30 19:30:40 -04:00
|
|
|
use deno::JSError;
|
2019-04-16 15:13:42 -04:00
|
|
|
use deno::Loader;
|
2019-04-08 10:12:43 -04:00
|
|
|
use deno::StartupData;
|
2019-04-05 00:04:06 -04:00
|
|
|
use futures::future::Either;
|
2019-03-14 19:17:52 -04:00
|
|
|
use futures::Async;
|
2018-09-18 14:53:16 -04:00
|
|
|
use futures::Future;
|
2019-03-25 17:43:31 -04:00
|
|
|
use std::sync::atomic::Ordering;
|
2019-04-16 15:13:42 -04:00
|
|
|
use url::Url;
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2019-03-30 19:30:40 -04:00
|
|
|
/// Wraps deno::Isolate to provide source maps, ops for the CLI, and
|
2019-03-14 19:17:52 -04:00
|
|
|
/// high-level module loading
|
2019-04-08 17:10:00 -04:00
|
|
|
pub struct Worker {
|
2019-04-23 18:58:00 -04:00
|
|
|
inner: deno::Isolate,
|
2019-04-16 15:13:42 -04:00
|
|
|
pub modules: deno::Modules,
|
|
|
|
pub state: ThreadSafeState,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
impl Worker {
|
|
|
|
pub fn new(
|
|
|
|
_name: String,
|
|
|
|
startup_data: StartupData,
|
2019-04-09 13:11:25 -04:00
|
|
|
state: ThreadSafeState,
|
2019-04-08 17:10:00 -04:00
|
|
|
) -> Worker {
|
2019-04-09 13:11:25 -04:00
|
|
|
let state_ = state.clone();
|
2019-04-23 18:58:00 -04:00
|
|
|
let mut config = Config::default();
|
|
|
|
config.dispatch(move |control_buf, zero_copy_buf| {
|
|
|
|
state_.dispatch(control_buf, zero_copy_buf)
|
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
Self {
|
2019-04-23 18:58:00 -04:00
|
|
|
inner: deno::Isolate::new(startup_data, config),
|
2019-04-16 15:13:42 -04:00
|
|
|
modules: deno::Modules::new(),
|
2018-11-30 03:30:49 -05:00
|
|
|
state,
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:03:58 -05:00
|
|
|
/// Same as execute2() but the filename defaults to "<anonymous>".
|
2019-03-14 19:17:52 -04:00
|
|
|
pub fn execute(&mut self, js_source: &str) -> Result<(), JSError> {
|
2018-12-11 14:03:58 -05:00
|
|
|
self.execute2("<anonymous>", js_source)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes the provided JavaScript source code. The js_filename argument is
|
|
|
|
/// provided only for debugging purposes.
|
|
|
|
pub fn execute2(
|
2019-03-14 19:17:52 -04:00
|
|
|
&mut self,
|
2018-09-17 20:41:13 -04:00
|
|
|
js_filename: &str,
|
|
|
|
js_source: &str,
|
2018-12-06 23:05:36 -05:00
|
|
|
) -> Result<(), JSError> {
|
2019-03-14 19:17:52 -04:00
|
|
|
self.inner.execute(js_filename, js_source)
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
/// Consumes worker. Executes the provided JavaScript module.
|
|
|
|
pub fn execute_mod_async(
|
|
|
|
self,
|
|
|
|
js_url: &Url,
|
|
|
|
is_prefetch: bool,
|
|
|
|
) -> impl Future<Item = Self, Error = (RustOrJsError, Self)> {
|
|
|
|
let recursive_load = deno::RecursiveLoad::new(js_url.as_str(), self);
|
|
|
|
recursive_load.and_then(
|
|
|
|
move |(id, mut self_)| -> Result<Self, (deno::JSErrorOr<DenoError>, Self)> {
|
|
|
|
if is_prefetch {
|
|
|
|
Ok(self_)
|
|
|
|
} else {
|
|
|
|
let result = self_.inner.mod_evaluate(id);
|
|
|
|
if let Err(err) = result {
|
|
|
|
Err((deno::JSErrorOr::JSError(err), self_))
|
|
|
|
} else {
|
|
|
|
Ok(self_)
|
|
|
|
}
|
2019-04-01 21:46:40 -04:00
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.map_err(|(err, self_)| {
|
|
|
|
// Convert to RustOrJsError AND apply_source_map.
|
|
|
|
let err = match err {
|
|
|
|
deno::JSErrorOr::JSError(err) => RustOrJsError::Js(self_.apply_source_map(err)),
|
|
|
|
deno::JSErrorOr::Other(err) => RustOrJsError::Rust(err),
|
|
|
|
};
|
|
|
|
(err, self_)
|
|
|
|
})
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
/// Consumes worker. Executes the provided JavaScript module.
|
2019-01-15 12:19:58 -05:00
|
|
|
pub fn execute_mod(
|
2019-04-16 15:13:42 -04:00
|
|
|
self,
|
|
|
|
js_url: &Url,
|
2019-01-15 12:19:58 -05:00
|
|
|
is_prefetch: bool,
|
2019-04-16 15:13:42 -04:00
|
|
|
) -> Result<Self, (RustOrJsError, Self)> {
|
|
|
|
tokio_util::block_on(self.execute_mod_async(js_url, is_prefetch))
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
/// Applies source map to the error.
|
|
|
|
fn apply_source_map(&self, err: JSError) -> JSError {
|
|
|
|
js_errors::apply_source_map(&err, &self.state.dir)
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 21:46:40 -04:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
|
|
|
|
// TODO(ry) Add tests.
|
|
|
|
// TODO(ry) Move this to core?
|
|
|
|
pub fn resolve_module_spec(
|
|
|
|
specifier: &str,
|
|
|
|
base: &str,
|
|
|
|
) -> Result<String, url::ParseError> {
|
|
|
|
// 1. Apply the URL parser to specifier. If the result is not failure, return
|
|
|
|
// the result.
|
|
|
|
// let specifier = parse_local_or_remote(specifier)?.to_string();
|
|
|
|
if let Ok(specifier_url) = Url::parse(specifier) {
|
|
|
|
return Ok(specifier_url.to_string());
|
|
|
|
}
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
// 2. If specifier does not start with the character U+002F SOLIDUS (/), the
|
|
|
|
// two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./), or the
|
|
|
|
// three-character sequence U+002E FULL STOP, U+002E FULL STOP, U+002F
|
|
|
|
// SOLIDUS (../), return failure.
|
2019-04-17 09:25:51 -04:00
|
|
|
if !specifier.starts_with('/')
|
2019-04-16 15:13:42 -04:00
|
|
|
&& !specifier.starts_with("./")
|
|
|
|
&& !specifier.starts_with("../")
|
|
|
|
{
|
|
|
|
// TODO(ry) This is (probably) not the correct error to return here.
|
|
|
|
return Err(url::ParseError::RelativeUrlWithCannotBeABaseBase);
|
|
|
|
}
|
2019-03-25 17:43:31 -04:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
// 3. Return the result of applying the URL parser to specifier with base URL
|
|
|
|
// as the base URL.
|
|
|
|
let base_url = Url::parse(base)?;
|
|
|
|
let u = base_url.join(&specifier)?;
|
|
|
|
Ok(u.to_string())
|
|
|
|
}
|
2019-03-25 17:43:31 -04:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
/// Takes a string representing a path or URL to a module, but of the type
|
|
|
|
/// passed through the command-line interface for the main module. This is
|
|
|
|
/// slightly different than specifiers used in import statements: "foo.js" for
|
|
|
|
/// example is allowed here, whereas in import statements a leading "./" is
|
|
|
|
/// required ("./foo.js"). This function is aware of the current working
|
|
|
|
/// directory and returns an absolute URL.
|
|
|
|
pub fn root_specifier_to_url(
|
|
|
|
root_specifier: &str,
|
|
|
|
) -> Result<Url, url::ParseError> {
|
|
|
|
let maybe_url = Url::parse(root_specifier);
|
|
|
|
if let Ok(url) = maybe_url {
|
|
|
|
Ok(url)
|
|
|
|
} else {
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
|
|
|
let base = Url::from_directory_path(cwd).unwrap();
|
|
|
|
base.join(root_specifier)
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2018-09-18 14:53:16 -04:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
impl Loader for Worker {
|
|
|
|
type Error = DenoError;
|
2018-09-18 14:53:16 -04:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
fn resolve(specifier: &str, referrer: &str) -> Result<String, Self::Error> {
|
2019-04-17 09:25:51 -04:00
|
|
|
resolve_module_spec(specifier, referrer).map_err(DenoError::from)
|
2019-04-01 21:46:40 -04:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
/// Given an absolute url, load its source code.
|
2019-04-18 21:33:50 -04:00
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
url: &str,
|
|
|
|
) -> Box<deno::SourceCodeInfoFuture<Self::Error>> {
|
2019-04-16 15:13:42 -04:00
|
|
|
self
|
|
|
|
.state
|
|
|
|
.metrics
|
|
|
|
.resolve_count
|
|
|
|
.fetch_add(1, Ordering::SeqCst);
|
|
|
|
Box::new(
|
|
|
|
fetch_module_meta_data_and_maybe_compile_async(&self.state, url, ".")
|
|
|
|
.map_err(|err| {
|
|
|
|
eprintln!("{}", err);
|
|
|
|
err
|
2019-04-18 21:33:50 -04:00
|
|
|
}).map(|module_meta_data| deno::SourceCodeInfo {
|
|
|
|
// Real module name, might be different from initial URL
|
|
|
|
// due to redirections.
|
|
|
|
code: module_meta_data.js_source(),
|
|
|
|
module_name: module_meta_data.module_name,
|
|
|
|
}),
|
2019-04-16 15:13:42 -04:00
|
|
|
)
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
fn isolate_and_modules<'a: 'b + 'c, 'b, 'c>(
|
|
|
|
&'a mut self,
|
2019-04-23 18:58:00 -04:00
|
|
|
) -> (&'b mut deno::Isolate, &'c mut deno::Modules) {
|
2019-04-16 15:13:42 -04:00
|
|
|
(&mut self.inner, &mut self.modules)
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
impl Future for Worker {
|
2019-03-14 19:17:52 -04:00
|
|
|
type Item = ();
|
|
|
|
type Error = JSError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Result<Async<()>, Self::Error> {
|
|
|
|
self.inner.poll().map_err(|err| self.apply_source_map(err))
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 16:05:41 -04:00
|
|
|
|
2019-03-20 11:38:43 -04:00
|
|
|
fn fetch_module_meta_data_and_maybe_compile_async(
|
2019-04-09 13:11:25 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-03-20 11:38:43 -04:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
) -> impl Future<Item = ModuleMetaData, Error = DenoError> {
|
2019-03-28 16:05:41 -04:00
|
|
|
let use_cache = !state.flags.reload;
|
2019-05-03 11:09:51 -04:00
|
|
|
let no_fetch = state.flags.no_fetch;
|
2019-03-20 11:38:43 -04:00
|
|
|
let state_ = state.clone();
|
|
|
|
let specifier = specifier.to_string();
|
|
|
|
let referrer = referrer.to_string();
|
|
|
|
state
|
|
|
|
.dir
|
2019-05-03 11:09:51 -04:00
|
|
|
.fetch_module_meta_data_async(&specifier, &referrer, use_cache, no_fetch)
|
2019-04-05 00:04:06 -04:00
|
|
|
.and_then(move |out| {
|
2019-03-27 16:10:31 -04:00
|
|
|
if out.media_type == msg::MediaType::TypeScript
|
2019-03-28 16:05:41 -04:00
|
|
|
&& !out.has_output_code_and_source_map()
|
2019-03-20 11:38:43 -04:00
|
|
|
{
|
|
|
|
debug!(">>>>> compile_sync START");
|
2019-04-05 00:04:06 -04:00
|
|
|
Either::A(
|
|
|
|
compile_async(state_.clone(), &specifier, &referrer, &out)
|
|
|
|
.map_err(|e| {
|
|
|
|
debug!("compiler error exiting!");
|
|
|
|
eprintln!("{}", JSErrorColor(&e).to_string());
|
|
|
|
std::process::exit(1);
|
|
|
|
}).and_then(move |out| {
|
|
|
|
debug!(">>>>> compile_sync END");
|
|
|
|
state_.dir.code_cache(&out)?;
|
|
|
|
Ok(out)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Either::B(futures::future::ok(out))
|
2019-03-20 11:38:43 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
pub fn fetch_module_meta_data_and_maybe_compile(
|
2019-04-09 13:11:25 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-01-09 12:59:46 -05:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-02-18 10:42:15 -05:00
|
|
|
) -> Result<ModuleMetaData, DenoError> {
|
2019-03-20 11:38:43 -04:00
|
|
|
tokio_util::block_on(fetch_module_meta_data_and_maybe_compile_async(
|
|
|
|
state, specifier, referrer,
|
|
|
|
))
|
2019-01-09 12:59:46 -05:00
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::flags;
|
2019-04-11 10:58:31 -04:00
|
|
|
use crate::ops::op_selector_std;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::resources;
|
|
|
|
use crate::startup_data;
|
2019-04-09 13:11:25 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::tokio_util;
|
|
|
|
use deno::js_check;
|
2019-03-14 19:17:52 -04:00
|
|
|
use futures::future::lazy;
|
|
|
|
use std::sync::atomic::Ordering;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
|
|
|
#[test]
|
2019-04-16 15:13:42 -04:00
|
|
|
fn execute_mod_esm_imports_a() {
|
2019-01-01 06:24:05 -05:00
|
|
|
let filename = std::env::current_dir()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/esm_imports_a.js");
|
2019-04-16 15:13:42 -04:00
|
|
|
let js_url = Url::from_file_path(filename).unwrap();
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
let argv = vec![String::from("./deno"), js_url.to_string()];
|
2019-04-21 11:34:18 -04:00
|
|
|
let state =
|
|
|
|
ThreadSafeState::new(flags::DenoFlags::default(), argv, op_selector_std);
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
|
|
|
tokio_util::run(lazy(move || {
|
2019-04-16 15:13:42 -04:00
|
|
|
let worker = Worker::new("TEST".to_string(), StartupData::None, state);
|
|
|
|
let result = worker.execute_mod(&js_url, false);
|
|
|
|
let worker = match result {
|
|
|
|
Err((err, worker)) => {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
Ok(worker) => worker,
|
|
|
|
};
|
2019-04-08 17:10:00 -04:00
|
|
|
tokio_util::panic_on_error(worker)
|
2019-03-14 19:17:52 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-04-16 15:13:42 -04:00
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_circular() {
|
|
|
|
let filename = std::env::current_dir().unwrap().join("tests/circular1.js");
|
2019-04-16 15:13:42 -04:00
|
|
|
let js_url = Url::from_file_path(filename).unwrap();
|
2019-01-30 17:21:31 -05:00
|
|
|
|
2019-04-16 15:13:42 -04:00
|
|
|
let argv = vec![String::from("./deno"), js_url.to_string()];
|
2019-04-21 11:34:18 -04:00
|
|
|
let state =
|
|
|
|
ThreadSafeState::new(flags::DenoFlags::default(), argv, op_selector_std);
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
|
|
|
tokio_util::run(lazy(move || {
|
2019-04-16 15:13:42 -04:00
|
|
|
let worker = Worker::new("TEST".to_string(), StartupData::None, state);
|
|
|
|
let result = worker.execute_mod(&js_url, false);
|
|
|
|
let worker = match result {
|
|
|
|
Err((err, worker)) => {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
Ok(worker) => worker,
|
|
|
|
};
|
2019-04-08 17:10:00 -04:00
|
|
|
tokio_util::panic_on_error(worker)
|
2019-03-14 19:17:52 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-01-30 17:21:31 -05:00
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
|
2019-01-01 06:24:05 -05:00
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
|
|
|
|
fn create_test_worker() -> Worker {
|
2019-04-09 13:11:25 -04:00
|
|
|
let state = ThreadSafeState::mock();
|
2019-04-08 17:10:00 -04:00
|
|
|
let mut worker =
|
2019-04-09 13:11:25 -04:00
|
|
|
Worker::new("TEST".to_string(), startup_data::deno_isolate_init(), state);
|
2019-04-08 17:10:00 -04:00
|
|
|
js_check(worker.execute("denoMain()"));
|
|
|
|
js_check(worker.execute("workerMain()"));
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_worker_messages() {
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let source = r#"
|
|
|
|
onmessage = function(e) {
|
|
|
|
console.log("msg from main script", e.data);
|
|
|
|
if (e.data == "exit") {
|
2019-04-14 16:07:24 -04:00
|
|
|
delete window.onmessage;
|
2019-04-08 17:10:00 -04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
console.assert(e.data === "hi");
|
|
|
|
}
|
|
|
|
postMessage([1, 2, 3]);
|
|
|
|
console.log("after postMessage");
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
js_check(worker.execute(source));
|
|
|
|
|
|
|
|
let resource = worker.state.resource.clone();
|
|
|
|
let resource_ = resource.clone();
|
|
|
|
|
|
|
|
tokio::spawn(lazy(move || {
|
|
|
|
worker.then(move |r| -> Result<(), ()> {
|
|
|
|
resource_.close();
|
|
|
|
js_check(r);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
|
|
|
|
let r = resources::post_message_to_worker(resource.rid, msg).wait();
|
|
|
|
assert!(r.is_ok());
|
|
|
|
|
|
|
|
let maybe_msg = resources::get_message_from_worker(resource.rid)
|
|
|
|
.wait()
|
|
|
|
.unwrap();
|
|
|
|
assert!(maybe_msg.is_some());
|
|
|
|
// Check if message received is [1, 2, 3] in json
|
|
|
|
assert_eq!(*maybe_msg.unwrap(), *b"[1,2,3]");
|
|
|
|
|
|
|
|
let msg = json!("exit")
|
|
|
|
.to_string()
|
|
|
|
.into_boxed_str()
|
|
|
|
.into_boxed_bytes();
|
|
|
|
let r = resources::post_message_to_worker(resource.rid, msg).wait();
|
|
|
|
assert!(r.is_ok());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn removed_from_resource_table_on_close() {
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
js_check(
|
2019-04-16 15:13:42 -04:00
|
|
|
worker.execute("onmessage = () => { delete window.onmessage; }"),
|
2019-04-08 17:10:00 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
let resource = worker.state.resource.clone();
|
|
|
|
let rid = resource.rid;
|
|
|
|
|
2019-04-12 18:39:31 -04:00
|
|
|
let worker_future = worker
|
|
|
|
.then(move |r| -> Result<(), ()> {
|
2019-04-08 17:10:00 -04:00
|
|
|
resource.close();
|
|
|
|
println!("workers.rs after resource close");
|
|
|
|
js_check(r);
|
|
|
|
Ok(())
|
2019-04-12 18:39:31 -04:00
|
|
|
}).shared();
|
|
|
|
|
|
|
|
let worker_future_ = worker_future.clone();
|
|
|
|
tokio::spawn(lazy(move || worker_future_.then(|_| Ok(()))));
|
2019-04-08 17:10:00 -04:00
|
|
|
|
|
|
|
assert_eq!(resources::get_type(rid), Some("worker".to_string()));
|
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
let r = resources::post_message_to_worker(rid, msg).wait();
|
|
|
|
assert!(r.is_ok());
|
|
|
|
debug!("rid {:?}", rid);
|
|
|
|
|
2019-04-12 18:39:31 -04:00
|
|
|
worker_future.wait().unwrap();
|
2019-04-08 17:10:00 -04:00
|
|
|
assert_eq!(resources::get_type(rid), None);
|
|
|
|
})
|
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_resolve_error() {
|
|
|
|
// "foo" is not a vailid module specifier so this should return an error.
|
|
|
|
let worker = create_test_worker();
|
|
|
|
let js_url = root_specifier_to_url("does-not-exist").unwrap();
|
|
|
|
let result = worker.execute_mod_async(&js_url, false).wait();
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_002_hello() {
|
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
|
|
|
let worker = create_test_worker();
|
|
|
|
let js_url = root_specifier_to_url("./tests/002_hello.ts").unwrap();
|
|
|
|
let result = worker.execute_mod_async(&js_url, false).wait();
|
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|