General changes across the runtime crate in support of compile module
* Compile module is able to compile bytecode (or so it seems...) * Runtime crate has had some new stuff added to it, mostly with objects and vtables. Still not 100% on the object method function call story, but I guess it'll be tackled when we get there. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -8,8 +8,9 @@ pub mod intern;
|
||||
pub mod names;
|
||||
pub mod str;
|
||||
pub mod sym;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
pub mod ty;
|
||||
#[cfg(test)] mod test;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::obj::{attrs::*, fun::*, int::*, intern::*, str::*, sym::*, ty::*, Obj, ObjRef};
|
||||
@@ -17,8 +18,8 @@ pub mod prelude {
|
||||
|
||||
use shredder::{Gc, Scan};
|
||||
use std::{
|
||||
ops::{Deref, DerefMut, CoerceUnsized},
|
||||
marker::Unsize,
|
||||
ops::{CoerceUnsized, Deref, DerefMut},
|
||||
sync::RwLock,
|
||||
};
|
||||
|
||||
@@ -30,11 +31,18 @@ use sym::Sym;
|
||||
//
|
||||
|
||||
pub trait Obj: Scan + std::fmt::Debug {
|
||||
fn vtable(&self) -> &Vtable;
|
||||
fn attrs(&self) -> &Attrs;
|
||||
fn attrs_mut(&mut self) -> Option<&mut Attrs>;
|
||||
|
||||
fn get_attr(&self, sym: &Sym) -> Option<&ObjRef> {
|
||||
self.attrs().get(&sym)
|
||||
fn get_attr(&self, sym: &Sym) -> Option<ObjRef> {
|
||||
self.attrs()
|
||||
.get(&sym)
|
||||
.cloned()
|
||||
.or_else(|| {
|
||||
let vtable = self.vtable().get();
|
||||
vtable.get(&sym).cloned()
|
||||
})
|
||||
}
|
||||
|
||||
fn set_attr(&mut self, sym: Sym, value: ObjRef) -> Option<ObjRef> {
|
||||
@@ -47,13 +55,21 @@ pub trait Obj: Scan + std::fmt::Debug {
|
||||
//
|
||||
|
||||
#[derive(Debug, Scan)]
|
||||
pub struct ObjRef<T: Obj + ?Sized + Send + Sync = (dyn Obj + Send + Sync + 'static)> {
|
||||
pub struct ObjRef<T = (dyn Obj + Send + Sync + 'static)>
|
||||
where
|
||||
T: Obj + ?Sized + Send + Sync,
|
||||
{
|
||||
gc: Gc<RwLock<T>>,
|
||||
}
|
||||
|
||||
impl<T: Obj + ?Sized + Send + Sync> Clone for ObjRef<T> {
|
||||
impl<T> Clone for ObjRef<T>
|
||||
where
|
||||
T: Obj + ?Sized + Send + Sync,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
ObjRef { gc: self.gc.clone() }
|
||||
ObjRef {
|
||||
gc: self.gc.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +77,10 @@ impl<T: Obj + ?Sized + Send + Sync> Clone for ObjRef<T> {
|
||||
// impl ObjRef
|
||||
//
|
||||
|
||||
impl<T: Obj + ?Sized + Send + Sync> ObjRef<T> {
|
||||
impl<T> ObjRef<T>
|
||||
where
|
||||
T: Obj + ?Sized + Send + Sync,
|
||||
{
|
||||
/// Check object reference equality.
|
||||
pub fn ref_eq(&self, other: &Self) -> bool {
|
||||
let lhs: &RwLock<T> = &*self.gc.get();
|
||||
@@ -70,7 +89,10 @@ impl<T: Obj + ?Sized + Send + Sync> ObjRef<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Obj + Send + Sync + 'static> ObjRef<T> {
|
||||
impl<T> ObjRef<T>
|
||||
where
|
||||
T: Obj + Send + Sync + 'static,
|
||||
{
|
||||
pub fn new(obj: T) -> Self {
|
||||
ObjRef {
|
||||
gc: Gc::new(RwLock::new(obj)),
|
||||
|
||||
Reference in New Issue
Block a user