Skip to main content

hirofa_quickjs_sys/
lib.rs

1#![allow(improper_ctypes)]
2//! FFI Bindings for [quickjs](https://bellard.org/quickjs/),
3//! a Javascript engine.
4//! See the [quickjs](https://crates.io/crates/quickjs) crate for a high-level
5//! wrapper.
6
7#![allow(non_upper_case_globals)]
8#![allow(non_camel_case_types)]
9#![allow(non_snake_case)]
10
11include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
12
13// import the functions from static-functions.c
14
15include!("static-functions.rs");
16
17#[cfg(test)]
18mod tests {
19    use std::ffi::CStr;
20
21    use super::*;
22
23    // Small sanity test that starts the runtime and evaluates code.
24    #[test]
25    fn test_eval() {
26        unsafe {
27            let rt = JS_NewRuntime();
28            let ctx = JS_NewContext(rt);
29
30            let code_str = "1 + 1\0";
31            let code = CStr::from_bytes_with_nul(code_str.as_bytes()).unwrap();
32            let script = CStr::from_bytes_with_nul("script\0".as_bytes()).unwrap();
33
34            let value = JS_Eval(
35                ctx,
36                code.as_ptr(),
37                (code_str.len() - 1) as usize,
38                script.as_ptr(),
39                JS_EVAL_TYPE_GLOBAL as i32,
40            );
41            assert_eq!(value.tag, 0);
42
43            #[cfg(feature = "bellard")]
44            assert_eq!(value.u.uint64, 2);
45
46            #[cfg(feature = "quickjs-ng")]
47            assert_eq!(value.u.int32, 2);
48
49            JS_DupValue(ctx, value);
50            JS_FreeValue(ctx, value);
51
52            let ival = JS_NewInt32(ctx, 12);
53            assert_eq!(ival.tag, 0);
54            //let fval = JS_NewFloat64(ctx, f64::MAX);
55            //assert_eq!(fval.tag, 7);
56            let bval = JS_NewBool(ctx, true);
57            assert_eq!(bval.tag, 1);
58        }
59    }
60}