Skip to content

Commit

Permalink
Move core to workspace crate rune-core
Browse files Browse the repository at this point in the history
It's key to note that in the process, we include different changes that
guide the build process of the crates, in order to arrive at a place
that is both ergonomic and easy to maintain:

We removed the `defvar`, `defvar_bool` and `defsym` macros. They were
not adding that much regarding work saved (just create a constant and
initialize it) and they were convoluting the dependency graph between
the crates. It's key that the core crate can use the symbols that were
previously generated on the build.rs file, but it does not need to know
the defuns.

For that, we moved the variables and symbols (not the defun symbols) to
a static file `env/sym.rs`. That file contains all the required
constants for the variables and symbols.

On the other hand, the defun symbols and constants are still generated
with the build script, to avoid loosing the convenience of defining the
functions with the `defun` proc macro.

Additionally, the macros were moved into their own module:
`rune_core::macros`, by re-exporting the now privately named macros. The
motivation behind it is that we need to access macros by name that may
conflict with other modules with the same name. An example of that is
the `error` module, which we import with self. The `error!` macro needs
to be imported as well, but a conflict arises. Now, we can import the
macro with `use rune_core::macros::error` and the module with `use rune_core::error`.
  • Loading branch information
Qkessler committed Dec 16, 2023
1 parent 78926ab commit 0ef4e15
Show file tree
Hide file tree
Showing 58 changed files with 1,171 additions and 1,058 deletions.
56 changes: 36 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,65 @@ edition = "2021"
[workspace]
members = [
"crates/text-buffer",
"crates/rune-macros"
"crates/rune-macros",
"crates/rune-core"
]

[workspace.dependencies]
quote = "1.0.26"
syn = { version = "2.0.15", features = ["full"]}

[workspace.package]
edition = "2021"

[dependencies]
[workspace.dependencies]
syn = { version = "2.0.15", features = ["full"] }
quote = "1.0.26"
proc-macro2 = "1.0.60"
anyhow = "1.0.69"
sptr = "0.3.2"
fallible-iterator = "0.3.0"
fallible-streaming-iterator = "0.1.9"
fxhash = "0.2.1"
indexmap = "2.1.0"
memoffset = "0.9.0"
bstr = "1.3.0"
float-cmp = "0.9.0"

text-buffer = { version = "0.1.0", path = "crates/text-buffer" }
rune-core = { version = "0.1.0", path = "crates/rune-core" }
rune-macros = { version = "0.1.0", path = "crates/rune-macros" }

[dependencies]
anyhow = { workspace = true }
bstr = { workspace = true }
bytecount = "0.6.3"
druid = "0.8.3"
fancy-regex = "0.12.0"
float-cmp = "0.9.0"
float-cmp = { workspace = true }
hostname = "0.3.1"
memoffset = "0.9.0"
memoffset = { workspace = true }
num_enum = "0.7.1"
paste = "1.0.12"
rand = "0.8.5"
rune-macros = { version = "0.1.0", path = "crates/rune-macros" }
sptr = "0.3.2"
sptr = { workspace = true }
streaming-iterator = "0.1.9"
text-buffer = { version = "0.1.0", path = "crates/text-buffer" }
titlecase = "2.2.1"
fallible-iterator = "0.3.0"
fallible-streaming-iterator = "0.1.9"
indexmap = "2.1.0"
fxhash = "0.2.1"
fallible-iterator = { workspace = true }
fallible-streaming-iterator = { workspace = true }
indexmap = { workspace = true }
fxhash = { workspace = true }

text-buffer = { workspace = true }
rune-macros = { workspace = true }
rune-core = { workspace = true }

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = "0.5"

# [dev-dependencies]
# backtrace-on-stack-overflow = "0.3.0"

[build-dependencies]
syn = { workspace = true }
quote = { workspace = true }

[profile.dev.build-override]
opt-level = 3

Expand All @@ -57,11 +78,6 @@ debug = true
default = []
debug_bytecode = []

[build-dependencies]
quote = { workspace = true}
syn = { workspace = true }
proc-macro2 = "1.0.60"

[workspace.lints.rust]
macro_use_extern_crate = "deny"
keyword_idents = "deny"
Expand Down
12 changes: 6 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ fn main() {
#[allow(non_snake_case)]
#[allow(dead_code)]
pub(crate) mod sym {{
use crate::core::env::SymbolCell;
use crate::core::env::Symbol;
use rune_core::env::SymbolCell;
use rune_core::env::Symbol;
pub(super) static BUILTIN_SYMBOLS: [SymbolCell; {symbol_len}] = [
SymbolCell::new_const(\"nil\"),
Expand Down Expand Up @@ -199,7 +199,7 @@ pub(super) static BUILTIN_SYMBOLS: [SymbolCell; {symbol_len}] = [

// all SubrFn
let subr_len = all_defun.len();
writeln!(f, "static SUBR_DEFS: [&crate::core::object::SubrFn; {subr_len}] = [",).unwrap();
writeln!(f, "static SUBR_DEFS: [&rune_core::object::SubrFn; {subr_len}] = [",).unwrap();
for (subr_name, _, _) in &all_defun {
writeln!(f, " &{subr_name},",).unwrap();
}
Expand Down Expand Up @@ -250,10 +250,10 @@ pub(crate) fn interned_symbols() -> &'static Mutex<ObjectMap> {{
"
#[allow(unused_qualifications)]
pub(crate) fn init_variables(
cx: &crate::core::gc::Context,
env: &mut crate::core::gc::Rt<crate::core::env::Env>,
cx: &rune_core::gc::Context,
env: &mut rune_core::gc::Rt<rune_core::env::Env>,
) {{
use crate::core::object::Object;
use rune_core::object::Object;
"
)
.unwrap();
Expand Down
24 changes: 24 additions & 0 deletions crates/rune-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "rune-core"
version = "0.1.0"
description = "Core functionality of the Rune Emacs Runtime."
edition.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { workspace = true }
bstr = { workspace = true }
sptr = { workspace = true }
fallible-iterator = { workspace = true }
fallible-streaming-iterator = { workspace = true }
float-cmp = { workspace = true }
fxhash = { workspace = true }
indexmap = { workspace = true }
memoffset = { workspace = true }

rune-macros = { workspace = true }
text-buffer = { workspace = true }

[lints]
workspace = true
67 changes: 30 additions & 37 deletions src/core/cons.rs → crates/rune-core/src/cons.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::hashmap::HashSet;
use crate::macros::define_unbox;

use super::gc::{Block, Context, GcManaged, GcMark, Trace};
use super::object::{nil, CloneIn, Gc, GcObj, IntoObject, ObjCell, Object, RawObj};
use crate::gc::{Block, Context, GcManaged, GcMark, Trace};
use crate::object::{nil, CloneIn, Gc, GcObj, IntoObject, ObjCell, Object, RawObj};

Check failure on line 5 in crates/rune-core/src/cons.rs

View workflow job for this annotation

GitHub Actions / clippy

unresolved import `crate::object::nil`

error[E0432]: unresolved import `crate::object::nil` --> crates/rune-core/src/cons.rs:5:21 | 5 | use crate::object::{nil, CloneIn, Gc, GcObj, IntoObject, ObjCell, Object, RawObj}; | ^^^ no `nil` in `object`
use anyhow::{anyhow, Result};
use std::fmt::{self, Debug, Display, Write};

mod iter;

pub(crate) use iter::*;
pub use iter::*;

#[derive(Eq)]
pub(crate) struct Cons {
pub struct Cons {
marked: GcMark,
mutable: bool,
car: ObjCell,
Expand All @@ -27,7 +27,7 @@ impl Cons {
// SAFETY: Cons must always be allocated in the GC heap, it cannot live on
// the stack. Otherwise it could outlive it's objects since it has no
// lifetimes.
pub(crate) unsafe fn new_unchecked(car: GcObj, cdr: GcObj) -> Self {
pub unsafe fn new_unchecked(car: GcObj, cdr: GcObj) -> Self {
Self {
marked: GcMark::default(),
mutable: true,
Expand All @@ -37,7 +37,7 @@ impl Cons {
}

/// Create a new cons cell
pub(crate) fn new<'ob, T, Tx, U, Ux>(car: T, cdr: U, cx: &'ob Context) -> &'ob Self
pub fn new<'ob, T, Tx, U, Ux>(car: T, cdr: U, cx: &'ob Context) -> &'ob Self
where
T: IntoObject<Out<'ob> = Tx>,
Gc<Tx>: Into<GcObj<'ob>>,
Expand All @@ -51,7 +51,7 @@ impl Cons {
}

/// Create a new cons cell with the cdr set to nil
pub(crate) fn new1<'ob, T, Tx>(car: T, cx: &'ob Context) -> &'ob Self
pub fn new1<'ob, T, Tx>(car: T, cx: &'ob Context) -> &'ob Self
where
T: IntoObject<Out<'ob> = Tx>,
Gc<Tx>: Into<GcObj<'ob>>,
Expand All @@ -61,19 +61,34 @@ impl Cons {
cons.into_obj(cx).untag()
}

pub(in crate::core) fn mark_const(&mut self) {
pub fn mark_const(&mut self) {
self.mutable = false;
}

pub(crate) fn car(&self) -> GcObj {
/// See [`Cons::cdr`]. The CAR of a list is, quite simply, the first item in the list.
/// See [Emacs manual](https://www.gnu.org/software/emacs/manual/html_node/eintr/car-_0026-cdr.html).
///
/// # Examples:
///
/// '(rose violet daisy buttercup) -> car -> rose
/// '(test) -> car -> test
pub fn car(&self) -> GcObj {
self.car.get()
}

pub(crate) fn cdr(&self) -> GcObj {
/// See [`Cons::car`]. The CDR of a list is the rest of the list, that is, the cdr function
/// returns the part of the list that follows the first item.
/// See [Emacs manual](https://www.gnu.org/software/emacs/manual/html_node/eintr/car-_0026-cdr.html).
///
/// # Examples:
///
/// '(rose violet daisy buttercup) -> cdr -> (violet daisy buttercup)
/// '(test) -> cdr -> nil
pub fn cdr(&self) -> GcObj {
self.cdr.get()
}

pub(crate) fn set_car(&self, new_car: GcObj) -> Result<()> {
pub fn set_car(&self, new_car: GcObj) -> Result<()> {
if self.mutable {
unsafe { self.car.as_mut().set(new_car) }
Ok(())
Expand All @@ -82,7 +97,7 @@ impl Cons {
}
}

pub(crate) fn set_cdr(&self, new_cdr: GcObj) -> Result<()> {
pub fn set_cdr(&self, new_cdr: GcObj) -> Result<()> {
if self.mutable {
unsafe { self.cdr.as_mut().set(new_cdr) }
Ok(())
Expand Down Expand Up @@ -180,32 +195,10 @@ impl Cons {

define_unbox!(Cons, &'ob Cons);

#[macro_export]
macro_rules! cons {
($car:expr, $cdr:expr; $cx:expr) => {
$cx.add({
let car = $cx.add($car);
let cdr = $cx.add($cdr);
unsafe { $crate::core::cons::Cons::new_unchecked(car, cdr) }
})
};
($car:expr; $cx:expr) => {
$cx.add({
let car = $cx.add($car);
unsafe { $crate::core::cons::Cons::new_unchecked(car, $crate::core::object::nil()) }
})
};
}

#[macro_export]
macro_rules! list {
($x:expr; $cx:expr) => ($crate::cons!($x; $cx));
($x:expr, $($y:expr),+ $(,)? ; $cx:expr) => ($crate::cons!($x, list!($($y),+ ; $cx) ; $cx));
}

#[cfg(test)]
mod test {
use crate::core::gc::Context;
use crate::gc::Context;
use crate::macros::{cons, list};

use super::super::gc::RootSet;
use super::*;
Expand Down
Loading

0 comments on commit 0ef4e15

Please sign in to comment.