2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2023-03-13 13:50:23 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::FsModuleLoader;
|
2021-05-22 12:08:24 -04:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
2021-07-05 09:34:37 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
2021-10-05 16:41:14 -04:00
|
|
|
use deno_runtime::BootstrapOptions;
|
2020-12-13 13:45:53 -05:00
|
|
|
use std::path::Path;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
fn get_error_class_name(e: &AnyError) -> &'static str {
|
|
|
|
deno_runtime::errors::get_error_class_name(e).unwrap_or("Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), AnyError> {
|
|
|
|
let module_loader = Rc::new(FsModuleLoader);
|
|
|
|
let create_web_worker_cb = Arc::new(|_| {
|
|
|
|
todo!("Web workers are not supported in the example");
|
|
|
|
});
|
2022-08-16 21:00:35 -04:00
|
|
|
let web_worker_event_cb = Arc::new(|_| {
|
2022-02-11 07:41:56 -05:00
|
|
|
todo!("Web workers are not supported in the example");
|
|
|
|
});
|
2020-12-13 13:45:53 -05:00
|
|
|
|
|
|
|
let options = WorkerOptions {
|
2023-03-26 00:38:45 -04:00
|
|
|
bootstrap: BootstrapOptions::default(),
|
2021-10-08 11:03:49 -04:00
|
|
|
extensions: vec![],
|
2022-11-10 06:46:26 -05:00
|
|
|
startup_snapshot: None,
|
2021-08-10 07:19:45 -04:00
|
|
|
unsafely_ignore_certificate_errors: None,
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider: None,
|
2020-12-13 13:45:53 -05:00
|
|
|
seed: None,
|
2022-04-15 10:08:09 -04:00
|
|
|
source_map_getter: None,
|
2022-04-26 19:06:10 -04:00
|
|
|
format_js_error_fn: None,
|
2022-08-16 21:00:35 -04:00
|
|
|
web_worker_preload_module_cb: web_worker_event_cb.clone(),
|
|
|
|
web_worker_pre_execute_module_cb: web_worker_event_cb,
|
2020-12-13 13:45:53 -05:00
|
|
|
create_web_worker_cb,
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
2022-12-12 09:33:30 -05:00
|
|
|
should_wait_for_inspector_session: false,
|
2020-12-13 13:45:53 -05:00
|
|
|
module_loader,
|
2023-04-24 19:44:35 -04:00
|
|
|
node_fs: None,
|
2022-08-20 11:31:33 -04:00
|
|
|
npm_resolver: None,
|
2020-12-13 13:45:53 -05:00
|
|
|
get_error_class_fn: Some(&get_error_class_name),
|
2022-09-28 08:11:12 -04:00
|
|
|
cache_storage_dir: None,
|
2021-05-27 01:23:12 -04:00
|
|
|
origin_storage_dir: None,
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store: BlobStore::default(),
|
2021-05-22 12:08:24 -04:00
|
|
|
broadcast_channel: InMemoryBroadcastChannel::default(),
|
2021-07-06 13:42:52 -04:00
|
|
|
shared_array_buffer_store: None,
|
2021-09-29 04:47:24 -04:00
|
|
|
compiled_wasm_module_store: None,
|
2022-04-26 19:00:04 -04:00
|
|
|
stdio: Default::default(),
|
2020-12-13 13:45:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let js_path =
|
|
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js");
|
2023-03-13 13:50:23 -04:00
|
|
|
let main_module = deno_core::resolve_path(
|
|
|
|
&js_path.to_string_lossy(),
|
|
|
|
&std::env::current_dir().context("Unable to get CWD")?,
|
|
|
|
)?;
|
2023-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::allow_all();
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2021-10-05 16:41:14 -04:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
options,
|
|
|
|
);
|
2021-09-17 21:44:53 -04:00
|
|
|
worker.execute_main_module(&main_module).await?;
|
2021-05-26 15:07:12 -04:00
|
|
|
worker.run_event_loop(false).await?;
|
2020-12-13 13:45:53 -05:00
|
|
|
Ok(())
|
|
|
|
}
|