Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aapoalas committed Nov 1, 2023
1 parent 3ae8c61 commit 96a4415
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
13 changes: 10 additions & 3 deletions nova_vm/src/ecmascript/types/language/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ impl From<f32> for Number {
}
}

const MIN_NUMBER: f64 = -9007199254740991.0;
const MAX_NUMBER: f64 = 9007199254740991.0;
const MAX_NUMBER: f64 = ((1u64 << 53) - 1) as f64;
const MIN_NUMBER: f64 = -MAX_NUMBER;

impl TryFrom<f64> for Number {
type Error = ();
Expand Down Expand Up @@ -104,7 +104,14 @@ impl TryFrom<Value> for Number {

impl Number {
pub fn from_f64(agent: &mut Agent, value: f64) -> Self {
agent.heap.create(value)
if let Ok(value) = Number::try_from(value) {
value
} else {
// SAFETY: Number was not representable as a
// stack-allocated Number.
let id = unsafe { agent.heap.alloc_number(value) };
Number::Number(id)
}
}

pub fn nan() -> Self {
Expand Down
3 changes: 3 additions & 0 deletions nova_vm/src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub trait GetHeapData<'a, T, F: 'a> {

impl CreateHeapData<f64, Number> for Heap<'_, '_> {
fn create(&mut self, data: f64) -> Number {
// NOTE: This function cannot currently be implemented
// directly using `Number::from_f64` as it takes an Agent
// parameter that we do not have access to here.
if let Ok(value) = Number::try_from(data) {
value
} else {
Expand Down
7 changes: 4 additions & 3 deletions nova_vm/src/heap/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use super::{object::ObjectEntry, CreateHeapData, Heap};
use crate::{
ecmascript::{
execution::JsResult,
types::{Object, PropertyKey, Value},
types::{Number, Object, PropertyKey, Value},
},
heap::{
heap_constants::{get_constructor_index, BuiltinObjectIndexes},
FunctionHeapData, PropertyDescriptor,
},
SmallInteger,
};

pub fn initialize_number_heap(heap: &mut Heap) {
Expand All @@ -22,15 +23,15 @@ pub fn initialize_number_heap(heap: &mut Heap) {
ObjectEntry::new_prototype_function_entry(heap, "isSafeInteger", 1, false),
ObjectEntry::new(
PropertyKey::from_str(heap, "MAX_SAFE_INTEGER"),
PropertyDescriptor::roh(heap.create(9007199254740991.0).into()),
PropertyDescriptor::roh(Number::try_from(SmallInteger::MAX_NUMBER).unwrap().into()),
),
ObjectEntry::new(
PropertyKey::from_str(heap, "MAX_VALUE"),
PropertyDescriptor::roh(heap.create(f64::MAX).into()),
),
ObjectEntry::new(
PropertyKey::from_str(heap, "MIN_SAFE_INTEGER"),
PropertyDescriptor::roh(heap.create(-9007199254740991.0).into()),
PropertyDescriptor::roh(Number::try_from(SmallInteger::MIN_NUMBER).unwrap().into()),
),
ObjectEntry::new(
PropertyKey::from_str(heap, "MIN_VALUE"),
Expand Down
33 changes: 17 additions & 16 deletions nova_vm/src/heap/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::{
indexes::{FunctionIndex, StringIndex, SymbolIndex},
indexes::{FunctionIndex, SymbolIndex},
object::ObjectEntry,
CreateHeapData,
};
use crate::{
ecmascript::{
execution::JsResult,
types::{Object, PropertyKey, Value},
types::{Object, PropertyKey, String, Value},
},
heap::{
heap_constants::{get_constructor_index, BuiltinObjectIndexes, WellKnownSymbolIndexes},
Expand All @@ -15,74 +16,74 @@ use crate::{

#[derive(Debug, Clone, Copy)]
pub struct SymbolHeapData {
pub(super) descriptor: Option<StringIndex>,
pub(super) descriptor: Option<String>,
}

pub fn initialize_symbol_heap(heap: &mut Heap) {
// AsyncIterator
heap.symbols[WellKnownSymbolIndexes::AsyncIterator as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.asyncIterator") }),
descriptor: Some(heap.create("Symbol.asyncIterator")),
});
// HasInstance
heap.symbols[WellKnownSymbolIndexes::HasInstance as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.hasInstance") }),
descriptor: Some(heap.create("Symbol.hasInstance")),
});
// IsConcatSpreadable
heap.symbols[WellKnownSymbolIndexes::IsConcatSpreadable as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.isConcatSpreadable") }),
descriptor: Some(heap.create("Symbol.isConcatSpreadable")),
});
// Iterator
heap.symbols[WellKnownSymbolIndexes::Iterator as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.iterator") }),
descriptor: Some(heap.create("Symbol.iterator")),
});
// Match
heap.symbols[WellKnownSymbolIndexes::Match as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.match") }),
descriptor: Some(heap.create("Symbol.match")),
});
// MatchAll
heap.symbols[WellKnownSymbolIndexes::MatchAll as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.matchAll") }),
descriptor: Some(heap.create("Symbol.matchAll")),
});
// Replace
heap.symbols[WellKnownSymbolIndexes::Replace as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.replace") }),
descriptor: Some(heap.create("Symbol.replace")),
});
// Search
heap.symbols[WellKnownSymbolIndexes::Search as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.search") }),
descriptor: Some(heap.create("Symbol.search")),
});
// Species
heap.symbols[WellKnownSymbolIndexes::Species as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.species") }),
descriptor: Some(heap.create("Symbol.species")),
});
// Split
heap.symbols[WellKnownSymbolIndexes::Split as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.split") }),
descriptor: Some(heap.create("Symbol.split")),
});
// ToPrimitive
heap.symbols[WellKnownSymbolIndexes::ToPrimitive as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.toPrimitive") }),
descriptor: Some(heap.create("Symbol.toPrimitive")),
});
// ToStringTag
heap.symbols[WellKnownSymbolIndexes::ToStringTag as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.toStringTag") }),
descriptor: Some(heap.create("Symbol.toStringTag")),
});
// Unscopables
heap.symbols[WellKnownSymbolIndexes::Unscopables as usize] = Some(SymbolHeapData {
// SAFETY: Descriptor string is too long to be a SmallString.
descriptor: Some(unsafe { heap.alloc_string("Symbol.unscopables") }),
descriptor: Some(heap.create("Symbol.unscopables")),
});

let entries = vec![
Expand Down

0 comments on commit 96a4415

Please sign in to comment.