3 releases

0.1.2 Aug 27, 2019
0.1.1 Aug 16, 2019
0.1.0 Jul 26, 2019

#22 in #quickjs


Used in qjs-derive

MIT license

1MB
6K SLoC

qjs travis Build status crate docs dependency status

qjs is an experimental Rust binding for the QuickJS Javascript Engine

Usage

To use qjs in your project, add the following to your Cargo.toml:

[dependencies]
qjs = "0.1"

Example

let v: Option<i32> = qjs::eval("1+2").unwrap();

assert_eq!(v, Some(3));

lib.rs:

qjs is an experimental Rust binding for the QuickJS Javascript Engine

Examples

qjs macro can evalute the Javascript code in an anonymouse context.

use qjs::qjs;

let v: i32 = qjs!(1+2).unwrap().unwrap();

assert_eq!(v, 3);

qjs macro can also convert a Javascript closure to a rust function.

use qjs::qjs;

let f = qjs!{ (name: &str) -> String => { return "hello " + name; } };
let s: String = f("world").unwrap().unwrap();

assert_eq!(s, "hello world");

Variable interpolation is done with #var (similar to $var in macro_rules! macros). This grabs the var variable that is currently in scope and inserts it in that location in the output tokens.

use qjs::qjs;


let f = |name| qjs!{ "hello " + #name };
let s: String = f("world").unwrap().unwrap();

assert_eq!(s, "hello world");

The primitive types, including bool, i32, i64, u64, f64, String etc, and other type which implements NewValue trait could be used in the variable interpolation.

The function which parameters implements ExtractValue trait and output type implements NewValue trait can also be used in the variable interpolation.

use qjs::qjs;

fn hello(name: String) -> String {
    format!("hello {}", name)
}

let hello: fn(String) -> String = hello;
//let s: String = qjs!{ #hello ("world") }.unwrap().unwrap();

// assert_eq!(s, "hello world");

Dependencies

~3–11MB
~121K SLoC