diff --git a/src/compile.rs b/src/compile.rs index 322fd46..68462b2 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -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 { 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) diff --git a/src/obj/builtin.rs b/src/obj/builtin.rs index df4b73f..45a55b3 100644 --- a/src/obj/builtin.rs +++ b/src/obj/builtin.rs @@ -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 { + Gc::new(BuiltinFnObj { value, vtable: vtable! {}, - } + }) } #[allow(dead_code)] diff --git a/src/obj/float.rs b/src/obj/float.rs index 1d616ac..614a786 100644 --- a/src/obj/float.rs +++ b/src/obj/float.rs @@ -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 { + // 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::() { - 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)] diff --git a/src/obj/int.rs b/src/obj/int.rs index ca9137b..1a9bfe8 100644 --- a/src/obj/int.rs +++ b/src/obj/int.rs @@ -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 { + // TODO : intern int value + Gc::new(IntObj { value, vtable: vtable! {}, - } + }) } #[allow(dead_code)] diff --git a/src/obj/macros.rs b/src/obj/macros.rs index 2cc8d9c..b0ef6af 100644 --- a/src/obj/macros.rs +++ b/src/obj/macros.rs @@ -18,6 +18,5 @@ macro_rules! builtin_fn { $name.to_string(), $fun, )) - .into_gc() }}; } diff --git a/src/obj/mod.rs b/src/obj/mod.rs index 0b0aa1f..5ef2907 100644 --- a/src/obj/mod.rs +++ b/src/obj/mod.rs @@ -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 - 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); } diff --git a/src/obj/quote.rs b/src/obj/quote.rs index 855f66e..66f62c5 100644 --- a/src/obj/quote.rs +++ b/src/obj/quote.rs @@ -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 { + Gc::new(QuoteObj { value, vtable: vtable! {}, - } + }) } #[allow(dead_code)] diff --git a/src/obj/str.rs b/src/obj/str.rs index d0f7539..a0c03f6 100644 --- a/src/obj/str.rs +++ b/src/obj/str.rs @@ -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 { 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)] diff --git a/src/vm/machine.rs b/src/vm/machine.rs index e375afa..507096b 100644 --- a/src/vm/machine.rs +++ b/src/vm/machine.rs @@ -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)), ); }