-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* A first implementation of CharTable * Implement Trace, CloneIn, and IntoObject for CharTable * Implement display_walk for LispChartable * Create todo!() aset/aref for ObjectType::CharTable * Implemented aref for LispCharTable * Fix clippy elided lifetime * Rename LispCharTable and CharTable * Return NIL in data.rs instead of get in chartab * defsym! CHAR_TABLE * Implement Display for CharTable and remove display_walk * Wrap data of CharTableInner with RefCell --------- Co-authored-by: Troy Hinckley <[email protected]>
- Loading branch information
1 parent
44aedd4
commit a97f4eb
Showing
8 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::core::object::{CharTableInner, Object, Symbol}; | ||
use rune_macros::defun; | ||
|
||
#[defun] | ||
fn make_char_table<'ob>(_purpose: Symbol<'ob>, init: Option<Object<'ob>>) -> CharTableInner<'ob> { | ||
CharTableInner::new(init) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use super::{CloneIn, Gc, IntoObject, Object, WithLifetime}; | ||
use crate::{ | ||
core::gc::{Block, GcHeap, Slot}, | ||
derive_markable, | ||
}; | ||
use rune_core::hashmap::HashMap; | ||
use rune_macros::Trace; | ||
use std::{cell::RefCell, fmt}; | ||
|
||
#[derive(Debug, Eq, Trace)] | ||
pub struct CharTableInner<'ob> { | ||
parent: Option<Slot<&'ob CharTable>>, | ||
data: RefCell<HashMap<usize, Object<'ob>>>, | ||
init: Option<Object<'ob>>, | ||
} | ||
|
||
impl<'ob> CharTableInner<'ob> { | ||
pub fn new(init: Option<Object<'ob>>) -> Self { | ||
CharTableInner { parent: None, data: RefCell::new(HashMap::default()), init } | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq, Trace, Debug)] | ||
pub(crate) struct CharTable(GcHeap<CharTableInner<'static>>); | ||
|
||
derive_markable!(CharTable); | ||
|
||
impl PartialEq for CharTableInner<'_> { | ||
fn eq(&self, other: &Self) -> bool { | ||
std::ptr::eq(self, other) | ||
} | ||
} | ||
|
||
impl<'new> CloneIn<'new, &'new Self> for CharTable { | ||
fn clone_in<const C: bool>(&self, bk: &'new Block<C>) -> Gc<&'new Self> { | ||
let parent = self.0.parent.as_ref().map(|p| Slot::new(p.clone_in(bk).untag())); | ||
|
||
let mut data = HashMap::default(); | ||
for (key, value) in self.0.data.borrow().iter() { | ||
let new_value = value.clone_in(bk); | ||
data.insert(*key, new_value); | ||
} | ||
let data = RefCell::new(data); | ||
let init = self.0.init.map(|i| i.clone_in(bk)); | ||
CharTableInner { parent, data, init }.into_obj(bk) | ||
} | ||
} | ||
|
||
impl CharTable { | ||
pub(in crate::core) unsafe fn new(table: CharTableInner<'_>, constant: bool) -> Self { | ||
// transmute lifetime to static | ||
let table = | ||
unsafe { std::mem::transmute::<CharTableInner<'_>, CharTableInner<'static>>(table) }; | ||
Self(GcHeap::new(table, constant)) | ||
} | ||
|
||
pub fn get(&self, idx: usize) -> Option<Object> { | ||
self.0.data.borrow().get(&idx).copied().or(self.0.init) | ||
} | ||
|
||
pub fn set(&self, idx: usize, item: Object) { | ||
unsafe { self.0.data.borrow_mut().insert(idx, item.with_lifetime()) }; | ||
} | ||
} | ||
|
||
impl fmt::Display for CharTable { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "[")?; | ||
let data = self.0.data.borrow(); | ||
|
||
let mut entries: Vec<_> = data.iter().collect(); | ||
entries.sort_by_key(|&(key, _)| key); | ||
|
||
let mut iter = entries.into_iter(); | ||
if let Some((_, first)) = iter.next() { | ||
write!(f, "{}", first)?; | ||
for (_, value) in iter { | ||
write!(f, " {}", value)?; | ||
} | ||
} | ||
write!(f, "]") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters