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
//! This module defines the object internal methods.
//!
//! More information:
//!  - [ECMAScript reference][spec]
//!
//! [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots

use crate::{
    object::{GcObject, Object, ObjectData},
    property::{AccessorDescriptor, Attribute, DataDescriptor, PropertyDescriptor, PropertyKey},
    value::{same_value, Value},
    BoaProfiler, Context, Result,
};

impl GcObject {
    /// Check if object has property.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-hasproperty-p
    #[inline]
    pub fn has_property(&self, key: &PropertyKey) -> bool {
        let prop = self.get_own_property(key);
        if prop.is_none() {
            let parent = self.get_prototype_of();
            return if let Value::Object(ref object) = parent {
                object.has_property(key)
            } else {
                false
            };
        }
        true
    }

    /// Check if it is extensible.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-isextensible
    #[inline]
    pub fn is_extensible(&self) -> bool {
        self.borrow().extensible
    }

    /// Disable extensibility.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-preventextensions
    #[inline]
    pub fn prevent_extensions(&mut self) -> bool {
        self.borrow_mut().extensible = false;
        true
    }

    /// Delete property.
    #[inline]
    pub fn delete(&mut self, key: &PropertyKey) -> bool {
        match self.get_own_property(key) {
            Some(desc) if desc.configurable() => {
                self.remove(&key);
                true
            }
            Some(_) => false,
            None => true,
        }
    }

    /// `[[Get]]`
    /// <https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver>
    pub fn get(&self, key: &PropertyKey, receiver: Value, context: &mut Context) -> Result<Value> {
        match self.get_own_property(key) {
            None => {
                // parent will either be null or an Object
                if let Some(parent) = self.get_prototype_of().as_object() {
                    Ok(parent.get(key, receiver, context)?)
                } else {
                    Ok(Value::undefined())
                }
            }
            Some(ref desc) => match desc {
                PropertyDescriptor::Data(desc) => Ok(desc.value()),
                PropertyDescriptor::Accessor(AccessorDescriptor { get: Some(get), .. }) => {
                    get.call(&receiver, &[], context)
                }
                _ => Ok(Value::undefined()),
            },
        }
    }

    /// `[[Set]]`
    /// <https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-set-p-v-receiver>
    pub fn set(
        &mut self,
        key: PropertyKey,
        val: Value,
        receiver: Value,
        context: &mut Context,
    ) -> Result<bool> {
        let _timer = BoaProfiler::global().start_event("Object::set", "object");

        // Fetch property key
        let own_desc = if let Some(desc) = self.get_own_property(&key) {
            desc
        } else if let Some(ref mut parent) = self.get_prototype_of().as_object() {
            return Ok(parent.set(key, val, receiver, context)?);
        } else {
            DataDescriptor::new(Value::undefined(), Attribute::all()).into()
        };

        match &own_desc {
            PropertyDescriptor::Data(desc) => {
                if !desc.writable() {
                    return Ok(false);
                }
                if let Some(ref mut receiver) = receiver.as_object() {
                    if let Some(ref existing_desc) = receiver.get_own_property(&key) {
                        match existing_desc {
                            PropertyDescriptor::Accessor(_) => Ok(false),
                            PropertyDescriptor::Data(existing_data_desc) => {
                                if !existing_data_desc.writable() {
                                    return Ok(false);
                                }
                                receiver.define_own_property(
                                    key,
                                    DataDescriptor::new(val, existing_data_desc.attributes())
                                        .into(),
                                    context,
                                )
                            }
                        }
                    } else {
                        receiver.define_own_property(
                            key,
                            DataDescriptor::new(val, Attribute::all()).into(),
                            context,
                        )
                    }
                } else {
                    Ok(false)
                }
            }
            PropertyDescriptor::Accessor(AccessorDescriptor { set: Some(set), .. }) => {
                set.call(&receiver, &[val], context)?;
                Ok(true)
            }
            _ => Ok(false),
        }
    }

    /// Define an own property.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
    pub fn define_own_property<K>(
        &mut self,
        key: K,
        desc: PropertyDescriptor,
        context: &mut Context,
    ) -> Result<bool>
    where
        K: Into<PropertyKey>,
    {
        if self.is_array() {
            self.array_define_own_property(key, desc, context)
        } else {
            Ok(self.ordinary_define_own_property(key, desc))
        }
    }

    /// Define an own property for an ordinary object.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinarydefineownproperty
    pub fn ordinary_define_own_property<K>(&mut self, key: K, desc: PropertyDescriptor) -> bool
    where
        K: Into<PropertyKey>,
    {
        let _timer = BoaProfiler::global().start_event("Object::define_own_property", "object");

        let key = key.into();
        let extensible = self.is_extensible();

        let current = if let Some(desc) = self.get_own_property(&key) {
            desc
        } else {
            if !extensible {
                return false;
            }

            self.insert(key, desc);
            return true;
        };

        // 4
        if !current.configurable() {
            if desc.configurable() {
                return false;
            }

            if desc.enumerable() != current.enumerable() {
                return false;
            }
        }

        match (&current, &desc) {
            (PropertyDescriptor::Data(current), PropertyDescriptor::Data(desc)) => {
                if !current.configurable() && !current.writable() {
                    if desc.writable() {
                        return false;
                    }

                    if !same_value(&desc.value(), &current.value()) {
                        return false;
                    }
                }
            }
            (PropertyDescriptor::Data(current), PropertyDescriptor::Accessor(_)) => {
                if !current.configurable() {
                    return false;
                }

                let current = AccessorDescriptor::new(None, None, current.attributes());
                self.insert(key, current);
                return true;
            }
            (PropertyDescriptor::Accessor(current), PropertyDescriptor::Data(_)) => {
                if !current.configurable() {
                    return false;
                }

                let current = DataDescriptor::new(Value::undefined(), current.attributes());
                self.insert(key, current);
                return true;
            }
            (PropertyDescriptor::Accessor(current), PropertyDescriptor::Accessor(desc)) => {
                if !current.configurable() {
                    if let (Some(current_get), Some(desc_get)) = (current.getter(), desc.getter()) {
                        if !GcObject::equals(&current_get, &desc_get) {
                            return false;
                        }
                    }

                    if let (Some(current_set), Some(desc_set)) = (current.setter(), desc.setter()) {
                        if !GcObject::equals(&current_set, &desc_set) {
                            return false;
                        }
                    }
                }
            }
        }

        self.insert(key, desc);
        true
    }

    /// Define an own property for an array.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-array-exotic-objects-defineownproperty-p-desc
    fn array_define_own_property<K>(
        &mut self,
        key: K,
        desc: PropertyDescriptor,
        context: &mut Context,
    ) -> Result<bool>
    where
        K: Into<PropertyKey>,
    {
        let key = key.into();
        match key {
            PropertyKey::String(ref s) if s == "length" => {
                match desc {
                    PropertyDescriptor::Accessor(_) => {
                        return Ok(self.ordinary_define_own_property("length", desc))
                    }
                    PropertyDescriptor::Data(ref d) => {
                        if d.value().is_undefined() {
                            return Ok(self.ordinary_define_own_property("length", desc));
                        }
                        let new_len = d.value().to_u32(context)?;
                        let number_len = d.value().to_number(context)?;
                        #[allow(clippy::float_cmp)]
                        if new_len as f64 != number_len {
                            return Err(context.construct_range_error("bad length for array"));
                        }
                        let mut new_len_desc =
                            PropertyDescriptor::Data(DataDescriptor::new(new_len, d.attributes()));
                        let old_len_desc = self.get_own_property(&"length".into()).unwrap();
                        let old_len_desc = old_len_desc.as_data_descriptor().unwrap();
                        let old_len = old_len_desc.value();
                        if new_len >= old_len.to_u32(context)? {
                            return Ok(self.ordinary_define_own_property("length", new_len_desc));
                        }
                        if !old_len_desc.writable() {
                            return Ok(false);
                        }
                        let new_writable = if new_len_desc.attributes().writable() {
                            true
                        } else {
                            let mut new_attributes = new_len_desc.attributes();
                            new_attributes.set_writable(true);
                            new_len_desc = PropertyDescriptor::Data(DataDescriptor::new(
                                new_len,
                                new_attributes,
                            ));
                            false
                        };
                        if !self.ordinary_define_own_property("length", new_len_desc.clone()) {
                            return Ok(false);
                        }
                        let keys_to_delete = {
                            let obj = self.borrow();
                            let mut keys = obj
                                .index_property_keys()
                                .filter(|&&k| k >= new_len)
                                .cloned()
                                .collect::<Vec<_>>();
                            keys.sort_unstable();
                            keys
                        };
                        for key in keys_to_delete.into_iter().rev() {
                            if !self.delete(&key.into()) {
                                let mut new_len_desc_attribute = new_len_desc.attributes();
                                if !new_writable {
                                    new_len_desc_attribute.set_writable(false);
                                }
                                let new_len_desc = PropertyDescriptor::Data(DataDescriptor::new(
                                    key + 1,
                                    new_len_desc_attribute,
                                ));
                                self.ordinary_define_own_property("length", new_len_desc);
                                return Ok(false);
                            }
                        }
                        if !new_writable {
                            let mut new_desc_attr = new_len_desc.attributes();
                            new_desc_attr.set_writable(false);
                            let new_desc = PropertyDescriptor::Data(DataDescriptor::new(
                                new_len,
                                new_desc_attr,
                            ));
                            self.ordinary_define_own_property("length", new_desc);
                        }
                    }
                }
                Ok(true)
            }
            PropertyKey::Index(index) => {
                let old_len_desc = self.get_own_property(&"length".into()).unwrap();
                let old_len_data_desc = old_len_desc.as_data_descriptor().unwrap();
                let old_len = old_len_data_desc.value().to_u32(context)?;
                if index >= old_len && !old_len_data_desc.writable() {
                    return Ok(false);
                }
                if self.ordinary_define_own_property(key, desc) {
                    if index >= old_len && index < std::u32::MAX {
                        let desc = PropertyDescriptor::Data(DataDescriptor::new(
                            index + 1,
                            old_len_data_desc.attributes(),
                        ));
                        self.ordinary_define_own_property("length", desc);
                    }
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            _ => Ok(self.ordinary_define_own_property(key, desc)),
        }
    }

    /// The specification returns a Property Descriptor or Undefined.
    ///
    /// These are 2 separate types and we can't do that here.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getownproperty-p
    #[inline]
    pub fn get_own_property(&self, key: &PropertyKey) -> Option<PropertyDescriptor> {
        let _timer = BoaProfiler::global().start_event("Object::get_own_property", "object");

        let object = self.borrow();
        let property = match key {
            PropertyKey::Index(index) => object.indexed_properties.get(&index),
            PropertyKey::String(ref st) => object.string_properties.get(st),
            PropertyKey::Symbol(ref symbol) => object.symbol_properties.get(symbol),
        };

        property.cloned()
    }

    /// Essential internal method OwnPropertyKeys
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#table-essential-internal-methods
    #[inline]
    #[track_caller]
    pub fn own_property_keys(&self) -> Vec<PropertyKey> {
        self.borrow().keys().collect()
    }

    /// The abstract operation ObjectDefineProperties
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-object.defineproperties
    #[inline]
    pub fn define_properties(&mut self, props: Value, context: &mut Context) -> Result<()> {
        let props = &props.to_object(context)?;
        let keys = props.own_property_keys();
        let mut descriptors: Vec<(PropertyKey, PropertyDescriptor)> = Vec::new();

        for next_key in keys {
            if let Some(prop_desc) = props.get_own_property(&next_key) {
                if prop_desc.enumerable() {
                    let desc_obj = props.get(&next_key, props.clone().into(), context)?;
                    let desc = desc_obj.to_property_descriptor(context)?;
                    descriptors.push((next_key, desc));
                }
            }
        }

        for (p, d) in descriptors {
            self.define_own_property(p, d, context)?;
        }

        Ok(())
    }

    /// `Object.setPropertyOf(obj, prototype)`
    ///
    /// This method sets the prototype (i.e., the internal `[[Prototype]]` property)
    /// of a specified object to another object or `null`.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-setprototypeof-v
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
    #[inline]
    pub fn set_prototype_of(&mut self, val: Value) -> bool {
        debug_assert!(val.is_object() || val.is_null());
        let current = self.get_prototype_of();
        if same_value(&current, &val) {
            return true;
        }
        if !self.is_extensible() {
            return false;
        }
        let mut p = val.clone();
        let mut done = false;
        while !done {
            if p.is_null() {
                done = true
            } else if same_value(&Value::from(self.clone()), &p) {
                return false;
            } else {
                let prototype = p
                    .as_object()
                    .expect("prototype should be null or object")
                    .get_prototype_of();
                p = prototype;
            }
        }
        self.set_prototype_instance(val);
        true
    }

    /// Returns either the prototype or null
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///  - [MDN documentation][mdn]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-getprototypeof
    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
    #[inline]
    #[track_caller]
    pub fn get_prototype_of(&self) -> Value {
        self.borrow().prototype.clone()
    }

    /// Helper function for property insertion.
    #[inline]
    #[track_caller]
    pub(crate) fn insert<K, P>(&mut self, key: K, property: P) -> Option<PropertyDescriptor>
    where
        K: Into<PropertyKey>,
        P: Into<PropertyDescriptor>,
    {
        self.borrow_mut().insert(key, property)
    }

    /// Helper function for property removal.
    #[inline]
    #[track_caller]
    pub(crate) fn remove(&mut self, key: &PropertyKey) -> Option<PropertyDescriptor> {
        self.borrow_mut().remove(key)
    }

    /// Inserts a field in the object `properties` without checking if it's writable.
    ///
    /// If a field was already in the object with the same name that a `Some` is returned
    /// with that field, otherwise None is returned.
    #[inline]
    pub(crate) fn insert_property<K, V>(
        &mut self,
        key: K,
        value: V,
        attribute: Attribute,
    ) -> Option<PropertyDescriptor>
    where
        K: Into<PropertyKey>,
        V: Into<Value>,
    {
        self.insert(key.into(), DataDescriptor::new(value, attribute))
    }

    /// It determines if Object is a callable function with a `[[Call]]` internal method.
    ///
    /// More information:
    /// - [EcmaScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-iscallable
    #[inline]
    #[track_caller]
    pub fn is_callable(&self) -> bool {
        self.borrow().is_callable()
    }

    /// It determines if Object is a function object with a `[[Construct]]` internal method.
    ///
    /// More information:
    /// - [EcmaScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-isconstructor
    #[inline]
    #[track_caller]
    pub fn is_constructable(&self) -> bool {
        self.borrow().is_constructable()
    }

    /// Returns true if the GcObject is the global for a Realm
    pub fn is_global(&self) -> bool {
        matches!(self.borrow().data, ObjectData::Global)
    }
}

impl Object {
    /// Helper function for property insertion.
    #[inline]
    pub(crate) fn insert<K, P>(&mut self, key: K, property: P) -> Option<PropertyDescriptor>
    where
        K: Into<PropertyKey>,
        P: Into<PropertyDescriptor>,
    {
        let property = property.into();
        match key.into() {
            PropertyKey::Index(index) => self.indexed_properties.insert(index, property),
            PropertyKey::String(ref string) => {
                self.string_properties.insert(string.clone(), property)
            }
            PropertyKey::Symbol(ref symbol) => {
                self.symbol_properties.insert(symbol.clone(), property)
            }
        }
    }

    /// Helper function for property removal.
    #[inline]
    pub(crate) fn remove(&mut self, key: &PropertyKey) -> Option<PropertyDescriptor> {
        match key {
            PropertyKey::Index(index) => self.indexed_properties.remove(&index),
            PropertyKey::String(ref string) => self.string_properties.remove(string),
            PropertyKey::Symbol(ref symbol) => self.symbol_properties.remove(symbol),
        }
    }

    /// Inserts a field in the object `properties` without checking if it's writable.
    ///
    /// If a field was already in the object with the same name that a `Some` is returned
    /// with that field, otherwise None is retuned.
    #[inline]
    pub(crate) fn insert_property<K, V>(
        &mut self,
        key: K,
        value: V,
        attribute: Attribute,
    ) -> Option<PropertyDescriptor>
    where
        K: Into<PropertyKey>,
        V: Into<Value>,
    {
        self.insert(key.into(), DataDescriptor::new(value, attribute))
    }
}