D
Deno

help

Updated examples for "Roll your own JavaScript runtime" blog posts?

AAmi12/28/2023
Hi all. I'd like to embed Deno in a project and I discovered the excellent series of "Roll your own JavaScript" posts on the Deno blog. Unfortunately, the examples don't work with current Deno because "op" has been replaced by "op2". I know updating the blogs is likely to take a while, but is it possible for somebody to post updated example code, or point to some example code using op2 that does something similar to what's the blog posts? I've tried to find examples in various places, including in the Deno code itself, but I really need a complete but simple example in order to understand what's going on.
DAHDeno AI Helper12/29/2023
Hello! I understand you're looking for updated examples that align with the changes in Deno's runtime, specifically regarding the transition from op to op2. While the blog posts provide a great starting point, they might not always reflect the most recent changes in the Deno codebase. The documentation does mention a series of blog posts about creating your own JavaScript runtime, including a part 3 that discusses optimizing startup time by creating and loading a snapshot of a custom JavaScript runtime. However, it doesn't provide specific code examples for op2. For a complete and simple example using op2, I would recommend looking at the more recent examples in the Deno codebase, as you've mentioned. The Deno runtime is actively developed, and the source code is often the most up-to-date resource for understanding how to use new features. If you're looking for a specific example and can't find one in the Deno codebase, you might want to check out the Deno examples repository or the manual for guidance on the new APIs. The Deno community is also a great resource, and you can often find help and examples by reaching out on platforms like the Deno Discord channel, forums, or GitHub discussions. Remember that while the blog posts are informative, they might not be updated as frequently as the actual codebase, so the Deno repository and official examples are your best bet for the latest patterns and practices. 🦕
Ggio12/29/2023
Hi! I'm in the same boat as you but I'm on part 3 trying to figure out snapshotting. I was able to finish part 1 by looking at example ops over at https://docs.rs/crate/deno_url/latest/source/lib.rs
#[op2(async)]
#[string]
pub async fn op_read_file(#[string] path: String) -> Result<String, Error> {
let contents = tokio::fs::read_to_string(path).await?;
Ok(contents)
}

#[op2(async)]
pub async fn op_write_file(
#[string] path: String,
#[string] contents: String,
) -> Result<(), Error> {
tokio::fs::write(path, contents).await?;
Ok(())
}

#[op2(fast)]
pub fn op_remove_file(#[string] path: String) -> Result<(), Error> {
std::fs::remove_file(path)?;
Ok(())
}
#[op2(async)]
#[string]
pub async fn op_read_file(#[string] path: String) -> Result<String, Error> {
let contents = tokio::fs::read_to_string(path).await?;
Ok(contents)
}

#[op2(async)]
pub async fn op_write_file(
#[string] path: String,
#[string] contents: String,
) -> Result<(), Error> {
tokio::fs::write(path, contents).await?;
Ok(())
}

#[op2(fast)]
pub fn op_remove_file(#[string] path: String) -> Result<(), Error> {
std::fs::remove_file(path)?;
Ok(())
}
and
let runjs_extension = Extension {
ops: vec![
op_read_file::DECL,
op_write_file::DECL,
op_remove_file::DECL,
op_fetch::DECL,
]
.into(),
..Default::default()
};
let runjs_extension = Extension {
ops: vec![
op_read_file::DECL,
op_write_file::DECL,
op_remove_file::DECL,
op_fetch::DECL,
]
.into(),
..Default::default()
};
AAmi12/29/2023
Cool. In fact, after writing this question, I eventually stumbled upon the github repo with updated code from the blog. You can find it here: https://github.com/denoland/roll-your-own-javascript-runtime/tree/main
GitHub
GitHub - denoland/roll-your-own-javascript-runtime
Contribute to denoland/roll-your-own-javascript-runtime development by creating an account on GitHub.
AAmi12/29/2023
There are still some deprecated functions in that code, but it builds and appears to work. Now I'm trying to figure out how to pass Javascript Objects back and forth. @Deno AI Helper Do you know how to pass Javascript Objects (v8::Object) between Rust and Javascript?
DAHDeno AI Helper12/29/2023
Passing JavaScript objects between Rust and JavaScript in a Deno plugin involves serializing and deserializing the objects. When you're working with the V8 engine (which Deno uses internally), you typically deal with v8::Local<v8::Value> for JavaScript values. In the context of Deno's FFI (Foreign Function Interface), you would use the serde_v8 crate to serialize and deserialize values between Rust and JavaScript. This crate allows you to convert between V8 objects and Rust structs or enums that implement the serde::Serialize and serde::Deserialize traits. Here's a high-level overview of how you might pass objects between Rust and JavaScript: 1. From JavaScript to Rust: - JavaScript objects are serialized into a format that Rust can understand (typically using JSON). - The serialized object is passed to Rust through an FFI boundary. - Rust deserializes the object into a Rust struct or enum using serde_v8. 2. From Rust to JavaScript: - Rust structs or enums are serialized into a format that V8 can understand. - The serialized data is passed back to JavaScript through the FFI boundary. - JavaScript deserializes the data back into a JavaScript object. Please note that the advice provided here is not based on the Deno documentation, and you should double-check the accuracy of this advice. The Deno documentation and examples would be the best source of truth for the most current and idiomatic ways to pass objects between Rust and JavaScript in Deno. If you're looking for more detailed guidance or examples, I would recommend exploring the Deno manual, the serde_v8 crate documentation, or reaching out to the Deno community for more specific use cases.
AAmi12/29/2023
Hmm. Serializing to/from JSON seems like a terrible way to do it. Probably good enough for my application, but surely there must be a better way.

Looking for more? Join the community!

Recommended Posts
Executing module multiple timesI have a script that can be imported using `await import()` statement. what goes inside brackets is Queues in production not workinghere's the main queue code , works locally ```ts import { getOneRepoPackageJson } from "./getOneRepoRemote KV access error : Missing DENO_KV_ACCESS_TOKEN environment variablei wen through the recomnded steps of adding a .env to the root of the project and adding ```sh DENODeno.serve() with SSL cert timeoutI have a very simple script that serves a `/ping` route. After some time (randomly) it stalls and noList kv items using a partial keyHey guys am trying to figure out kv and how to get stuffout of it how do i get iteme that have theIs there a recommended library to do dependency injection in Deno?I'm creating a Rest API with the repository and controllers pattern but my code in the `main.ts` filReact (CRA) deploy via denoGetting an error while building react CRA via deno deploy. Error: ENOENT: no such file or directoryCustom executable icon for `deno compile`?Is it possible to set a different icon rather than the default deno icon for the executable generateDeno.Command output as it runsHello, I am trying to run a command and have its output be logged as it runs , but for some reason telectrons using denoIn the recent deno version I see it is possible to runn executable in ./bin in node_modules using Cannot find moduleHi <:vmathi:812484657547313212> I'm trying to run a project with Deno, but i'm getting "Cannot find Uncaught error during deno deploy of vite appWhen creating a new vite app with vue and typescript I get this error message. This error is reprodDeno, TypeScript, ESBuild, WebGL, VSCodeHello! As the title suggests, I am using these technologies to create a web application: * Deno * TPossible issue with re-export compatibility with NodeIn https://github.com/japa/runner/issues/35, I have described an issue where Deno seems to be incompMake deno.serve handler asynchronousHow can i make the Deno.serve handler asynchronous? or like make it so that it can execute asynchronWhy my place.x -= 5 not working?At this part if(moveKey.right.pressed) { finishPostion += 5 quota exceededi hit the free limit tonight and upgraded to pro 20 minutes ago— when do my sites stop erroring withDeploying WASM via Deno Deploy Github ActionsHello, I'm trying to deploy a Leptos project that serves a .wasm bundle for the frontend, and uses