2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 19:45:53 +01:00
|
|
|
|
2023-05-31 22:26:24 +02:00
|
|
|
use std::path::Path;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2020-12-13 19:45:53 +01:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-21 16:08:23 +02:00
|
|
|
use deno_core::op2;
|
2020-12-13 19:45:53 +01:00
|
|
|
use deno_core::FsModuleLoader;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-01-07 17:25:34 +01:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2020-12-13 19:45:53 +01:00
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
|
|
|
|
2023-06-03 21:22:32 +01:00
|
|
|
deno_core::extension!(hello_runtime, ops = [op_hello]);
|
2020-12-13 19:45:53 +01:00
|
|
|
|
2023-09-21 16:08:23 +02:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_hello(#[string] text: &str) {
|
2023-05-31 22:26:24 +02:00
|
|
|
println!("Hello {}!", text);
|
|
|
|
}
|
|
|
|
|
2020-12-13 19:45:53 +01:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), AnyError> {
|
2023-05-31 22:26:24 +02:00
|
|
|
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("examples/extension_with_ops/main.js");
|
2023-05-10 20:06:59 -04:00
|
|
|
let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
|
2021-10-05 22:41:14 +02:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
|
|
|
main_module.clone(),
|
2023-05-04 01:44:59 +01:00
|
|
|
PermissionsContainer::allow_all(),
|
|
|
|
WorkerOptions {
|
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
2023-06-26 13:54:10 +02:00
|
|
|
extensions: vec![hello_runtime::init_ops()],
|
2023-05-04 01:44:59 +01:00
|
|
|
..Default::default()
|
|
|
|
},
|
2021-10-05 22:41:14 +02:00
|
|
|
);
|
2021-09-18 03:44:53 +02:00
|
|
|
worker.execute_main_module(&main_module).await?;
|
2021-05-26 21:07:12 +02:00
|
|
|
worker.run_event_loop(false).await?;
|
2020-12-13 19:45:53 +01:00
|
|
|
Ok(())
|
|
|
|
}
|