kyren / luster
- вторник, 5 марта 2019 г. в 00:17:34
Rust
An experimental Lua VM implemented in pure Rust
My eventual goals with luster
are somewhat ambitious:
rlua
and PUC-Rio's C API.This project is in active development and currently very WIP
luster
has a real, cycle detecting, incremental garbage collector with
zero-cost Gc
pointers (they are machine pointer sized and implement Copy
)
and are usable from safe Rust. It achieves this by combining three techniques:
Collect
trait which allows tracing through garbage collected
types that, despite being unsafe, can be implemented safely using procedural
macros.Gc
pointers by unique, invariant "generative" lifetimes to ensure
that such pointers are isolated to a single root object, and to guarantee
that, outside an active call to mutate
, all such pointers are either
reachable from the root object or are safe to collect.mutate
, long running
mutations are problematic. By using a futures
-like combinator based
"sequencing" API, we can recover the ability for garbage collect to take
place with as fine of a granularity as necessary, with garbage collection
taking place in-between the "sequence" steps.The last point has benefits beyond safe garbage collection: it means that the
entire VM including sequences of Lua -> Rust and Rust -> Lua callbacks is
expressed in a sort of "stackless" or what is sometimes called "trampoline"
style. Rather than implementing the VM or callbacks with recursion and the Rust
stack, VM executions and callbacks are constructed as Sequence
state machines
via combinators. The interpreter receives this Sequence
to execute and simply
loops, calling Sequence::step
until the operation is finished (and garbage
collecting in-between the step
calls). This "stackless" style allows for some
interesting concurrency patterns that are difficult or impossible to do using
PUC-Rio Lua.
(These ideas are not all mine, the garbage collector is heavily derived from
rust-gc,
the idea of using "generativity" comes from You can't spell trust without
Rust, the
vast majority of the Sequence
design is taken directly from
futures-rs, and the idea of
using a "trampoline" loop is taken from scheme / Stackless Python.)
While the interface to garbage collected pointers is interesting, the actual garbage collector itself is currently only a very basic (but adequate) incremental mark-and-sweep collector. This could be replaced in the future with a better design.
pcall
)print
, error
, pcall
, a lot of of math
,
and the hard bits from coroutine
)cargo run luster
!)debug
(which may never be completely
implemented), io
, os
, package
, string
, table
, utf8
, most top-level
functions are unimplemented.__gc
, which will require implementing finalizers in
gc-arena
.__gc
metamethods.Box<Any>
userdata type is not difficult,
but letting userdata safely participate in garbage collection and having easy,
performant APIs for userdata methods are much harder.This is not an exhaustive list, but these are some things which I currently consider non-goals. This list is also preliminary, everything here is up for discussion:
luster
is more or less aiming to emulate PUC-Rio
Lua behavior with the "C" locale set with the default settings in
luaconf.h
on 64-bit Linux.debug
library may be problematic to implement (I am not
completely sure what yet, though)os.setlocale
package.loadlib
and all functionality which allows loading C libraries.__gc
errors in Lua (I am not sure about this
one yet, this may be difficult or it may not).The project is still in an early state and there is lots left to do! If you are interested in contributing, please take a look at TODO.md for ideas.
Much of the work left to do is design work rather than simply implementing
features, and this is probably the place where help would be most appreciated.
Almost none of the internal APIs are what I would consider final, and for some
of the very tricky pieces like gc-sequence
and callbacks, there is a LOT of
room for improvement in the API design (to put it mildly!). If you think you
have a way to make using luster
more ergonomic, or simply want to complain
about ways that it is not ergonomic (there are many), please feel free to file
an issue and we can discuss it!
luster
is licensed under either of:
at your option.