From f3193e0e1c12ea139f00d6b19d152b95f37b73c3 Mon Sep 17 00:00:00 2001 From: Exidex <16986685+Exidex@users.noreply.github.com> Date: Wed, 31 May 2023 22:26:24 +0200 Subject: feat(runtime): Add example for extension with ops (#19204) Spend quite some time trying to get this working. With proper example would have been a lot faster. So this is pr with the example. I also rearranged examples a little bit to allow for addition of more examples --- runtime/Cargo.toml | 8 +++-- runtime/examples/extension_with_esm/bootstrap.js | 5 +++ runtime/examples/extension_with_esm/main.js | 4 +++ runtime/examples/extension_with_esm/main.rs | 36 +++++++++++++++++++ runtime/examples/extension_with_ops/main.js | 2 ++ runtime/examples/extension_with_ops/main.rs | 44 ++++++++++++++++++++++++ runtime/examples/hello_runtime.js | 4 --- runtime/examples/hello_runtime.rs | 35 ------------------- runtime/examples/hello_runtime_bootstrap.js | 5 --- 9 files changed, 97 insertions(+), 46 deletions(-) create mode 100644 runtime/examples/extension_with_esm/bootstrap.js create mode 100644 runtime/examples/extension_with_esm/main.js create mode 100644 runtime/examples/extension_with_esm/main.rs create mode 100644 runtime/examples/extension_with_ops/main.js create mode 100644 runtime/examples/extension_with_ops/main.rs delete mode 100644 runtime/examples/hello_runtime.js delete mode 100644 runtime/examples/hello_runtime.rs delete mode 100644 runtime/examples/hello_runtime_bootstrap.js (limited to 'runtime') diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index c464d3f04..5a7196ea8 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -29,8 +29,12 @@ name = "deno_runtime" path = "lib.rs" [[example]] -name = "hello_runtime" -path = "examples/hello_runtime.rs" +name = "extension_with_esm" +path = "examples/extension_with_esm/main.rs" + +[[example]] +name = "extension_with_ops" +path = "examples/extension_with_ops/main.rs" [build-dependencies] deno_ast.workspace = true diff --git a/runtime/examples/extension_with_esm/bootstrap.js b/runtime/examples/extension_with_esm/bootstrap.js new file mode 100644 index 000000000..759dde939 --- /dev/null +++ b/runtime/examples/extension_with_esm/bootstrap.js @@ -0,0 +1,5 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +function hello() { + console.log("Hello from extension!"); +} +globalThis.Extension = { hello }; diff --git a/runtime/examples/extension_with_esm/main.js b/runtime/examples/extension_with_esm/main.js new file mode 100644 index 000000000..5b079d8d8 --- /dev/null +++ b/runtime/examples/extension_with_esm/main.js @@ -0,0 +1,4 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +console.log("Hello world!"); +console.log(Deno); +Extension.hello(); diff --git a/runtime/examples/extension_with_esm/main.rs b/runtime/examples/extension_with_esm/main.rs new file mode 100644 index 000000000..6b21460a3 --- /dev/null +++ b/runtime/examples/extension_with_esm/main.rs @@ -0,0 +1,36 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use std::path::Path; +use std::rc::Rc; + +use deno_core::error::AnyError; +use deno_core::FsModuleLoader; +use deno_core::ModuleSpecifier; +use deno_runtime::permissions::PermissionsContainer; +use deno_runtime::worker::MainWorker; +use deno_runtime::worker::WorkerOptions; + +deno_core::extension!( + hello_runtime, + esm_entry_point = "ext:hello_runtime/bootstrap.js", + esm = ["bootstrap.js"] +); + +#[tokio::main] +async fn main() -> Result<(), AnyError> { + let js_path = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("examples/extension_with_esm/main.js"); + let main_module = ModuleSpecifier::from_file_path(js_path).unwrap(); + let mut worker = MainWorker::bootstrap_from_options( + main_module.clone(), + PermissionsContainer::allow_all(), + WorkerOptions { + module_loader: Rc::new(FsModuleLoader), + extensions: vec![hello_runtime::init_ops_and_esm()], + ..Default::default() + }, + ); + worker.execute_main_module(&main_module).await?; + worker.run_event_loop(false).await?; + Ok(()) +} diff --git a/runtime/examples/extension_with_ops/main.js b/runtime/examples/extension_with_ops/main.js new file mode 100644 index 000000000..042573c08 --- /dev/null +++ b/runtime/examples/extension_with_ops/main.js @@ -0,0 +1,2 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +Deno[Deno.internal].core.ops.op_hello("World"); diff --git a/runtime/examples/extension_with_ops/main.rs b/runtime/examples/extension_with_ops/main.rs new file mode 100644 index 000000000..47feaeaeb --- /dev/null +++ b/runtime/examples/extension_with_ops/main.rs @@ -0,0 +1,44 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use std::path::Path; +use std::rc::Rc; + +use deno_core::error::AnyError; +use deno_core::op; +use deno_core::FsModuleLoader; +use deno_core::ModuleSpecifier; +use deno_runtime::permissions::PermissionsContainer; +use deno_runtime::worker::MainWorker; +use deno_runtime::worker::WorkerOptions; + +deno_core::extension!( + hello_runtime, + ops = [op_hello], + customizer = |ext: &mut deno_core::ExtensionBuilder| { + ext.force_op_registration(); + }, +); + +#[op] +fn op_hello(text: &str) { + println!("Hello {}!", text); +} + +#[tokio::main] +async fn main() -> Result<(), AnyError> { + let js_path = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("examples/extension_with_ops/main.js"); + let main_module = ModuleSpecifier::from_file_path(js_path).unwrap(); + let mut worker = MainWorker::bootstrap_from_options( + main_module.clone(), + PermissionsContainer::allow_all(), + WorkerOptions { + module_loader: Rc::new(FsModuleLoader), + extensions: vec![hello_runtime::init_ops()], + ..Default::default() + }, + ); + worker.execute_main_module(&main_module).await?; + worker.run_event_loop(false).await?; + Ok(()) +} diff --git a/runtime/examples/hello_runtime.js b/runtime/examples/hello_runtime.js deleted file mode 100644 index 5b079d8d8..000000000 --- a/runtime/examples/hello_runtime.js +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -console.log("Hello world!"); -console.log(Deno); -Extension.hello(); diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs deleted file mode 100644 index d4e39dd2d..000000000 --- a/runtime/examples/hello_runtime.rs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. - -use deno_core::error::AnyError; -use deno_core::FsModuleLoader; -use deno_core::ModuleSpecifier; -use deno_runtime::permissions::PermissionsContainer; -use deno_runtime::worker::MainWorker; -use deno_runtime::worker::WorkerOptions; -use std::path::Path; -use std::rc::Rc; - -deno_core::extension!( - hello_runtime, - esm_entry_point = "ext:hello_runtime/hello_runtime_bootstrap.js", - esm = ["hello_runtime_bootstrap.js"] -); - -#[tokio::main] -async fn main() -> Result<(), AnyError> { - let js_path = - Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js"); - let main_module = ModuleSpecifier::from_file_path(js_path).unwrap(); - let mut worker = MainWorker::bootstrap_from_options( - main_module.clone(), - PermissionsContainer::allow_all(), - WorkerOptions { - module_loader: Rc::new(FsModuleLoader), - extensions: vec![hello_runtime::init_ops_and_esm()], - ..Default::default() - }, - ); - worker.execute_main_module(&main_module).await?; - worker.run_event_loop(false).await?; - Ok(()) -} diff --git a/runtime/examples/hello_runtime_bootstrap.js b/runtime/examples/hello_runtime_bootstrap.js deleted file mode 100644 index 759dde939..000000000 --- a/runtime/examples/hello_runtime_bootstrap.js +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -function hello() { - console.log("Hello from extension!"); -} -globalThis.Extension = { hello }; -- cgit v1.2.3