Skip to content

Commit

Permalink
implement Object.defineProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed May 5, 2024
1 parent 5b4613a commit c581dce
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/dash_middle/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub mod sym {
getOwnPropertyDescriptor,
getOwnPropertyDescriptors,
defineProperty,
defineProperties,
entries,
assign,
Object,
Expand Down
2 changes: 1 addition & 1 deletion crates/dash_node_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Object for RequireFunction {

let is_path = matches!(arg.chars().next(), Some('.' | '/' | '~'));
let result = if is_path {
if !arg.ends_with(".js") {
if !arg.ends_with(".js") && !arg.ends_with(".json") {
arg += ".js";
}

Expand Down
21 changes: 21 additions & 0 deletions crates/dash_vm/src/js_std/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ pub fn define_property(cx: CallContext) -> Result<Value, Value> {
Ok(Value::Object(object.clone()))
}

pub fn define_properties(cx: CallContext) -> Result<Value, Value> {
let object = match cx.args.first() {
Some(Value::Object(o)) => o.clone(),
_ => throw!(
cx.scope,
TypeError,
"Object.prototype.hasOwnProperty called on non-object"
),
};

let properties = cx.args.get(1).unwrap_or_undefined();
for key in properties.own_keys(cx.scope)? {
let key = key.to_js_string(cx.scope)?;
let descriptor = properties.get_property(cx.scope, key.into()).root(cx.scope)?;
let descriptor = PropertyValue::from_descriptor_value(cx.scope, descriptor)?;
object.set_property(cx.scope, key.into(), descriptor)?;
}

Ok(Value::Object(object))
}

pub fn assign(cx: CallContext) -> Result<Value, Value> {
let mut args = cx.args.into_iter();
let to = args.next().unwrap_or_undefined().to_object(cx.scope)?;
Expand Down
1 change: 1 addition & 0 deletions crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl Vm {
(sym::getOwnPropertyDescriptor, scope.statics.object_get_own_property_descriptor.clone()),
(sym::getOwnPropertyDescriptors, scope.statics.object_get_own_property_descriptors.clone()),
(sym::defineProperty, scope.statics.object_define_property.clone()),
(sym::defineProperties, scope.statics.object_define_properties.clone()),
(sym::entries, scope.statics.object_entries.clone()),
(sym::assign, scope.statics.object_assign.clone()),
(sym::getPrototypeOf, scope.statics.object_get_prototype_of.clone()),
Expand Down
2 changes: 2 additions & 0 deletions crates/dash_vm/src/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct Statics {
pub object_get_own_property_descriptors: Handle,
pub object_has_own_property: Handle,
pub object_define_property: Handle,
pub object_define_properties: Handle,
pub object_assign: Handle,
pub object_entries: Handle,
pub object_get_prototype_of: Handle,
Expand Down Expand Up @@ -293,6 +294,7 @@ impl Statics {
),
object_has_own_property: function(gc, sym::hasOwnProperty, js_std::object::has_own_property),
object_define_property: function(gc, sym::defineProperty, js_std::object::define_property),
object_define_properties: function(gc, sym::defineProperties, js_std::object::define_properties),
object_assign: function(gc, sym::assign, js_std::object::assign),
object_entries: function(gc, sym::entries, js_std::object::entries),
object_get_prototype_of: function(gc, sym::getPrototypeOf, js_std::object::get_prototype_of),
Expand Down
6 changes: 3 additions & 3 deletions crates/dash_vm/src/value/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,13 @@ impl PropertyValue {
let enumerable = value.get_property(sc, sym::enumerable.into()).root(sc)?.into_option();
let writable = value.get_property(sc, sym::writable.into()).root(sc)?.into_option();

if configurable.map_or(true, |v| v.is_truthy(sc)) {
if configurable.is_some_and(|v| v.is_truthy(sc)) {
flags |= PropertyDataDescriptor::CONFIGURABLE;
}
if enumerable.map_or(true, |v| v.is_truthy(sc)) {
if enumerable.is_some_and(|v| v.is_truthy(sc)) {
flags |= PropertyDataDescriptor::ENUMERABLE;
}
if writable.map_or(true, |v| v.is_truthy(sc)) {
if writable.is_some_and(|v| v.is_truthy(sc)) {
flags |= PropertyDataDescriptor::WRITABLE;
}

Expand Down

0 comments on commit c581dce

Please sign in to comment.