summaryrefslogtreecommitdiff
path: root/runtime/examples
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2023-05-04 01:44:59 +0100
committerGitHub <noreply@github.com>2023-05-04 02:44:59 +0200
commit7a8bb3b611f02b272b1c19b6f3d8a85b099ca317 (patch)
tree04d37bb0f8c3fc719b3494114a054f5439733e7a /runtime/examples
parente3276fbb71093faf4e8850f68ed2e080a9bda222 (diff)
fix(core): allow esm extensions not included in snapshot (#18980)
Fixes #18979. This changes the predicate for allowing `ext:` specifier resolution from `snapshot_loaded_and_not_snapshotting` to `ext_resolution_allowed` which is only set to true during the extension module loading phase. Module loaders as used in core are now declared as `ExtModuleLoader` rather than `dyn ModuleLoader`.
Diffstat (limited to 'runtime/examples')
-rw-r--r--runtime/examples/hello_runtime.js1
-rw-r--r--runtime/examples/hello_runtime.rs57
-rw-r--r--runtime/examples/hello_runtime_bootstrap.js5
3 files changed, 14 insertions, 49 deletions
diff --git a/runtime/examples/hello_runtime.js b/runtime/examples/hello_runtime.js
index 066fa21d6..5b079d8d8 100644
--- a/runtime/examples/hello_runtime.js
+++ b/runtime/examples/hello_runtime.js
@@ -1,3 +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/hello_runtime.rs b/runtime/examples/hello_runtime.rs
index 2e930a03f..157a200f4 100644
--- a/runtime/examples/hello_runtime.rs
+++ b/runtime/examples/hello_runtime.rs
@@ -1,72 +1,31 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::FsModuleLoader;
-use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
-use deno_runtime::deno_web::BlobStore;
use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;
-use deno_runtime::BootstrapOptions;
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")
-}
+deno_core::extension!(hello_runtime, esm = ["hello_runtime_bootstrap.js"]);
#[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 web_worker_event_cb = Arc::new(|_| {
- todo!("Web workers are not supported in the example");
- });
-
- let options = WorkerOptions {
- bootstrap: BootstrapOptions::default(),
- extensions: vec![],
- startup_snapshot: None,
- unsafely_ignore_certificate_errors: None,
- root_cert_store_provider: None,
- seed: None,
- source_map_getter: None,
- format_js_error_fn: None,
- web_worker_preload_module_cb: web_worker_event_cb.clone(),
- web_worker_pre_execute_module_cb: web_worker_event_cb,
- create_web_worker_cb,
- maybe_inspector_server: None,
- should_break_on_first_statement: false,
- should_wait_for_inspector_session: false,
- module_loader,
- node_fs: None,
- npm_resolver: None,
- get_error_class_fn: Some(&get_error_class_name),
- cache_storage_dir: None,
- origin_storage_dir: None,
- blob_store: BlobStore::default(),
- broadcast_channel: InMemoryBroadcastChannel::default(),
- shared_array_buffer_store: None,
- compiled_wasm_module_store: None,
- stdio: Default::default(),
- };
-
let js_path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js");
let main_module = deno_core::resolve_path(
&js_path.to_string_lossy(),
- &std::env::current_dir().context("Unable to get CWD")?,
+ &std::env::current_dir()?,
)?;
- let permissions = PermissionsContainer::allow_all();
-
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
- permissions,
- options,
+ 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?;
diff --git a/runtime/examples/hello_runtime_bootstrap.js b/runtime/examples/hello_runtime_bootstrap.js
new file mode 100644
index 000000000..759dde939
--- /dev/null
+++ b/runtime/examples/hello_runtime_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 };