Skip to content

Commit

Permalink
feat: Move ObjectHeapData to types/language/object/data
Browse files Browse the repository at this point in the history
  • Loading branch information
aapoalas committed Oct 20, 2023
1 parent d3bc05f commit 97cb094
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 58 deletions.
4 changes: 2 additions & 2 deletions nova_vm/src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +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},
object::{initialize_object_heap, ObjectEntry, 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, PropertyKey, String, StringHeapData, Value},
types::{Function, Number, Object, ObjectHeapData, PropertyKey, String, StringHeapData, Value},
};
use wtf8::{Wtf8, Wtf8Buf};

Expand Down
6 changes: 3 additions & 3 deletions nova_vm/src/heap/indexes.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::array_buffer::ArrayBufferHeapData;
use super::{
array::ArrayHeapData, bigint::BigIntHeapData, date::DateHeapData, error::ErrorHeapData,
function::FunctionHeapData, number::NumberHeapData, object::ObjectHeapData,
regexp::RegExpHeapData, symbol::SymbolHeapData,
function::FunctionHeapData, number::NumberHeapData, regexp::RegExpHeapData,
symbol::SymbolHeapData,
};
use crate::types::{StringHeapData, Value};
use crate::types::{ObjectHeapData, StringHeapData, Value};
use crate::Heap;
use core::fmt::Debug;
use std::hash::{Hash, Hasher};
Expand Down
47 changes: 1 addition & 46 deletions nova_vm/src/heap/object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use super::{
element_array::ElementsVector,
indexes::{FunctionIndex, ObjectIndex, SymbolIndex},
};
use super::indexes::{FunctionIndex, ObjectIndex, SymbolIndex};
use crate::{
execution::JsResult,
heap::{
Expand Down Expand Up @@ -203,48 +200,6 @@ impl PropertyDescriptor {
}
}

#[derive(Debug, Clone, Copy)]
pub struct ObjectHeapData {
pub extensible: bool,
pub prototype: Option<Object>,
pub keys: ElementsVector,
pub values: ElementsVector,
}

impl ObjectHeapData {
pub fn new(
extensible: bool,
prototype: Value,
keys: ElementsVector,
values: ElementsVector,
) -> Self {
let prototype = if prototype.is_null() {
None
} else {
// TODO: Throw error.
Some(Object::try_from(prototype).unwrap())
};
Self {
extensible,
// TODO: Number, Boolean, etc. objects exist. These can all be
// modeled with their own heap vector or alternatively by adding
// a [[PrimitiveValue]] field to objects: Normally this field is None
// to signal that the object is its own primitive value. For
// Number objects etc the field is Some(Value).
// TODO: Move prototype and key vector into shapes
prototype,
// TODO: Consider using SmallVec<[Option<Value>; 3]> or such?
keys,
values,
}
}

pub fn has(&self, heap: &Heap, key: Value) -> bool {
debug_assert!(key.is_string() || key.is_number() || key.is_symbol());
heap.elements.has(self.keys, key)
}
}

pub fn initialize_object_heap(heap: &mut Heap) {
let entries = vec![
ObjectEntry::new_prototype_function_entry(heap, "assign", 1, true),
Expand Down
1 change: 1 addition & 0 deletions nova_vm/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod language;
mod spec;

pub(crate) use language::ObjectHeapData;
pub(crate) use language::StringHeapData;
pub use language::{Function, InternalMethods, Number, Object, PropertyKey, String, Value};
pub use spec::{Base, PropertyDescriptor, Reference, ReferencedName};
Expand Down
2 changes: 1 addition & 1 deletion nova_vm/src/types/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod value;
pub use bigint::BigInt;
pub use function::Function;
pub use number::Number;
pub use object::{InternalMethods, Object, ObjectData, PropertyKey, PropertyStorage};
pub use object::{InternalMethods, Object, ObjectHeapData, PropertyKey, PropertyStorage};
pub(crate) use string::data::StringHeapData;
pub use string::String;
pub use value::Value;
2 changes: 1 addition & 1 deletion nova_vm/src/types/language/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
Heap,
};

pub use data::ObjectData;
pub use data::ObjectHeapData;
pub use internal_methods::InternalMethods;
pub use property_key::PropertyKey;
pub use property_storage::PropertyStorage;
Expand Down
43 changes: 38 additions & 5 deletions nova_vm/src/types/language/object/data.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
use super::Object;
use crate::{heap::element_array::ElementsVector, types::Value, Heap};

#[derive(Debug)]
pub struct ObjectData {
/// [[Prototype]]
#[derive(Debug, Clone, Copy)]
pub struct ObjectHeapData {
pub extensible: bool,
pub prototype: Option<Object>,
pub keys: ElementsVector,
pub values: ElementsVector,
}

/// [[Extensible]]
pub extensible: bool,
impl ObjectHeapData {
pub fn new(
extensible: bool,
prototype: Value,
keys: ElementsVector,
values: ElementsVector,
) -> Self {
let prototype = if prototype.is_null() {
None
} else {
// TODO: Throw error.
Some(Object::try_from(prototype).unwrap())
};
Self {
extensible,
// TODO: Number, Boolean, etc. objects exist. These can all be
// modeled with their own heap vector or alternatively by adding
// a [[PrimitiveValue]] field to objects: Normally this field is None
// to signal that the object is its own primitive value. For
// Number objects etc the field is Some(Value).
// TODO: Move prototype and key vector into shapes
prototype,
keys,
values,
}
}

pub fn has(&self, heap: &Heap, key: Value) -> bool {
debug_assert!(key.is_string() || key.is_number() || key.is_symbol());
heap.elements.has(self.keys, key)
}
}

0 comments on commit 97cb094

Please sign in to comment.