feat: gc (does compile, but WIP)
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use derive_more::Constructor;
|
||||
use itertools::Itertools;
|
||||
use gc_arena::{Collect, Gc, Mutation};
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::env::VmEnv;
|
||||
use crate::error::Result;
|
||||
@@ -13,25 +14,25 @@ use super::super::public as p;
|
||||
use super::Value;
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Constructor, Clone, PartialEq)]
|
||||
#[derive(Constructor, Clone, PartialEq, Collect)]
|
||||
#[collect(no_drop)]
|
||||
pub struct AttrSet<'gc> {
|
||||
data: HashMap<usize, Value<'gc>>,
|
||||
}
|
||||
|
||||
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for AttrSet<'gc> {
|
||||
fn trace(&self, cc: &gc_arena::Collection) {
|
||||
for (_, v) in self.data.iter() {
|
||||
v.trace(cc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> From<HashMap<usize, Value<'gc>>> for AttrSet<'gc> {
|
||||
fn from(data: HashMap<usize, Value<'gc>>) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Deref for AttrSet<'gc> {
|
||||
type Target = HashMap<usize, Value<'gc>>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
|
||||
impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
||||
pub fn with_capacity(cap: usize) -> Self {
|
||||
AttrSet {
|
||||
@@ -61,7 +62,7 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
||||
pub fn capture(&mut self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
|
||||
self.data.iter().for_each(|(_, v)| {
|
||||
if let Value::Thunk(ref thunk) = v.clone() {
|
||||
thunk.capture(env, mc);
|
||||
thunk.capture_env(env, mc);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -85,12 +86,13 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
||||
}
|
||||
|
||||
pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> {
|
||||
let mut map: Vec<_> = self.data.iter().map(|(k, v)| (*k, v.clone())).collect();
|
||||
todo!()
|
||||
/* let mut map: Vec<_> = self.data.iter().map(|(k, v)| (*k, v.clone())).collect();
|
||||
for (_, v) in map.iter_mut() {
|
||||
v.force_deep(vm, mc)?;
|
||||
}
|
||||
self.data = map.into_iter().collect();
|
||||
Ok(())
|
||||
Ok(()) */
|
||||
}
|
||||
|
||||
pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool {
|
||||
@@ -102,7 +104,7 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
|
||||
.all(|((_, v1), (_, v2))| v1.eq_impl(v2))
|
||||
}
|
||||
|
||||
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 {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data
|
||||
.iter()
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
use std::cell::Cell;
|
||||
|
||||
use derive_more::Constructor;
|
||||
use gc_arena::lock::GcRefLock;
|
||||
use gc_arena::lock::{GcRefLock, RefLock};
|
||||
use gc_arena::{Arena, Collect, Gc, Mutation, Rootable};
|
||||
use hashbrown::HashMap;
|
||||
use inkwell::execution_engine::JitFunction;
|
||||
use itertools::Itertools;
|
||||
use gc_arena::{Collect, Gc, Mutation};
|
||||
|
||||
use crate::bytecode::Func as BFunc;
|
||||
use crate::env::VmEnv;
|
||||
@@ -13,7 +11,7 @@ use crate::error::Result;
|
||||
use crate::ir;
|
||||
use crate::jit::JITFunc;
|
||||
use crate::ty::internal::{Thunk, Value};
|
||||
use crate::vm::VM;
|
||||
use crate::vm::{GcRoot, VM, eval};
|
||||
|
||||
#[derive(Debug, Clone, Collect)]
|
||||
#[collect(no_drop)]
|
||||
@@ -46,7 +44,7 @@ impl From<ir::Param> for Param {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Constructor)]
|
||||
#[derive(Clone)]
|
||||
pub struct Func<'gc> {
|
||||
pub func: &'gc BFunc,
|
||||
pub env: Gc<'gc, VmEnv<'gc>>,
|
||||
@@ -54,17 +52,33 @@ pub struct Func<'gc> {
|
||||
pub count: Cell<usize>,
|
||||
}
|
||||
|
||||
unsafe impl<'gc> Collect for Func<'gc> {
|
||||
fn trace(&self, cc: &gc_arena::Collection) {
|
||||
unsafe impl<'gc> Collect<'gc> for Func<'gc> {
|
||||
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||
self.env.trace(cc);
|
||||
self.compiled.trace(cc);
|
||||
self.count.trace(cc);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
||||
pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
||||
use Param::*;
|
||||
impl<'gc> Func<'gc> {
|
||||
pub fn new(func: &'gc BFunc, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) -> Self {
|
||||
Self {
|
||||
func,
|
||||
env,
|
||||
compiled: Gc::new(mc, RefLock::new(None)),
|
||||
count: Cell::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(
|
||||
&self,
|
||||
arg: Value<'gc>,
|
||||
vm: &'gc VM<'gc>,
|
||||
mc: &Mutation<'gc>,
|
||||
arena: &Arena<impl for<'a> Rootable<'a, Root = GcRoot<'a>>>,
|
||||
) -> Result<Value<'gc>> {
|
||||
todo!()
|
||||
/* use Param::*;
|
||||
|
||||
let mut env = self.env;
|
||||
env = match self.func.param.clone() {
|
||||
@@ -75,8 +89,7 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
||||
alias,
|
||||
} => {
|
||||
let arg = arg.unwrap_attr_set();
|
||||
let mut new =
|
||||
HashMap::with_capacity(formals.len() + alias.iter().len());
|
||||
let mut new = HashMap::with_capacity(formals.len() + alias.iter().len());
|
||||
if !ellipsis
|
||||
&& arg
|
||||
.as_inner()
|
||||
@@ -91,7 +104,8 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
||||
let arg = arg
|
||||
.select(formal)
|
||||
.or_else(|| {
|
||||
default.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx), mc).into()))
|
||||
default
|
||||
.map(|idx| Value::Thunk(Thunk::new(vm.get_thunk(idx), mc).into()))
|
||||
})
|
||||
.unwrap();
|
||||
new.insert(formal, arg);
|
||||
@@ -106,15 +120,18 @@ impl<'jit: 'vm, 'vm, 'gc> Func<'gc> {
|
||||
let count = self.count.get();
|
||||
self.count.replace(count + 1);
|
||||
if count >= 1 {
|
||||
let compiled = self.compiled.borrow_mut(mc).get_or_insert_with(|| vm.compile_func(self.func));
|
||||
let ret = unsafe { compiled.call(vm as *const VM, env.as_ref() as *const VmEnv) };
|
||||
let compiled = &mut *self.compiled.borrow_mut(mc);
|
||||
let compiled = compiled.get_or_insert_with(|| vm.compile_func(self.func));
|
||||
let ret = unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) };
|
||||
return Ok(ret.into());
|
||||
}
|
||||
vm.eval(self.func.opcodes.iter().copied(), env)
|
||||
eval(self.func.opcodes.iter().copied(), arena, |val, _| {
|
||||
Ok(val)
|
||||
}) */
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Func<'_, '_, '_> {
|
||||
impl PartialEq for Func<'_> {
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
use hashbrown::HashSet;
|
||||
|
||||
use derive_more::Constructor;
|
||||
use gc_arena::{Arena, Collect, Rootable};
|
||||
use rpds::Vector;
|
||||
use gc_arena::{Collect, Mutation};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::ty::public as p;
|
||||
use crate::vm::VM;
|
||||
use crate::vm::{GcRoot, VM};
|
||||
|
||||
use super::Value;
|
||||
|
||||
#[derive(Constructor, Clone, PartialEq)]
|
||||
pub struct List<'gc> {
|
||||
data: Vector<Value<'gc>>
|
||||
data: Vector<Value<'gc>>,
|
||||
}
|
||||
|
||||
unsafe impl Collect for List<'_> {
|
||||
fn trace(&self, cc: &gc_arena::Collection) {
|
||||
unsafe impl<'gc> Collect<'gc> for List<'gc> {
|
||||
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||
for v in self.data.iter() {
|
||||
v.trace(cc);
|
||||
}
|
||||
@@ -40,15 +40,6 @@ impl<'gc> List<'gc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> {
|
||||
let mut vec: Vec<_> = self.data.iter().cloned().collect();
|
||||
for v in vec.iter_mut() {
|
||||
v.force_deep(vm, mc)?;
|
||||
}
|
||||
self.data = vec.into_iter().collect();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||
p::Value::List(p::List::new(
|
||||
self.data
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,41 @@
|
||||
use derive_more::Constructor;
|
||||
use gc_arena::Mutation;
|
||||
use gc_arena::Collect;
|
||||
use gc_arena::Mutation;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::vm::VM;
|
||||
|
||||
use super::Value;
|
||||
use super::CoW;
|
||||
use super::Value;
|
||||
|
||||
#[derive(Debug, Clone, Constructor, Collect)]
|
||||
#[collect(require_static)]
|
||||
pub struct PrimOp<'gc> {
|
||||
pub struct PrimOp {
|
||||
pub name: &'static str,
|
||||
arity: usize,
|
||||
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||
func: for<'gc> fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||
}
|
||||
|
||||
impl PartialEq for PrimOp<'_> {
|
||||
impl PartialEq for PrimOp {
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> PrimOp<'gc> {
|
||||
pub fn call(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
||||
impl PrimOp {
|
||||
pub fn call<'gc>(&self, arg: Value<'gc>, vm: &VM, mc: &Mutation<'gc>) -> Result<Value<'gc>> {
|
||||
let mut args = Vec::with_capacity(self.arity);
|
||||
args.push(arg);
|
||||
if self.arity > 1 {
|
||||
Value::PartialPrimOp(
|
||||
CoW::new(PartialPrimOp {
|
||||
Value::PartialPrimOp(CoW::new(
|
||||
PartialPrimOp {
|
||||
name: self.name,
|
||||
arity: self.arity - 1,
|
||||
args,
|
||||
func: self.func,
|
||||
}, mc)
|
||||
)
|
||||
},
|
||||
mc,
|
||||
))
|
||||
.ok()
|
||||
} else {
|
||||
(self.func)(args, vm, mc)
|
||||
@@ -50,8 +51,8 @@ pub struct PartialPrimOp<'gc> {
|
||||
func: fn(Vec<Value<'gc>>, &VM, &Mutation<'gc>) -> Result<Value<'gc>>,
|
||||
}
|
||||
|
||||
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for PartialPrimOp<'gc> {
|
||||
fn trace(&self, cc: &gc_arena::Collection) {
|
||||
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect<'gc> for PartialPrimOp<'gc> {
|
||||
fn trace<Tr: gc_arena::collect::Trace<'gc>>(&self, cc: &mut Tr) {
|
||||
for v in self.args.iter() {
|
||||
v.trace(cc);
|
||||
}
|
||||
@@ -69,7 +70,7 @@ impl<'gc> PartialPrimOp<'gc> {
|
||||
self: &mut CoW<'gc, Self>,
|
||||
arg: Value<'gc>,
|
||||
vm: &VM,
|
||||
mc: &Mutation<'gc>
|
||||
mc: &Mutation<'gc>,
|
||||
) -> Result<Value<'gc>> {
|
||||
let func = self.func;
|
||||
let self_mut = self.make_mut(mc);
|
||||
|
||||
Reference in New Issue
Block a user