-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into error-rework
# Conflicts: # core/modules/map.rs
- Loading branch information
Showing
41 changed files
with
672 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
# Note: Since Cargo "example" targets don't discover/use `build.rs` files, this | ||
# example is a member of the root `deno_core` workspace. That means it will | ||
# compile with `cargo build` in the root, so that this example/documentation | ||
# stays in-sync with development. | ||
|
||
[package] | ||
name = "build-your-own-js-snapshot" | ||
version = "0.1.0" | ||
edition = "2021" | ||
build = "build.rs" | ||
|
||
|
||
[dependencies] | ||
deno_core.workspace = true | ||
tokio.workspace = true | ||
|
||
[build-dependencies] | ||
deno_core.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Snapshot Example | ||
|
||
This example roughly follows the blog post | ||
[Roll Your Own JavaScript Runtime: Part 3][blog] to create a `JsRuntime` with an | ||
embedded startup snapshot. | ||
|
||
That blog post and the two that preceded it were no longer accurate. By | ||
including this example in the repository, it will continually be built, so it | ||
will hopefully stay up-to-date. | ||
|
||
## Running | ||
|
||
The example can be run by changing to the `core/examples/snapshot` directory and | ||
running `cargo run`. | ||
|
||
## Differences | ||
|
||
Differences from those blog posts: | ||
|
||
- The `create_snapshot()` API has changed in various ways. | ||
- New API features for extensions: | ||
- `#[op2]` ([read more][op2]) | ||
- `extension!(...)` macro replaces `Extension::builder()` | ||
- ESM-based extensions. | ||
|
||
Missing features vs. those blog posts: | ||
|
||
- Does not implement [TsModuleLoader], to keep this example more concise. | ||
|
||
[blog]: https://deno.com/blog/roll-your-own-javascript-runtime-pt3#creating-a-snapshot-in-buildrs | ||
[op2]: https://github.com/denoland/deno_core/tree/main/ops/op2#readme | ||
[TsModuleLoader]: https://deno.com/blog/roll-your-own-javascript-runtime-pt2#supporting-typescript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
use deno_core::{ | ||
extension, | ||
snapshot::{create_snapshot, CreateSnapshotOptions}, | ||
}; | ||
use std::path::PathBuf; | ||
use std::{env, fs}; | ||
|
||
fn main() { | ||
extension!( | ||
runjs_extension, | ||
// Must specify an entrypoint so that our module gets loaded while snapshotting: | ||
esm_entry_point = "my:runtime", | ||
esm = [ | ||
dir "src", | ||
"my:runtime" = "runtime.js", | ||
], | ||
); | ||
|
||
let options = CreateSnapshotOptions { | ||
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"), | ||
startup_snapshot: None, | ||
extensions: vec![runjs_extension::init_ops_and_esm()], | ||
with_runtime_cb: None, | ||
skip_op_registration: false, | ||
extension_transpiler: None, | ||
}; | ||
let warmup_script = None; | ||
|
||
let snapshot = | ||
create_snapshot(options, warmup_script).expect("Error creating snapshot"); | ||
|
||
// Save the snapshot for use by our source code: | ||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
let file_path = out_dir.join("RUNJS_SNAPSHOT.bin"); | ||
fs::write(file_path, snapshot.output).expect("Failed to write snapshot"); | ||
|
||
// Let cargo know that builds depend on these files: | ||
for path in snapshot.files_loaded_during_snapshot { | ||
println!("cargo:rerun-if-changed={}", path.display()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
// Run this script with `cargo run`. | ||
|
||
import { callRust } from "my:runtime"; | ||
|
||
callRust("Hello from example.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
use std::{env::current_dir, rc::Rc}; | ||
|
||
use deno_core::{ | ||
error::AnyError, extension, op2, FsModuleLoader, JsRuntime, | ||
PollEventLoopOptions, RuntimeOptions, | ||
}; | ||
|
||
fn main() { | ||
let runtime = tokio::runtime::Builder::new_current_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap(); | ||
if let Err(error) = runtime.block_on(run_js("./example.js")) { | ||
eprintln!("error: {}", error); | ||
} | ||
} | ||
|
||
#[op2(fast)] | ||
fn op_call_rust(#[string] value: String) { | ||
println!("Received this value from JS: {value}"); | ||
} | ||
|
||
extension!(runjs_extension, ops = [op_call_rust,],); | ||
|
||
async fn run_js(file_path: &str) -> Result<(), AnyError> { | ||
let cwd = current_dir()?; | ||
let main_module = deno_core::resolve_path(file_path, &cwd)?; | ||
|
||
let mut js_runtime = JsRuntime::new(RuntimeOptions { | ||
module_loader: Some(Rc::new(FsModuleLoader)), | ||
startup_snapshot: Some(RUNTIME_SNAPSHOT), | ||
extensions: vec![runjs_extension::init_ops()], | ||
..Default::default() | ||
}); | ||
|
||
let mod_id = js_runtime.load_main_es_module(&main_module).await?; | ||
let result = js_runtime.mod_evaluate(mod_id); | ||
js_runtime | ||
.run_event_loop(PollEventLoopOptions::default()) | ||
.await?; | ||
result.await | ||
} | ||
|
||
// Load the snapshot generated by build.rs: | ||
static RUNTIME_SNAPSHOT: &[u8] = | ||
include_bytes!(concat!(env!("OUT_DIR"), "/RUNJS_SNAPSHOT.bin")); |
Oops, something went wrong.