1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! `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 _ = pretty_env_logger::try_init();
//!
//! 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");
//! ```
#[macro_use]
extern crate log;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate foreign_types;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate cstr;

pub use qjs_sys as ffi;

use proc_macro_hack::proc_macro_hack;
#[proc_macro_hack]
pub use qjs_derive::qjs;

#[macro_use]
mod macros;
mod arraybuf;
mod atom;
mod cfunc;
mod class;
mod context;
mod error;
mod eval;
mod func;
mod handle;
mod job;
mod module;
mod precompile;
mod prop;
mod runtime;
#[cfg(feature = "stdlib")]
mod stdlib;
mod userdata;
mod value;

pub use arraybuf::{ArrayBuffer, SharedArrayBuffer};
pub use atom::{Atom, NewAtom};
pub use cfunc::{CFunc, CFunction, UnsafeCFunction, UnsafeCFunctionData, UnsafeCFunctionMagic};
pub use class::{ClassDef, ClassId};
pub use context::{Builder as ContextBuilder, Context, ContextRef};
pub use error::ErrorKind;
pub use eval::{eval, Eval, EvalBinary, Source};
pub use func::Args;
pub use handle::{Bindable, Local, Unbindable};
pub use job::JobFunc;
pub use module::{ModuleDef, ModuleInitFunc, ModuleLoaderFunc, ModuleNormalizeFunc};
pub use precompile::{ReadObj, WriteObj};
pub use prop::{
    DefinePropertyGetSet, DefinePropertyValue, DeleteProperty, Descriptor as PropertyDescriptor,
    GetProperty, HasProperty, Names as PropertyNames, Prop, SetProperty,
};
pub use runtime::{Interrupt, InterruptHandler, MallocFunctions, MemoryUsage, Runtime, RuntimeRef};
pub use value::{ExtractValue, NewValue, Value};

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

lazy_static! {
    pub static ref LONG_VERSION: String = format!(
        "{} (quickjs {}{})",
        VERSION,
        ffi::VERSION.trim(),
        if cfg!(feature = "bignum") {
            " with BigNum"
        } else {
            ""
        },
    );
}