[][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

ArrayBuffer represent a generic, fixed-length raw binary data buffer.

Context

Context represents a Javascript context (or Realm).

ContextBuilder
ContextRef

A borrowed reference to a Context.

Eval

Flags for eval method.

EvalBinary

Flags for eval_binary method.

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 get_own_property_names

ReadObj
Runtime

Runtime represents a Javascript runtime corresponding to an object heap.

RuntimeRef

A borrowed reference to a Runtime.

SharedArrayBuffer

SharedArrayBuffer represent a generic, fixed-length raw binary data buffer, similar to the ArrayBuffer object, but in a way that they can be used to create views on shared memory.

Value

Value represents a Javascript value which can be a primitive type or an object.

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 Local<Value>.

GetProperty

Get a property value on an object.

HasProperty

Check if a property on an object.

NewAtom

Create or find an Atom base on &str, *const c_char or u32.

NewValue

Create new Value from primitive.

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

CFunction is a shortcut to easily add functions, setters and getters properties to a given object.

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