diff --git a/cli/src/debug/object.rs b/cli/src/debug/object.rs index c81d91be689..1df662784db 100644 --- a/cli/src/debug/object.rs +++ b/cli/src/debug/object.rs @@ -1,6 +1,7 @@ use boa_engine::{ - js_string, object::ObjectInitializer, Context, JsNativeError, JsObject, JsResult, JsValue, - NativeFunction, + js_string, + object::{IndexProperties, ObjectInitializer}, + Context, JsNativeError, JsObject, JsResult, JsValue, NativeFunction, }; /// Returns objects pointer in memory. @@ -21,8 +22,36 @@ fn id(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult { Ok(js_string!(format!("0x{:X}", ptr.cast::<()>() as usize)).into()) } +/// Returns objects pointer in memory. +fn indexed_elements_type(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult { + let Some(value) = args.first() else { + return Err(JsNativeError::typ() + .with_message("expected object argument") + .into()); + }; + + let Some(object) = value.as_object() else { + return Err(JsNativeError::typ() + .with_message(format!("expected object, got {}", value.type_of())) + .into()); + }; + + let typ = match object.borrow().properties().index_properties() { + IndexProperties::DenseI32(_) => "DenseI32", + IndexProperties::DenseF64(_) => "DenseF64", + IndexProperties::DenseElement(_) => "DenseElement", + IndexProperties::Sparse(_) => "SparseElement", + }; + Ok(js_string!(typ).into()) +} + pub(super) fn create_object(context: &mut Context) -> JsObject { ObjectInitializer::new(context) .function(NativeFunction::from_fn_ptr(id), js_string!("id"), 1) + .function( + NativeFunction::from_fn_ptr(indexed_elements_type), + js_string!("indexedElementsType"), + 1, + ) .build() } diff --git a/core/engine/src/object/property_map.rs b/core/engine/src/object/property_map.rs index e466872fd78..fc9f9f501c5 100644 --- a/core/engine/src/object/property_map.rs +++ b/core/engine/src/object/property_map.rs @@ -943,6 +943,7 @@ impl FusedIterator for IndexPropertyKeys<'_> {} /// An iterator over the index values (`Property`) of an `Object`. #[derive(Debug, Clone)] +#[allow(variant_size_differences)] pub enum IndexPropertyValues<'a> { /// An iterator over dense, Vec backed indexed property entries of an `Object`. DenseI32(std::slice::Iter<'a, i32>), diff --git a/docs/boa_object.md b/docs/boa_object.md index da32712fe3b..e5e5a92f67d 100644 --- a/docs/boa_object.md +++ b/docs/boa_object.md @@ -163,6 +163,30 @@ $boa.object.id(o) // '0x7F5B3251B718' $boa.object.id($boa) // '0x7F5B3251B5D8' ``` +## Function `$boa.object.indexedStorageType(object)` + +This function returns indexed storage type. + +Example: + +```JavaScript +let a = [1, 2] + +$boa.object.indexedStorageType(a) // 'DenseI32' + +a.push(0xdeadbeef) +$boa.object.indexedStorageType(a) // 'DenseI32' + +a.push(0.5) +$boa.object.indexedStorageType(a) // 'DenseF64' + +a.push("Hello") +$boa.object.indexedStorageType(a) // 'DenseElement' + +a[100] = 100 // Make a hole +$boa.object.indexedStorageType(a) // 'SparseElement' +``` + ## Module `$boa.optimizer` This modules contains getters and setters for enabling and disabling optimizations.