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:
@@ -66,7 +66,7 @@ impl<'s> Compile<'s> {
|
|||||||
let quote =
|
let quote =
|
||||||
self.quote_table
|
self.quote_table
|
||||||
.insert(expr.span().clone(), locals, stmts.clone(), compiled);
|
.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)]
|
vec![SpInst::new(expr.span().clone(), inst)]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,9 +74,9 @@ impl<'s> Compile<'s> {
|
|||||||
|
|
||||||
fn compile_atom(&mut self, atom: &SpAtom) -> Vec<SpInst> {
|
fn compile_atom(&mut self, atom: &SpAtom) -> Vec<SpInst> {
|
||||||
let inst = match atom.inner() {
|
let inst = match atom.inner() {
|
||||||
Atom::Float(f) => Inst::PushValue(FloatObj::new(*f).into_gc()),
|
Atom::Float(f) => Inst::PushValue(FloatObj::new(*f)),
|
||||||
Atom::Int(i) => Inst::PushValue(IntObj::new(*i).into_gc()),
|
Atom::Int(i) => Inst::PushValue(IntObj::new(*i)),
|
||||||
Atom::Str(s) => Inst::PushValue(StrObj::new(s.clone()).into_gc()),
|
Atom::Str(s) => Inst::PushValue(StrObj::new(s.clone())),
|
||||||
Atom::Assign(text) => {
|
Atom::Assign(text) => {
|
||||||
let word = self.scope_stack.insert_local(text);
|
let word = self.scope_stack.insert_local(text);
|
||||||
Inst::Store(word)
|
Inst::Store(word)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::obj::{quote::Quote, Obj, VTable};
|
use crate::obj::{quote::Quote, Obj, VTable};
|
||||||
use crate::syn::span::Span;
|
use crate::syn::span::Span;
|
||||||
use crate::vm::{error::Result, machine::Machine};
|
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::fmt::{self, Debug};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -77,11 +77,11 @@ pub struct BuiltinFnObj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BuiltinFnObj {
|
impl BuiltinFnObj {
|
||||||
pub fn new(value: BuiltinFn) -> Self {
|
pub fn new(value: BuiltinFn) -> Gc<Self> {
|
||||||
BuiltinFnObj {
|
Gc::new(BuiltinFnObj {
|
||||||
value,
|
value,
|
||||||
vtable: vtable! {},
|
vtable: vtable! {},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::obj::{builtin::BuiltinExit, str::StrObj, Obj, ObjPtr, VTable};
|
use crate::obj::{builtin::BuiltinExit, str::StrObj, Obj, ObjPtr, VTable};
|
||||||
use crate::syn::span::Span;
|
use crate::syn::span::Span;
|
||||||
use crate::vm::{error::*, machine::Machine};
|
use crate::vm::{error::*, machine::Machine};
|
||||||
use gc::{Finalize, Trace};
|
use gc::{Finalize, Gc, Trace};
|
||||||
|
|
||||||
pub type Float = f64;
|
pub type Float = f64;
|
||||||
|
|
||||||
@@ -12,12 +12,13 @@ pub struct FloatObj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 float_str: ObjPtr = builtin_fn!("__str__", |machine, _| {
|
||||||
let obj_ptr: ObjPtr = machine.stack_pop()?;
|
let obj_ptr: ObjPtr = machine.stack_pop()?;
|
||||||
let obj: &(dyn Obj + 'static) = &*obj_ptr;
|
let obj: &(dyn Obj + 'static) = &*obj_ptr;
|
||||||
if let Some(float) = obj.as_any().downcast_ref::<FloatObj>() {
|
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)?;
|
machine.stack_push(string)?;
|
||||||
Ok(BuiltinExit::Return)
|
Ok(BuiltinExit::Return)
|
||||||
} else {
|
} else {
|
||||||
@@ -25,12 +26,12 @@ impl FloatObj {
|
|||||||
}
|
}
|
||||||
// Create the builtin float string
|
// Create the builtin float string
|
||||||
});
|
});
|
||||||
FloatObj {
|
Gc::new(FloatObj {
|
||||||
value,
|
value,
|
||||||
vtable: vtable! {
|
vtable: vtable! {
|
||||||
"__str__" => float_str,
|
"__str__" => float_str,
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::obj::{Obj, VTable};
|
use crate::obj::{Obj, VTable};
|
||||||
use crate::syn::span::Span;
|
use crate::syn::span::Span;
|
||||||
use crate::vm::{error::*, machine::Machine};
|
use crate::vm::{error::*, machine::Machine};
|
||||||
use gc::{Finalize, Trace};
|
use gc::{Finalize, Gc, Trace};
|
||||||
|
|
||||||
pub type Int = i64;
|
pub type Int = i64;
|
||||||
|
|
||||||
@@ -12,11 +12,12 @@ pub struct IntObj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IntObj {
|
impl IntObj {
|
||||||
pub fn new(value: Int) -> Self {
|
pub fn new(value: Int) -> Gc<Self> {
|
||||||
IntObj {
|
// TODO : intern int value
|
||||||
|
Gc::new(IntObj {
|
||||||
value,
|
value,
|
||||||
vtable: vtable! {},
|
vtable: vtable! {},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|||||||
@@ -18,6 +18,5 @@ macro_rules! builtin_fn {
|
|||||||
$name.to_string(),
|
$name.to_string(),
|
||||||
$fun,
|
$fun,
|
||||||
))
|
))
|
||||||
.into_gc()
|
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,17 +54,5 @@ pub trait Obj: Trace + Finalize + Debug {
|
|||||||
self.vtable_mut().insert(key, value)
|
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);
|
fn as_any(&self) -> &(dyn Any + 'static);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use crate::obj::{Obj, VTable};
|
|||||||
use crate::syn::ast::SpStmt;
|
use crate::syn::ast::SpStmt;
|
||||||
use crate::vm::{error::*, machine::Machine};
|
use crate::vm::{error::*, machine::Machine};
|
||||||
use crate::{scope::Scope, syn::span::Span, vm::inst::SpInst};
|
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::fmt::Debug;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -27,11 +27,11 @@ pub struct QuoteObj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl QuoteObj {
|
impl QuoteObj {
|
||||||
pub fn new(value: Quote) -> Self {
|
pub fn new(value: Quote) -> Gc<Self> {
|
||||||
QuoteObj {
|
Gc::new(QuoteObj {
|
||||||
value,
|
value,
|
||||||
vtable: vtable! {},
|
vtable: vtable! {},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::obj::{builtin::BuiltinExit, Obj, ObjPtr, VTable};
|
use crate::obj::{builtin::BuiltinExit, Obj, ObjPtr, VTable};
|
||||||
use crate::syn::span::Span;
|
use crate::syn::span::Span;
|
||||||
use crate::vm::{error::*, machine::Machine};
|
use crate::vm::{error::*, machine::Machine};
|
||||||
use gc::{Finalize, Trace};
|
use gc::{Finalize, Gc, Trace};
|
||||||
|
|
||||||
pub type Str = String;
|
pub type Str = String;
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ pub struct StrObj {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 str_str: ObjPtr = builtin_fn!("__str__", |machine, _| {
|
||||||
let obj_ptr: ObjPtr = machine.stack_pop()?;
|
let obj_ptr: ObjPtr = machine.stack_pop()?;
|
||||||
let obj: &(dyn Obj + 'static) = &*obj_ptr;
|
let obj: &(dyn Obj + 'static) = &*obj_ptr;
|
||||||
@@ -23,12 +23,12 @@ impl StrObj {
|
|||||||
Err(RuntimeError::WrongValue("float".to_string()))
|
Err(RuntimeError::WrongValue("float".to_string()))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
StrObj {
|
Gc::new(StrObj {
|
||||||
value,
|
value,
|
||||||
vtable: vtable! {
|
vtable: vtable! {
|
||||||
"__str__" => str_str,
|
"__str__" => str_str,
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|||||||
@@ -332,7 +332,7 @@ impl MachineBuilder {
|
|||||||
) {
|
) {
|
||||||
self.register_global(
|
self.register_global(
|
||||||
name,
|
name,
|
||||||
BuiltinFnObj::new(BuiltinFn::new(name.to_string(), fun)).into_gc(),
|
BuiltinFnObj::new(BuiltinFn::new(name.to_string(), fun)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user