From cb35c79e6efc2e303d6420d9aebee6965f36949f Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Sun, 29 Oct 2023 12:02:49 +0200 Subject: [PATCH] chore(heap): Move NumberHeapData to ecmascript/types/language/number --- nova_vm/src/ecmascript/types.rs | 2 +- nova_vm/src/ecmascript/types/language.rs | 2 +- nova_vm/src/ecmascript/types/language/number.rs | 4 ++++ .../ecmascript/types/language/number/data.rs | 17 +++++++++++++++++ nova_vm/src/heap.rs | 8 ++++---- nova_vm/src/heap/indexes.rs | 8 +++++--- nova_vm/src/heap/number.rs | 15 --------------- 7 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 nova_vm/src/ecmascript/types/language/number/data.rs diff --git a/nova_vm/src/ecmascript/types.rs b/nova_vm/src/ecmascript/types.rs index 3f7d65cf0..67292b4ed 100644 --- a/nova_vm/src/ecmascript/types.rs +++ b/nova_vm/src/ecmascript/types.rs @@ -1,7 +1,7 @@ mod language; mod spec; -pub(crate) use language::{BigIntHeapData, ObjectHeapData, StringHeapData}; +pub(crate) use language::{BigIntHeapData, NumberHeapData, ObjectHeapData, StringHeapData}; pub use language::{Function, InternalMethods, Number, Object, PropertyKey, String, Value}; pub use spec::{Base, PropertyDescriptor, Reference, ReferencedName}; diff --git a/nova_vm/src/ecmascript/types/language.rs b/nova_vm/src/ecmascript/types/language.rs index e79284ce3..d70b84f90 100644 --- a/nova_vm/src/ecmascript/types/language.rs +++ b/nova_vm/src/ecmascript/types/language.rs @@ -7,7 +7,7 @@ mod value; pub use bigint::{BigInt, BigIntHeapData}; pub use function::Function; -pub use number::Number; +pub use number::{Number, NumberHeapData}; pub use object::{InternalMethods, Object, ObjectHeapData, PropertyKey, PropertyStorage}; pub use string::{String, StringHeapData}; pub use value::Value; diff --git a/nova_vm/src/ecmascript/types/language/number.rs b/nova_vm/src/ecmascript/types/language/number.rs index 5d22e3372..d448301c0 100644 --- a/nova_vm/src/ecmascript/types/language/number.rs +++ b/nova_vm/src/ecmascript/types/language/number.rs @@ -1,3 +1,5 @@ +mod data; + use super::{ value::{FLOAT_DISCRIMINANT, INTEGER_DISCRIMINANT, NUMBER_DISCRIMINANT}, Value, @@ -8,6 +10,8 @@ use crate::{ SmallInteger, }; +pub use data::NumberHeapData; + /// 6.1.6.1 The Number Type /// https://tc39.es/ecma262/#sec-ecmascript-language-types-number-type #[derive(Clone, Copy)] diff --git a/nova_vm/src/ecmascript/types/language/number/data.rs b/nova_vm/src/ecmascript/types/language/number/data.rs new file mode 100644 index 000000000..59a3ca248 --- /dev/null +++ b/nova_vm/src/ecmascript/types/language/number/data.rs @@ -0,0 +1,17 @@ +#[derive(Debug, Clone, Copy)] +pub struct NumberHeapData { + pub(crate) data: f64, +} + +impl From for NumberHeapData { + #[inline(always)] + fn from(data: f64) -> Self { + Self { data } + } +} + +impl From for f64 { + fn from(value: NumberHeapData) -> Self { + value.data + } +} diff --git a/nova_vm/src/heap.rs b/nova_vm/src/heap.rs index 7583a20b3..c3d5512de 100644 --- a/nova_vm/src/heap.rs +++ b/nova_vm/src/heap.rs @@ -36,7 +36,7 @@ use self::{ }, indexes::{BaseIndex, FunctionIndex, NumberIndex, ObjectIndex, StringIndex}, math::initialize_math_object, - number::{initialize_number_heap, NumberHeapData}, + number::initialize_number_heap, object::{initialize_object_heap, ObjectEntry, PropertyDescriptor}, regexp::{initialize_regexp_heap, RegExpHeapData}, string::initialize_string_heap, @@ -46,8 +46,8 @@ use crate::ecmascript::{ builtins::ArrayHeapData, execution::{Environments, Realm, RealmIdentifier}, types::{ - BigIntHeapData, Function, Number, Object, ObjectHeapData, PropertyKey, String, - StringHeapData, Value, + BigIntHeapData, Function, Number, NumberHeapData, Object, ObjectHeapData, PropertyKey, + String, StringHeapData, Value, }, }; use wtf8::{Wtf8, Wtf8Buf}; @@ -269,7 +269,7 @@ impl<'ctx, 'host> Heap<'ctx, 'host> { } pub fn alloc_number(&mut self, number: f64) -> NumberIndex { - self.numbers.push(Some(NumberHeapData::new(number))); + self.numbers.push(Some(number.into())); NumberIndex::last(&self.numbers) } diff --git a/nova_vm/src/heap/indexes.rs b/nova_vm/src/heap/indexes.rs index 71c5343d0..e3d391364 100644 --- a/nova_vm/src/heap/indexes.rs +++ b/nova_vm/src/heap/indexes.rs @@ -1,10 +1,12 @@ use super::array_buffer::ArrayBufferHeapData; use super::{ - date::DateHeapData, error::ErrorHeapData, function::FunctionHeapData, number::NumberHeapData, - regexp::RegExpHeapData, symbol::SymbolHeapData, + date::DateHeapData, error::ErrorHeapData, function::FunctionHeapData, regexp::RegExpHeapData, + symbol::SymbolHeapData, }; use crate::ecmascript::builtins::ArrayHeapData; -use crate::ecmascript::types::{BigIntHeapData, ObjectHeapData, StringHeapData, Value}; +use crate::ecmascript::types::{ + BigIntHeapData, NumberHeapData, ObjectHeapData, StringHeapData, Value, +}; use crate::Heap; use core::fmt::Debug; use std::hash::{Hash, Hasher}; diff --git a/nova_vm/src/heap/number.rs b/nova_vm/src/heap/number.rs index d900b4db6..8d1692757 100644 --- a/nova_vm/src/heap/number.rs +++ b/nova_vm/src/heap/number.rs @@ -10,21 +10,6 @@ use crate::{ }, }; -#[derive(Debug, Clone, Copy)] -pub struct NumberHeapData { - pub(super) data: f64, -} - -impl NumberHeapData { - pub(super) fn new(data: f64) -> NumberHeapData { - NumberHeapData { data } - } - - pub fn value(&self) -> f64 { - self.data - } -} - pub fn initialize_number_heap(heap: &mut Heap) { let entries = vec![ ObjectEntry::new(