libquickjs_ng_sys/
lib.rs

1//! FFI Bindings for [quickjs-ng](https://github.com/quickjs-ng/quickjs),
2//! a Javascript engine.
3//! See the [quickjs-rusty](https://crates.io/crates/quickjs-rusty) crate for a high-level
4//! wrapper.
5
6#![allow(non_upper_case_globals)]
7#![allow(non_camel_case_types)]
8#![allow(non_snake_case)]
9
10include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
11
12#[cfg(test)]
13mod tests {
14    use std::ffi::CStr;
15
16    use super::*;
17
18    // Small sanity test that starts the runtime and evaluates code.
19    #[test]
20    fn test_eval() {
21        unsafe {
22            let rt = JS_NewRuntime();
23            let ctx = JS_NewContext(rt);
24
25            let code_str = "1 + 1\0";
26            let code = CStr::from_bytes_with_nul(code_str.as_bytes()).unwrap();
27            let script = CStr::from_bytes_with_nul("script\0".as_bytes()).unwrap();
28
29            let value = JS_Eval(
30                ctx,
31                code.as_ptr(),
32                code_str.len() - 1,
33                script.as_ptr(),
34                JS_EVAL_TYPE_GLOBAL as i32,
35            );
36            assert_eq!(JS_Ext_ValueGetTag(value), JS_TAG_INT);
37            assert_eq!(JS_Ext_GetInt(value), 2);
38
39            JS_DupValue(ctx, value);
40            JS_FreeValue(ctx, value);
41
42            let ival = JS_Ext_NewInt32(ctx, 12);
43            assert_eq!(JS_Ext_ValueGetTag(ival), JS_TAG_INT);
44            let fval = JS_Ext_NewFloat64(ctx, f64::MAX);
45            assert_eq!(JS_Ext_ValueGetTag(fval), JS_TAG_FLOAT64);
46            let bval = JS_Ext_NewBool(ctx, 1);
47            assert_eq!(JS_Ext_ValueGetTag(bval), JS_TAG_BOOL);
48        }
49    }
50}