2023-01-13 02:51:32 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
use std::path::PathBuf;
|
2022-08-20 11:31:33 -04:00
|
|
|
use std::rc::Rc;
|
2022-08-11 16:59:12 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::task::LocalFutureObj;
|
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
use deno_core::located_script_name;
|
2022-11-21 08:36:26 -05:00
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_v8;
|
|
|
|
use deno_core::v8;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::ModuleId;
|
2023-02-21 12:03:48 -05:00
|
|
|
use deno_graph::npm::NpmPackageReqReference;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::colors;
|
2023-02-10 10:26:39 -05:00
|
|
|
use deno_runtime::deno_node;
|
2022-09-02 16:53:23 -04:00
|
|
|
use deno_runtime::fmt_errors::format_js_error;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
|
2022-08-16 21:00:35 -04:00
|
|
|
use deno_runtime::ops::worker_host::WorkerEventCb;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::web_worker::WebWorker;
|
|
|
|
use deno_runtime::web_worker::WebWorkerOptions;
|
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
|
|
|
use deno_runtime::BootstrapOptions;
|
|
|
|
|
2022-08-23 22:01:21 -04:00
|
|
|
use crate::args::DenoSubcommand;
|
2022-08-11 16:59:12 -04:00
|
|
|
use crate::errors;
|
|
|
|
use crate::module_loader::CliModuleLoader;
|
2022-08-20 11:31:33 -04:00
|
|
|
use crate::node;
|
2022-08-11 16:59:12 -04:00
|
|
|
use crate::ops;
|
|
|
|
use crate::proc_state::ProcState;
|
|
|
|
use crate::tools;
|
|
|
|
use crate::tools::coverage::CoverageCollector;
|
|
|
|
use crate::tools::test::TestMode;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
2022-08-11 16:59:12 -04:00
|
|
|
use crate::version;
|
|
|
|
|
|
|
|
pub struct CliMainWorker {
|
|
|
|
main_module: ModuleSpecifier,
|
2022-08-23 10:39:19 -04:00
|
|
|
is_main_cjs: bool,
|
2022-08-11 16:59:12 -04:00
|
|
|
worker: MainWorker,
|
|
|
|
ps: ProcState,
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
js_run_tests_callback: Option<v8::Global<v8::Function>>,
|
|
|
|
js_run_benchmarks_callback: Option<v8::Global<v8::Function>>,
|
|
|
|
js_enable_test_callback: Option<v8::Global<v8::Function>>,
|
|
|
|
js_enable_bench_callback: Option<v8::Global<v8::Function>>,
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliMainWorker {
|
|
|
|
pub fn into_main_worker(self) -> MainWorker {
|
|
|
|
self.worker
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn setup_repl(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.worker.run_event_loop(false).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&mut self) -> Result<i32, AnyError> {
|
|
|
|
let mut maybe_coverage_collector =
|
|
|
|
self.maybe_setup_coverage_collector().await?;
|
|
|
|
log::debug!("main_module {}", self.main_module);
|
|
|
|
|
2022-09-03 12:19:30 -04:00
|
|
|
if self.is_main_cjs {
|
2023-03-20 00:40:21 -04:00
|
|
|
self.initialize_main_module_for_node()?;
|
2023-02-10 10:26:39 -05:00
|
|
|
deno_node::load_cjs_module(
|
2022-08-23 10:39:19 -04:00
|
|
|
&mut self.worker.js_runtime,
|
|
|
|
&self.main_module.to_file_path().unwrap().to_string_lossy(),
|
|
|
|
true,
|
2022-11-28 15:59:36 -05:00
|
|
|
self.ps.options.inspect_brk().is_some(),
|
2022-08-23 10:39:19 -04:00
|
|
|
)?;
|
2022-08-11 16:59:12 -04:00
|
|
|
} else {
|
2022-08-20 11:31:33 -04:00
|
|
|
self.execute_main_module_possibly_with_npm().await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_load_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
loop {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
|
|
|
if !self
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())?
|
2022-08-11 16:59:12 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.with_event_loop(coverage_collector.stop_collecting().boxed_local())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:29:50 -05:00
|
|
|
Ok(self.worker.exit_code())
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_for_watcher(self) -> Result<(), AnyError> {
|
|
|
|
/// The FileWatcherModuleExecutor provides module execution with safe dispatching of life-cycle events by tracking the
|
|
|
|
/// state of any pending events and emitting accordingly on drop in the case of a future
|
|
|
|
/// cancellation.
|
|
|
|
struct FileWatcherModuleExecutor {
|
2022-08-20 11:31:33 -04:00
|
|
|
inner: CliMainWorker,
|
2022-08-11 16:59:12 -04:00
|
|
|
pending_unload: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileWatcherModuleExecutor {
|
2022-08-20 11:31:33 -04:00
|
|
|
pub fn new(worker: CliMainWorker) -> FileWatcherModuleExecutor {
|
2022-08-11 16:59:12 -04:00
|
|
|
FileWatcherModuleExecutor {
|
2022-08-20 11:31:33 -04:00
|
|
|
inner: worker,
|
2022-08-11 16:59:12 -04:00
|
|
|
pending_unload: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the given main module emitting load and unload events before and after execution
|
|
|
|
/// respectively.
|
2022-08-20 11:31:33 -04:00
|
|
|
pub async fn execute(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.inner.execute_main_module_possibly_with_npm().await?;
|
|
|
|
self
|
|
|
|
.inner
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_load_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
self.pending_unload = true;
|
|
|
|
|
|
|
|
let result = loop {
|
2022-09-06 07:18:23 -04:00
|
|
|
match self.inner.worker.run_event_loop(false).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(error) => break Err(error),
|
|
|
|
}
|
|
|
|
match self
|
2022-08-20 11:31:33 -04:00
|
|
|
.inner
|
2022-08-11 16:59:12 -04:00
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())
|
2022-08-11 16:59:12 -04:00
|
|
|
{
|
2022-09-06 07:18:23 -04:00
|
|
|
Ok(default_prevented) if default_prevented => {} // continue loop
|
|
|
|
Ok(_) => break Ok(()),
|
|
|
|
Err(error) => break Err(error),
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
self.pending_unload = false;
|
|
|
|
|
2022-09-26 15:52:16 -04:00
|
|
|
result?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
self
|
|
|
|
.inner
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for FileWatcherModuleExecutor {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.pending_unload {
|
2022-09-06 07:18:23 -04:00
|
|
|
let _ = self
|
2022-08-20 11:31:33 -04:00
|
|
|
.inner
|
2022-08-11 16:59:12 -04:00
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_unload_event(located_script_name!());
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
let mut executor = FileWatcherModuleExecutor::new(self);
|
|
|
|
executor.execute().await
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_test_specifier(
|
|
|
|
&mut self,
|
|
|
|
mode: TestMode,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-11-21 08:36:26 -05:00
|
|
|
self.enable_test();
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
// Enable op call tracing in core to enable better debugging of op sanitizer
|
|
|
|
// failures.
|
|
|
|
if self.ps.options.trace_ops() {
|
2023-01-14 23:06:46 -05:00
|
|
|
self.worker.js_runtime.execute_script(
|
2023-03-21 18:33:12 -04:00
|
|
|
located_script_name!(),
|
2023-01-24 12:54:10 -05:00
|
|
|
"Deno[Deno.internal].core.enableOpCallTracing();",
|
2023-01-14 23:06:46 -05:00
|
|
|
)?;
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut maybe_coverage_collector =
|
|
|
|
self.maybe_setup_coverage_collector().await?;
|
|
|
|
|
|
|
|
// We only execute the specifier as a module if it is tagged with TestMode::Module or
|
|
|
|
// TestMode::Both.
|
|
|
|
if mode != TestMode::Documentation {
|
2022-09-03 12:19:30 -04:00
|
|
|
// We execute the module module as a side module so that import.meta.main is not set.
|
|
|
|
self.execute_side_module_possibly_with_npm().await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_load_event(located_script_name!())?;
|
2022-11-21 08:36:26 -05:00
|
|
|
self.run_tests(&self.ps.options.shuffle_tests()).await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
loop {
|
|
|
|
if !self
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())?
|
2022-08-11 16:59:12 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.worker.run_event_loop(false).await?;
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.with_event_loop(coverage_collector.stop_collecting().boxed_local())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_lsp_test_specifier(
|
|
|
|
&mut self,
|
|
|
|
mode: TestMode,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-11-21 08:36:26 -05:00
|
|
|
self.enable_test();
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2023-01-14 23:06:46 -05:00
|
|
|
self.worker.execute_script(
|
2023-03-21 18:33:12 -04:00
|
|
|
located_script_name!(),
|
2023-01-24 12:54:10 -05:00
|
|
|
"Deno[Deno.internal].core.enableOpCallTracing();",
|
2023-01-14 23:06:46 -05:00
|
|
|
)?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
if mode != TestMode::Documentation {
|
2022-08-20 11:31:33 -04:00
|
|
|
// We execute the module module as a side module so that import.meta.main is not set.
|
|
|
|
self.execute_side_module_possibly_with_npm().await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_load_event(located_script_name!())?;
|
2022-11-21 08:36:26 -05:00
|
|
|
self.run_tests(&None).await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
loop {
|
|
|
|
if !self
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())?
|
2022-08-11 16:59:12 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.worker.run_event_loop(false).await?;
|
|
|
|
}
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_bench_specifier(&mut self) -> Result<(), AnyError> {
|
2022-11-21 08:36:26 -05:00
|
|
|
self.enable_bench();
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2022-09-03 12:19:30 -04:00
|
|
|
// We execute the module module as a side module so that import.meta.main is not set.
|
|
|
|
self.execute_side_module_possibly_with_npm().await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_load_event(located_script_name!())?;
|
2022-11-21 08:36:26 -05:00
|
|
|
self.run_benchmarks().await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
loop {
|
|
|
|
if !self
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())?
|
2022-08-11 16:59:12 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.worker.run_event_loop(false).await?;
|
|
|
|
}
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
async fn execute_main_module_possibly_with_npm(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let id = self.worker.preload_main_module(&self.main_module).await?;
|
|
|
|
self.evaluate_module_possibly_with_npm(id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn execute_side_module_possibly_with_npm(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let id = self.worker.preload_side_module(&self.main_module).await?;
|
|
|
|
self.evaluate_module_possibly_with_npm(id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn evaluate_module_possibly_with_npm(
|
|
|
|
&mut self,
|
|
|
|
id: ModuleId,
|
|
|
|
) -> Result<(), AnyError> {
|
2023-02-24 14:42:45 -05:00
|
|
|
if self.ps.npm_resolver.has_packages() || self.ps.graph().has_node_specifier
|
2023-02-15 13:44:52 -05:00
|
|
|
{
|
2023-03-20 00:40:21 -04:00
|
|
|
self.initialize_main_module_for_node()?;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
self.worker.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
2023-03-20 00:40:21 -04:00
|
|
|
fn initialize_main_module_for_node(&mut self) -> Result<(), AnyError> {
|
2023-03-20 14:05:13 -04:00
|
|
|
let mut maybe_binary_command_name = None;
|
|
|
|
|
2022-08-23 22:01:21 -04:00
|
|
|
if let DenoSubcommand::Run(flags) = self.ps.options.sub_command() {
|
2023-02-21 12:03:48 -05:00
|
|
|
if let Ok(pkg_ref) = NpmPackageReqReference::from_str(&flags.script) {
|
2022-08-23 22:01:21 -04:00
|
|
|
// if the user ran a binary command, we'll need to set process.argv[0]
|
|
|
|
// to be the name of the binary command instead of deno
|
|
|
|
let binary_name = pkg_ref
|
|
|
|
.sub_path
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(pkg_ref.req.name.as_str());
|
2023-03-20 14:05:13 -04:00
|
|
|
maybe_binary_command_name = Some(binary_name.to_string());
|
2022-08-23 22:01:21 -04:00
|
|
|
}
|
|
|
|
}
|
2023-03-20 14:05:13 -04:00
|
|
|
|
|
|
|
deno_node::initialize_runtime(
|
|
|
|
&mut self.worker.js_runtime,
|
|
|
|
self.ps.options.has_node_modules_dir(),
|
|
|
|
maybe_binary_command_name,
|
|
|
|
)?;
|
|
|
|
|
2022-08-23 22:01:21 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
async fn maybe_setup_coverage_collector(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<Option<CoverageCollector>, AnyError> {
|
2022-08-12 15:21:17 -04:00
|
|
|
if let Some(ref coverage_dir) = self.ps.options.coverage_dir() {
|
2022-08-11 16:59:12 -04:00
|
|
|
let session = self.worker.create_inspector_session().await;
|
|
|
|
|
|
|
|
let coverage_dir = PathBuf::from(coverage_dir);
|
|
|
|
let mut coverage_collector =
|
|
|
|
tools::coverage::CoverageCollector::new(coverage_dir, session);
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.with_event_loop(coverage_collector.start_collecting().boxed_local())
|
|
|
|
.await?;
|
|
|
|
Ok(Some(coverage_collector))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
/// Run tests declared with `Deno.test()`. Test events will be dispatched
|
|
|
|
/// by calling ops which are currently only implemented in the CLI crate.
|
|
|
|
pub async fn run_tests(
|
|
|
|
&mut self,
|
|
|
|
shuffle: &Option<u64>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let promise = {
|
|
|
|
let scope = &mut self.worker.js_runtime.handle_scope();
|
|
|
|
let cb = self.js_run_tests_callback.as_ref().unwrap().open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
let options =
|
|
|
|
serde_v8::to_v8(scope, json!({ "shuffle": shuffle })).unwrap();
|
|
|
|
let promise = cb.call(scope, this, &[options]).unwrap();
|
|
|
|
v8::Global::new(scope, promise)
|
|
|
|
};
|
|
|
|
self.worker.js_runtime.resolve_value(promise).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run benches declared with `Deno.bench()`. Bench events will be dispatched
|
|
|
|
/// by calling ops which are currently only implemented in the CLI crate.
|
|
|
|
pub async fn run_benchmarks(&mut self) -> Result<(), AnyError> {
|
|
|
|
let promise = {
|
|
|
|
let scope = &mut self.worker.js_runtime.handle_scope();
|
|
|
|
let cb = self
|
|
|
|
.js_run_benchmarks_callback
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
let promise = cb.call(scope, this, &[]).unwrap();
|
|
|
|
v8::Global::new(scope, promise)
|
|
|
|
};
|
|
|
|
self.worker.js_runtime.resolve_value(promise).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable `Deno.test()`. If this isn't called before executing user code,
|
|
|
|
/// `Deno.test()` calls will noop.
|
|
|
|
pub fn enable_test(&mut self) {
|
|
|
|
let scope = &mut self.worker.js_runtime.handle_scope();
|
|
|
|
let cb = self.js_enable_test_callback.as_ref().unwrap().open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
cb.call(scope, this, &[]).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable `Deno.bench()`. If this isn't called before executing user code,
|
|
|
|
/// `Deno.bench()` calls will noop.
|
|
|
|
pub fn enable_bench(&mut self) {
|
|
|
|
let scope = &mut self.worker.js_runtime.handle_scope();
|
|
|
|
let cb = self.js_enable_bench_callback.as_ref().unwrap().open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
cb.call(scope, this, &[]).unwrap();
|
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2022-08-23 10:39:19 -04:00
|
|
|
pub async fn create_main_worker(
|
2022-08-11 16:59:12 -04:00
|
|
|
ps: &ProcState,
|
|
|
|
main_module: ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2022-11-21 08:36:26 -05:00
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
create_main_worker_internal(
|
|
|
|
ps,
|
|
|
|
main_module,
|
|
|
|
permissions,
|
|
|
|
vec![],
|
|
|
|
Default::default(),
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_main_worker_for_test_or_bench(
|
|
|
|
ps: &ProcState,
|
|
|
|
main_module: ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2022-11-21 08:36:26 -05:00
|
|
|
custom_extensions: Vec<Extension>,
|
2023-03-04 19:39:48 -05:00
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
2022-11-21 08:36:26 -05:00
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
create_main_worker_internal(
|
|
|
|
ps,
|
|
|
|
main_module,
|
|
|
|
permissions,
|
|
|
|
custom_extensions,
|
|
|
|
stdio,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_main_worker_internal(
|
|
|
|
ps: &ProcState,
|
|
|
|
main_module: ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2022-08-11 16:59:12 -04:00
|
|
|
mut custom_extensions: Vec<Extension>,
|
2023-03-04 19:39:48 -05:00
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
2022-11-21 08:36:26 -05:00
|
|
|
bench_or_test: bool,
|
2022-08-23 10:39:19 -04:00
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
let (main_module, is_main_cjs) = if let Ok(package_ref) =
|
2023-02-21 12:03:48 -05:00
|
|
|
NpmPackageReqReference::from_specifier(&main_module)
|
2022-08-23 10:39:19 -04:00
|
|
|
{
|
|
|
|
ps.npm_resolver
|
|
|
|
.add_package_reqs(vec![package_ref.req.clone()])
|
|
|
|
.await?;
|
2023-02-22 14:15:25 -05:00
|
|
|
let pkg_nv = ps
|
2023-03-12 23:32:59 -04:00
|
|
|
.npm_resolution
|
2023-02-22 14:15:25 -05:00
|
|
|
.resolve_pkg_id_from_pkg_req(&package_ref.req)?
|
|
|
|
.nv;
|
2022-09-06 06:57:28 -04:00
|
|
|
let node_resolution = node::node_resolve_binary_export(
|
2023-02-22 14:15:25 -05:00
|
|
|
&pkg_nv,
|
2022-08-23 10:39:19 -04:00
|
|
|
package_ref.sub_path.as_deref(),
|
|
|
|
&ps.npm_resolver,
|
|
|
|
)?;
|
2022-09-06 06:57:28 -04:00
|
|
|
let is_main_cjs =
|
|
|
|
matches!(node_resolution, node::NodeResolution::CommonJs(_));
|
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
2023-02-20 13:14:06 -05:00
|
|
|
} else if ps.options.is_npm_main() {
|
2022-09-28 13:04:16 -04:00
|
|
|
let node_resolution =
|
|
|
|
node::url_to_node_resolution(main_module, &ps.npm_resolver)?;
|
|
|
|
let is_main_cjs =
|
|
|
|
matches!(node_resolution, node::NodeResolution::CommonJs(_));
|
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
2022-08-23 10:39:19 -04:00
|
|
|
} else {
|
|
|
|
(main_module, false)
|
|
|
|
};
|
2022-09-28 13:04:16 -04:00
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
let module_loader = CliModuleLoader::new(
|
|
|
|
ps.clone(),
|
|
|
|
PermissionsContainer::allow_all(),
|
|
|
|
permissions.clone(),
|
|
|
|
);
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
let maybe_inspector_server = ps.maybe_inspector_server.clone();
|
|
|
|
|
|
|
|
let create_web_worker_cb =
|
|
|
|
create_web_worker_callback(ps.clone(), stdio.clone());
|
|
|
|
let web_worker_preload_module_cb =
|
|
|
|
create_web_worker_preload_module_callback(ps.clone());
|
2022-08-20 11:31:33 -04:00
|
|
|
let web_worker_pre_execute_module_cb =
|
|
|
|
create_web_worker_pre_execute_module_callback(ps.clone());
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
let maybe_storage_key = ps.options.resolve_storage_key(&main_module);
|
2022-09-28 08:11:12 -04:00
|
|
|
let origin_storage_dir = maybe_storage_key.as_ref().map(|key| {
|
2022-08-11 16:59:12 -04:00
|
|
|
ps.dir
|
2022-11-25 19:04:30 -05:00
|
|
|
.origin_data_folder_path()
|
2022-08-11 16:59:12 -04:00
|
|
|
.join(checksum::gen(&[key.as_bytes()]))
|
|
|
|
});
|
2022-09-28 08:11:12 -04:00
|
|
|
let cache_storage_dir = maybe_storage_key.map(|key| {
|
|
|
|
// TODO(@satyarohith): storage quota management
|
|
|
|
// Note: we currently use temp_dir() to avoid managing storage size.
|
|
|
|
std::env::temp_dir()
|
|
|
|
.join("deno_cache")
|
|
|
|
.join(checksum::gen(&[key.as_bytes()]))
|
|
|
|
});
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
let mut extensions = ops::cli_exts(ps.clone());
|
|
|
|
extensions.append(&mut custom_extensions);
|
|
|
|
|
|
|
|
let options = WorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: ps.options.argv().clone(),
|
|
|
|
cpu_count: std::thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1),
|
|
|
|
debug_flag: ps
|
|
|
|
.options
|
|
|
|
.log_level()
|
2023-03-15 17:46:36 -04:00
|
|
|
.map(|l| l == log::Level::Debug)
|
|
|
|
.unwrap_or(false),
|
2022-08-11 16:59:12 -04:00
|
|
|
enable_testing_features: ps.options.enable_testing_features(),
|
2022-10-18 09:33:35 -04:00
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
2022-12-08 11:50:09 -05:00
|
|
|
location: ps.options.location_flag().clone(),
|
2022-08-11 16:59:12 -04:00
|
|
|
no_color: !colors::use_color(),
|
|
|
|
is_tty: colors::is_tty(),
|
2023-03-23 18:27:58 -04:00
|
|
|
runtime_version: version::deno().to_string(),
|
2022-08-11 16:59:12 -04:00
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
unstable: ps.options.unstable(),
|
2023-03-23 18:27:58 -04:00
|
|
|
user_agent: version::get_user_agent().to_string(),
|
2022-09-17 06:04:43 -04:00
|
|
|
inspect: ps.options.is_inspecting(),
|
2022-08-11 16:59:12 -04:00
|
|
|
},
|
|
|
|
extensions,
|
2022-11-21 08:36:26 -05:00
|
|
|
startup_snapshot: Some(crate::js::deno_isolate_init()),
|
2022-08-11 16:59:12 -04:00
|
|
|
unsafely_ignore_certificate_errors: ps
|
|
|
|
.options
|
|
|
|
.unsafely_ignore_certificate_errors()
|
2022-12-08 11:50:09 -05:00
|
|
|
.clone(),
|
2022-08-11 16:59:12 -04:00
|
|
|
root_cert_store: Some(ps.root_cert_store.clone()),
|
|
|
|
seed: ps.options.seed(),
|
|
|
|
source_map_getter: Some(Box::new(module_loader.clone())),
|
|
|
|
format_js_error_fn: Some(Arc::new(format_js_error)),
|
|
|
|
create_web_worker_cb,
|
|
|
|
web_worker_preload_module_cb,
|
2022-08-20 11:31:33 -04:00
|
|
|
web_worker_pre_execute_module_cb,
|
2022-08-11 16:59:12 -04:00
|
|
|
maybe_inspector_server,
|
2022-12-12 09:33:30 -05:00
|
|
|
should_break_on_first_statement: ps.options.inspect_brk().is_some(),
|
|
|
|
should_wait_for_inspector_session: ps.options.inspect_wait().is_some(),
|
2022-08-11 16:59:12 -04:00
|
|
|
module_loader,
|
2022-08-20 11:31:33 -04:00
|
|
|
npm_resolver: Some(Rc::new(ps.npm_resolver.clone())),
|
2022-08-11 16:59:12 -04:00
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
2022-09-28 08:11:12 -04:00
|
|
|
cache_storage_dir,
|
2022-08-11 16:59:12 -04:00
|
|
|
origin_storage_dir,
|
|
|
|
blob_store: ps.blob_store.clone(),
|
|
|
|
broadcast_channel: ps.broadcast_channel.clone(),
|
|
|
|
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
|
|
|
|
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
|
|
|
|
stdio,
|
|
|
|
};
|
|
|
|
|
2022-11-21 08:36:26 -05:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
2022-08-11 16:59:12 -04:00
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
options,
|
|
|
|
);
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
let (
|
|
|
|
js_run_tests_callback,
|
|
|
|
js_run_benchmarks_callback,
|
|
|
|
js_enable_test_callback,
|
|
|
|
js_enable_bench_callback,
|
|
|
|
) = if bench_or_test {
|
|
|
|
let scope = &mut worker.js_runtime.handle_scope();
|
|
|
|
let js_run_tests_callback = deno_core::JsRuntime::eval::<v8::Function>(
|
|
|
|
scope,
|
|
|
|
"Deno[Deno.internal].testing.runTests",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let js_run_benchmarks_callback =
|
|
|
|
deno_core::JsRuntime::eval::<v8::Function>(
|
|
|
|
scope,
|
|
|
|
"Deno[Deno.internal].testing.runBenchmarks",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let js_enable_tests_callback = deno_core::JsRuntime::eval::<v8::Function>(
|
|
|
|
scope,
|
|
|
|
"Deno[Deno.internal].testing.enableTest",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let js_enable_bench_callback = deno_core::JsRuntime::eval::<v8::Function>(
|
|
|
|
scope,
|
|
|
|
"Deno[Deno.internal].testing.enableBench",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
(
|
|
|
|
Some(v8::Global::new(scope, js_run_tests_callback)),
|
|
|
|
Some(v8::Global::new(scope, js_run_benchmarks_callback)),
|
|
|
|
Some(v8::Global::new(scope, js_enable_tests_callback)),
|
|
|
|
Some(v8::Global::new(scope, js_enable_bench_callback)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(None, None, None, None)
|
|
|
|
};
|
|
|
|
|
2022-08-23 10:39:19 -04:00
|
|
|
Ok(CliMainWorker {
|
2022-08-11 16:59:12 -04:00
|
|
|
main_module,
|
2022-08-23 10:39:19 -04:00
|
|
|
is_main_cjs,
|
2022-08-11 16:59:12 -04:00
|
|
|
worker,
|
|
|
|
ps: ps.clone(),
|
2022-11-21 08:36:26 -05:00
|
|
|
js_run_tests_callback,
|
|
|
|
js_run_benchmarks_callback,
|
|
|
|
js_enable_test_callback,
|
|
|
|
js_enable_bench_callback,
|
2022-08-23 10:39:19 -04:00
|
|
|
})
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2022-09-03 12:19:30 -04:00
|
|
|
// TODO(bartlomieju): this callback could have default value
|
|
|
|
// and not be required
|
2022-08-11 16:59:12 -04:00
|
|
|
fn create_web_worker_preload_module_callback(
|
2022-09-03 12:19:30 -04:00
|
|
|
_ps: ProcState,
|
2022-08-16 21:00:35 -04:00
|
|
|
) -> Arc<WorkerEventCb> {
|
2022-09-03 12:19:30 -04:00
|
|
|
Arc::new(move |worker| {
|
|
|
|
let fut = async move { Ok(worker) };
|
2022-08-11 16:59:12 -04:00
|
|
|
LocalFutureObj::new(Box::new(fut))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
fn create_web_worker_pre_execute_module_callback(
|
|
|
|
ps: ProcState,
|
|
|
|
) -> Arc<WorkerEventCb> {
|
|
|
|
Arc::new(move |mut worker| {
|
|
|
|
let ps = ps.clone();
|
|
|
|
let fut = async move {
|
|
|
|
// this will be up to date after pre-load
|
|
|
|
if ps.npm_resolver.has_packages() {
|
2023-02-10 10:26:39 -05:00
|
|
|
deno_node::initialize_runtime(
|
2022-12-16 17:41:51 -05:00
|
|
|
&mut worker.js_runtime,
|
2023-02-22 20:16:16 -05:00
|
|
|
ps.options.has_node_modules_dir(),
|
2023-03-20 14:05:13 -04:00
|
|
|
None,
|
2023-03-20 00:40:21 -04:00
|
|
|
)?;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(worker)
|
|
|
|
};
|
|
|
|
LocalFutureObj::new(Box::new(fut))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
fn create_web_worker_callback(
|
|
|
|
ps: ProcState,
|
2023-03-04 19:39:48 -05:00
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
2022-08-11 16:59:12 -04:00
|
|
|
) -> Arc<CreateWebWorkerCb> {
|
|
|
|
Arc::new(move |args| {
|
|
|
|
let maybe_inspector_server = ps.maybe_inspector_server.clone();
|
|
|
|
|
|
|
|
let module_loader = CliModuleLoader::new_for_worker(
|
|
|
|
ps.clone(),
|
|
|
|
args.parent_permissions.clone(),
|
2023-01-10 08:35:44 -05:00
|
|
|
args.permissions.clone(),
|
2022-08-11 16:59:12 -04:00
|
|
|
);
|
|
|
|
let create_web_worker_cb =
|
|
|
|
create_web_worker_callback(ps.clone(), stdio.clone());
|
|
|
|
let preload_module_cb =
|
|
|
|
create_web_worker_preload_module_callback(ps.clone());
|
2022-08-20 11:31:33 -04:00
|
|
|
let pre_execute_module_cb =
|
|
|
|
create_web_worker_pre_execute_module_callback(ps.clone());
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
let extensions = ops::cli_exts(ps.clone());
|
|
|
|
|
2022-09-28 08:11:12 -04:00
|
|
|
let maybe_storage_key = ps.options.resolve_storage_key(&args.main_module);
|
|
|
|
let cache_storage_dir = maybe_storage_key.map(|key| {
|
|
|
|
// TODO(@satyarohith): storage quota management
|
|
|
|
// Note: we currently use temp_dir() to avoid managing storage size.
|
|
|
|
std::env::temp_dir()
|
|
|
|
.join("deno_cache")
|
|
|
|
.join(checksum::gen(&[key.as_bytes()]))
|
|
|
|
});
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
let options = WebWorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: ps.options.argv().clone(),
|
|
|
|
cpu_count: std::thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1),
|
|
|
|
debug_flag: ps
|
|
|
|
.options
|
|
|
|
.log_level()
|
2023-03-15 17:46:36 -04:00
|
|
|
.map(|l| l == log::Level::Debug)
|
|
|
|
.unwrap_or(false),
|
2022-08-11 16:59:12 -04:00
|
|
|
enable_testing_features: ps.options.enable_testing_features(),
|
2022-10-18 09:33:35 -04:00
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
2022-08-11 16:59:12 -04:00
|
|
|
location: Some(args.main_module.clone()),
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
is_tty: colors::is_tty(),
|
2023-03-23 18:27:58 -04:00
|
|
|
runtime_version: version::deno().to_string(),
|
2022-08-11 16:59:12 -04:00
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
unstable: ps.options.unstable(),
|
2023-03-23 18:27:58 -04:00
|
|
|
user_agent: version::get_user_agent().to_string(),
|
2022-09-17 06:04:43 -04:00
|
|
|
inspect: ps.options.is_inspecting(),
|
2022-08-11 16:59:12 -04:00
|
|
|
},
|
|
|
|
extensions,
|
2022-11-21 08:36:26 -05:00
|
|
|
startup_snapshot: Some(crate::js::deno_isolate_init()),
|
2022-08-11 16:59:12 -04:00
|
|
|
unsafely_ignore_certificate_errors: ps
|
|
|
|
.options
|
|
|
|
.unsafely_ignore_certificate_errors()
|
2022-12-08 11:50:09 -05:00
|
|
|
.clone(),
|
2022-08-11 16:59:12 -04:00
|
|
|
root_cert_store: Some(ps.root_cert_store.clone()),
|
|
|
|
seed: ps.options.seed(),
|
|
|
|
create_web_worker_cb,
|
|
|
|
preload_module_cb,
|
2022-08-20 11:31:33 -04:00
|
|
|
pre_execute_module_cb,
|
2022-08-11 16:59:12 -04:00
|
|
|
format_js_error_fn: Some(Arc::new(format_js_error)),
|
|
|
|
source_map_getter: Some(Box::new(module_loader.clone())),
|
|
|
|
module_loader,
|
2022-08-20 11:31:33 -04:00
|
|
|
npm_resolver: Some(Rc::new(ps.npm_resolver.clone())),
|
2022-08-11 16:59:12 -04:00
|
|
|
worker_type: args.worker_type,
|
|
|
|
maybe_inspector_server,
|
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
|
|
|
blob_store: ps.blob_store.clone(),
|
|
|
|
broadcast_channel: ps.broadcast_channel.clone(),
|
|
|
|
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
|
|
|
|
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
|
|
|
|
stdio: stdio.clone(),
|
2022-09-28 08:11:12 -04:00
|
|
|
cache_storage_dir,
|
2022-08-11 16:59:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
WebWorker::bootstrap_from_options(
|
|
|
|
args.name,
|
|
|
|
args.permissions,
|
|
|
|
args.main_module,
|
|
|
|
args.worker_id,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-03-13 19:31:03 -04:00
|
|
|
use deno_core::resolve_path;
|
2023-01-14 23:18:58 -05:00
|
|
|
use deno_core::FsModuleLoader;
|
2022-11-21 08:36:26 -05:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
|
|
|
use deno_runtime::deno_web::BlobStore;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
fn create_test_worker() -> MainWorker {
|
2023-03-13 19:31:03 -04:00
|
|
|
let main_module =
|
|
|
|
resolve_path("./hello.js", &std::env::current_dir().unwrap()).unwrap();
|
2023-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::default());
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
let options = WorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: vec![],
|
|
|
|
cpu_count: 1,
|
|
|
|
debug_flag: false,
|
|
|
|
enable_testing_features: false,
|
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
|
|
|
location: None,
|
|
|
|
no_color: true,
|
|
|
|
is_tty: false,
|
|
|
|
runtime_version: "x".to_string(),
|
|
|
|
ts_version: "x".to_string(),
|
|
|
|
unstable: false,
|
|
|
|
user_agent: "x".to_string(),
|
|
|
|
inspect: false,
|
|
|
|
},
|
|
|
|
extensions: vec![],
|
|
|
|
startup_snapshot: Some(crate::js::deno_isolate_init()),
|
|
|
|
unsafely_ignore_certificate_errors: None,
|
|
|
|
root_cert_store: None,
|
|
|
|
seed: None,
|
|
|
|
format_js_error_fn: None,
|
|
|
|
source_map_getter: None,
|
|
|
|
web_worker_preload_module_cb: Arc::new(|_| unreachable!()),
|
|
|
|
web_worker_pre_execute_module_cb: Arc::new(|_| unreachable!()),
|
|
|
|
create_web_worker_cb: Arc::new(|_| unreachable!()),
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
2022-12-12 09:33:30 -05:00
|
|
|
should_wait_for_inspector_session: false,
|
2022-11-21 08:36:26 -05:00
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
|
|
|
npm_resolver: None,
|
|
|
|
get_error_class_fn: None,
|
|
|
|
cache_storage_dir: None,
|
|
|
|
origin_storage_dir: None,
|
|
|
|
blob_store: BlobStore::default(),
|
|
|
|
broadcast_channel: InMemoryBroadcastChannel::default(),
|
|
|
|
shared_array_buffer_store: None,
|
|
|
|
compiled_wasm_module_store: None,
|
|
|
|
stdio: Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
MainWorker::bootstrap_from_options(main_module, permissions, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_esm_imports_a() {
|
|
|
|
let p = test_util::testdata_path().join("runtime/esm_imports_a.js");
|
2023-03-13 21:12:09 -04:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
2023-01-27 10:43:16 -05:00
|
|
|
eprintln!("execute_mod err {err:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("Future got unexpected error: {e:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_circular() {
|
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/circular1.js");
|
2023-03-13 19:31:03 -04:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
2023-01-27 10:43:16 -05:00
|
|
|
eprintln!("execute_mod err {err:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("Future got unexpected error: {e:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_resolve_error() {
|
|
|
|
// "foo" is not a valid module specifier so this should return an error.
|
|
|
|
let mut worker = create_test_worker();
|
2023-03-13 19:31:03 -04:00
|
|
|
let module_specifier =
|
|
|
|
resolve_path("./does-not-exist", &std::env::current_dir().unwrap())
|
|
|
|
.unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_002_hello() {
|
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let p = test_util::testdata_path().join("run/001_hello.js");
|
2023-03-13 19:31:03 -04:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
}
|