Skip to main content

Crate quickjs

Crate quickjs 

Source
Expand description

quickjs — an embeddable JavaScript engine written in pure Rust.

The crate is being built as a drop-in replacement for bellard/quickjs; the language level tracked is the upstream release whose version string is COMPAT_VERSION.

§Current state (v0.15.3 — milestones M0–M12a)

Source is parsed, compiled to bytecode and executed: Context::eval runs a script and returns its completion value, and Context::output holds whatever print wrote.

let rt = quickjs::Runtime::new();
let mut ctx = quickjs::Context::new(&rt);
ctx.eval("print(1 + 1)").expect("runs");
assert_eq!(ctx.output(), "2\n");

The standard library covers Object, Function, Array, String, Number, Math, JSON, Reflect, Date, RegExp, Symbol, Map/Set, BigInt, TypedArray, DataView, the error classes and the Iterator helpers; the ES6+ runtime is in place (class including private fields, destructuring, generators, Proxy, Promise/async, for await); and ES modules run through Context::eval_module, or as precompiled bytecode through Context::eval_bytecode with no parser at hand.

The host can call into the script and the script back into Rust: Context::set_global_function, Context::set_global_constructor, Context::call, Context::new_object, Context::set_property.

Resource limits are enforced rather than merely accepted: Runtime::set_memory_limit counts the payload inside containers (string encodings, array elements, Map/Set entries, ArrayBuffer), not just the GC boxes, and Runtime::set_stack_size bounds recursion by frame size. Both raise a catchable InternalError — out of memory and stack overflow, the reference’s wording and class.

§Not there yet

Every gap below is a loud failure rather than a wrong answer:

  • std/os and the event loop live in quickjs-host, not here, so setTimeout is undefined in a bare Context. The same goes for the other host-provided globals — queueMicrotask, structuredClone, performance — and for Intl, which is ECMA-402 rather than ECMA-262;
  • Function.prototype.toString answers with the original source for a function written in source, but a method (m(){}, get x(){}) still reports the [native code] form, and so does TypedArray.prototype.toSpliced, which the C reference does not have either;
  • performance is 0.04×–0.18× the C reference on the twelve measured cases (scripts/bench.sh in the repository); there is no NaN boxing and no superinstruction dispatch yet;
  • a handful of Array methods bound how many elements they will walk one at a time (MAX_ARRAY_LIKE, 2^26). The spec sets no such bound, but {length: 2**53-1} is legal and a fill/indexOf/copyWithin over it would never return, so the range is refused with a RangeError instead. Narrowing the range with start/end — which is what test262’s own near-integer-limit cases do — stays unaffected.

Where the spec does set a bound, it is enforced and the error class matters: push, unshift, splice and concat throw a TypeError when the result length would pass 2^53 - 1 (§23.1.3.24 step 6 and siblings). Those checks were missing until v0.15.1, and their absence was not a wrong answer but a hang — push wrote a length past the exact-integer range and unshift fell into a 2^53-iteration loop.

Iteration is bounded where the spec has no bound, and the two cases are different: Interp::collect_iterable refuses past MAX_ITEMS, and Array.from counts as it walks. Array.from had neither until v0.15.2 — it drained the iterator into a Vec and only then ran the mapper, so Array.from(neverEnding, () => { throw x }) hung instead of throwing x, and an endless iterator with no mapper hung outright. §23.1.2.1 interleaves the three steps for exactly this reason.

One detail of the iterator protocol is easy to get wrong and worth naming: §7.4.3 reads value only when done is false. Reading it unconditionally ran a value getter on the terminal step, which made AggregateError/errors-iterabletolist collect three values where two exist and made every Iterator.prototype helper fail next-method-returns-throwing-value-done; fixing it cleared nineteen tests at once.

See docs/requirements.md in the repository for the milestone plan and docs/test262-status.md for the measured language coverage.

Re-exports§

pub use interp::FdInterest;
pub use interp::Interp;
pub use interp::JsResult;
pub use interp::Signal;
pub use interp::Task;
pub use interp::Turn;
pub use memory::ClassCensus;
pub use memory::MemoryUsage;
pub use object::Callable;
pub use object::ClassDef;
pub use object::ClassId;
pub use object::Exotic;
pub use object::JsObject;
pub use object::ObjectKind;
pub use object::Opaque;
pub use object::Property;
pub use value::Cell;
pub use value::JsString;
pub use value::Value;
pub use js_compile;
pub use js_parse;
pub use quickjs_regexp as regexp;

Modules§

bigint
Arbitrary-precision integers, the engine’s BigInt.
builtins
The standard library: the globals a script reaches without an import.
insn
The instruction stream the interpreter actually runs.
interp
The bytecode interpreter.
memory
The memory report behind qjs -d / --dump and JS_ComputeMemoryUsage.
module
The module system: records, linking, namespaces, and evaluation.
normalize
Unicode normalization: the four forms String.prototype.normalize offers.
object
The object model: property tables, arrays, functions, and the prototype chain.
print
The reference’s JS_PrintValue, which lives in the engine (quickjs.c) and not in the host library. std.__printObject is a one-line wrapper over it, and so is the reporting of an uncaught exception — which is why this belongs here rather than in quickjs-host. std.__sprintObject / std.__printObject: the reference’s JS_PrintValue.
value
JavaScript values, and the conversions between them.

Structs§

CollectStats
Statistics returned by Heap::collect.
Context
An execution context: a global object plus the builtins installed on it.
Gc
A strong, reference-counted handle to a heap value.
Heap
Owns the live-object registry for one JavaScript runtime.
Runtime
A JavaScript runtime: one heap, one memory limit, one set of host hooks.
Tracer
Visitor used by Trace implementations.
WeakGc
A non-owning handle to a heap value.

Enums§

Error
Error raised by the engine.
EvalError
Why Context::eval_detailed failed, keeping the thrown value intact.

Constants§

COMPAT_VERSION
Version string of the upstream QuickJS release this port tracks.

Traits§

HostSurface
What a host module needs from the engine, whichever way the engine was reached.
Trace
Types that may own Gc handles.

Functions§

is_module_source
js_parse::is_module_source, which is the rule qjs uses to decide module mode.

Type Aliases§

Result
Convenience alias used throughout the crate.