d4rt_rs
A Dart interpreter and runtime environment written in Rust, embeddable as a library. The public API takes inspiration from two projects:
~/d4rt— a Dart interpreter with bridged native types, module loading, permissions, and REPL-style evaluation.- rquickjs — the ergonomic
QuickJS bindings for Rust, with
Context, function registration, and typed value conversion (FromValue/ToValue).
d4rt_rs combines the two: a QuickJS-style Context runtime with
d4rt-style execution, bridged classes, per-Context sandboxing, and
optional dart:* libraries behind Cargo feature flags.
The standard library bundles math, async/streams, JSON/UTF-8 conversion,
collections, typed data, isolates, process, and dart:io (files, sockets,
HTTP server/client) — so it is well suited to embedding a scripting
engine, running untrusted Dart models at the edge, interactive REPLs, or
testing tooling.
Usage
Add to your Cargo.toml:
[]
= "0.1"
The crate compiles with the full standard library by default. See Feature flags to slim it down.
Running Dart code
use Context;
Async top-level main functions are awaited to completion; top-level
state persists across eval calls (REPL-style).
Registering native functions
use ;
let mut ctx = new;
ctx.register_function;
ctx.register_function;
assert_eq!;
assert_eq!;
Native functions can call back into Dart code through the visitor:
ctx.register_function;
Calling Dart functions from Rust (execute)
let mut ctx = new;
let result = ctx
.execute
.unwrap;
assert_eq!;
Isolated runtimes
Each Context is fully independent — its own globals and its own
dart:core:
let mut a = new;
let mut b = new;
a.eval.unwrap;
assert!;
Capturing print output
let mut ctx = new;
ctx.set_print_handler;
ctx.eval.unwrap;
Sandboxing
By default a Context denies every dangerous operation (filesystem,
network, process, isolate, environment). Grant capabilities up front:
use ;
use ;
let mut ctx = new_with;
assert!;
assert!;
Permissions can be checked and withdrawn at runtime with
Context::has_permission / Context::revoke; denied operations raise a
Dart-catchable runtime error.
Bridging native types
Bridged classes (native Rust types visible from Dart) are built with
BridgedClassDef and the helpers in d4rt_rs::stdlib::helpers, then
registered with Context::register_bridged_class. The stdlib itself uses
this for BigInt, DateTime, Duration, RegExp, Uri, sockets, and
more (see src/stdlib/ for worked examples).
Modules
import/export resolve in this order: per-Context module libraries
(Context::register_module_library), process-global libraries
(d4rt_rs::stdlib::register_package_library), virtual dart: modules
(Context::register_dart_module, with a process-global fallback in
d4rt_rs::stdlib::register_dart_module), then real .dart files on disk
(when filesystem permissions allow).
Command-line interpreter
The main.rs binary is a small, permissively-configured frontend to the
same Context API (see below).
Features
The interpreter core and dart:core are always compiled. The rest of the
standard library ships behind optional feature flags:
| Feature | Dart libraries gated |
|---|---|
stdlib (default) |
All of the below |
async |
dart:async — Future/Stream/Timer/Completer |
math |
dart:math — Random, Point, Rectangle, trig... |
convert |
dart:convert — utf8/base64/json/ascii |
collection |
dart:collection — LinkedHashMap/LinkedHashSet/Queue |
typed_data |
dart:typed_data — ByteData/Endian/*List buffers |
io |
dart:io — File, Socket, HttpServer/Client, Process... |
isolate |
dart:isolate — ReceivePort/Isolate.run/spawn |
Feature dependencies: async, math, convert, collection,
typed_data need core; io needs core + async + convert;
isolate needs core + async.
[]
= { = "0.1", = false, = ["math", "convert"] }
What is supported
The interpreter implements a large subset of Dart 3:
- Functions (positional/named/optional parameters, defaults, closures),
classes, inheritance,
mixins, enums, records, extensions. - Collections, sync generators (
sync*), iterables,??/?., pattern matching. dart:core(int, double, String, List, Map, Set, BigInt, DateTime, Duration, RegExp, Uri, FormatException, ...).- Async/await, streams, futures, timers, completers (
dart:async). - Permissions, module loading,
dart:isolate, anddart:io.
Top-level Function RuntimeType, records, and some reflection corners
are still incomplete; see tests/ for the precise conformance surface.
Development
Dart conformance is exercised by both hand-written tests
(tests/stdlib_*.rs) and interpreter-driven reference suites
(tests/stdlib_dart_*_ref_test.rs). cargo clippy lints the library
and binaries only; cargo clippy --all-targets additionally sweeps the
per-file test crates.
License
MIT