2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
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;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
|
|
|
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 {
|
2021-10-05 16:41:14 -04:00
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: vec![],
|
|
|
|
cpu_count: 1,
|
|
|
|
debug_flag: false,
|
|
|
|
enable_testing_features: false,
|
2022-10-18 09:33:35 -04:00
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
2021-10-05 16:41:14 -04:00
|
|
|
location: None,
|
|
|
|
no_color: false,
|
2022-02-28 22:37:50 -05:00
|
|
|
is_tty: false,
|
2021-10-05 16:41:14 -04:00
|
|
|
runtime_version: "x".to_string(),
|
|
|
|
ts_version: "x".to_string(),
|
|
|
|
unstable: false,
|
2022-05-14 06:00:02 -04:00
|
|
|
user_agent: "hello_runtime".to_string(),
|
2022-09-17 06:04:43 -04:00
|
|
|
inspect: false,
|
2021-10-05 16:41:14 -04:00
|
|
|
},
|
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,
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store: 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,
|
|
|
|
module_loader,
|
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");
|
2021-02-17 13:47:18 -05:00
|
|
|
let main_module = deno_core::resolve_path(&js_path.to_string_lossy())?;
|
2020-12-13 13:45:53 -05:00
|
|
|
let permissions = Permissions::allow_all();
|
|
|
|
|
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(())
|
|
|
|
}
|