https://github.com/duyet/deno-runner-rs
Run Javascript inside Rust
https://github.com/duyet/deno-runner-rs
Last synced: 8 months ago
JSON representation
Run Javascript inside Rust
- Host: GitHub
- URL: https://github.com/duyet/deno-runner-rs
- Owner: duyet
- License: mit
- Created: 2023-12-10T06:57:16.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-04-11T20:32:02.000Z (about 1 year ago)
- Last Synced: 2025-04-11T21:35:30.538Z (about 1 year ago)
- Language: Rust
- Homepage:
- Size: 37.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Javascript runner in Rust
[](https://github.com/fossil-engineering/deno-runner-rs/actions/workflows/cargo-test.yaml)
[](https://github.com/fossil-engineering/deno-runner-rs/actions/workflows/cargo-doc.yaml)
[](https://github.com/fossil-engineering/deno-runner-rs/actions/workflows/cargo-clippy.yml)
[](https://codecov.io/gh/fossil-engineering/deno-runner-rs)
This runner using [deno_core](https://crates.io/crates/deno_core) with additional helper function to run Javascript inside Rust.
You can also binding the Rust functions into Javascript.
# Usage
Add this crate to your Cargo.toml
```toml
[dependencies]
deno_runner = { git = "https://github.com/fossil-engineering/deno-runner-rs" }
```
## Example
Basic usage
```rust
use deno_runner::Builder;
use std::collections::HashMap;
#[tokio::main]
async fn main() {
let code = r#"
const add = (a, b) => a + b;
add(a, b)
"#;
let runner = Builder::new().build();
let vars = HashMap::from([("a", 1), ("b", 2)]);
let result = runner.run(code, Some(vars)).await.unwrap();
assert_eq!(result, "3");
}
```
Calling Rust functions from Javascript
```rust
use deno_runner::{anyhow::Result, op, Builder};
use std::collections::HashMap;
#[op]
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[tokio::main]
async fn main() {
let code = "add(a, b)";
let runner = Builder::new().add_op(add::decl()).build();
let vars = HashMap::from([("a", 1), ("b", 2)]);
let result = runner.run(code, Some(vars)).await.unwrap();
assert_eq!(result, "3");
}
```
# License
MIT