1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
//! This module implements the global `Object` object.
//!
//! The `Object` class represents one of JavaScript's data types.
//!
//! It is used to store various keyed collections and more complex entities.
//! Objects can be created using the `Object()` constructor or the
//! object initializer / literal syntax.
//!
//! More information:
//! - [ECMAScript reference][spec]
//! - [MDN documentation][mdn]
//!
//! [spec]: https://tc39.es/ecma262/#sec-objects
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
use crate::{
builtins::{BuiltIn, JsArgs},
context::StandardObjects,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, IntegrityLevel,
JsObject, Object as BuiltinObject, ObjectData, ObjectInitializer, ObjectKind,
},
property::{Attribute, DescriptorKind, PropertyDescriptor, PropertyKey, PropertyNameKind},
symbol::WellKnownSymbols,
value::{JsValue, Type},
BoaProfiler, Context, JsResult,
};
use super::Array;
pub mod for_in_iterator;
#[cfg(test)]
mod tests;
/// The global JavaScript object.
#[derive(Debug, Clone, Copy)]
pub struct Object;
impl BuiltIn for Object {
const NAME: &'static str = "Object";
fn attribute() -> Attribute {
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE
}
fn init(context: &mut Context) -> (&'static str, JsValue, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
let object = ConstructorBuilder::with_standard_object(
context,
Self::constructor,
context.standard_objects().object_object().clone(),
)
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(JsValue::null())
.method(Self::has_own_property, "hasOwnProperty", 0)
.method(Self::property_is_enumerable, "propertyIsEnumerable", 0)
.method(Self::to_string, "toString", 0)
.method(Self::value_of, "valueOf", 0)
.method(Self::is_prototype_of, "isPrototypeOf", 0)
.static_method(Self::create, "create", 2)
.static_method(Self::set_prototype_of, "setPrototypeOf", 2)
.static_method(Self::get_prototype_of, "getPrototypeOf", 1)
.static_method(Self::define_property, "defineProperty", 3)
.static_method(Self::define_properties, "defineProperties", 2)
.static_method(Self::assign, "assign", 2)
.static_method(Self::is, "is", 2)
.static_method(Self::keys, "keys", 1)
.static_method(Self::values, "values", 1)
.static_method(Self::entries, "entries", 1)
.static_method(Self::seal, "seal", 1)
.static_method(Self::is_sealed, "isSealed", 1)
.static_method(Self::freeze, "freeze", 1)
.static_method(Self::is_frozen, "isFrozen", 1)
.static_method(Self::prevent_extensions, "preventExtensions", 1)
.static_method(Self::is_extensible, "isExtensible", 1)
.static_method(
Self::get_own_property_descriptor,
"getOwnPropertyDescriptor",
2,
)
.static_method(
Self::get_own_property_descriptors,
"getOwnPropertyDescriptors",
1,
)
.build();
(Self::NAME, object.into(), Self::attribute())
}
}
impl Object {
const LENGTH: usize = 1;
fn constructor(
new_target: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
if !new_target.is_undefined() {
let prototype = get_prototype_from_constructor(
new_target,
StandardObjects::object_object,
context,
)?;
let object = JsValue::new_object(context);
object
.as_object()
.expect("this should be an object")
.set_prototype_instance(prototype.into());
return Ok(object);
}
if let Some(arg) = args.get(0) {
if !arg.is_null_or_undefined() {
return Ok(arg.to_object(context)?.into());
}
}
Ok(JsValue::new_object(context))
}
/// `Object.create( proto, [propertiesObject] )`
///
/// Creates a new object from the provided prototype.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.create
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
pub fn create(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let prototype = args.get_or_undefined(0);
let properties = args.get_or_undefined(1);
let obj = match prototype {
JsValue::Object(_) | JsValue::Null => JsObject::new(BuiltinObject::with_prototype(
prototype.clone(),
ObjectData::ordinary(),
)),
_ => {
return context.throw_type_error(format!(
"Object prototype may only be an Object or null: {}",
prototype.display()
))
}
};
if !properties.is_undefined() {
object_define_properties(&obj, properties.clone(), context)?;
return Ok(obj.into());
}
Ok(obj.into())
}
/// `Object.getOwnPropertyDescriptor( object, property )`
///
/// Returns an object describing the configuration of a specific property on a given object.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
pub fn get_own_property_descriptor(
_: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let object = args.get_or_undefined(0).to_object(context)?;
if let Some(key) = args.get(1) {
let key = key.to_property_key(context)?;
if let Some(desc) = object.__get_own_property__(&key, context)? {
return Ok(Self::from_property_descriptor(desc, context));
}
}
Ok(JsValue::undefined())
}
/// `Object.getOwnPropertyDescriptors( object )`
///
/// Returns all own property descriptors of a given object.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors
pub fn get_own_property_descriptors(
_: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let object = args
.get(0)
.unwrap_or(&JsValue::undefined())
.to_object(context)?;
let descriptors = context.construct_object();
for key in object.borrow().properties().keys() {
let descriptor = {
let desc = object
.__get_own_property__(&key, context)?
.expect("Expected property to be on object.");
Self::from_property_descriptor(desc, context)
};
if !descriptor.is_undefined() {
descriptors.borrow_mut().insert(
key,
PropertyDescriptor::builder()
.value(descriptor)
.writable(true)
.enumerable(true)
.configurable(true),
);
}
}
Ok(JsValue::Object(descriptors))
}
/// The abstract operation `FromPropertyDescriptor`.
///
/// [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-frompropertydescriptor
fn from_property_descriptor(desc: PropertyDescriptor, context: &mut Context) -> JsValue {
let mut descriptor = ObjectInitializer::new(context);
// TODO: use CreateDataPropertyOrThrow
match desc.kind() {
DescriptorKind::Data { value, writable } => {
if let Some(value) = value {
descriptor.property("value", value.clone(), Attribute::all());
}
if let Some(writable) = writable {
descriptor.property("writable", *writable, Attribute::all());
}
}
DescriptorKind::Accessor { get, set } => {
if let Some(get) = get {
descriptor.property("get", get.clone(), Attribute::all());
}
if let Some(set) = set {
descriptor.property("set", set.clone(), Attribute::all());
}
}
_ => {}
}
if let Some(enumerable) = desc.enumerable() {
descriptor.property("enumerable", enumerable, Attribute::all());
}
if let Some(configurable) = desc.configurable() {
descriptor.property("configurable", configurable, Attribute::all());
}
descriptor.build().into()
}
/// Uses the SameValue algorithm to check equality of objects
pub fn is(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let x = args.get_or_undefined(0);
let y = args.get_or_undefined(1);
Ok(JsValue::same_value(x, y).into())
}
/// Get the `prototype` of an object.
pub fn get_prototype_of(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
if args.is_empty() {
return ctx.throw_type_error(
"Object.getPrototypeOf: At least 1 argument required, but only 0 passed",
);
}
// 1. Let obj be ? ToObject(O).
let obj = args[0].clone().to_object(ctx)?;
// 2. Return ? obj.[[GetPrototypeOf]]().
Ok(obj.prototype_instance())
}
/// Set the `prototype` of an object.
///
/// [More information][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.setprototypeof
pub fn set_prototype_of(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
if args.len() < 2 {
return ctx.throw_type_error(format!(
"Object.setPrototypeOf: At least 2 arguments required, but only {} passed",
args.len()
));
}
// 1. Set O to ? RequireObjectCoercible(O).
let obj = args
.get(0)
.cloned()
.unwrap_or_default()
.require_object_coercible(ctx)?
.clone();
// 2. If Type(proto) is neither Object nor Null, throw a TypeError exception.
let proto = args.get_or_undefined(1);
if !matches!(proto.get_type(), Type::Object | Type::Null) {
return ctx.throw_type_error(format!(
"expected an object or null, got {}",
proto.type_of()
));
}
// 3. If Type(O) is not Object, return O.
if !obj.is_object() {
return Ok(obj);
}
// 4. Let status be ? O.[[SetPrototypeOf]](proto).
let status = obj
.as_object()
.expect("obj was not an object")
.__set_prototype_of__(proto.clone(), ctx)?;
// 5. If status is false, throw a TypeError exception.
if !status {
return ctx.throw_type_error("can't set prototype of this object");
}
// 6. Return O.
Ok(obj)
}
/// `Object.prototype.isPrototypeOf( proto )`
///
/// Check whether or not an object exists within another object's prototype chain.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.isprototypeof
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf
pub fn is_prototype_of(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let v = args.get_or_undefined(0);
if !v.is_object() {
return Ok(JsValue::new(false));
}
let mut v = v.clone();
let o = JsValue::new(this.to_object(context)?);
loop {
v = Self::get_prototype_of(this, &[v], context)?;
if v.is_null() {
return Ok(JsValue::new(false));
}
if JsValue::same_value(&o, &v) {
return Ok(JsValue::new(true));
}
}
}
/// Define a property in an object
pub fn define_property(
_: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let object = args.get_or_undefined(0);
if let Some(object) = object.as_object() {
let key = args
.get(1)
.unwrap_or(&JsValue::Undefined)
.to_property_key(context)?;
let desc = args
.get(2)
.unwrap_or(&JsValue::Undefined)
.to_property_descriptor(context)?;
object.define_property_or_throw(key, desc, context)?;
Ok(object.into())
} else {
context.throw_type_error("Object.defineProperty called on non-object")
}
}
/// `Object.defineProperties( proto, [propertiesObject] )`
///
/// Creates or update own properties to the object
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.defineproperties
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties
pub fn define_properties(
_: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let arg = args.get_or_undefined(0);
let arg_obj = arg.as_object();
if let Some(obj) = arg_obj {
let props = args.get_or_undefined(1);
object_define_properties(&obj, props.clone(), context)?;
Ok(arg.clone())
} else {
context.throw_type_error("Expected an object")
}
}
/// `Object.prototype.valueOf()`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.valueof
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
pub fn value_of(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Return ? ToObject(this value).
Ok(this.to_object(context)?.into())
}
/// `Object.prototype.toString()`
///
/// This method returns a string representing the object.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.tostring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString
#[allow(clippy::wrong_self_convention)]
pub fn to_string(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. If the this value is undefined, return "[object Undefined]".
if this.is_undefined() {
return Ok("[object Undefined]".into());
}
// 2. If the this value is null, return "[object Null]".
if this.is_null() {
return Ok("[object Null]".into());
}
// 3. Let O be ! ToObject(this value).
let o = this.to_object(context)?;
// TODO: 4. Let isArray be ? IsArray(O).
// TODO: 5. If isArray is true, let builtinTag be "Array".
// 6. Else if O has a [[ParameterMap]] internal slot, let builtinTag be "Arguments".
// 7. Else if O has a [[Call]] internal method, let builtinTag be "Function".
// 8. Else if O has an [[ErrorData]] internal slot, let builtinTag be "Error".
// 9. Else if O has a [[BooleanData]] internal slot, let builtinTag be "Boolean".
// 10. Else if O has a [[NumberData]] internal slot, let builtinTag be "Number".
// 11. Else if O has a [[StringData]] internal slot, let builtinTag be "String".
// 12. Else if O has a [[DateValue]] internal slot, let builtinTag be "Date".
// 13. Else if O has a [[RegExpMatcher]] internal slot, let builtinTag be "RegExp".
// 14. Else, let builtinTag be "Object".
let builtin_tag = {
let o = o.borrow();
match o.kind() {
ObjectKind::Array => "Array",
// TODO: Arguments Exotic Objects are currently not supported
ObjectKind::Function(_) => "Function",
ObjectKind::Error => "Error",
ObjectKind::Boolean(_) => "Boolean",
ObjectKind::Number(_) => "Number",
ObjectKind::String(_) => "String",
ObjectKind::Date(_) => "Date",
ObjectKind::RegExp(_) => "RegExp",
_ => "Object",
}
};
// 15. Let tag be ? Get(O, @@toStringTag).
let tag = o.get(WellKnownSymbols::to_string_tag(), context)?;
// 16. If Type(tag) is not String, set tag to builtinTag.
let tag_str = tag.as_string().map(|s| s.as_str()).unwrap_or(builtin_tag);
// 17. Return the string-concatenation of "[object ", tag, and "]".
Ok(format!("[object {}]", tag_str).into())
}
/// `Object.prototype.hasOwnPrototype( property )`
///
/// The method returns a boolean indicating whether the object has the specified property
/// as its own property (as opposed to inheriting it).
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
pub fn has_own_property(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let key = args
.get(0)
.unwrap_or(&JsValue::undefined())
.to_property_key(context)?;
let object = this.to_object(context)?;
Ok(object.has_own_property(key, context)?.into())
}
/// `Object.prototype.propertyIsEnumerable( property )`
///
/// This method returns a Boolean indicating whether the specified property is
/// enumerable and is the object's own property.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.prototype.propertyisenumerable
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
pub fn property_is_enumerable(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let key = match args.get(0) {
None => return Ok(JsValue::new(false)),
Some(key) => key,
};
let key = key.to_property_key(context)?;
let own_property = this
.to_object(context)?
.__get_own_property__(&key, context)?;
Ok(own_property.map_or(JsValue::new(false), |own_prop| {
JsValue::new(own_prop.enumerable())
}))
}
/// `Object.assign( target, ...sources )`
///
/// This method copies all enumerable own properties from one or more
/// source objects to a target object. It returns the target object.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.assign
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
pub fn assign(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let to be ? ToObject(target).
let to = args.get_or_undefined(0).to_object(context)?;
// 2. If only one argument was passed, return to.
if args.len() == 1 {
return Ok(to.into());
}
// 3. For each element nextSource of sources, do
for source in &args[1..] {
// 3.a. If nextSource is neither undefined nor null, then
if !source.is_null_or_undefined() {
// 3.a.i. Let from be ! ToObject(nextSource).
let from = source.to_object(context).unwrap();
// 3.a.ii. Let keys be ? from.[[OwnPropertyKeys]]().
let keys = from.__own_property_keys__(context)?;
// 3.a.iii. For each element nextKey of keys, do
for key in keys {
// 3.a.iii.1. Let desc be ? from.[[GetOwnProperty]](nextKey).
if let Some(desc) = from.__get_own_property__(&key, context)? {
// 3.a.iii.2. If desc is not undefined and desc.[[Enumerable]] is true, then
if desc.expect_enumerable() {
// 3.a.iii.2.a. Let propValue be ? Get(from, nextKey).
let property = from.get(key.clone(), context)?;
// 3.a.iii.2.b. Perform ? Set(to, nextKey, propValue, true).
to.set(key, property, true, context)?;
}
}
}
}
}
// 4. Return to.
Ok(to.into())
}
/// `Object.keys( target )`
///
/// This method returns an array of a given object's own enumerable
/// property names, iterated in the same order that a normal loop would.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.keys
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
pub fn keys(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let obj be ? ToObject(target).
let obj = args
.get(0)
.cloned()
.unwrap_or_default()
.to_object(context)?;
// 2. Let nameList be ? EnumerableOwnPropertyNames(obj, key).
let name_list = obj.enumerable_own_property_names(PropertyNameKind::Key, context)?;
// 3. Return CreateArrayFromList(nameList).
let result = Array::create_array_from_list(name_list, context);
Ok(result.into())
}
/// `Object.values( target )`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.values
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
pub fn values(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let obj be ? ToObject(target).
let obj = args
.get(0)
.cloned()
.unwrap_or_default()
.to_object(context)?;
// 2. Let nameList be ? EnumerableOwnPropertyNames(obj, value).
let name_list = obj.enumerable_own_property_names(PropertyNameKind::Value, context)?;
// 3. Return CreateArrayFromList(nameList).
let result = Array::create_array_from_list(name_list, context);
Ok(result.into())
}
/// `Object.entries( target )`
///
/// This method returns an array of a given object's own enumerable string-keyed property [key, value] pairs.
/// This is the same as iterating with a for...in loop,
/// except that a for...in loop enumerates properties in the prototype chain as well).
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.entries
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
pub fn entries(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let obj be ? ToObject(target).
let obj = args
.get(0)
.cloned()
.unwrap_or_default()
.to_object(context)?;
// 2. Let nameList be ? EnumerableOwnPropertyNames(obj, key+value).
let name_list =
obj.enumerable_own_property_names(PropertyNameKind::KeyAndValue, context)?;
// 3. Return CreateArrayFromList(nameList).
let result = Array::create_array_from_list(name_list, context);
Ok(result.into())
}
/// `Object.seal( target )`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.seal
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal
pub fn seal(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let o = args.get_or_undefined(0);
if let Some(o) = o.as_object() {
// 2. Let status be ? SetIntegrityLevel(O, sealed).
let status = o.set_integrity_level(IntegrityLevel::Sealed, context)?;
// 3. If status is false, throw a TypeError exception.
if !status {
return context.throw_type_error("cannot seal object");
}
}
// 1. If Type(O) is not Object, return O.
// 4. Return O.
Ok(o.clone())
}
/// `Object.isSealed( target )`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.issealed
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isSealed
pub fn is_sealed(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let o = args.get_or_undefined(0);
// 1. If Type(O) is not Object, return true.
// 2. Return ? TestIntegrityLevel(O, sealed).
if let Some(o) = o.as_object() {
Ok(o.test_integrity_level(IntegrityLevel::Sealed, context)?
.into())
} else {
Ok(JsValue::new(true))
}
}
/// `Object.freeze( target )`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.freeze
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
pub fn freeze(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let o = args.get_or_undefined(0);
if let Some(o) = o.as_object() {
// 2. Let status be ? SetIntegrityLevel(O, frozen).
let status = o.set_integrity_level(IntegrityLevel::Frozen, context)?;
// 3. If status is false, throw a TypeError exception.
if !status {
return context.throw_type_error("cannot freeze object");
}
}
// 1. If Type(O) is not Object, return O.
// 4. Return O.
Ok(o.clone())
}
/// `Object.isFrozen( target )`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.isfrozen
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen
pub fn is_frozen(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let o = args.get_or_undefined(0);
// 1. If Type(O) is not Object, return true.
// 2. Return ? TestIntegrityLevel(O, frozen).
if let Some(o) = o.as_object() {
Ok(o.test_integrity_level(IntegrityLevel::Frozen, context)?
.into())
} else {
Ok(JsValue::new(true))
}
}
/// `Object.preventExtensions( target )`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.preventextensions
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
pub fn prevent_extensions(
_: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let o = args.get_or_undefined(0);
if let Some(o) = o.as_object() {
// 2. Let status be ? O.[[PreventExtensions]]().
let status = o.__prevent_extensions__(context)?;
// 3. If status is false, throw a TypeError exception.
if !status {
return context.throw_type_error("cannot prevent extensions");
}
}
// 1. If Type(O) is not Object, return O.
// 4. Return O.
Ok(o.clone())
}
/// `Object.isExtensible( target )`
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.isextensible
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible
pub fn is_extensible(
_: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let o = args.get_or_undefined(0);
// 1. If Type(O) is not Object, return false.
if let Some(o) = o.as_object() {
// 2. Return ? IsExtensible(O).
Ok(o.is_extensible(context)?.into())
} else {
Ok(JsValue::new(false))
}
}
}
/// The abstract operation ObjectDefineProperties
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-object.defineproperties
#[inline]
fn object_define_properties(
object: &JsObject,
props: JsValue,
context: &mut Context,
) -> JsResult<()> {
// 1. Assert: Type(O) is Object.
// 2. Let props be ? ToObject(Properties).
let props = &props.to_object(context)?;
// 3. Let keys be ? props.[[OwnPropertyKeys]]().
let keys = props.__own_property_keys__(context)?;
// 4. Let descriptors be a new empty List.
let mut descriptors: Vec<(PropertyKey, PropertyDescriptor)> = Vec::new();
// 5. For each element nextKey of keys, do
for next_key in keys {
// a. Let propDesc be ? props.[[GetOwnProperty]](nextKey).
// b. If propDesc is not undefined and propDesc.[[Enumerable]] is true, then
if let Some(prop_desc) = props.__get_own_property__(&next_key, context)? {
if prop_desc.expect_enumerable() {
// i. Let descObj be ? Get(props, nextKey).
let desc_obj = props.get(next_key.clone(), context)?;
// ii. Let desc be ? ToPropertyDescriptor(descObj).
let desc = desc_obj.to_property_descriptor(context)?;
// iii. Append the pair (a two element List) consisting of nextKey and desc to the end of descriptors.
descriptors.push((next_key, desc));
}
}
}
// 6. For each element pair of descriptors, do
// a. Let P be the first element of pair.
// b. Let desc be the second element of pair.
for (p, d) in descriptors {
// c. Perform ? DefinePropertyOrThrow(O, P, desc).
object.define_property_or_throw(p, d, context)?;
}
// 7. Return O.
Ok(())
}