libquickjs_sys/
lib.rs

1//! FFI Bindings for [quickjs](https://bellard.org/quickjs/),
2//! a Javascript engine.
3//! See the [quickjs](https://crates.io/crates/quickjs) 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// import the functions from static-functions.c
13
14include!("static-functions.rs");
15
16#[cfg(test)]
17mod tests {
18    use std::ffi::CStr;
19
20    use super::*;
21
22    // Small sanity test that starts the runtime and evaluates code.
23    #[test]
24    fn test_eval() {
25        unsafe {
26            let rt = JS_NewRuntime();
27            let ctx = JS_NewContext(rt);
28
29            let code_str = "1 + 1\0";
30            let code = CStr::from_bytes_with_nul(code_str.as_bytes()).unwrap();
31            let script = CStr::from_bytes_with_nul("script\0".as_bytes()).unwrap();
32
33            let value = JS_Eval(
34                ctx,
35                code.as_ptr(),
36                (code_str.len() - 1) as u64,
37                script.as_ptr(),
38                JS_EVAL_TYPE_GLOBAL as i32,
39            );
40            assert_eq!(value.tag, 0);
41            assert_eq!(value.u.int32, 2);
42
43            JS_DupValue(ctx, value);
44            JS_FreeValue(ctx, value);
45
46            let ival = JS_NewInt32(ctx, 12);
47            assert_eq!(ival.tag, 0);
48            let fval = JS_NewFloat64(ctx, f64::MAX);
49            assert_eq!(fval.tag, 7);
50            let bval = JS_NewBool(ctx, true);
51            assert_eq!(bval.tag, 1);
52        }
53    }
54}