Skip to content

Commit

Permalink
implement Object.prototype.propertyIsEnumerable
Browse files Browse the repository at this point in the history
required by test262's propertyHelper.js
  • Loading branch information
y21 committed Feb 17, 2024
1 parent 6cb02c3 commit 44799f8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion crates/dash_middle/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ pub mod sym {
one: "1",
getPrototypeOf,
isPrototypeOf,
arguments
arguments,
propertyIsEnumerable
}
]
}
Expand Down
11 changes: 10 additions & 1 deletion crates/dash_vm/src/js_std/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::localscope::LocalScope;
use crate::throw;
use crate::value::array::Array;
use crate::value::function::native::CallContext;
use crate::value::object::{NamedObject, Object, PropertyKey, PropertyValue};
use crate::value::object::{NamedObject, Object, PropertyDataDescriptor, PropertyKey, PropertyValue};
use crate::value::ops::conversions::ValueConversion;
use crate::value::root_ext::RootErrExt;
use crate::value::{Root, Typeof, Value, ValueContext};
Expand Down Expand Up @@ -208,3 +208,12 @@ pub fn is_prototype_of(cx: CallContext) -> Result<Value, Value> {
};
}
}

pub fn property_is_enumerable(cx: CallContext) -> Result<Value, Value> {
let prop = PropertyKey::from_value(cx.scope, cx.args.first().unwrap_or_undefined())?;
let obj = cx.this.to_object(cx.scope)?;
let desc = obj.get_own_property_descriptor(cx.scope, prop).root_err(cx.scope)?;
Ok(Value::Boolean(desc.is_some_and(|val| {
val.descriptor.contains(PropertyDataDescriptor::ENUMERABLE)
})))
}
1 change: 1 addition & 0 deletions crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl Vm {
(sym::toString, scope.statics.object_to_string.clone()),
(sym::hasOwnProperty, scope.statics.object_has_own_property.clone()),
(sym::isPrototypeOf, scope.statics.object_is_prototype_of.clone()),
(sym::propertyIsEnumerable, scope.statics.object_property_is_enumerable.clone())
],
[],
[],
Expand Down
6 changes: 6 additions & 0 deletions crates/dash_vm/src/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct Statics {
pub object_entries: Handle,
pub object_get_prototype_of: Handle,
pub object_is_prototype_of: Handle,
pub object_property_is_enumerable: Handle,
pub number_ctor: Handle,
pub number_prototype: Handle,
pub number_tostring: Handle,
Expand Down Expand Up @@ -293,6 +294,11 @@ impl Statics {
object_entries: function(gc, sym::entries, js_std::object::entries),
object_get_prototype_of: function(gc, sym::getPrototypeOf, js_std::object::get_prototype_of),
object_is_prototype_of: function(gc, sym::isPrototypeOf, js_std::object::is_prototype_of),
object_property_is_enumerable: function(
gc,
sym::propertyIsEnumerable,
js_std::object::property_is_enumerable,
),
number_ctor: function(gc, sym::Number, js_std::number::constructor),
number_prototype: builtin_object(gc, BoxedNumber::with_obj(0.0, NamedObject::null())),
number_tostring: function(gc, sym::toString, js_std::number::to_string),
Expand Down
1 change: 1 addition & 0 deletions crates/dash_vm/src/value/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ impl PropertyKey {
}

pub fn from_value(sc: &mut LocalScope, value: Value) -> Result<Self, Value> {
// TODO: call ToPrimitive as specified by ToPropertyKey in the spec?
match value {
Value::Symbol(s) => Ok(Self::Symbol(s)),
other => Ok(PropertyKey::String(other.to_js_string(sc)?)),
Expand Down

0 comments on commit 44799f8

Please sign in to comment.