2021-01-12 02:51:10 -05:00
|
|
|
// Copyright 2018-2021 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;
|
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
|
|
|
let options = WorkerOptions {
|
|
|
|
apply_source_maps: false,
|
|
|
|
args: vec![],
|
|
|
|
debug_flag: false,
|
|
|
|
unstable: false,
|
2021-08-09 10:53:21 -04:00
|
|
|
unsafely_treat_insecure_origin_as_secure: None,
|
2021-08-07 08:49:38 -04:00
|
|
|
root_cert_store: None,
|
2020-12-13 13:45:53 -05:00
|
|
|
user_agent: "hello_runtime".to_string(),
|
|
|
|
seed: None,
|
|
|
|
js_error_create_fn: None,
|
|
|
|
create_web_worker_cb,
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
|
|
|
module_loader,
|
|
|
|
runtime_version: "x".to_string(),
|
|
|
|
ts_version: "x".to_string(),
|
|
|
|
no_color: false,
|
|
|
|
get_error_class_fn: Some(&get_error_class_name),
|
2021-01-07 13:06:08 -05:00
|
|
|
location: 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-07-29 15:45:11 -04:00
|
|
|
cpu_count: 1,
|
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();
|
|
|
|
|
|
|
|
let mut worker =
|
|
|
|
MainWorker::from_options(main_module.clone(), permissions, &options);
|
|
|
|
worker.bootstrap(&options);
|
|
|
|
worker.execute_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(())
|
|
|
|
}
|