Rune:Emacs 的 Rust 虚拟机
Rune: Rust VM for Emacs

原始链接: https://github.com/CeleritasCelery/rune

Rune 是一个用 Rust 编写的实验性 Emacs 核心,目标是实现多线程 Emacs Lisp、错误兼容性以及性能改进。它处于早期阶段,目标是实现一个编辑器 MVP,具备基本的 elisp 运行时和编辑功能,如缓冲区文本插入/删除、光标、行换行、滚动、文件 I/O 和显示表。它使用标记指针对象、读取器、全局 obarray、GC、字节码 VM、解释器和内置函数;Lisp 文件来自 Emacs 29.1,引导更改标记为 RUNE-BOOTSTRAP。贡献者可以通过 defun 宏实现原语并加载文件。Rune 支持通过 assert_elprop! 对 GNU Emacs 进行属性测试,Windows 测试可以在 Linux 上使用 Wine 运行。它汲取了 Remacs、emacs-ng、Helix 和其他资源的灵感。

Hacker News 列出了 “Rune: Rust VM for Emacs”(github.com/celeritascelery),有 22 分。一位评论者称其很酷,称赞 Emacs,并想知道它可能带来哪些最终用户改进,例如更好的线程或 GUI 选项。他们还回忆起 Remacs,一个类似的 Rust 重写项目,似乎已经停止了。
相关文章

原文

R ust UN der E macs video introduction

Docs

This project is an experimental Emacs core written in Rust. The project is still at a very early phase but has the following goals:

  • Bring multi-threaded elisp to Emacs
  • Be “bug-compatible” with existing Emacs Lisp packages (everything should still work)
  • Enable performance improvements (including faster GC, regex, and JSON) by leveraging the Rust Ecosystem.

See the design doc for more details.

The current goal of this project is to create an editor MVP. We have a basic elisp runtime, and we are working on adding basic editing functionality in a minimal GUI. This will include:

  • buffer
  • text insertion/deletion
  • cursor
  • line wrapping
  • scrolling
  • file IO
  • display tables

If you want to contribute or have ideas for things to add, please open an issue.

Lisp files are currently pulled from

https://github.com/emacs-mirror/emacs/tree/emacs-29.1/lisp

Any modification for bootstrapping contain the tag RUNE-BOOTSTRAP.

cargo run --release -- --repl
Load the bootstrapped elisp and open the REPL
cargo run --release -- --no-bootstrap --repl
Open the REPL with only the builtin functions loaded
cargo run --release
Load the bootstrapped elisp and exit

Run the test suite with MIRI

MIRIFLAGS='-Zmiri-strict-provenance' cargo +nightly miri test

The project is defined by a main package rune, which depends on the crates included in the crates directory. One of those is the rune-macros crate, which defines the defun proc macro for defining builtin functions. The rest of the code is contained in src/. The modules are described below.

objects
The basic objects used in the interpreter. These are modeled after Emacs objects using tagged pointers with inline fixnums. Conversion between different primitives and object types is also found here.
reader
The Emacs lisp reader that translates a string to a cons cell. Due to the simple nature of lisp syntax, the reader is hand rolled and does not rely on any parsing libraries.
env
The global obarray. Currently, function bindings are global and immutable and value bindings are thread-local and mutable. When the ability is added to share data between threads, this will enable new threads to safely run functions without the need to copy them.
gc
Contains the allocator and garbage collector. All code for rooting and managing objects lives here as well.
bytecode
The bytecode VM. This uses the same opcodes as Emacs and uses the bytecomp.el to compile.
interpreter
The basic elisp interpreter. This is used only to bootstrap the elisp byte-compiler.
fns, data, alloc
These modules contain definitions of builtin in functions. Some of these are just stubbed out until the functionality is actually needed.

See the architecture doc for more info on the structure of Rust Emacs internals.

This project is moved forward by trying to load new elisp files and seeing what breaks. The best way to do that is with cargo run, which will load the currently bootstrapped files. The bootstrapped files are located in main.rs as part of the load function.

Usually what is needed is to implement more primitive functions. This is done with the defun macro. For example, if we wanted to implement the substring function, we would first look at the lisp signature.

(substring STRING &optional FROM TO)

Then we would translate the types to their Rust equivalent. If the correct type is not known we can use Object. In this example we would write our Rust signature as follows:

#[defun]
fn substring(string: &str, from: Option<i64>, to: Option<i64>) -> String {...}

To load a new elisp file, run cargo run --release -- --load <file>. Files that are not bootstrapped are not yet included in this repo, but are part of Emacs. Once the file is bootstrapped it can be added to the lisp directory.

Rune includes the `assert_elprop!` macro, which enables property testing against GNU Emacs. This macro works by sending the same Lisp form to both Rune and Emacs and then comparing their results. It can also be used in conjunction with the `proptest` crate.

#[test]
fn test_addition() {
    // Verify that (+ 40 2) evaluates to 42 for both Rune and Emacs
    assert_elprop!["(+ 40 2)"];
}

// Verify that make-vector works for any arbitrary byte
proptest! {
    #[test]
    fn test_make_vector_consistency(size in arb_byte()) {
        assert_elprop!["(make-vector {} ?A)", size];
    }
}

How to run Windows tests on Linux with Wine

  1. Define WINEPREFIX: export WINEPREFIX=$PWD/.wine
  2. Add x86_64-pc-windows-gnu to rust toolchain: rustup target add x86_64-pc-windows-gnu
  3. Setup the wineprefix: `./scripts/setup-wineprefix`. This script will install Emacs inside the prefix as well

Now you can run the tests on Windows like that: cargo test --target x86_64-pc-windows-gnu. In case you use Nix enviroment you should skip 1 and 2 points.

Also, you can tell eglot to check the code marked with #[cfg(windows)]. Just add the following to .dir-locals.el and restart eglot:

((nil
       . ((eglot-workspace-configuration
           . (:rust-analyzer (:cargo (:target "x86_64-pc-windows-gnu")))))))
tagged pointers in Rust
My initial approach to creating tagged pointers in Rust. It serves as in intro to this project.
implementing a safe garbage collector
An overview of the garbage collector used in this project and how Rust enables safe GC abstractions.
Design of Emacs in Rust
Some of the unique benefits that Rust could bring to Emacs.
Remacs
The original Rust and Emacs project. Remacs took the approach of enabling interop between Emacs C core and Rust, enabling them to replace parts of Emacs piecemeal. The project is currently unmaintained but is a big inspiration for Rune.
emacs-ng
The spiritual successor to remacs. This project integrates the Deno runtime into emacs, allowing you to write extensions in elisp or javascript. Which sounds cool if you happen to be a web developer. It really shows the power of integrating Emacs with a more modern ecosystem (which is part of the promise of Rust).
helix
A fast modern text editor written in Rust.
crafting interpreters
This was a big inspiration for this project, and it’s probably one of the best introductions to programming language implementations.
联系我们 contact @ memedata.com