[−][src]Crate qjs
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 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");
Re-exports
pub use qjs_sys as ffi; |
Macros
| qjs |
Structs
| ArrayBuffer |
|
| Context |
|
| ContextBuilder | |
| ContextRef | A borrowed reference to a |
| Eval | Flags for |
| EvalBinary | Flags for |
| LONG_VERSION | |
| Local | |
| MallocFunctions | |
| MemoryUsage | |
| Prop | Flags for property |
| PropertyDescriptor | A property descriptor is a record with some of the following attributes: |
| PropertyNames | Flags for |
| ReadObj | |
| Runtime |
|
| RuntimeRef | A borrowed reference to a |
| SharedArrayBuffer |
|
| Value |
|
| WriteObj |
Enums
| CFunc | C function definition |
| ErrorKind | Javascript error. |
| Interrupt | Interrupt the execution code. |
Constants
| VERSION |
Traits
| Args | |
| Bindable | |
| DefinePropertyGetSet | |
| DefinePropertyValue | |
| DeleteProperty | Delete a property on an object. |
| ExtractValue | Extract primitive from |
| GetProperty | Get a property value on an object. |
| HasProperty | Check if a property on an object. |
| NewAtom | Create or find an |
| NewValue | Create new |
| SetProperty | Set a property value on an object. |
| Source | Script source. |
| Unbindable |
Functions
| eval | Evaluate a script or module source. |
Type Definitions
| Atom | Object property names and some strings are stored as Atoms (unique strings) to save memory and allow fast comparison. |
| CFunction |
|
| ClassDef | The Javascript class definition. |
| ClassId | A globally allocated class ID. |
| InterruptHandler | |
| JobFunc | |
| ModuleDef | The C module definition. |
| ModuleInitFunc | The C module init function. |
| ModuleLoaderFunc | The module loader function. |
| ModuleNormalizeFunc | The filename normalizer function. |
| UnsafeCFunction | Unsafe C function |
| UnsafeCFunctionData | Unsafe C function with data |
| UnsafeCFunctionMagic | Unsafe C function with magic |