2025-01-01 04:12:39 +09:00
|
|
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
2020-12-13 19:45:53 +01:00
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#![allow(clippy::print_stdout)]
|
|
|
|
#![allow(clippy::print_stderr)]
|
|
|
|
|
2023-05-31 22:26:24 +02:00
|
|
|
use std::path::Path;
|
|
|
|
use std::rc::Rc;
|
2024-09-16 21:39:37 +01:00
|
|
|
use std::sync::Arc;
|
2023-05-31 22:26:24 +02:00
|
|
|
|
2020-12-13 19:45:53 +01:00
|
|
|
use deno_core::error::AnyError;
|
2024-03-13 21:23:37 -06: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;
|
2024-09-16 21:39:37 +01:00
|
|
|
use deno_fs::RealFs;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_permissions::PermissionsContainer;
|
2024-09-16 21:39:37 +01:00
|
|
|
use deno_runtime::permissions::RuntimePermissionDescriptorParser;
|
2020-12-13 19:45:53 +01:00
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
2024-09-29 20:07:50 -04:00
|
|
|
use deno_runtime::worker::WorkerServiceOptions;
|
2020-12-13 19:45:53 +01:00
|
|
|
|
2024-03-13 21:23:37 -06:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_hello(#[string] text: &str) {
|
|
|
|
println!("Hello {} from an op!", text);
|
|
|
|
}
|
|
|
|
|
2023-05-14 19:55:26 +00:00
|
|
|
deno_core::extension!(
|
|
|
|
hello_runtime,
|
2024-03-13 21:23:37 -06:00
|
|
|
ops = [op_hello],
|
2023-05-31 22:26:24 +02:00
|
|
|
esm_entry_point = "ext:hello_runtime/bootstrap.js",
|
2024-03-13 21:23:37 -06:00
|
|
|
esm = [dir "examples/extension", "bootstrap.js"]
|
2023-05-14 19:55:26 +00:00
|
|
|
);
|
2020-12-13 19:45:53 +01:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), AnyError> {
|
2024-03-13 21:23:37 -06: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 21:23:37 -06:00
|
|
|
eprintln!("Running {main_module}...");
|
2024-09-29 20:07:50 -04:00
|
|
|
let fs = Arc::new(RealFs);
|
2024-12-31 11:29:07 -05:00
|
|
|
let permission_desc_parser = Arc::new(
|
|
|
|
RuntimePermissionDescriptorParser::new(sys_traits::impls::RealSys),
|
|
|
|
);
|
2021-10-05 22:41:14 +02:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
|
|
|
main_module.clone(),
|
2024-12-31 11:29:07 -05:00
|
|
|
WorkerServiceOptions::<sys_traits::impls::RealSys> {
|
2023-05-04 01:44:59 +01:00
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
2024-09-30 09:19:24 -04:00
|
|
|
permissions: PermissionsContainer::allow_all(permission_desc_parser),
|
2024-09-29 20:07:50 -04:00
|
|
|
blob_store: Default::default(),
|
|
|
|
broadcast_channel: Default::default(),
|
|
|
|
feature_checker: Default::default(),
|
|
|
|
node_services: Default::default(),
|
|
|
|
npm_process_state_provider: Default::default(),
|
|
|
|
root_cert_store_provider: Default::default(),
|
2024-11-15 14:14:11 +03:30
|
|
|
fetch_dns_resolver: Default::default(),
|
2024-09-29 20:07:50 -04:00
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
compiled_wasm_module_store: Default::default(),
|
|
|
|
v8_code_cache: Default::default(),
|
|
|
|
fs,
|
|
|
|
},
|
|
|
|
WorkerOptions {
|
2023-06-26 13:54:10 +02:00
|
|
|
extensions: vec![hello_runtime::init_ops_and_esm()],
|
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(())
|
|
|
|
}
|