MCPcopy Index your code
hub / github.com/HiRoFa/quickjs_es_runtime

github.com/HiRoFa/quickjs_es_runtime @0.17.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.17.2 ↗ · + Follow
737 symbols 3,053 edges 40 files 250 documented · 34%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

quickjs_runtime

Quickjs_runtime is a library for quickly getting started with embedding a javascript engine in your rust project.

Relies on hirofa-quickjs-sys to support quickjs-ng as well as the original quickjs

Quickjs_runtime runs all javascript action in a single thread using an EventLoop. This means you can call javascript safely from several threads by adding tasks to the EventLoop.

quickjs or quickjs-ng

Quickjs_runtime supports both the original quickjs and the quickjs-ng project.

You can use quickjs-ng by adding the dep to quickjs_runtime like this:

quickjs_runtime = { version = "0.16", features = ["console", "setimmediate", "setinterval", "settimeout", "typescript", "quickjs-ng"], default-features = false }

OS support

features linux mac windows
bellard yes yes mingW only
quickjs-ng yes yes mingW and MSVC

Usage and Features

An example on how to embed a script engine in rust using this lib can be found here: github.com/andrieshiemstra/ScriptExtensionLayerExample. It was published in TWIR as a walkthrough.

Quickjs_runtime focuses on making quickjs easy to use and does not add any additional features, that's where these projects come in:

Please see the DOCS for all inner workings

This lib serves two main goals:

1. Provide simple utils for working with quickjs (these are located in the quickjs_utils mod)

  • The QuickJsRuntime struct, this is to be used from a single thread
  • E.g. objects::set_property(), functions::invoke_func()
  • Wrap JSValue to provide reference counting (+1 on init, -1 on drop) (QuickJsValueAdapter)
  • Pass a module loader

2. Wrap quickjs for use as a ready to go JavaScript Runtime

  • Start at the QuickjsRuntimeFacade, it provides an EventQueue which has a thread_local QuickJsRuntimeAdapter
  • All values are copied or abstracted in a JsValueFacades
  • So no need to worry about Garbage collection
  • Evaluate script and invoke functions while waiting for results blocking or with async/await
  • Get Promise result blocking or with async/await

What works?

Script and Modules

  • Typescript (via SWC)
  • Console ( .log/info/debug/trace/error) (docs)
  • Eval script (docs)
  • Create promises in JavaScript which execute async
  • Eval modules (docs)
  • Load modules (dynamic and static) (docs)
  • Fetch api (impl in GreenCopperRuntime)
  • setImmediate
  • setTimeout/Interval (and clear)
  • Script preprocessing (impls for ifdef/macro's/typescript can be found in GreenCopperRuntime)

Rust-Script interoperability

  • Return Promises from rust functions and resolve them from rust (docs)
  • Add functions from rust (docs)
  • Invoke JS functions from rust (docs)
  • Pass primitives, objects and arrays from and to rust (docs)
  • Create Classes from rust (docs)
  • Async/await support on eval/call_function/promise resolution (docs)
  • Import native Modules (e.g. dynamic loading of rust functions or Proxy classes) (docs)

Goals

Embedding a script engine in a rust project seems a very tedious job which involves learning a lot about the inner workings of that engine.

The main goal of this project is to make that job easy!

The manner in which this is achieved is primarily focused on abstracting the workings of the engine from the implementor, therefore some functionality may not be the fastest way of getting things done.

So a second goal is to make implementing a fast and efficient integration doable for the uninitiated, the most common tasks you do with the engine should be doable with the utils in this package and working examples should be provided in the test modules.

The reason I chose QuickJS as the engine is that I've been dealing with less modern engines in my java projects and not being able to use the latest and greatest ECMA-script features becomes quite disappointing at times.

The fun stuff about QuickJS:

  • small footprint
  • fast compilation / startup
  • great JS compatibility

examples

Cargo.toml

[dependencies]
quickjs_runtime = "0.16"

Here are some quickstarts:

The quickjs Api utils:

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 368
Method 330
Class 29
Interface 6
Enum 4

Languages

Rust100%

Modules by API surface

src/quickjsrealmadapter.rs82 symbols
src/facades.rs64 symbols
src/reflection/mod.rs53 symbols
src/quickjsruntimeadapter.rs53 symbols
src/values.rs52 symbols
src/quickjsvalueadapter.rs41 symbols
src/quickjs_utils/functions.rs32 symbols
src/quickjs_utils/objects.rs31 symbols
src/quickjs_utils/promises.rs25 symbols
src/reflection/eventtarget.rs24 symbols
src/quickjs_utils/maps.rs22 symbols
src/jsutils/mod.rs22 symbols

For agents

$ claude mcp add quickjs_es_runtime \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page