Expand description
High-level bindings to quickjs
The rquickjs crate provides safe high-level bindings to the quickjs javascript engine.
This crate is heavily inspired by the rlua crate.
The Runtime and Context objects
The main entry point of this library is the Runtime struct.
It represents the interperter state and is used to create Context
objects. As the quickjs library does not support threading the runtime is locked behind a
mutex. Multiple threads cannot run as script or create objects from the same runtime at the
same time.
The Context object represents a global environment. Contexts of the same runtime
can share javascript objects like in browser between frames of the same origin.
Converting Values
This library has multiple traits for converting to and from javascript.
The IntoJs trait is used for taking rust values and turning them into javascript values.
The FromJs is for converting javascript value to rust.
Note that this trait does not automatic coercion but the Coerced can be used to convert
the values with coercion.
For values which represent the name of variables or indecies the
trait IntoAtom is available to convert values to the represention
quickjs requires.
Optional features
Default
This crate can be customized via features. The following features is enabled by default but can be disabled when not needed:
classesenables support for ES6 classes. Any user-defined Rust type can be exported to JS as an ES6 class which can be derived and extended by JS.propertiesenables support for object properties (Object.defineProperty).exportsadds an ability to read the module exports.
Advanced
The following features may be enabled to get an extra functionality:
allocatoradds support for custom allocators forRuntime. The allocators should implementsAllocatortrait and can be plugged onRuntimecreation viaRuntime::new_with_alloc.rust-allocforces using Rust’s global allocator by default instead of libc’s one.loaderadds support for custom ES6 modules resolvers and loaders. The resolvers and loaders should implementsResolverandLoadertraits respectively and can be plugged in already existingRuntimebefore loading modules viaRuntime::set_loader. The resolvers and loaders can be easily combined via tuples. When the previous resolver or loader failed the next one will be applied.dyn-loadadds support for loadable native modules (so/dll/dylib).array-bufferadds support forArrayBufferandTypedArray.futuresadds support for async Rust. When enabled the Rust futures can be passed to JS as ES6 Promises and ES6 Promises can be given back as Rust futures.tokioadds integration withtokioasync runtime. The methodRuntime::spawn_executorcan be used withTokioto spawn async executor.async-stdadds integration withasync_stdruntime. The methodRuntime::spawn_executorcan be used withAsyncStdto spawn async executor.smoladds integrations withsmolasync runtime. The methodRuntime::spawn_executorcan be used withSmolto spawn async executor.macroenables some useful procedural macros which gets Rust/JS interop much easy. An attribute macros can be applied to functions, constants and modules. An derive macros can be used with structs and enums.phfenables using Perfect Hash Function for builtin modules lookup
Custom
To gets build faster the numbers of arguments which can be passed to and given by the functions is limited to 6. If you need more arguments you can enabled feature max-args-N where N is number from 7 to 16.
Extra types
This crate has support for conversion of many Rust types like Option, Result, Vec and other collections. In addition an extra types support can be enabled via features:
eitheraddsFromJs/IntoJsimplementations forEitherindexmapaddsFromJs/IntoJsimplementations forIndexSetandIndexMap
Bindings
The bindings is pre-built for the following platforms:
i686-unknown-linux-gnu,x86_64-unknown-linux-gnux86_64-apple-darwini686-pc-windows-gnu,x86_64-pc-windows-gnu,i686-pc-windows-msvc,x86_64-pc-windows-msvc
To build the crate for unsupported target you must enable bindgen feature.
Experimental
parallelenables multithreading support.
Note that the experimental features which may not works as expected. Use it for your own risk.
Debugging
QuickJS can be configured to output some info which can help debug. The following features enables that:
dump-bytecodedump-gcdump-gc-freedump-freedump-leaksdump-memdump-objectsdump-atomsdump-shapesdump-module-resolvedump-promisedump-read-object
Modules
Macros
classesThe macro to simplify class definition.
loaderThe helper macro to impl Loader traits for generic module kind.
Helper macro to provide module init function
Structs
propertiesThe accessor descriptor of a readonly property
Rust representation of a javascript object optimized as an array.
array-bufferRust representation of a javascript object of class ArrayBuffer.
futuresThe wrapper for async functons
An atom is value representing the name of a variable of an objects and can be created from any javascript value.
loaderThe builtin script module loader
loaderThe builtin module resolver
The resolver and loader for bundles of compiled modules
classesThe class object interface
classesThe type of identifier of class
The wrapper for values to force coercion
loaderModules compiling data
classesThe wrapper for constructor function
A single execution context with its own global variables and stack.
Used for building a Context with a specific set of intrinsics
The marker for the module which is created but not loaded yet
Context in use, passed to Context::with.
Eval options.
The marker for the module which is already loaded and evaluated
futuresThe async executor future
exportsAn iterator over the items exported out a module
exportsAn iterator over the items exported out a module
loaderThe file module resolver
The property filter
The wrapper for function to convert is into JS
Rust representation of a javascript function.
The idle awaiting future
The marker for the module which is loaded but not evaluated yet
The wrapper for method functions
Javascript module with certain exports and imports
loaderThe builtin native module loader
The wrapper for mutable functions
dyn-loadThe native module loader
The placeholder which treated as null value
Rust representation of a javascript object.
The wrapper for once functions
The wrapper to get optional argument from input
The wrapper for JS values to keep it from GC
futuresFuture-aware promise
futuresWrapper for futures to convert to JS promises
propertiesThe data descriptor of a property
classesThe helper for QuickJS garbage collector which helps it find internal JS object references.
registeryKey for a registery of a context.
The wrapper the rest arguments from input
Quickjs runtime, entry point of the library.
The allocator which uses Rust global allocator
The marker for the module which is created from text source
loaderThe script module loader
Rust representation of a javascript string.
Rust representation of a javascript symbol.
The wrapper to get this from input
array-bufferRust representation of a javascript objects of TypedArray classes.
The placeholder which treated as undefined value
The placeholder which treated as uninitialized JS value
Any javascript value
classesThe prototype setting wrapper
Enums
Traits
allocatorThe allocator interface
A helper trait to pass arguments on a function calls.
The trait to wrap rust function to JS directly
propertiesThe property interface
classesThe ES6 class definition trait
futuresThe trait to spawn execution of pending jobs on async runtime
Trait for converting values from atoms.
The Rust’s FromIterator trait to use with Ctx
For converting javascript values to rust values
The module data which contains bytecode
classesThe helper trait to mark internal JS value refs
Trait for converting values to atoms.
For converting rust values to javascript values
The internal trait to add JS builting
loaderModule loader interface
Module definition trait
A trait for using multiple contexts at the same time.
The helper trait to define objects
The trait to help break lifetime rules when JS objects leaves current context via Persistent wrapper.
loaderModule resolver interface
Type Definitions
The raw module load function (js_module_init)
allocatorRaw memory pointer
Result type used throught the library.
Attribute Macros
An attribute to generate bindings easy
An attribute to convert scripts modules into builtins