feat: gc (does compile, but WIP)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::cell::Cell;
|
||||
use std::hash::Hash;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::Deref;
|
||||
|
||||
@@ -30,22 +31,24 @@ pub use primop::*;
|
||||
|
||||
#[derive(Collect)]
|
||||
#[collect(unsafe_drop)]
|
||||
pub struct CoW<'gc, T: Clone + Collect> {
|
||||
inner: Gc<'gc, CoWInner<T>>,
|
||||
pub struct CoW<'gc, T: Clone + Collect<'gc>> {
|
||||
inner: Gc<'gc, CoWInner<'gc, T>>,
|
||||
}
|
||||
|
||||
struct CoWInner<T: Clone + Collect> {
|
||||
struct CoWInner<'gc, T: Clone + Collect<'gc>> {
|
||||
ref_count: Cell<usize>,
|
||||
data: MaybeUninit<T>,
|
||||
_marker: PhantomData<Cell<&'gc ()>>,
|
||||
}
|
||||
|
||||
unsafe impl<T: Clone + Collect> Collect for CoWInner<T> {
|
||||
fn trace(&self, cc: &gc_arena::Collection) {
|
||||
unsafe impl<'gc, T: Clone + Collect<'gc>> Collect<'gc> for CoWInner<'gc, T> {
|
||||
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||
unsafe { self.data.assume_init_ref() }.trace(cc);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, T: Clone + Collect> CoW<'gc, T> {
|
||||
#[allow(mutable_transmutes)]
|
||||
impl<'gc, T: Clone + Collect<'gc>> CoW<'gc, T> {
|
||||
pub fn new(data: T, mc: &Mutation<'gc>) -> Self {
|
||||
Self {
|
||||
inner: Gc::new(mc, CoWInner::new(data)),
|
||||
@@ -58,6 +61,7 @@ impl<'gc, T: Clone + Collect> CoW<'gc, T> {
|
||||
CoWInner {
|
||||
ref_count: Cell::new(1),
|
||||
data: MaybeUninit::uninit(),
|
||||
_marker: PhantomData,
|
||||
},
|
||||
);
|
||||
let data = datafn(CoW { inner });
|
||||
@@ -81,37 +85,38 @@ impl<'gc, T: Clone + Collect> CoW<'gc, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, T: Clone + Collect> AsRef<T> for CoW<'gc, T> {
|
||||
impl<'gc, T: Clone + Collect<'gc>> AsRef<T> for CoW<'gc, T> {
|
||||
fn as_ref(&self) -> &T {
|
||||
unsafe { self.inner.data.assume_init_ref() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, T: Clone + Collect> Deref for CoW<'gc, T> {
|
||||
impl<'gc, T: Clone + Collect<'gc>> Deref for CoW<'gc, T> {
|
||||
type Target = T;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, T: Clone + Collect> Clone for CoW<'gc, T> {
|
||||
impl<'gc, T: Clone + Collect<'gc>> Clone for CoW<'gc, T> {
|
||||
fn clone(&self) -> Self {
|
||||
self.inner.ref_count.set(self.inner.ref_count.get() + 1);
|
||||
Self { inner: self.inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc, T: Clone + Collect> Drop for CoW<'gc, T> {
|
||||
impl<'gc, T: Clone + Collect<'gc>> Drop for CoW<'gc, T> {
|
||||
fn drop(&mut self) {
|
||||
self.inner.ref_count.set(self.inner.ref_count.get() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + Collect> CoWInner<T> {
|
||||
impl<'gc, T: Clone + Collect<'gc>> CoWInner<'gc, T> {
|
||||
fn new(data: T) -> Self {
|
||||
Self {
|
||||
ref_count: Cell::new(1),
|
||||
data: MaybeUninit::new(data),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,12 +124,16 @@ impl<T: Clone + Collect> CoWInner<T> {
|
||||
#[derive(IsVariant, Unwrap, Clone, Collect)]
|
||||
#[collect(no_drop)]
|
||||
pub enum Value<'gc> {
|
||||
Const(Const),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
String(CoW<'gc, String>),
|
||||
Null,
|
||||
Thunk(Thunk<'gc>),
|
||||
AttrSet(CoW<'gc, AttrSet<'gc>>),
|
||||
List(CoW<'gc, List<'gc>>),
|
||||
Catchable(Catchable),
|
||||
PrimOp(Gc<'gc, PrimOp<'gc>>),
|
||||
Catchable(Gc<'gc, String>),
|
||||
PrimOp(Gc<'gc, PrimOp>),
|
||||
PartialPrimOp(CoW<'gc, PartialPrimOp<'gc>>),
|
||||
Func(Gc<'gc, Func<'gc>>),
|
||||
}
|
||||
@@ -134,7 +143,11 @@ impl Hash for Value<'_> {
|
||||
use Value::*;
|
||||
std::mem::discriminant(self).hash(state);
|
||||
match self {
|
||||
Const(x) => x.hash(state),
|
||||
Int(x) => x.hash(state),
|
||||
Float(x) => x.to_bits().hash(state),
|
||||
Bool(x) => x.hash(state),
|
||||
Null => (),
|
||||
String(x) => x.as_ptr().hash(state),
|
||||
Thunk(x) => (x as *const self::Thunk).hash(state),
|
||||
AttrSet(x) => x.as_ptr().hash(state),
|
||||
List(x) => x.as_ptr().hash(state),
|
||||
@@ -150,7 +163,13 @@ impl<'gc> Value<'gc> {
|
||||
fn eq_impl(&self, other: &Self) -> bool {
|
||||
use Value::*;
|
||||
match (self, other) {
|
||||
(Const(a), Const(b)) => a.eq(b),
|
||||
(Bool(a), Bool(b)) => a == b,
|
||||
(Int(a), Int(b)) => a == b,
|
||||
(Float(a), Float(b)) => a == b,
|
||||
(Int(a), Float(b)) => *a as f64 == *b,
|
||||
(Float(a), Int(b)) => *b as f64 == *a,
|
||||
(String(a), String(b)) => a.as_str().eq(b.as_str()),
|
||||
(Null, Null) => true,
|
||||
(AttrSet(a), AttrSet(b)) => a.eq_impl(b),
|
||||
(List(a), List(b)) => a.eq(b),
|
||||
_ => false,
|
||||
@@ -162,7 +181,6 @@ impl<'jit: 'vm, 'vm, 'gc> PartialEq for Value<'gc> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
use Value::*;
|
||||
match (self, other) {
|
||||
(Const(a), Const(b)) => a.eq(b),
|
||||
(AttrSet(a), AttrSet(b)) => a.as_ptr().eq(&b.as_ptr()),
|
||||
(List(a), List(b)) => a.as_ptr().eq(&b.as_ptr()),
|
||||
_ => false,
|
||||
@@ -174,24 +192,32 @@ impl Eq for Value<'_> {}
|
||||
|
||||
#[derive(IsVariant, Unwrap, Clone)]
|
||||
pub enum ValueAsRef<'v, 'gc> {
|
||||
Const(&'v Const),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
String(&'v str),
|
||||
Null,
|
||||
Thunk(&'v Thunk<'gc>),
|
||||
AttrSet(&'v AttrSet<'gc>),
|
||||
List(&'v List<'gc>),
|
||||
Catchable(&'v Catchable),
|
||||
PrimOp(&'v PrimOp<'gc>),
|
||||
Catchable(&'v str),
|
||||
PrimOp(&'v PrimOp),
|
||||
PartialPrimOp(&'v PartialPrimOp<'gc>),
|
||||
Func(&'v Func<'gc>),
|
||||
}
|
||||
|
||||
#[derive(IsVariant, Unwrap)]
|
||||
pub enum ValueAsMut<'v, 'gc> {
|
||||
Const(&'v mut Const),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
String(&'v str),
|
||||
Null,
|
||||
Thunk(&'v Thunk<'gc>),
|
||||
AttrSet(&'v mut AttrSet<'gc>),
|
||||
List(&'v mut List<'gc>),
|
||||
Catchable(&'v mut Catchable),
|
||||
PrimOp(&'v PrimOp<'gc>),
|
||||
Catchable(&'v str),
|
||||
PrimOp(&'v PrimOp),
|
||||
PartialPrimOp(&'v mut PartialPrimOp<'gc>),
|
||||
Func(&'v Func<'gc>),
|
||||
}
|
||||
@@ -201,7 +227,11 @@ impl<'gc, 'v> Value<'gc> {
|
||||
use Value::*;
|
||||
use ValueAsRef as R;
|
||||
match self {
|
||||
Const(x) => R::Const(x),
|
||||
Int(x) => R::Int(*x),
|
||||
Float(x) => R::Float(*x),
|
||||
Bool(x) => R::Bool(*x),
|
||||
String(x) => R::String(x),
|
||||
Null => R::Null,
|
||||
Thunk(x) => R::Thunk(x),
|
||||
AttrSet(x) => R::AttrSet(x),
|
||||
List(x) => R::List(x),
|
||||
@@ -216,7 +246,11 @@ impl<'gc, 'v> Value<'gc> {
|
||||
use Value::*;
|
||||
use ValueAsMut as M;
|
||||
match self {
|
||||
Const(x) => M::Const(x),
|
||||
Int(x) => M::Int(*x),
|
||||
Float(x) => M::Float(*x),
|
||||
Bool(x) => M::Bool(*x),
|
||||
String(x) => M::String(x),
|
||||
Null => M::Null,
|
||||
Thunk(x) => M::Thunk(x),
|
||||
AttrSet(x) => M::AttrSet(x.make_mut(mc)),
|
||||
List(x) => M::List(x.make_mut(mc)),
|
||||
@@ -227,8 +261,6 @@ impl<'gc, 'v> Value<'gc> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use Value::Const as VmConst;
|
||||
impl<'gc> Value<'gc> {
|
||||
pub fn ok(self) -> Result<Self> {
|
||||
Ok(self)
|
||||
@@ -237,11 +269,11 @@ impl<'gc> Value<'gc> {
|
||||
pub fn typename(&self) -> &'static str {
|
||||
use Value::*;
|
||||
match self {
|
||||
Const(self::Const::Int(_)) => "int",
|
||||
Const(self::Const::Float(_)) => "float",
|
||||
Const(self::Const::Bool(_)) => "bool",
|
||||
Const(self::Const::String(_)) => "string",
|
||||
Const(self::Const::Null) => "null",
|
||||
Int(_) => "int",
|
||||
Float(_) => "float",
|
||||
Bool(_) => "bool",
|
||||
String(_) => "string",
|
||||
Null => "null",
|
||||
Thunk(_) => "thunk",
|
||||
AttrSet(_) => "set",
|
||||
List(_) => "list",
|
||||
@@ -269,7 +301,7 @@ impl<'gc> Value<'gc> {
|
||||
*self = match self {
|
||||
PrimOp(func) => func.call(arg, vm, mc),
|
||||
PartialPrimOp(func) => func.call(arg, vm, mc),
|
||||
Value::Func(func) => func.call(arg, vm, mc),
|
||||
// Value::Func(func) => func.call(arg, vm, mc),
|
||||
Catchable(_) => return Ok(()),
|
||||
_ => todo!(),
|
||||
}?;
|
||||
@@ -277,18 +309,18 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
|
||||
pub fn not(&mut self) {
|
||||
use Const::*;
|
||||
use Value::*;
|
||||
*self = match &*self {
|
||||
VmConst(Bool(bool)) => VmConst(Bool(!bool)),
|
||||
Bool(bool) => Bool(!bool),
|
||||
Value::Catchable(_) => return,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn and(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
use Value::*;
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(*a && b)),
|
||||
(Bool(a), Bool(b)) => Bool(*a && b),
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
@@ -296,9 +328,9 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
|
||||
pub fn or(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
use Value::*;
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(*a || b)),
|
||||
(Bool(a), Bool(b)) => Bool(*a || b),
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
@@ -306,52 +338,52 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
|
||||
pub fn eq(&mut self, other: Self) {
|
||||
use Const::Bool;
|
||||
use Value::Bool;
|
||||
*self = match (&*self, other) {
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
(s, other) => VmConst(Bool(s.eq_impl(&other))),
|
||||
(s, other) => Bool(s.eq_impl(&other)),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn lt(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
*self = VmConst(Bool(match (&*self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => *a < b,
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => (*a as f64) < b,
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => *a < b as f64,
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => *a < b,
|
||||
(VmConst(String(a)), VmConst(String(b))) => a < &b,
|
||||
use Value::*;
|
||||
*self = Bool(match (&*self, other) {
|
||||
(Int(a), Int(b)) => *a < b,
|
||||
(Int(a), Float(b)) => (*a as f64) < b,
|
||||
(Float(a), Int(b)) => *a < b as f64,
|
||||
(Float(a), Float(b)) => *a < b,
|
||||
(String(a), String(b)) => a.as_str() < b.as_str(),
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => {
|
||||
*self = x;
|
||||
return;
|
||||
}
|
||||
_ => todo!(),
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn neg(&mut self) {
|
||||
use Const::*;
|
||||
use Value::*;
|
||||
*self = match &*self {
|
||||
VmConst(Int(int)) => VmConst(Int(-int)),
|
||||
VmConst(Float(float)) => VmConst(Float(-float)),
|
||||
Int(int) => Int(-int),
|
||||
Float(float) => Float(-float),
|
||||
Value::Catchable(_) => return,
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
pub fn add(&mut self, other: Self, mc: &Mutation<'gc>) {
|
||||
use Value::*;
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a + b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 + b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a + b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a + b)),
|
||||
(VmConst(String(a)), VmConst(String(b))) => {
|
||||
let mut string = a.clone();
|
||||
string.push_str(b.as_str());
|
||||
VmConst(String(string))
|
||||
(Int(a), Int(b)) => Int(a + b),
|
||||
(Int(a), Float(b)) => Float(*a as f64 + b),
|
||||
(Float(a), Int(b)) => Float(a + b as f64),
|
||||
(Float(a), Float(b)) => Float(a + b),
|
||||
(String(a), String(b)) => {
|
||||
let mut a = a.clone();
|
||||
a.make_mut(mc).push_str(b.as_str());
|
||||
String(a)
|
||||
}
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
@@ -360,12 +392,12 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
|
||||
pub fn mul(&mut self, other: Self) {
|
||||
use Const::*;
|
||||
use Value::*;
|
||||
*self = match (&*self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a * b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 * b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a * b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a * b)),
|
||||
(Int(a), Int(b)) => Int(a * b),
|
||||
(Int(a), Float(b)) => Float(*a as f64 * b),
|
||||
(Float(a), Int(b)) => Float(a * b as f64),
|
||||
(Float(a), Float(b)) => Float(a * b),
|
||||
(Value::Catchable(_), _) => return,
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
@@ -373,16 +405,16 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
|
||||
pub fn div(&mut self, other: Self) -> Result<()> {
|
||||
use Const::*;
|
||||
use Value::*;
|
||||
*self = match (&*self, other) {
|
||||
(_, VmConst(Int(0))) => return Err(Error::EvalError("division by zero".to_string())),
|
||||
(_, VmConst(Float(0.))) => {
|
||||
(_, Int(0)) => return Err(Error::EvalError("division by zero".to_string())),
|
||||
(_, Float(0.)) => {
|
||||
return Err(Error::EvalError("division by zero".to_string()));
|
||||
}
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a / b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(*a as f64 / b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a / b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a / b)),
|
||||
(Int(a), Int(b)) => Int(a / b),
|
||||
(Int(a), Float(b)) => Float(*a as f64 / b),
|
||||
(Float(a), Int(b)) => Float(a / b as f64),
|
||||
(Float(a), Float(b)) => Float(a / b),
|
||||
(Value::Catchable(_), _) => return Ok(()),
|
||||
(_, x @ Value::Catchable(_)) => x,
|
||||
_ => todo!(),
|
||||
@@ -390,9 +422,12 @@ impl<'gc> Value<'gc> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn concat_string(&mut self, mut other: Self) -> &mut Self {
|
||||
pub fn concat_string(&mut self, mut other: Self, mc: &Mutation<'gc>) -> &mut Self {
|
||||
match (self.coerce_to_string(), other.coerce_to_string()) {
|
||||
(VmConst(Const::String(a)), VmConst(Const::String(b))) => a.push_str(b.as_str()),
|
||||
(Value::String(a), Value::String(b)) => {
|
||||
let mut a = a.clone();
|
||||
a.make_mut(mc).push_str(b.as_str())
|
||||
},
|
||||
(_, Value::Catchable(_)) => *self = other,
|
||||
(Value::Catchable(_), _) => (),
|
||||
_ => todo!(),
|
||||
@@ -484,17 +519,17 @@ impl<'gc> Value<'gc> {
|
||||
|
||||
pub fn has_attr(&mut self, sym: usize) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
let val = VmConst(Const::Bool(attrs.has_attr(sym)));
|
||||
let val = Value::Bool(attrs.has_attr(sym));
|
||||
*self = val;
|
||||
} else if let Value::Catchable(_) = self {
|
||||
} else {
|
||||
*self = VmConst(Const::Bool(false));
|
||||
*self = Value::Bool(false);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn coerce_to_string(&mut self) -> &mut Self {
|
||||
if let VmConst(Const::String(_)) = self {
|
||||
if let Value::String(_) = self {
|
||||
} else if let Value::Catchable(_) = self {
|
||||
} else {
|
||||
todo!()
|
||||
@@ -502,33 +537,7 @@ impl<'gc> Value<'gc> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn force(&mut self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<&mut Self> {
|
||||
if let Value::Thunk(thunk) = self {
|
||||
let value = thunk.force(vm, mc)?.clone();
|
||||
*self = value
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn force_deep(&mut self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<&mut Self> {
|
||||
match self {
|
||||
Value::Thunk(thunk) => {
|
||||
let mut value = thunk.force(vm, mc)?.clone();
|
||||
let _ = value.force_deep(vm, mc)?;
|
||||
*self = value;
|
||||
}
|
||||
Value::List(list) => list.make_mut(mc).force_deep(vm, mc)?,
|
||||
Value::AttrSet(attrs) => attrs.make_mut(mc).force_deep(vm, mc)?,
|
||||
_ => (),
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn to_public(
|
||||
&self,
|
||||
vm: &VM<'gc>,
|
||||
seen: &mut HashSet<Value<'gc>>,
|
||||
) -> p::Value {
|
||||
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||
use self::Value::*;
|
||||
use p::Value;
|
||||
if seen.contains(self) {
|
||||
@@ -543,8 +552,12 @@ impl<'gc> Value<'gc> {
|
||||
seen.insert(self.clone());
|
||||
list.to_public(vm, seen)
|
||||
}
|
||||
Catchable(catchable) => Value::Catchable(catchable.clone()),
|
||||
Const(cnst) => Value::Const(cnst.clone()),
|
||||
Catchable(catchable) => Value::Catchable(catchable.as_ref().clone().into()),
|
||||
Int(x) => Value::Const(Const::Int(*x)),
|
||||
Float(x) => Value::Const(Const::Float(*x)),
|
||||
Bool(x) => Value::Const(Const::Bool(*x)),
|
||||
String(x) => Value::Const(Const::String(x.as_ref().into())),
|
||||
Null => Value::Const(Const::Null),
|
||||
Thunk(_) => Value::Thunk,
|
||||
PrimOp(primop) => Value::PrimOp(primop.name),
|
||||
PartialPrimOp(primop) => Value::PartialPrimOp(primop.name),
|
||||
@@ -562,16 +575,13 @@ pub struct Thunk<'gc> {
|
||||
|
||||
#[derive(IsVariant, Unwrap)]
|
||||
pub enum _Thunk<'gc> {
|
||||
Code(
|
||||
&'gc OpCodes,
|
||||
GcRefLock<'gc, Option<Gc<'gc, VmEnv<'gc>>>>,
|
||||
),
|
||||
Code(&'gc OpCodes, Option<Gc<'gc, VmEnv<'gc>>>),
|
||||
Suspended,
|
||||
Value(Value<'gc>),
|
||||
}
|
||||
|
||||
unsafe impl<'gc> Collect for _Thunk<'gc> {
|
||||
fn trace(&self, cc: &gc_arena::Collection) {
|
||||
unsafe impl<'gc> Collect<'gc> for _Thunk<'gc> {
|
||||
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, cc: &mut T) {
|
||||
use _Thunk::*;
|
||||
match self {
|
||||
Code(_, env) => env.trace(cc),
|
||||
@@ -584,48 +594,34 @@ unsafe impl<'gc> Collect for _Thunk<'gc> {
|
||||
impl<'gc> Thunk<'gc> {
|
||||
pub fn new(opcodes: &'gc OpCodes, mc: &Mutation<'gc>) -> Self {
|
||||
Thunk {
|
||||
thunk: Gc::new(
|
||||
mc,
|
||||
RefLock::new(_Thunk::Code(opcodes, Gc::new(mc, RefLock::new(None)))),
|
||||
),
|
||||
thunk: Gc::new(mc, RefLock::new(_Thunk::Code(opcodes, None))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn capture(&self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
||||
if let _Thunk::Code(_, envcell) = &*self.thunk.borrow() {
|
||||
envcell.borrow_mut(mc).insert(env);
|
||||
pub fn capture_env(&self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
||||
if let _Thunk::Code(_, envcell) = &mut *self.thunk.borrow_mut(mc) {
|
||||
let _ = envcell.insert(env);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn force(&self, vm: &VM<'gc>, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
||||
use _Thunk::*;
|
||||
match &*self.thunk.borrow() {
|
||||
Value(_) => {
|
||||
return Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } {
|
||||
Value(value) => value.clone(),
|
||||
_ => unreachable!(),
|
||||
});
|
||||
}
|
||||
Suspended => {
|
||||
return Err(Error::EvalError("infinite recursion encountered".into()));
|
||||
}
|
||||
Code(..) => (),
|
||||
}
|
||||
let (opcodes, env) =
|
||||
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended).unwrap_code();
|
||||
let env = env.as_ref().borrow().unwrap();
|
||||
let value = vm.eval(opcodes.iter().copied(), env)?;
|
||||
let _ = std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Value(value));
|
||||
Ok(match unsafe { &*(&*self.thunk.borrow() as *const _) } {
|
||||
Value(value) => value.clone(),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
pub fn suspend(&self, mc: &Mutation<'gc>) -> (&'gc OpCodes, Gc<'gc, VmEnv<'gc>>) {
|
||||
let _Thunk::Code(opcodes, env) =
|
||||
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended)
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
(opcodes, env.unwrap())
|
||||
}
|
||||
|
||||
pub fn value(&self) -> Option<Value<'gc>> {
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => Some(value.clone()),
|
||||
_ => None,
|
||||
pub fn insert_value(&self, value: Value<'gc>, mc: &Mutation<'gc>) {
|
||||
*self.thunk.borrow_mut(mc) = _Thunk::Value(value);
|
||||
}
|
||||
|
||||
pub fn get_value(&self) -> Option<Value<'gc>> {
|
||||
if let _Thunk::Value(val) = &*self.thunk.borrow() {
|
||||
Some(val.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user