chore: cleanup

This commit is contained in:
2025-06-08 00:59:31 +08:00
parent 0fd846e844
commit 3797544fc2
25 changed files with 1028 additions and 1481 deletions

View File

@@ -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(),
))
}