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 interpreter
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 a 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 the browser between
frames of the same origin.
As both Runtime and Context use a lock it is discouraged to use them in a async
environment. Instead, when the futures feature is enabled this library also exposes
AsyncRuntime and AsyncContext which use a future aware lock.
§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 values to Rust. Note that this trait does not perform automatic
coercion but Coerced can be used to convert the values with coercion.
For values which represent the name of variables or indices, the trait IntoAtom is
available to convert values to the representation QuickJS requires.
§Optional features
§Default
This crate can be customized via features. The following features are 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).
§Advanced
The following features may be enabled to get an extra functionality:
-
allocatoradds support for custom allocators forRuntime. The allocators should implementsstd::alloc::Allocatortrait 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 implementsResolverand Loader traits 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 library exportsAsyncRuntimeandAsyncContext. These are the asynchronous variants of the normal runtime and context. In order to ensure that QuickJS is used properly the runtime is placed behind a lock. For the normal runtime this is a normal mutex. You should avoid blocking threads in asynchronous Rust so the async runtime uses a future aware mutex. In the async runtime Rust futures can be passed to JS as ES6 Promises and ES6 Promises can be given back as Rust futures. -
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
§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:
§Bindings
The bindings are pre-generated for the following platforms:
- Linux:
aarch64-unknown-linux-musli686-unknown-linux-gnux86_64-unknown-linux-gnux86_64-unknown-linux-muslloongarch64-unknown-linux-gnuloongarch64-unknown-linux-musl
- macOS:
aarch64-apple-darwinx86_64-apple-darwin
- Windows:
i686-pc-windows-gnux86_64-pc-windows-gnui686-pc-windows-msvcx86_64-pc-windows-msvc
To build the crate for any other target you must enable the 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 enable that:
dump-bytecodedump-gcdump-gc-freedump-freedump-leaksdump-memdump-objectsdump-atomsdump-shapesdump-module-resolvedump-promisedump-read-object
Modules§
- allocator
- Tools for using different allocators with QuickJS.
- array
- JavaScript array types.
- atom
- QuickJS atom functionality.
- class
- JavaScript classes defined from Rust.
- context
- JS Contexts related types.
- convert
- Utilities for converting to and from JavaScript values.
- function
- JavaScript function functionality
- loader
loader - Loaders and resolvers for loading JS modules.
- markers
- Utility types and traits.
- module
- Types for loading and handling JS modules.
- object
- Module for types dealing with JS objects.
- prelude
- A group of often used types.
- promise
- Javascript promises and future integration.
- qjs
- Native low-level bindings
- runtime
- QuickJS runtime related types.
Macros§
- async_
with futures - A macro for safely using an asynchronous context while capturing the environment.
- embed
macro - A macro for embedding JavaScript code into a binary.
- module_
init - Helper macro to provide module init function. Use for exporting module definitions to be loaded as part of a dynamic library.
Structs§
- Array
- Rust representation of a JavaScript object optimized as an array.
- Array
Buffer - Rust representation of a JavaScript object of class ArrayBuffer.
- Async
Context futures - An asynchronous single execution context with its own global variables and stack.
- Async
Runtime futures - Asynchronous QuickJS runtime, entry point of the library.
- Atom
- A QuickJS Atom.
- BigInt
- Rust representation of a JavaScript big int.
- CString
- Rust representation of a JavaScript C string.
- Class
- A object which is instance of a Rust class.
- Coerced
- The wrapper for values to force coercion
- Context
- A single execution context with its own global variables and stack.
- Ctx
- Context in use, passed to
Context::with. - Exception
- A JavaScript instance of Error
- Filter
- The property filter
- Function
- A JavaScript function.
- Module
- A JavaScript module.
- Null
- The placeholder which treated as
nullvalue - Object
- Rust representation of a JavaScript object.
- Persistent
- The wrapper for JS values to keep it from GC
- Promise
- A JavaScript promise.
- Runtime
- QuickJS runtime, entry point of the library.
- String
- Rust representation of a JavaScript string.
- Symbol
- Rust representation of a JavaScript symbol.
- Typed
Array - Rust representation of a JavaScript objects of TypedArray classes.
- Undefined
- The placeholder which treated as
undefinedvalue - Value
- Any JavaScript value
Enums§
- Caught
Error - An error type containing possible thrown exception values.
- Error
- Error type of the library.
- Type
- The type of JavaScript value
Traits§
- Catch
Result Ext - Extension trait to easily turn results with
Errorinto results withCaughtError - From
Atom - Trait for converting values from atoms.
- From
Iterator Js - The Rust’s
FromIteratortrait to use withCtx - FromJs
- For converting JavaScript values to Rust values
- Into
Atom - Trait for converting values to atoms.
- IntoJs
- For converting Rust values to JavaScript values
- Iterator
Js - The Rust’s
Iteratortrait extension which works withCtx - JsLifetime
- The trait which signifies a type using the rquickjs
'jslifetime trick for maintaining safety around Javascript values. - Throw
Result Ext - Extension trait to easily turn results with
CaughtErrorinto results withError
Type Aliases§
- Caught
Result - Result type containing an the JavaScript exception if there was one.
- Result
- Result type used throughout the library.
Attribute Macros§
- class
macro - An attribute for implementing
JsClassfor a Rust type. - function
macro - A attribute for implementing
IntoJsFuncfor a certain function. - methods
macro - A attribute for implementing methods for a class.
- module
macro - An attribute which generates code for exporting a module to Rust.
Derive Macros§
- JsLifetime
macro - A Macro for auto deriving the JsLifetime trait.