├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── build.rs ├── examples └── run-script.rs └── src ├── lib.rs └── wrapper.h /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | 4 | 5 | #Added by cargo 6 | # 7 | #already existing elements are commented out 8 | 9 | #/target 10 | #**/*.rs.bk 11 | Cargo.lock 12 | .DS_Store 13 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "quickjs-rs" 3 | version = "0.1.0" 4 | authors = ["llgoer "] 5 | edition = "2018" 6 | repository = "https://github.com/quickjs-zh/quickjs-rs" 7 | homepage = "https://bellard.org/quickjs/" 8 | documentation = "https://github.com/quickjs-zh/QuickJS" 9 | description = "Rust bindings to Fabrice Bellards QuickJS Javascript Engine" 10 | 11 | [dependencies] 12 | 13 | [build-dependencies] 14 | bindgen = "0.50" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 QuickJS中文 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # quickjs-rs 2 | 3 | Rust bindings to QuickJS 4 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | extern crate bindgen; 2 | 3 | use std::env; 4 | use std::path::PathBuf; 5 | use std::process::Command; 6 | 7 | fn main() { 8 | if cfg!(target_os = "linux") || cfg!(target_os = "macos") { 9 | Command::new("make") 10 | .current_dir("QuickJS") 11 | .status() 12 | .expect("failed to make!"); 13 | } else { 14 | unimplemented!() 15 | } 16 | 17 | println!("cargo:rustc-link-search=QuickJS"); 18 | println!("cargo:rustc-link-lib=quickjs"); 19 | 20 | let bindings = bindgen::Builder::default() 21 | .header("./src/wrapper.h") 22 | .clang_arg("-IQuickJS") 23 | .generate() 24 | .expect("Unable to generate bindings"); 25 | 26 | // 文件写入到$OUT_DIR/bindings.rs 27 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 28 | bindings 29 | .write_to_file(out_path.join("bindings.rs")) 30 | .expect("Couldn't write bindings!"); 31 | } -------------------------------------------------------------------------------- /examples/run-script.rs: -------------------------------------------------------------------------------- 1 | use std::convert::TryInto; 2 | 3 | // examples/run-script.rs 4 | extern crate quickjs_rs; 5 | 6 | use quickjs_rs::*; 7 | 8 | fn main() { 9 | unsafe { 10 | let rt = JS_NewRuntime(); 11 | let ctx = JS_NewContext(rt); 12 | let jsmodnfunc:JSModuleNormalizeFunc = None; 13 | const NULL:* mut std::os::raw::c_void = 0 as *mut std::os::raw::c_void; 14 | 15 | // 加载ES6模块 16 | JS_SetModuleLoaderFunc(rt, jsmodnfunc, Some(js_module_loader), NULL); 17 | 18 | // 加载标准模块 19 | js_init_module_std(ctx, "std".as_ptr() as *const i8); 20 | js_init_module_os(ctx, "os".as_ptr() as *const i8); 21 | 22 | // 这里尝试执行一行简单的数字相加的代码 23 | let code = "8+7;"; 24 | 25 | // 获取到JSValue结构 26 | let val = JS_Eval(ctx, code.as_ptr() as *const i8, code.len(), "".as_ptr() as *const i8, JS_EVAL_FLAG_SHEBANG.try_into().unwrap()); 27 | let i = JS_IsNumber(val); 28 | if i > 0 { 29 | println!("is number:{}", i); 30 | println!("number is :{}", val.u.int32); 31 | } 32 | js_std_loop(ctx); 33 | 34 | // 释放相关资源 35 | js_std_free_handlers(rt); 36 | JS_FreeContext(ctx); 37 | JS_FreeRuntime(rt); 38 | } 39 | 40 | println!("Hello from an quickjs_rs example!"); 41 | } -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_upper_case_globals)] 2 | #![allow(non_camel_case_types)] 3 | #![allow(non_snake_case)] 4 | 5 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 6 | 7 | #[cfg(test)] 8 | mod tests { 9 | #[test] 10 | fn it_works() { 11 | assert_eq!(2 + 2, 4); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/wrapper.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include --------------------------------------------------------------------------------