chore: cleanup
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
|
||||
use ecow::EcoString;
|
||||
use derive_more::Constructor;
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::vm::VM;
|
||||
use crate::engine::Engine;
|
||||
|
||||
use super::super::public as p;
|
||||
use super::Value;
|
||||
@@ -13,17 +14,17 @@ use super::Value;
|
||||
#[repr(transparent)]
|
||||
#[derive(Constructor, Clone, PartialEq)]
|
||||
pub struct AttrSet<'gc> {
|
||||
data: HashMap<usize, Value<'gc>>,
|
||||
data: HashMap<EcoString, Value<'gc>>,
|
||||
}
|
||||
|
||||
impl<'gc> From<HashMap<usize, Value<'gc>>> for AttrSet<'gc> {
|
||||
fn from(data: HashMap<usize, Value<'gc>>) -> Self {
|
||||
impl<'gc> From<HashMap<EcoString, Value<'gc>>> for AttrSet<'gc> {
|
||||
fn from(data: HashMap<EcoString, Value<'gc>>) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gc> Deref for AttrSet<'gc> {
|
||||
type Target = HashMap<usize, Value<'gc>>;
|
||||
type Target = HashMap<EcoString, Value<'gc>>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.data
|
||||
}
|
||||
@@ -36,57 +37,57 @@ impl<'gc> AttrSet<'gc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_attr_force(&mut self, sym: usize, val: Value<'gc>) {
|
||||
pub fn push_attr_force(&mut self, sym: EcoString, val: Value<'gc>) {
|
||||
self.data.insert(sym, val);
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: usize, val: Value<'gc>) {
|
||||
pub fn push_attr(&mut self, sym: EcoString, val: Value<'gc>) {
|
||||
if self.data.get(&sym).is_some() {
|
||||
todo!()
|
||||
}
|
||||
self.data.insert(sym, val);
|
||||
}
|
||||
|
||||
pub fn select(&self, sym: usize) -> Option<Value<'gc>> {
|
||||
self.data.get(&sym).cloned()
|
||||
pub fn select(&self, sym: &EcoString) -> Option<Value<'gc>> {
|
||||
self.data.get(sym).cloned()
|
||||
}
|
||||
|
||||
pub fn has_attr(&self, sym: usize) -> bool {
|
||||
self.data.get(&sym).is_some()
|
||||
pub fn has_attr(&self, sym: &EcoString) -> bool {
|
||||
self.data.get(sym).is_some()
|
||||
}
|
||||
|
||||
pub fn update(&mut self, other: &AttrSet<'gc>) {
|
||||
for (k, v) in other.data.iter() {
|
||||
self.push_attr_force(*k, v.clone())
|
||||
self.push_attr_force(k.clone(), v.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_inner(&self) -> &HashMap<usize, Value<'gc>> {
|
||||
pub fn as_inner(&self) -> &HashMap<EcoString, Value<'gc>> {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<usize, Value<'gc>>> {
|
||||
pub fn into_inner(self: Rc<Self>) -> Rc<HashMap<EcoString, Value<'gc>>> {
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub fn from_inner(data: HashMap<usize, Value<'gc>>) -> Self {
|
||||
pub fn from_inner(data: HashMap<EcoString, Value<'gc>>) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
|
||||
pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool {
|
||||
self.data.iter().len() == other.data.iter().len()
|
||||
&& std::iter::zip(
|
||||
self.data.iter().sorted_by_key(|(k, _)| **k),
|
||||
self.data.iter().sorted_by_key(|(k, _)| **k),
|
||||
self.data.iter().sorted_by(|(a, _), (b, _)| a.cmp(b)),
|
||||
other.data.iter().sorted_by(|(a, _), (b, _)| a.cmp(b)),
|
||||
)
|
||||
.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, engine: &'gc Engine, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data
|
||||
.iter()
|
||||
.map(|(&sym, value)| (vm.get_sym(sym), value.to_public(vm, seen)))
|
||||
.map(|(sym, value)| (sym.as_str().into(), value.to_public(engine, seen)))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,67 +1,20 @@
|
||||
use std::cell::{Cell, OnceCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::bytecode::Func as BFunc;
|
||||
use crate::env::VmEnv;
|
||||
use crate::error::Result;
|
||||
|
||||
use crate::ir;
|
||||
use crate::jit::JITFunc;
|
||||
use crate::ty::internal::Value;
|
||||
use crate::vm::VM;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Param {
|
||||
Ident(usize),
|
||||
Formals {
|
||||
formals: Vec<(usize, Option<usize>)>,
|
||||
ellipsis: bool,
|
||||
alias: Option<usize>,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<ir::Param> for Param {
|
||||
fn from(value: ir::Param) -> Self {
|
||||
match value {
|
||||
ir::Param::Ident(ident) => Param::Ident(ident),
|
||||
ir::Param::Formals {
|
||||
formals,
|
||||
ellipsis,
|
||||
alias,
|
||||
} => Param::Formals {
|
||||
formals: formals
|
||||
.into_iter()
|
||||
.map(|(sym, default)| (sym, default.map(|default| default.idx)))
|
||||
.collect(),
|
||||
ellipsis,
|
||||
alias,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Func<'gc> {
|
||||
pub func: &'gc BFunc,
|
||||
pub func: &'gc ir::Func,
|
||||
pub env: Rc<VmEnv<'gc>>,
|
||||
pub compiled: OnceCell<JITFunc<'gc>>,
|
||||
pub count: Cell<usize>,
|
||||
}
|
||||
|
||||
impl<'gc> Func<'gc> {
|
||||
pub fn new(func: &'gc BFunc, env: Rc<VmEnv<'gc>>) -> Self {
|
||||
pub fn new(func: &'gc ir::Func, env: Rc<VmEnv<'gc>>) -> Self {
|
||||
Self {
|
||||
func,
|
||||
env,
|
||||
compiled: OnceCell::new(),
|
||||
count: Cell::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call_compile(&self, arg: Value<'gc>, vm: &'gc VM<'gc>) -> Result<Value<'gc>> {
|
||||
let env = self.env.clone().enter_arg(arg);
|
||||
let compiled = self.compiled.get_or_init(|| vm.compile_func(self.func));
|
||||
let ret = unsafe { compiled(env.as_ref() as *const VmEnv) };
|
||||
Ok(ret.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Func<'_> {
|
||||
|
||||
@@ -4,7 +4,7 @@ use hashbrown::HashSet;
|
||||
|
||||
use crate::env::VmEnv;
|
||||
use crate::ty::public as p;
|
||||
use crate::vm::VM;
|
||||
use crate::engine::Engine;
|
||||
|
||||
use super::Value;
|
||||
|
||||
@@ -52,11 +52,11 @@ impl<'gc> List<'gc> {
|
||||
self.data
|
||||
}
|
||||
|
||||
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||
pub fn to_public(&self, engine: &'gc Engine, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||
p::Value::List(p::List::new(
|
||||
self.data
|
||||
.iter()
|
||||
.map(|value| value.clone().to_public(vm, seen))
|
||||
.map(|value| value.clone().to_public(engine, seen))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -3,16 +3,16 @@ use std::hash::Hash;
|
||||
use std::rc::{Rc, Weak};
|
||||
|
||||
use derive_more::{IsVariant, Unwrap};
|
||||
use ecow::EcoString;
|
||||
use hashbrown::HashSet;
|
||||
use replace_with::replace_with_or_abort;
|
||||
|
||||
use super::common::*;
|
||||
use super::public as p;
|
||||
use super::public::{self as p, Symbol};
|
||||
|
||||
use crate::bytecode::OpCodes;
|
||||
use crate::env::VmEnv;
|
||||
use crate::error::*;
|
||||
use crate::vm::VM;
|
||||
use crate::engine::Engine;
|
||||
|
||||
mod attrset;
|
||||
mod func;
|
||||
@@ -30,12 +30,12 @@ pub enum Value<'gc> {
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
String(Rc<String>),
|
||||
String(EcoString),
|
||||
Null,
|
||||
Thunk(Thunk<'gc>),
|
||||
AttrSet(Rc<AttrSet<'gc>>),
|
||||
List(Rc<List<'gc>>),
|
||||
Catchable(Rc<String>),
|
||||
Catchable(EcoString),
|
||||
PrimOp(Rc<PrimOp>),
|
||||
PartialPrimOp(Rc<PartialPrimOp<'gc>>),
|
||||
Func(Rc<Func<'gc>>),
|
||||
@@ -151,7 +151,7 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(&mut self, arg: Self, vm: &VM) -> Result<()> {
|
||||
pub fn call(&mut self, arg: Self, vm: &Engine) -> Result<()> {
|
||||
use Value::*;
|
||||
if matches!(arg, Value::Catchable(_)) {
|
||||
*self = arg;
|
||||
@@ -239,7 +239,7 @@ impl<'gc> Value<'gc> {
|
||||
(Float(a), Int(b)) => Float(a + b as f64),
|
||||
(Float(a), Float(b)) => Float(a + b),
|
||||
(String(mut a), String(b)) => {
|
||||
Rc::make_mut(&mut a).push_str(&b);
|
||||
a.push_str(&b);
|
||||
String(a)
|
||||
}
|
||||
(a @ Value::Catchable(_), _) => a,
|
||||
@@ -282,7 +282,7 @@ impl<'gc> Value<'gc> {
|
||||
pub fn concat_string(&mut self, mut other: Self) -> &mut Self {
|
||||
match (self.coerce_to_string(), other.coerce_to_string()) {
|
||||
(Value::String(a), Value::String(b)) => {
|
||||
Rc::make_mut(a).push_str(b.as_str());
|
||||
a.push_str(b.as_str());
|
||||
}
|
||||
(_, Value::Catchable(_)) => *self = other,
|
||||
(Value::Catchable(_), _) => (),
|
||||
@@ -317,7 +317,7 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: usize, val: Self) -> &mut Self {
|
||||
pub fn push_attr(&mut self, sym: EcoString, val: Self) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
Rc::make_mut(attrs).push_attr(sym, val);
|
||||
} else if let Value::Catchable(_) = self {
|
||||
@@ -343,11 +343,11 @@ impl<'gc> Value<'gc> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, sym: usize, vm: &VM<'gc>) -> Result<&mut Self> {
|
||||
pub fn select(&mut self, sym: &EcoString) -> Result<&mut Self> {
|
||||
let val = match self {
|
||||
Value::AttrSet(attrs) => attrs
|
||||
.select(sym)
|
||||
.ok_or_else(|| Error::EvalError(format!("{} not found", vm.get_sym(sym)))),
|
||||
.ok_or_else(|| Error::EvalError(format!("{} not found", Symbol::from(sym.as_str())))),
|
||||
Value::Catchable(_) => return Ok(self),
|
||||
_ => Err(Error::EvalError(format!(
|
||||
"cannot select from {:?}",
|
||||
@@ -358,7 +358,7 @@ impl<'gc> Value<'gc> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn select_with_default(&mut self, sym: usize, default: Self) -> Result<&mut Self> {
|
||||
pub fn select_with_default(&mut self, sym: &EcoString, default: Self) -> Result<&mut Self> {
|
||||
let val = match self {
|
||||
Value::AttrSet(attrs) => attrs.select(sym).unwrap_or(default),
|
||||
Value::Catchable(_) => return Ok(self),
|
||||
@@ -373,7 +373,7 @@ impl<'gc> Value<'gc> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn has_attr(&mut self, sym: usize) -> &mut Self {
|
||||
pub fn has_attr(&mut self, sym: &EcoString) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
let val = Value::Bool(attrs.has_attr(sym));
|
||||
*self = val;
|
||||
@@ -393,7 +393,7 @@ impl<'gc> Value<'gc> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||
pub fn to_public(&self, vm: &'gc Engine, seen: &mut HashSet<Value<'gc>>) -> p::Value {
|
||||
use self::Value::*;
|
||||
use p::Value;
|
||||
if seen.contains(self) {
|
||||
@@ -408,11 +408,11 @@ impl<'gc> Value<'gc> {
|
||||
seen.insert(self.clone());
|
||||
list.to_public(vm, seen)
|
||||
}
|
||||
Catchable(catchable) => Value::Catchable(catchable.as_ref().clone().into()),
|
||||
Catchable(catchable) => Value::Catchable(catchable.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_str().into())),
|
||||
String(x) => Value::String(x.clone()),
|
||||
Null => Value::Const(Const::Null),
|
||||
Thunk(_) => Value::Thunk,
|
||||
PrimOp(primop) => Value::PrimOp(primop.name),
|
||||
@@ -428,6 +428,9 @@ pub struct Thunk<'gc> {
|
||||
pub thunk: Rc<RefCell<_Thunk<'gc>>>,
|
||||
}
|
||||
|
||||
// TODO: impl
|
||||
type OpCodes = ();
|
||||
|
||||
#[derive(IsVariant, Unwrap)]
|
||||
pub enum _Thunk<'gc> {
|
||||
Code(&'gc OpCodes, Option<Env<'gc>>),
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::rc::Rc;
|
||||
use derive_more::Constructor;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::vm::VM;
|
||||
use crate::engine::Engine;
|
||||
|
||||
use super::Value;
|
||||
|
||||
@@ -11,7 +11,7 @@ use super::Value;
|
||||
pub struct PrimOp {
|
||||
pub name: &'static str,
|
||||
arity: usize,
|
||||
func: for<'gc> fn(Vec<Value<'gc>>, &VM) -> Result<Value<'gc>>,
|
||||
func: for<'gc> fn(Vec<Value<'gc>>, &Engine) -> Result<Value<'gc>>,
|
||||
}
|
||||
|
||||
impl PartialEq for PrimOp {
|
||||
@@ -21,7 +21,7 @@ impl PartialEq for PrimOp {
|
||||
}
|
||||
|
||||
impl PrimOp {
|
||||
pub fn call<'gc>(&self, arg: Value<'gc>, vm: &VM) -> Result<Value<'gc>> {
|
||||
pub fn call<'gc>(&self, arg: Value<'gc>, ctx: &Engine) -> Result<Value<'gc>> {
|
||||
let mut args = Vec::with_capacity(self.arity);
|
||||
args.push(arg);
|
||||
if self.arity > 1 {
|
||||
@@ -33,7 +33,7 @@ impl PrimOp {
|
||||
}))
|
||||
.ok()
|
||||
} else {
|
||||
(self.func)(args, vm)
|
||||
(self.func)(args, ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ pub struct PartialPrimOp<'gc> {
|
||||
pub name: &'static str,
|
||||
arity: usize,
|
||||
args: Vec<Value<'gc>>,
|
||||
func: fn(Vec<Value<'gc>>, &VM) -> Result<Value<'gc>>,
|
||||
func: fn(Vec<Value<'gc>>, &Engine) -> Result<Value<'gc>>,
|
||||
}
|
||||
|
||||
impl PartialEq for PartialPrimOp<'_> {
|
||||
@@ -53,14 +53,14 @@ impl PartialEq for PartialPrimOp<'_> {
|
||||
}
|
||||
|
||||
impl<'gc> PartialPrimOp<'gc> {
|
||||
pub fn call(self: &mut Rc<Self>, arg: Value<'gc>, vm: &VM) -> Result<Value<'gc>> {
|
||||
pub fn call(self: &mut Rc<Self>, arg: Value<'gc>, ctx: &Engine) -> Result<Value<'gc>> {
|
||||
let func = self.func;
|
||||
let Some(ret) = ({
|
||||
let self_mut = Rc::make_mut(self);
|
||||
self_mut.args.push(arg);
|
||||
self_mut.arity -= 1;
|
||||
if self_mut.arity == 0 {
|
||||
Some(func(std::mem::take(&mut self_mut.args), vm))
|
||||
Some(func(std::mem::take(&mut self_mut.args), ctx))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user