diff --git a/nova_vm/src/heap.rs b/nova_vm/src/heap.rs index fecc7eeda..d3a871002 100644 --- a/nova_vm/src/heap.rs +++ b/nova_vm/src/heap.rs @@ -37,16 +37,14 @@ use self::{ indexes::{BaseIndex, FunctionIndex, NumberIndex, ObjectIndex, StringIndex}, math::initialize_math_object, number::{initialize_number_heap, NumberHeapData}, - object::{ - initialize_object_heap, ObjectEntry, ObjectHeapData, PropertyDescriptor, PropertyKey, - }, + object::{initialize_object_heap, ObjectEntry, ObjectHeapData, PropertyDescriptor}, regexp::{initialize_regexp_heap, RegExpHeapData}, string::initialize_string_heap, symbol::{initialize_symbol_heap, SymbolHeapData}, }; use crate::{ execution::{Environments, Realm, RealmIdentifier}, - types::{Function, Number, Object, String, StringHeapData, Value}, + types::{Function, Number, Object, PropertyKey, String, StringHeapData, Value}, }; use wtf8::{Wtf8, Wtf8Buf}; @@ -275,7 +273,7 @@ impl<'ctx, 'host> Heap<'ctx, 'host> { &mut self, name: Value, length: u8, - uses_arguments: bool, + _uses_arguments: bool, // behaviour: Behaviour, ) -> FunctionIndex { let entries = vec![ diff --git a/nova_vm/src/heap/array.rs b/nova_vm/src/heap/array.rs index 2a789c1ed..b4c120770 100644 --- a/nova_vm/src/heap/array.rs +++ b/nova_vm/src/heap/array.rs @@ -1,18 +1,17 @@ +use super::{ + element_array::ElementsVector, + function::FunctionHeapData, + heap_constants::WellKnownSymbolIndexes, + indexes::{FunctionIndex, ObjectIndex}, + object::ObjectEntry, +}; use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, Heap, PropertyDescriptor, }, - types::{Object, Value}, -}; - -use super::{ - element_array::ElementsVector, - function::FunctionHeapData, - heap_constants::WellKnownSymbolIndexes, - indexes::{FunctionIndex, ObjectIndex}, - object::{ObjectEntry, PropertyKey}, + types::{Object, PropertyKey, Value}, }; #[derive(Debug, Clone, Copy)] diff --git a/nova_vm/src/heap/array_buffer.rs b/nova_vm/src/heap/array_buffer.rs index a183d06c2..b556c6544 100644 --- a/nova_vm/src/heap/array_buffer.rs +++ b/nova_vm/src/heap/array_buffer.rs @@ -1,16 +1,13 @@ use super::{ - function::FunctionHeapData, - heap_constants::WellKnownSymbolIndexes, - indexes::{ArrayBufferIndex, ObjectIndex}, - object::{ObjectEntry, PropertyKey}, + function::FunctionHeapData, heap_constants::WellKnownSymbolIndexes, indexes::ObjectIndex, + object::ObjectEntry, }; use crate::{ - execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, Heap, PropertyDescriptor, }, - types::{Object, Value}, + types::{Object, PropertyKey, Value}, }; use std::{ alloc::{alloc_zeroed, handle_alloc_error, Layout}, diff --git a/nova_vm/src/heap/bigint.rs b/nova_vm/src/heap/bigint.rs index 2f3ca62cc..70ee3d5a9 100644 --- a/nova_vm/src/heap/bigint.rs +++ b/nova_vm/src/heap/bigint.rs @@ -3,9 +3,9 @@ use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, - FunctionHeapData, Heap, ObjectEntry, PropertyDescriptor, PropertyKey, + FunctionHeapData, Heap, ObjectEntry, PropertyDescriptor, }, - types::{Object, Value}, + types::{Object, PropertyKey, Value}, }; use num_bigint_dig::BigInt; diff --git a/nova_vm/src/heap/boolean.rs b/nova_vm/src/heap/boolean.rs index 3ad74702b..4e8bfb836 100644 --- a/nova_vm/src/heap/boolean.rs +++ b/nova_vm/src/heap/boolean.rs @@ -1,15 +1,11 @@ +use super::{object::ObjectEntry, Heap}; use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, FunctionHeapData, PropertyDescriptor, }, - types::{Object, Value}, -}; - -use super::{ - object::{ObjectEntry, PropertyKey}, - Heap, + types::{Object, PropertyKey, Value}, }; pub fn initialize_boolean_heap(heap: &mut Heap) { diff --git a/nova_vm/src/heap/date.rs b/nova_vm/src/heap/date.rs index eaff453b3..accf48a6f 100644 --- a/nova_vm/src/heap/date.rs +++ b/nova_vm/src/heap/date.rs @@ -1,20 +1,18 @@ -use std::time::SystemTime; - +use super::{ + function::FunctionHeapData, + heap_constants::WellKnownSymbolIndexes, + indexes::{FunctionIndex, ObjectIndex}, + object::ObjectEntry, +}; use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, Heap, PropertyDescriptor, }, - types::{Object, Value}, -}; - -use super::{ - function::FunctionHeapData, - heap_constants::WellKnownSymbolIndexes, - indexes::{FunctionIndex, ObjectIndex}, - object::{ObjectEntry, PropertyKey}, + types::{Object, PropertyKey, Value}, }; +use std::time::SystemTime; #[derive(Debug, Clone, Copy)] pub struct DateHeapData { diff --git a/nova_vm/src/heap/element_array.rs b/nova_vm/src/heap/element_array.rs index ddecad424..d695b512f 100644 --- a/nova_vm/src/heap/element_array.rs +++ b/nova_vm/src/heap/element_array.rs @@ -1,10 +1,10 @@ use super::{ indexes::{ElementIndex, FunctionIndex}, - object::{ObjectEntry, PropertyDescriptor, PropertyKey}, + object::{ObjectEntry, PropertyDescriptor}, }; -use crate::{types::Value, Heap}; +use crate::types::{PropertyKey, Value}; use core::panic; -use std::{collections::HashMap, mem::MaybeUninit, num::NonZeroU16, vec}; +use std::{collections::HashMap, mem::MaybeUninit, num::NonZeroU16}; #[derive(Debug, Clone, Copy)] pub enum ElementArrayKey { @@ -789,8 +789,8 @@ impl ElementArrays { let (maybe_descriptor, maybe_value) = ElementDescriptor::from_property_descriptor(value); let key = match key { + PropertyKey::Integer(data) => Value::Integer(data), PropertyKey::SmallString(data) => Value::SmallString(data), - PropertyKey::Smi(data) => Value::from(data), PropertyKey::String(data) => Value::String(data), PropertyKey::Symbol(data) => Value::Symbol(data), }; diff --git a/nova_vm/src/heap/error.rs b/nova_vm/src/heap/error.rs index e16dec45f..c16f71e6d 100644 --- a/nova_vm/src/heap/error.rs +++ b/nova_vm/src/heap/error.rs @@ -1,16 +1,15 @@ +use super::{ + function::FunctionHeapData, + indexes::{FunctionIndex, ObjectIndex}, + object::ObjectEntry, +}; use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, Heap, PropertyDescriptor, }, - types::{Object, Value}, -}; - -use super::{ - function::FunctionHeapData, - indexes::{FunctionIndex, ObjectIndex}, - object::{ObjectEntry, PropertyKey}, + types::{Object, PropertyKey, Value}, }; #[derive(Debug, Clone, Copy)] diff --git a/nova_vm/src/heap/function.rs b/nova_vm/src/heap/function.rs index bc176e010..988704364 100644 --- a/nova_vm/src/heap/function.rs +++ b/nova_vm/src/heap/function.rs @@ -1,17 +1,15 @@ +use super::{ + heap_constants::WellKnownSymbolIndexes, + indexes::{FunctionIndex, ObjectIndex}, + object::ObjectEntry, +}; use crate::{ - builtins::Behaviour, execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, Heap, PropertyDescriptor, }, - types::{Object, Value}, -}; - -use super::{ - heap_constants::WellKnownSymbolIndexes, - indexes::{FunctionIndex, ObjectIndex}, - object::{ObjectEntry, PropertyKey}, + types::{Object, PropertyKey, Value}, }; #[derive(Debug, Clone)] diff --git a/nova_vm/src/heap/heap_bits.rs b/nova_vm/src/heap/heap_bits.rs index 77e8cc557..2b8ae6a84 100644 --- a/nova_vm/src/heap/heap_bits.rs +++ b/nova_vm/src/heap/heap_bits.rs @@ -1,7 +1,3 @@ -use std::sync::atomic::AtomicBool; - -use crate::types::Value; - use super::{ indexes::{ ArrayBufferIndex, ArrayIndex, BigIntIndex, DateIndex, ElementIndex, ErrorIndex, @@ -9,6 +5,8 @@ use super::{ }, Heap, }; +use crate::types::Value; +use std::sync::atomic::AtomicBool; pub struct HeapBits { pub e_2_4: Box<[AtomicBool]>, diff --git a/nova_vm/src/heap/heap_gc.rs b/nova_vm/src/heap/heap_gc.rs index d9cbb617a..981e789ba 100644 --- a/nova_vm/src/heap/heap_gc.rs +++ b/nova_vm/src/heap/heap_gc.rs @@ -1,7 +1,3 @@ -use std::sync::atomic::Ordering; - -use crate::types::Value; - use super::{ element_array::ElementArrayKey, heap_bits::{HeapBits, WorkQueues}, @@ -11,6 +7,8 @@ use super::{ }, ElementsVector, Heap, }; +use crate::types::Value; +use std::sync::atomic::Ordering; pub fn heap_gc(heap: &mut Heap) { let bits = HeapBits::new(heap); diff --git a/nova_vm/src/heap/math.rs b/nova_vm/src/heap/math.rs index c46fb255f..a29309f94 100644 --- a/nova_vm/src/heap/math.rs +++ b/nova_vm/src/heap/math.rs @@ -1,10 +1,12 @@ -use crate::{execution::JsResult, types::Value}; - use super::{ heap_constants::WellKnownSymbolIndexes, - object::{ObjectEntry, PropertyDescriptor, PropertyKey}, + object::{ObjectEntry, PropertyDescriptor}, Heap, }; +use crate::{ + execution::JsResult, + types::{PropertyKey, Value}, +}; pub(super) fn initialize_math_object(heap: &mut Heap) { let e = Value::from_f64(heap, std::f64::consts::E); diff --git a/nova_vm/src/heap/number.rs b/nova_vm/src/heap/number.rs index 7afe65534..d9979ae23 100644 --- a/nova_vm/src/heap/number.rs +++ b/nova_vm/src/heap/number.rs @@ -1,14 +1,11 @@ -use super::{ - object::{ObjectEntry, PropertyKey}, - Heap, -}; +use super::{object::ObjectEntry, Heap}; use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, FunctionHeapData, PropertyDescriptor, }, - types::{Object, Value}, + types::{Object, PropertyKey, Value}, }; #[derive(Debug, Clone, Copy)] diff --git a/nova_vm/src/heap/object.rs b/nova_vm/src/heap/object.rs index cab3e5667..0bd9edee3 100644 --- a/nova_vm/src/heap/object.rs +++ b/nova_vm/src/heap/object.rs @@ -1,6 +1,6 @@ use super::{ element_array::ElementsVector, - indexes::{FunctionIndex, ObjectIndex, StringIndex, SymbolIndex}, + indexes::{FunctionIndex, ObjectIndex, SymbolIndex}, }; use crate::{ execution::JsResult, @@ -8,8 +8,7 @@ use crate::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, FunctionHeapData, Heap, }, - types::{Object, Value}, - SmallString, + types::{Object, PropertyKey, Value}, }; use std::{fmt::Debug, vec}; @@ -34,7 +33,7 @@ impl ObjectEntry { let key = PropertyKey::from_str(heap, name); let name = match key { PropertyKey::SmallString(data) => Value::SmallString(data.clone()), - PropertyKey::Smi(_) => unreachable!("No prototype functions should have SMI names"), + PropertyKey::Integer(_) => unreachable!("No prototype functions should have SMI names"), PropertyKey::String(idx) => Value::String(idx), PropertyKey::Symbol(idx) => Value::Symbol(idx), }; @@ -78,24 +77,6 @@ impl ObjectEntry { } } -#[derive(Debug)] -pub enum PropertyKey { - SmallString(SmallString), - Smi(i32), - String(StringIndex), - Symbol(SymbolIndex), -} - -impl PropertyKey { - pub fn from_str(heap: &mut Heap, str: &str) -> Self { - if let Ok(ascii_string) = SmallString::try_from(str) { - PropertyKey::SmallString(ascii_string) - } else { - PropertyKey::String(heap.alloc_string(str)) - } - } -} - #[derive(Debug)] pub enum PropertyDescriptor { Data { diff --git a/nova_vm/src/heap/regexp.rs b/nova_vm/src/heap/regexp.rs index d8ba79b15..a0ef39132 100644 --- a/nova_vm/src/heap/regexp.rs +++ b/nova_vm/src/heap/regexp.rs @@ -1,17 +1,16 @@ +use super::{ + function::FunctionHeapData, + heap_constants::WellKnownSymbolIndexes, + indexes::{FunctionIndex, ObjectIndex}, + object::ObjectEntry, +}; use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes}, Heap, PropertyDescriptor, }, - types::{Object, Value}, -}; - -use super::{ - function::FunctionHeapData, - heap_constants::WellKnownSymbolIndexes, - indexes::{FunctionIndex, ObjectIndex}, - object::{ObjectEntry, PropertyKey}, + types::{Object, PropertyKey, Value}, }; #[derive(Debug, Clone, Copy)] diff --git a/nova_vm/src/heap/symbol.rs b/nova_vm/src/heap/symbol.rs index c6725921b..262ff4133 100644 --- a/nova_vm/src/heap/symbol.rs +++ b/nova_vm/src/heap/symbol.rs @@ -1,15 +1,14 @@ +use super::{ + indexes::{FunctionIndex, StringIndex, SymbolIndex}, + object::ObjectEntry, +}; use crate::{ execution::JsResult, heap::{ heap_constants::{get_constructor_index, BuiltinObjectIndexes, WellKnownSymbolIndexes}, FunctionHeapData, Heap, PropertyDescriptor, }, - types::{Object, Value}, -}; - -use super::{ - indexes::{FunctionIndex, StringIndex, SymbolIndex}, - object::{ObjectEntry, PropertyKey}, + types::{Object, PropertyKey, Value}, }; #[derive(Debug, Clone, Copy)] diff --git a/nova_vm/src/types/language/object/property_key.rs b/nova_vm/src/types/language/object/property_key.rs index 0748c6b1f..a4dd6113e 100644 --- a/nova_vm/src/types/language/object/property_key.rs +++ b/nova_vm/src/types/language/object/property_key.rs @@ -1,67 +1,31 @@ use crate::{ execution::Agent, - heap::{indexes::StringIndex, GetHeapData}, + heap::{ + indexes::{StringIndex, SymbolIndex}, + GetHeapData, + }, types::{String, Value}, - SmallInteger, SmallString, + Heap, SmallInteger, SmallString, }; #[derive(Debug, Clone, Copy)] pub enum PropertyKey { - String(StringIndex), + Integer(SmallInteger), SmallString(SmallString), - SmallInteger(SmallInteger), -} - -impl From for PropertyKey { - fn from(value: StringIndex) -> Self { - PropertyKey::String(value) - } -} - -impl From for PropertyKey { - fn from(value: SmallString) -> Self { - PropertyKey::SmallString(value) - } -} - -impl From for PropertyKey { - fn from(value: SmallInteger) -> Self { - PropertyKey::SmallInteger(value) - } -} - -impl From for PropertyKey { - fn from(value: String) -> Self { - match value { - String::String(x) => PropertyKey::String(x), - String::SmallString(x) => PropertyKey::SmallString(x), - } - } -} - -impl TryFrom for PropertyKey { - type Error = (); - fn try_from(value: Value) -> Result { - match value { - Value::String(x) => Ok(PropertyKey::String(x)), - Value::SmallString(x) => Ok(PropertyKey::SmallString(x)), - Value::Integer(x) => Ok(PropertyKey::SmallInteger(x)), - _ => Err(()), - } - } + String(StringIndex), + Symbol(SymbolIndex), } -impl From for Value { - fn from(value: PropertyKey) -> Self { - match value { - PropertyKey::String(x) => Value::String(x), - PropertyKey::SmallString(x) => Value::SmallString(x), - PropertyKey::SmallInteger(x) => Value::Integer(x), +impl PropertyKey { + // FIXME: This API is not necessarily in the right place. + pub fn from_str(heap: &mut Heap, str: &str) -> Self { + if let Ok(ascii_string) = SmallString::try_from(str) { + PropertyKey::SmallString(ascii_string) + } else { + PropertyKey::String(heap.alloc_string(str)) } } -} -impl PropertyKey { pub fn into_value(self) -> Value { self.into() } @@ -85,7 +49,7 @@ impl PropertyKey { (PropertyKey::SmallString(s1), PropertyKey::SmallString(s2)) => { s1.as_str() == s2.as_str() } - (PropertyKey::String(s), PropertyKey::SmallInteger(n)) => { + (PropertyKey::String(s), PropertyKey::Integer(n)) => { let s = agent.heap.get(s); let Some(s) = s.as_str() else { @@ -94,18 +58,73 @@ impl PropertyKey { Self::is_str_eq_num(s, n.into_i64()) } - (PropertyKey::SmallString(s), PropertyKey::SmallInteger(n)) => { + (PropertyKey::SmallString(s), PropertyKey::Integer(n)) => { Self::is_str_eq_num(s.as_str(), n.into_i64()) } - (PropertyKey::SmallInteger(n1), PropertyKey::SmallInteger(n2)) => { - n1.into_i64() == n2.into_i64() - } - (PropertyKey::SmallInteger(_), _) => y.equals(agent, self), + (PropertyKey::Integer(n1), PropertyKey::Integer(n2)) => n1.into_i64() == n2.into_i64(), + (PropertyKey::Integer(_), _) => y.equals(agent, self), _ => false, } } } +impl From for PropertyKey { + fn from(value: SmallInteger) -> Self { + PropertyKey::Integer(value) + } +} + +impl From for PropertyKey { + fn from(value: SmallString) -> Self { + PropertyKey::SmallString(value) + } +} + +impl From for PropertyKey { + fn from(value: StringIndex) -> Self { + PropertyKey::String(value) + } +} + +impl From for PropertyKey { + fn from(value: SymbolIndex) -> Self { + PropertyKey::Symbol(value) + } +} + +impl From for PropertyKey { + fn from(value: String) -> Self { + match value { + String::String(x) => PropertyKey::String(x), + String::SmallString(x) => PropertyKey::SmallString(x), + } + } +} + +impl From for Value { + fn from(value: PropertyKey) -> Self { + match value { + PropertyKey::Integer(x) => Value::Integer(x), + PropertyKey::SmallString(x) => Value::SmallString(x), + PropertyKey::String(x) => Value::String(x), + PropertyKey::Symbol(x) => Value::Symbol(x), + } + } +} + +impl TryFrom for PropertyKey { + type Error = (); + fn try_from(value: Value) -> Result { + match value { + Value::Integer(x) => Ok(PropertyKey::Integer(x)), + Value::SmallString(x) => Ok(PropertyKey::SmallString(x)), + Value::String(x) => Ok(PropertyKey::String(x)), + Value::Symbol(x) => Ok(PropertyKey::Symbol(x)), + _ => Err(()), + } + } +} + #[test] fn compare_num_str() { assert!(PropertyKey::is_str_eq_num("23", 23));