2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2023-05-31 16:26:24 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_core::error::AnyError;
|
2024-03-13 23:23:37 -04:00
|
|
|
use deno_core::op2;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_core::FsModuleLoader;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
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;
|
|
|
|
|
2024-03-13 23:23:37 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_hello(#[string] text: &str) {
|
|
|
|
println!("Hello {} from an op!", text);
|
|
|
|
}
|
|
|
|
|
2023-05-14 15:55:26 -04:00
|
|
|
deno_core::extension!(
|
|
|
|
hello_runtime,
|
2024-03-13 23:23:37 -04:00
|
|
|
ops = [op_hello],
|
2023-05-31 16:26:24 -04:00
|
|
|
esm_entry_point = "ext:hello_runtime/bootstrap.js",
|
2024-03-13 23:23:37 -04:00
|
|
|
esm = [dir "examples/extension", "bootstrap.js"]
|
2023-05-14 15:55:26 -04:00
|
|
|
);
|
2020-12-13 13:45:53 -05:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), AnyError> {
|
2024-03-13 23:23:37 -04:00
|
|
|
let js_path =
|
|
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/extension/main.js");
|
2023-05-10 20:06:59 -04:00
|
|
|
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
|
2024-03-13 23:23:37 -04:00
|
|
|
eprintln!("Running {main_module}...");
|
2021-10-05 16:41:14 -04:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
|
|
|
main_module.clone(),
|
2023-05-03 20:44:59 -04:00
|
|
|
PermissionsContainer::allow_all(),
|
|
|
|
WorkerOptions {
|
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
2023-06-26 07:54:10 -04:00
|
|
|
extensions: vec![hello_runtime::init_ops_and_esm()],
|
2023-05-03 20:44:59 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
2021-10-05 16:41:14 -04:00
|
|
|
);
|
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(())
|
|
|
|
}
|