Remove Obj::into_gc() and have Obj ctors return a Gc instead

Since we're nightly and using coerce_unsized, we can just coerce a
Gc<T: Obj> into a Gc<dyn Obj> for free.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-01-25 17:36:07 -08:00
parent 427d354857
commit ec8793a494
9 changed files with 28 additions and 39 deletions

View File

@@ -66,7 +66,7 @@ impl<'s> Compile<'s> {
let quote =
self.quote_table
.insert(expr.span().clone(), locals, stmts.clone(), compiled);
let inst = Inst::PushValue(QuoteObj::new(quote).into_gc());
let inst = Inst::PushValue(QuoteObj::new(quote));
vec![SpInst::new(expr.span().clone(), inst)]
}
}
@@ -74,9 +74,9 @@ impl<'s> Compile<'s> {
fn compile_atom(&mut self, atom: &SpAtom) -> Vec<SpInst> {
let inst = match atom.inner() {
Atom::Float(f) => Inst::PushValue(FloatObj::new(*f).into_gc()),
Atom::Int(i) => Inst::PushValue(IntObj::new(*i).into_gc()),
Atom::Str(s) => Inst::PushValue(StrObj::new(s.clone()).into_gc()),
Atom::Float(f) => Inst::PushValue(FloatObj::new(*f)),
Atom::Int(i) => Inst::PushValue(IntObj::new(*i)),
Atom::Str(s) => Inst::PushValue(StrObj::new(s.clone())),
Atom::Assign(text) => {
let word = self.scope_stack.insert_local(text);
Inst::Store(word)

View File

@@ -1,7 +1,7 @@
use crate::obj::{quote::Quote, Obj, VTable};
use crate::syn::span::Span;
use crate::vm::{error::Result, machine::Machine};
use gc::{unsafe_empty_trace, Finalize, Trace};
use gc::{unsafe_empty_trace, Finalize, Gc, Trace};
use std::fmt::{self, Debug};
use std::rc::Rc;
@@ -77,11 +77,11 @@ pub struct BuiltinFnObj {
}
impl BuiltinFnObj {
pub fn new(value: BuiltinFn) -> Self {
BuiltinFnObj {
pub fn new(value: BuiltinFn) -> Gc<Self> {
Gc::new(BuiltinFnObj {
value,
vtable: vtable! {},
}
})
}
#[allow(dead_code)]

View File

@@ -1,7 +1,7 @@
use crate::obj::{builtin::BuiltinExit, str::StrObj, Obj, ObjPtr, VTable};
use crate::syn::span::Span;
use crate::vm::{error::*, machine::Machine};
use gc::{Finalize, Trace};
use gc::{Finalize, Gc, Trace};
pub type Float = f64;
@@ -12,12 +12,13 @@ pub struct FloatObj {
}
impl FloatObj {
pub fn new(value: Float) -> Self {
pub fn new(value: Float) -> Gc<Self> {
// TODO : intern float value
let float_str: ObjPtr = builtin_fn!("__str__", |machine, _| {
let obj_ptr: ObjPtr = machine.stack_pop()?;
let obj: &(dyn Obj + 'static) = &*obj_ptr;
if let Some(float) = obj.as_any().downcast_ref::<FloatObj>() {
let string = StrObj::new(float.value.to_string()).into_gc();
let string = StrObj::new(float.value.to_string());
machine.stack_push(string)?;
Ok(BuiltinExit::Return)
} else {
@@ -25,12 +26,12 @@ impl FloatObj {
}
// Create the builtin float string
});
FloatObj {
Gc::new(FloatObj {
value,
vtable: vtable! {
"__str__" => float_str,
},
}
})
}
#[allow(dead_code)]

View File

@@ -1,7 +1,7 @@
use crate::obj::{Obj, VTable};
use crate::syn::span::Span;
use crate::vm::{error::*, machine::Machine};
use gc::{Finalize, Trace};
use gc::{Finalize, Gc, Trace};
pub type Int = i64;
@@ -12,11 +12,12 @@ pub struct IntObj {
}
impl IntObj {
pub fn new(value: Int) -> Self {
IntObj {
pub fn new(value: Int) -> Gc<Self> {
// TODO : intern int value
Gc::new(IntObj {
value,
vtable: vtable! {},
}
})
}
#[allow(dead_code)]

View File

@@ -18,6 +18,5 @@ macro_rules! builtin_fn {
$name.to_string(),
$fun,
))
.into_gc()
}};
}

View File

@@ -54,17 +54,5 @@ pub trait Obj: Trace + Finalize + Debug {
self.vtable_mut().insert(key, value)
}
/// Allocate a new GC pointer and convert it to a `dyn Obj` pointer.
fn into_gc(self) -> Gc<dyn Obj>
where
Self: Sized + 'static,
{
let gc = Gc::new(self);
unsafe {
let ptr = Gc::into_raw(gc) as *const dyn Obj;
Gc::from_raw(ptr)
}
}
fn as_any(&self) -> &(dyn Any + 'static);
}

View File

@@ -2,7 +2,7 @@ use crate::obj::{Obj, VTable};
use crate::syn::ast::SpStmt;
use crate::vm::{error::*, machine::Machine};
use crate::{scope::Scope, syn::span::Span, vm::inst::SpInst};
use gc::{unsafe_empty_trace, Finalize, Trace};
use gc::{unsafe_empty_trace, Finalize, Gc, Trace};
use std::fmt::Debug;
use std::rc::Rc;
@@ -27,11 +27,11 @@ pub struct QuoteObj {
}
impl QuoteObj {
pub fn new(value: Quote) -> Self {
QuoteObj {
pub fn new(value: Quote) -> Gc<Self> {
Gc::new(QuoteObj {
value,
vtable: vtable! {},
}
})
}
#[allow(dead_code)]

View File

@@ -1,7 +1,7 @@
use crate::obj::{builtin::BuiltinExit, Obj, ObjPtr, VTable};
use crate::syn::span::Span;
use crate::vm::{error::*, machine::Machine};
use gc::{Finalize, Trace};
use gc::{Finalize, Gc, Trace};
pub type Str = String;
@@ -12,7 +12,7 @@ pub struct StrObj {
}
impl StrObj {
pub fn new(value: String) -> Self {
pub fn new(value: String) -> Gc<Self> {
let str_str: ObjPtr = builtin_fn!("__str__", |machine, _| {
let obj_ptr: ObjPtr = machine.stack_pop()?;
let obj: &(dyn Obj + 'static) = &*obj_ptr;
@@ -23,12 +23,12 @@ impl StrObj {
Err(RuntimeError::WrongValue("float".to_string()))
}
});
StrObj {
Gc::new(StrObj {
value,
vtable: vtable! {
"__str__" => str_str,
},
}
})
}
#[allow(dead_code)]

View File

@@ -332,7 +332,7 @@ impl MachineBuilder {
) {
self.register_global(
name,
BuiltinFnObj::new(BuiltinFn::new(name.to_string(), fun)).into_gc(),
BuiltinFnObj::new(BuiltinFn::new(name.to_string(), fun)),
);
}