Skip to content

fossil-engineering/deno-runner-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Javascript runner in Rust

cargo-test cargo-doc cargo-clippy codecov

This runner using 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

[dependencies]
deno_runner = { git = "https://github.com/fossil-engineering/deno-runner-rs" }

Example

Basic usage

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

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