[ANN] Bindings to QuickJS

Hello discuss,

I’m happy to announce a new library (quickjs.ml) that I have been working on, sometimes on-stream with the help of a few people (specially @welltypedwitch) and other times, just coding lonely.

It’s an early project to bind to quickjs QuickJS Javascript Engine a small and embeddable JavaScript engine. It supports the ES2023 specification including modules, asynchronous generators, proxies and BigInt.

It passes nearly 100% of the ECMAScript Test Suite tests when selecting the ES2023 features. A summary is available at Test262 Report and has very low startup time (compared to other JS Engines).

The need to bind to a JavaScript engine on OCaml appeared while working with server-reason-react components. While moving ReasonReact components from the browser into the server.

Those components relied on JavaScript APIs such as DOM APIs, but also some “pure” JS. The part of the browser isn’t available on the server (obv) so we discard it with a ppx (called browser_ppx https://twitter.com/davesnx/status/1765059888550686892) but still there were some (”pure JS”) code that could run on the server such as Array/List/String operations, RegExp, i18n, Date, etc.

On the browser, those apis are available under melange.js (which are bindings to JS: https://melange.re/v2.0.0/communicate-with-javascript) and I made an effort of having similar implemenations on server-reason-react.js which implemented in OCaml the same APIs, but there were a few issues that I couldn’t resolve in OCaml (or I didn’t want to resolve), for example the JavaScript RegExp engine had backtracing and named groups which Pcre (the first approach I used) doesn’t have.

Those APIs are available (with a full test suite) on many JS engines, so It was a matter of choosing one and bind to it… which brings to release quickjs.ml into opam with RegExp support and ongoing BigInt, Date, parse numbers, and encode/decode URI.

Let me know if you find it useful, and feel free to collab!

PS: I’m very aware that the meme writes itself, OCaml to JS and now we bring JS into OCaml.

11 Likes

Sounds interesting! However, I could not access the documentation for the full library API… do you have a direct link?

Cheers,
Nicolas

I didn’t find anywhere the documentation for the API published, but it’s one file on the repo: quickjs/quickjs.h at master · bellard/quickjs · GitHub

Worth mentioning (I forgot to say it earlier) that I don’t bind to the public interface (for example: js__regex_exec) but rather the internal (lre_exec) since the outside requieres JS_Value, JS_Context and many JS_* that aren’t desired/possible to bind with OCaml.