Skip to content

Commit

Permalink
Implement debug object indexed elements type function
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Mar 23, 2024
1 parent fcf2194 commit ed12aa7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
33 changes: 31 additions & 2 deletions cli/src/debug/object.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,8 +22,36 @@ fn id(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
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<JsValue> {
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()
}
1 change: 1 addition & 0 deletions core/engine/src/object/property_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>),
Expand Down
24 changes: 24 additions & 0 deletions docs/boa_object.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ed12aa7

Please sign in to comment.