Skip to content

Commit

Permalink
Garbage collection part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Mar 26, 2024
1 parent 5a45290 commit 0b0f01c
Show file tree
Hide file tree
Showing 15 changed files with 977 additions and 380 deletions.
877 changes: 670 additions & 207 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ alot = "0.3.1"
ariadne = { version = "0.4.0", optional = true }
crossbeam-utils = "0.8.19"
dirs = { version = "5.0.1", optional = true }
interner = "0.2.1"
kempt = { version = "0.2.3", features = ["serde"] }
regex = "1.10.3"
rustyline = { version = "14.0.0", optional = true, default-features = false, features = [
Expand All @@ -38,6 +37,7 @@ rustyline = { version = "14.0.0", optional = true, default-features = false, fea
serde = { version = "1.0.195", features = ["derive", "rc"] }
unicode-ident = "1.0.12"
refuse = { git = "https://github.com/khonsulabs/refuse" }
refuse-pool = { git = "https://github.com/khonsulabs/refuse" }

[dev-dependencies]
rsn = "0.1"
Expand Down Expand Up @@ -65,3 +65,7 @@ harness = false
[profile.release]
debug = true
lto = true

[patch."https://github.com/khonsulabs/refuse"]
refuse = { path = "../refuse" }
refuse-pool = { path = "../refuse/refuse-pool" }
4 changes: 3 additions & 1 deletion amuse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ impl History {
.and(
match &self.result {
Ok(value) => match value.to_string(&mut context) {
Ok(formatted) => formatted.to_string(),
Ok(formatted) => formatted
.load(context.guard())
.map_or_else(String::new, ToString::to_string),
Err(_) => format!("{:?}", value),
}
.with(&FontFamily, FamilyOwned::Monospace)
Expand Down
10 changes: 5 additions & 5 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Compiler {
pub fn expand_macros(&mut self, expr: &mut Ranged<Expression>) {
match &mut expr.0 {
Expression::Macro(e) => {
if let Some(m) = self.macros.get_mut(&e.name) {
if let Some(m) = self.macros.get_mut(&e.name.0) {
let tokens = std::mem::take(&mut e.tokens);
let tokens = m.0.transform(tokens);
*expr = self.parse_macro_expansion(tokens);
Expand All @@ -146,7 +146,7 @@ impl Compiler {
}
}
Expression::InfixMacro(e) => {
if let Some(m) = self.infix_macros.get_mut(&e.invocation.name) {
if let Some(m) = self.infix_macros.get_mut(&e.invocation.name.0) {
let tokens = std::mem::take(&mut e.invocation.tokens);
let tokens = m.0.transform(&e.subject, tokens);
*expr = self.parse_macro_expansion(tokens);
Expand Down Expand Up @@ -547,7 +547,7 @@ impl<'a> Scope<'a> {
if let Some(label) = after_label {
self.compiler.code.label(label);
}
} else if let Some(var) = self.compiler.declarations.get(&lookup.name) {
} else if let Some(var) = self.compiler.declarations.get(&lookup.name.0) {
self.compiler.code.copy(var.stack, dest);
} else {
self.compiler.code.resolve(lookup.name.0.clone(), dest);
Expand Down Expand Up @@ -638,7 +638,7 @@ impl<'a> Scope<'a> {
.code
.invoke(target_source, Symbol::set_symbol().clone(), 2);
self.compiler.code.copy(Register(0), dest);
} else if let Some(var) = self.compiler.declarations.get(&target.name) {
} else if let Some(var) = self.compiler.declarations.get(&target.name.0) {
if var.mutable {
let var = var.stack;
self.compile_expression(&assign.value, OpDestination::Stack(var));
Expand Down Expand Up @@ -2049,7 +2049,7 @@ impl<'a> Scope<'a> {
Literal::Symbol(symbol) => ValueOrSource::Symbol(symbol.clone()),
},
Expression::Lookup(lookup) => {
if let Some(decl) = self.compiler.declarations.get(&lookup.name) {
if let Some(decl) = self.compiler.declarations.get(&lookup.name.0) {
ValueOrSource::Stack(decl.stack)
} else {
ValueOrSource::Stack(self.compile_expression_into_temporary(source))
Expand Down
2 changes: 1 addition & 1 deletion src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Exception {
impl CustomType for Exception {
fn muse_type(&self) -> &TypeRef {
static EXCEPTION_TYPE: RustType<Exception> = RustType::new("Exception", |t| {
t.with_fallback(|this, _guard| this.value.clone())
t.with_fallback(|this, _guard| this.value)
.with_eq(|_| {
|this, vm, rhs| {
if let Some(rhs) = rhs.as_rooted::<Exception>(vm.as_ref()) {
Expand Down
4 changes: 2 additions & 2 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub static LIST_TYPE: RustType<List> = RustType::new("List", |t| {
.with_fn(Symbol::set_symbol(), 2, |vm, this| {
let index = vm[Register(0)].take();
let value = vm[Register(1)].take();
this.set(&index, value.clone())?;
this.set(&index, value)?;
Ok(value)
})
.with_fn(
Expand Down Expand Up @@ -66,7 +66,7 @@ impl List {
};
let contents = self.0.lock().expect("poisoned");

contents.get(index).cloned().ok_or(Fault::OutOfBounds)
contents.get(index).copied().ok_or(Fault::OutOfBounds)
}

pub fn insert(&self, index: &Value, value: Value) -> Result<(), Fault> {
Expand Down
16 changes: 5 additions & 11 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ pub static MAP_TYPE: RustType<Map> = RustType::new("Map", |t| {
table
.with_fn(Symbol::set_symbol(), 1, |vm, this| {
let key = vm[Register(0)].take();
let value = key.clone();
this.insert(vm, key, value.clone())?;
Ok(value)
this.insert(vm, key, key)?;
Ok(key)
})
.with_fn(Symbol::set_symbol(), 2, |vm, this| {
let key = vm[Register(0)].take();
let value = vm[Register(1)].take();
this.insert(vm, key, value.clone())?;
this.insert(vm, key, value)?;
Ok(value)
})
.with_fn(Symbol::get_symbol(), 1, |vm, this| {
Expand Down Expand Up @@ -100,7 +99,7 @@ impl Map {
Ordering::Less => continue,
Ordering::Equal => {
if key.equals(ContextOrGuard::Context(vm), &field.key.value)? {
return Ok(Some(field.value.clone()));
return Ok(Some(field.value));
}
}
Ordering::Greater => break,
Expand All @@ -117,12 +116,7 @@ impl Map {
let contents = self.0.lock().expect("poisoned");
contents
.get(index)
.map(|field| {
Value::dynamic(
List::from_iter([field.key.value.clone(), field.value.clone()]),
guard,
)
})
.map(|field| Value::dynamic(List::from_iter([field.key.value, field.value]), guard))
.ok_or(Fault::OutOfBounds)
}

Expand Down
11 changes: 6 additions & 5 deletions src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use refuse::{CollectionGuard, ContainsNoRefs};
use regex::{Captures, Regex};

use crate::string::MuseString;
use crate::symbol::Symbol;
use crate::symbol::{Symbol, SymbolRef};
use crate::syntax::token::RegexLiteral;
use crate::value::{
AnyDynamic, CustomType, Rooted, RustType, StaticRustFunctionTable, TypeRef, Value,
Expand Down Expand Up @@ -127,7 +127,7 @@ impl CustomType for MuseRegex {
}
})
.with_truthy(|_| |this, _vm| !this.expr.as_str().is_empty())
.with_to_string(|_| |this, _vm| Ok(Symbol::from(this.expr.as_str().to_string())))
.with_to_string(|_| |this, _vm| Ok(SymbolRef::from(this.expr.as_str().to_string())))
.with_clone()
.with_matches(|_| {
|this, vm, rhs| {
Expand Down Expand Up @@ -215,14 +215,15 @@ impl CustomType for MuseCaptures {
let index = if let Some(index) = index.as_usize() {
index
} else {
let name = index.to_string(vm)?;
let name =
index.to_string(vm)?.try_upgrade(vm.guard())?;
let Some(index) = this.by_name.get(&name).copied()
else {
return Ok(Value::Nil);
};
index
};
this.matches.get(index).cloned().ok_or(Fault::OutOfBounds)
this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
},
)
.with_fn(
Expand All @@ -231,7 +232,7 @@ impl CustomType for MuseCaptures {
|vm: &mut VmContext<'_, '_>, this: &Rooted<MuseCaptures>| {
let index =
vm[Register(0)].as_usize().ok_or(Fault::OutOfBounds)?;
this.matches.get(index).cloned().ok_or(Fault::OutOfBounds)
this.matches.get(index).copied().ok_or(Fault::OutOfBounds)
},
)
});
Expand Down
27 changes: 15 additions & 12 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use refuse::ContainsNoRefs;

use crate::list::List;
use crate::regex::MuseRegex;
use crate::symbol::Symbol;
use crate::symbol::{Symbol, SymbolRef};
use crate::value::{
AnyDynamic, CustomType, Dynamic, Rooted, RustType, StaticRustFunctionTable, TypeRef, Value,
};
Expand Down Expand Up @@ -42,27 +42,30 @@ pub static STRING_TYPE: RustType<MuseString> = RustType::new("String", |t| {
if let Some(dynamic) = vm[Register(0)].as_dynamic::<MuseString>() {
Ok(dynamic)
} else {
let value = vm[Register(0)].take().to_string(vm)?;
Ok(Dynamic::new(MuseString::from(&*value), vm))
let value = vm[Register(0)].take().to_string(vm)?.try_load(vm.guard())?;
Ok(Dynamic::new(MuseString::from(value), vm))
}
} else if let Ok(length) = (0..arity.0).try_fold(0_usize, |accum, r| {
vm[Register(r)]
.as_symbol()
.as_symbol(vm.guard())
.map_or(Err(()), |sym| Ok(accum + sym.len()))
}) {
let mut joined = String::with_capacity(length);
for r in 0..arity.0 {
let Some(sym) = vm[Register(r)].as_symbol() else {
let Some(sym) = vm[Register(r)].as_symbol(vm.guard()) else {
unreachable!()
};
joined.push_str(sym);
joined.push_str(&sym);
}

Ok(Dynamic::new(MuseString::from(joined), vm))
} else {
let mut joined = String::new();
for r in 0..arity.0 {
let sym = vm[Register(r)].take().to_string(vm)?;
let sym = vm[Register(r)]
.take()
.to_string(vm)?
.try_upgrade(vm.guard())?;
joined.push_str(&sym);
}

Expand All @@ -79,8 +82,8 @@ pub static STRING_TYPE: RustType<MuseString> = RustType::new("String", |t| {
|this, vm, rhs| {
if let Some(rhs) = rhs.as_downcast_ref::<MuseString>(vm.as_ref()) {
Ok(*this.0.lock().expect("poisoned") == *rhs.0.lock().expect("poisoned"))
} else if let Some(rhs) = rhs.as_symbol() {
Ok(*this.0.lock().expect("poisoned") == **rhs)
} else if let Some(rhs) = rhs.as_symbol(vm.as_ref()) {
Ok(*this.0.lock().expect("poisoned") == *rhs)
} else {
Ok(false)
}
Expand Down Expand Up @@ -182,7 +185,7 @@ pub static STRING_TYPE: RustType<MuseString> = RustType::new("String", |t| {
Ok(Value::dynamic(MuseString::from(combined), &vm))
}
} else {
let rhs = rhs.to_string(vm)?;
let rhs = rhs.to_string(vm)?.try_upgrade(vm.guard())?;
let mut combined = String::with_capacity(lhs.len() + rhs.len());
combined.push_str(&lhs);
combined.push_str(&rhs);
Expand All @@ -192,7 +195,7 @@ pub static STRING_TYPE: RustType<MuseString> = RustType::new("String", |t| {
})
.with_add_right(|_| {
|this, vm, lhs| {
let lhs = lhs.to_string(vm)?;
let lhs = lhs.to_string(vm)?.try_upgrade(vm.guard())?;
let rhs = this.0.lock().expect("poisoned");
let mut combined = String::with_capacity(rhs.len() + lhs.len());
combined.push_str(&lhs);
Expand Down Expand Up @@ -221,7 +224,7 @@ pub static STRING_TYPE: RustType<MuseString> = RustType::new("String", |t| {
}
})
.with_truthy(|_| |this, _vm| !this.0.lock().expect("poisoned").is_empty())
.with_to_string(|_| |this, _vm| Ok(Symbol::from(&*this.0.lock().expect("poisoned"))))
.with_to_string(|_| |this, _vm| Ok(SymbolRef::from(&*this.0.lock().expect("poisoned"))))
.with_deep_clone(|_| {
|this, guard| {
Some(AnyDynamic::new(
Expand Down
Loading

0 comments on commit 0b0f01c

Please sign in to comment.