Rust run JS/TS
How can I embed this project into a rust application so that rust can run js ts and has runtime api For example fetch
4 Replies
use deno_core can run js but can't run ts, and no runtime api
To embed a JavaScript/TypeScript runtime into a Rust application, you can use the
deno_core
crate. This crate allows you to create a JavaScript runtime in your Rust application. Here's a step-by-step guide based on the documentation:
1. Add Dependencies: First, you need to add the deno_core
and tokio
dependencies to your project. You can do this by running the following commands:
2. Create a JavaScript Runtime: Next, you need to create an instance of JsRuntime
, which is responsible for JavaScript execution. You can do this in an asynchronous Rust function. Here's an example:
3. Add a Fetch API: To add a fetch
API, you can create a runtime.js
file that will instantiate and make the fetch
object globally available. Here's an example:
4. **Register the FetchAPI**: You need to register the
fetch
API in your run_js
function. Here's an example:
```rust
let runjs_extension = Extension::builder("runjs")
.ops(vec![
op_read_file::decl(),
op_write_file::declhow like deno cli The same supports all runtime api Instead of doing it manually