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
use std::ffi::CString;
use std::mem::MaybeUninit;
use std::ptr;
use std::slice;

use failure::{format_err, Error};
use foreign_types::ForeignTypeRef;

use crate::{
    ffi,
    value::{FALSE, TRUE},
    Atom, ContextRef, Local, NewAtom, NewValue, Value,
};

bitflags! {
    /// Flags for property
    pub struct Prop: u32 {
        /// This property descriptor may be changed or deleted from the corresponding object.
        const CONFIGURABLE = ffi::JS_PROP_CONFIGURABLE;
        /// The value associated with the property may be changed with an assignment operator.
        const WRITABLE = ffi::JS_PROP_WRITABLE;
        /// This property shows up during enumeration of the properties on the corresponding object.
        const ENUMERABLE = ffi::JS_PROP_ENUMERABLE;
        const C_W_E = ffi::JS_PROP_C_W_E;
        const PROP_LENGTH = ffi::JS_PROP_LENGTH;
        const TMASK = ffi::JS_PROP_TMASK;
        const NORMAL = ffi::JS_PROP_NORMAL;
        const GETSET = ffi::JS_PROP_GETSET;
        const VARREF = ffi::JS_PROP_VARREF;
        const AUTOINIT = ffi::JS_PROP_AUTOINIT;

        const HAS_SHIFT = ffi::JS_PROP_HAS_SHIFT;
        const HAS_CONFIGURABLE = ffi::JS_PROP_HAS_CONFIGURABLE;
        const HAS_WRITABLE = ffi::JS_PROP_HAS_WRITABLE;
        const HAS_ENUMERABLE = ffi::JS_PROP_HAS_ENUMERABLE;
        /// Has function which serves as a getter for the property.
        const HAS_GET = ffi::JS_PROP_HAS_GET;
        /// Has function which serves as a setter for the property.
        const HAS_SET = ffi::JS_PROP_HAS_SET;
        /// Has value associated with the property.
        const HAS_VALUE = ffi::JS_PROP_HAS_VALUE;

        const THROW = ffi::JS_PROP_THROW;
        const THROW_STRICT = ffi::JS_PROP_THROW_STRICT;

        const NO_ADD = ffi::JS_PROP_NO_ADD;
        const NO_EXOTIC = ffi::JS_PROP_NO_EXOTIC;
    }
}

bitflags! {
    /// Flags for `get_own_property_names`
    pub struct Names: u32 {
        const STRING = ffi::JS_GPN_STRING_MASK;
        const SYMBOL = ffi::JS_GPN_SYMBOL_MASK;
        /// only include the enumerable properties
        const ENUM_ONLY = ffi::JS_GPN_ENUM_ONLY;
    }
}

/// Get a property value on an object.
pub trait GetProperty {
    /// Get a property value on an object.
    fn get_property<'a>(&self, ctxt: &'a ContextRef, this: &Value) -> Option<Local<'a, Value>>;
}

impl GetProperty for &str {
    fn get_property<'a>(&self, ctxt: &'a ContextRef, this: &Value) -> Option<Local<'a, Value>> {
        ctxt.bind(unsafe {
            ffi::JS_GetPropertyStr(
                ctxt.as_ptr(),
                this.raw(),
                CString::new(*self).expect("prop").as_ptr(),
            )
        })
        .check_undefined()
    }
}

impl GetProperty for u32 {
    fn get_property<'a>(&self, ctxt: &'a ContextRef, this: &Value) -> Option<Local<'a, Value>> {
        ctxt.bind(unsafe { ffi::JS_GetPropertyUint32(ctxt.as_ptr(), this.raw(), *self) })
            .check_undefined()
    }
}

impl GetProperty for Local<'_, ffi::JSAtom> {
    fn get_property<'a>(&self, ctxt: &'a ContextRef, this: &Value) -> Option<Local<'a, Value>> {
        ctxt.bind(unsafe {
            ffi::JS_GetPropertyInternal(ctxt.as_ptr(), this.raw(), **self, this.raw(), FALSE)
        })
        .check_undefined()
    }
}

/// Set a property value on an object.
pub trait SetProperty {
    /// Set a property value on an object.
    fn set_property<T: NewValue>(
        &self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
    ) -> Result<bool, Error>;
}

impl SetProperty for u32 {
    fn set_property<T: NewValue>(
        &self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
    ) -> Result<bool, Error> {
        let ret = unsafe {
            ffi::JS_SetPropertyUint32(ctxt.as_ptr(), this.raw(), *self, val.new_value(ctxt))
        };

        ctxt.check_error(ret).and_then(|ret| match ret {
            TRUE => Ok(true),
            FALSE => Ok(false),
            _ => Err(format_err!("unexpected result: {}", ret)),
        })
    }
}

impl SetProperty for i64 {
    fn set_property<T: NewValue>(
        &self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
    ) -> Result<bool, Error> {
        let ret = unsafe {
            ffi::JS_SetPropertyInt64(ctxt.as_ptr(), this.raw(), *self, val.new_value(ctxt))
        };

        ctxt.check_error(ret).and_then(|ret| match ret {
            TRUE => Ok(true),
            FALSE => Ok(false),
            _ => Err(format_err!("unexpected result: {}", ret)),
        })
    }
}

impl SetProperty for &str {
    fn set_property<T: NewValue>(
        &self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
    ) -> Result<bool, Error> {
        ctxt.check_bool(unsafe {
            ffi::JS_SetPropertyStr(
                ctxt.as_ptr(),
                this.raw(),
                CString::new(*self)?.as_ptr(),
                val.new_value(ctxt),
            )
        })
    }
}

impl SetProperty for Local<'_, ffi::JSAtom> {
    fn set_property<T: NewValue>(
        &self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
    ) -> Result<bool, Error> {
        ctxt.check_bool(unsafe {
            ffi::JS_SetPropertyInternal(
                ctxt.as_ptr(),
                this.raw(),
                **self,
                val.new_value(ctxt),
                ffi::JS_PROP_THROW as i32,
            )
        })
    }
}

/// Check if a property on an object.
pub trait HasProperty {
    /// Check if a property on an object.
    fn has_property(self, ctxt: &ContextRef, this: &Value) -> Result<bool, Error>;
}

impl<'a, T> HasProperty for T
where
    T: NewAtom,
{
    fn has_property(self, ctxt: &ContextRef, this: &Value) -> Result<bool, Error> {
        let atom = self.new_atom(ctxt);
        let ret = unsafe { ffi::JS_HasProperty(ctxt.as_ptr(), this.raw(), atom) };

        ctxt.free_atom(atom);
        ctxt.check_bool(ret)
    }
}

/// Delete a property on an object.
pub trait DeleteProperty {
    /// Delete a property on an object.
    ///
    /// It returns a `bool` indicating whether or not the property was successfully deleted.
    fn delete_property(self, ctxt: &ContextRef, this: &Value) -> Result<bool, Error>;
}

impl<'a, T> DeleteProperty for T
where
    T: NewAtom,
{
    fn delete_property(self, ctxt: &ContextRef, this: &Value) -> Result<bool, Error> {
        let atom = self.new_atom(ctxt);
        let ret = unsafe {
            ffi::JS_DeleteProperty(ctxt.as_ptr(), this.raw(), atom, ffi::JS_PROP_THROW as i32)
        };

        ctxt.free_atom(atom);
        ctxt.check_bool(ret)
    }
}

/// Defines a new property directly on an object, or modifies an existing property on an object.
pub trait DefineProperty {
    /// Defines a new property directly on an object, or modifies an existing property on an object.
    fn define_property(
        self,
        ctxt: &ContextRef,
        this: &Value,
        val: Option<Value>,
        getter: Option<&Value>,
        setter: Option<&Value>,
        flags: Prop,
    ) -> Result<bool, Error>;
}

impl<'a, T> DefineProperty for T
where
    T: NewAtom,
{
    fn define_property(
        self,
        ctxt: &ContextRef,
        this: &Value,
        val: Option<Value>,
        getter: Option<&Value>,
        setter: Option<&Value>,
        mut flags: Prop,
    ) -> Result<bool, Error> {
        let atom = self.new_atom(ctxt);
        if val.is_some() {
            flags |= Prop::HAS_VALUE;
        }
        if getter.is_some() {
            flags |= Prop::HAS_GET;
        }
        if setter.is_some() {
            flags |= Prop::HAS_SET;
        }
        let ret = unsafe {
            ffi::JS_DefineProperty(
                ctxt.as_ptr(),
                this.raw(),
                atom,
                val.map_or_else(|| Value::undefined().raw(), |v| v.raw()),
                getter.map_or_else(|| Value::undefined().raw(), |v| v.raw()),
                setter.map_or_else(|| Value::undefined().raw(), |v| v.raw()),
                flags.bits as i32,
            )
        };
        ctxt.free_atom(atom);
        ctxt.check_bool(ret)
    }
}

pub trait DefinePropertyValue {
    /// Defines a new property with value directly on an object, or modifies an existing property on an object.
    fn define_property<T: NewValue>(
        self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
        flags: Prop,
    ) -> Result<bool, Error>;
}

impl DefinePropertyValue for u32 {
    fn define_property<T: NewValue>(
        self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
        flags: Prop,
    ) -> Result<bool, Error> {
        ctxt.check_bool(unsafe {
            ffi::JS_DefinePropertyValueUint32(
                ctxt.as_ptr(),
                this.raw(),
                self,
                val.new_value(ctxt),
                flags.bits as i32,
            )
        })
    }
}

impl DefinePropertyValue for &'_ str {
    fn define_property<T: NewValue>(
        self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
        flags: Prop,
    ) -> Result<bool, Error> {
        ctxt.check_bool(unsafe {
            ffi::JS_DefinePropertyValueStr(
                ctxt.as_ptr(),
                this.raw(),
                CString::new(self)?.as_ptr(),
                val.new_value(ctxt),
                flags.bits as i32,
            )
        })
    }
}

impl DefinePropertyValue for Local<'_, ffi::JSAtom> {
    fn define_property<T: NewValue>(
        self,
        ctxt: &ContextRef,
        this: &Value,
        val: T,
        flags: Prop,
    ) -> Result<bool, Error> {
        ctxt.check_bool(unsafe {
            ffi::JS_DefinePropertyValue(
                ctxt.as_ptr(),
                this.raw(),
                *self,
                val.new_value(ctxt),
                flags.bits as i32,
            )
        })
    }
}

pub trait DefinePropertyGetSet {
    /// Defines a new property with getter and setter directly on an object, or modifies an existing property on an object.
    fn define_property(
        self,
        ctxt: &ContextRef,
        this: &Value,
        getter: Option<&Value>,
        setter: Option<&Value>,
        flags: Prop,
    ) -> Result<bool, Error>;
}

impl<T> DefinePropertyGetSet for T
where
    T: NewAtom,
{
    fn define_property(
        self,
        ctxt: &ContextRef,
        this: &Value,
        getter: Option<&Value>,
        setter: Option<&Value>,
        mut flags: Prop,
    ) -> Result<bool, Error> {
        let atom = self.new_atom(ctxt);
        if getter.is_some() {
            flags |= Prop::HAS_GET;
        }
        if setter.is_some() {
            flags |= Prop::HAS_SET;
        }
        let ret = unsafe {
            ffi::JS_DefinePropertyGetSet(
                ctxt.as_ptr(),
                this.raw(),
                atom,
                getter.map_or_else(|| Value::undefined().raw(), |v| v.raw()),
                setter.map_or_else(|| Value::undefined().raw(), |v| v.raw()),
                flags.bits as i32,
            )
        };
        ctxt.free_atom(atom);
        ctxt.check_bool(ret)
    }
}

/// A property descriptor is a record with some of the following attributes:
#[derive(Debug, Default)]
pub struct Descriptor<'a> {
    /// `true` if and only if the value associated with the property may be changed (data descriptors only).
    pub writable: bool,
    /// The value associated with the property (data descriptors only).
    pub value: Option<Local<'a, Value>>,
    /// A function which serves as a getter for the property.
    pub getter: Option<Local<'a, Value>>,
    /// A function which serves as a setter for the property.
    pub setter: Option<Local<'a, Value>>,
    /// `true` if and only if the type of this property descriptor may be changed
    /// and if the property may be deleted from the corresponding object.
    pub configurable: bool,
    /// `true` if and only if this property shows up during enumeration of the properties on the corresponding object.
    pub enumerable: bool,
}

impl<'a> Local<'a, Value> {
    /// Returns an array of a given object's own property names, in the same order as we get with a normal loop.
    pub fn keys(&self) -> Result<Option<Vec<Atom>>, Error> {
        self.ctxt
            .get_own_property_names(self, Names::ENUM_ONLY | Names::STRING)
    }

    /// Returns an array of all properties (including non-enumerable properties except for those which use Symbol)
    /// found directly in a given object.
    pub fn get_own_property_names(&self) -> Result<Option<Vec<Atom>>, Error> {
        self.ctxt
            .get_own_property_names(self, Names::STRING | Names::SYMBOL)
    }

    /// Returns a property descriptor for an own property
    /// (that is, one directly present on an object and not in the object's prototype chain) of a given object.
    pub fn get_own_property_descriptor<T: NewAtom>(
        &self,
        prop: T,
    ) -> Result<Option<Descriptor>, Error> {
        self.ctxt.get_own_property_descriptor(self, prop)
    }

    /// Get a property value on an object.
    pub fn get_property<T: GetProperty>(&self, prop: T) -> Option<Local<Value>> {
        self.ctxt.get_property(self, prop)
    }

    /// Set a property value on an object.
    pub fn set_property<T: SetProperty, V: NewValue>(
        &self,
        prop: T,
        val: V,
    ) -> Result<bool, Error> {
        self.ctxt.set_property(self, prop, val)
    }

    /// Check if a property on an object.
    pub fn has_property<T: HasProperty>(&self, prop: T) -> Result<bool, Error> {
        self.ctxt.has_property(self, prop)
    }

    /// Delete a property on an object.
    ///
    /// It returns a `bool` indicating whether or not the property was successfully deleted.
    pub fn delete_property<T: DeleteProperty>(&self, prop: T) -> Result<bool, Error> {
        self.ctxt.delete_property(self, prop)
    }

    /// Defines a new property directly on an object, or modifies an existing property on an object.
    pub fn define_property<T: DefineProperty>(
        &self,
        prop: T,
        val: Option<Value>,
        getter: Option<&Value>,
        setter: Option<&Value>,
        flags: Prop,
    ) -> Result<bool, Error> {
        self.ctxt
            .define_property(self, prop, val, getter, setter, flags)
    }

    /// Defines a new property with value directly on an object,
    /// or modifies an existing property on an object.
    pub fn define_property_value<T: DefinePropertyValue, V: NewValue>(
        &self,
        prop: T,
        val: V,
        flags: Prop,
    ) -> Result<bool, Error> {
        self.ctxt.define_property_value(self, prop, val, flags)
    }

    /// Defines a new property with getter and setter directly on an object,
    /// or modifies an existing property on an object.
    pub fn define_property_get_set<T: DefinePropertyGetSet>(
        &self,
        prop: T,
        getter: Option<&Value>,
        setter: Option<&Value>,
        flags: Prop,
    ) -> Result<bool, Error> {
        self.ctxt
            .define_property_get_set(self, prop, getter, setter, flags)
    }

    /// Check if an object is extensible (whether it can have new properties added to it).
    pub fn is_extensible(&self) -> Result<bool, Error> {
        self.ctxt.is_extensible(self)
    }

    /// Prevents new properties from ever being added to an object (i.e. prevents future extensions to the object).
    pub fn prevent_extensions(&self) -> Result<bool, Error> {
        self.ctxt.prevent_extensions(self)
    }
}

impl ContextRef {
    /// Returns an array of all properties (including non-enumerable properties except for those which use Symbol)
    /// found directly in a given object.
    pub fn get_own_property_names(
        &self,
        value: &Value,
        flags: Names,
    ) -> Result<Option<Vec<Atom>>, Error> {
        let mut ptab = ptr::null_mut();
        let mut count = 0;

        self.check_error(unsafe {
            ffi::JS_GetOwnPropertyNames(
                self.as_ptr(),
                &mut ptab,
                &mut count,
                value.raw(),
                flags.bits() as i32,
            )
        })
        .map(|_| {
            let names = unsafe { slice::from_raw_parts(ptab, count as usize) };
            let names = names
                .iter()
                .map(|prop| self.bind_atom(prop.atom))
                .collect::<Vec<_>>();

            unsafe { ffi::js_free(self.as_ptr(), ptab as *mut _) }

            Some(names)
        })
    }

    /// Returns a property descriptor for an own property
    /// (that is, one directly present on an object and not in the object's prototype chain) of a given object.
    pub fn get_own_property_descriptor<T: NewAtom>(
        &self,
        value: &Value,
        prop: T,
    ) -> Result<Option<Descriptor>, Error> {
        let atom = prop.new_atom(self);
        let mut desc = MaybeUninit::<ffi::JSPropertyDescriptor>::uninit();
        let res =
            unsafe { ffi::JS_GetOwnProperty(self.as_ptr(), desc.as_mut_ptr(), value.raw(), atom) };
        self.free_atom(atom);

        self.check_bool(res).map(|exists| {
            if exists {
                let desc = unsafe { desc.assume_init() };
                let flags = Prop::from_bits_truncate(desc.flags as u32);

                Some(Descriptor {
                    writable: flags.contains(Prop::WRITABLE),
                    value: Value::new(desc.value).map(|v| self.bind(v)),
                    getter: Value::new(desc.getter).map(|v| self.bind(v)),
                    setter: Value::new(desc.setter).map(|v| self.bind(v)),
                    configurable: flags.contains(Prop::CONFIGURABLE),
                    enumerable: flags.contains(Prop::ENUMERABLE),
                })
            } else {
                None
            }
        })
    }

    /// Get a property value on an object.
    pub fn get_property<T: GetProperty>(&self, this: &Value, prop: T) -> Option<Local<Value>> {
        prop.get_property(self, this)
    }

    /// Set a property value on an object.
    pub fn set_property<T: SetProperty, V: NewValue>(
        &self,
        this: &Value,
        prop: T,
        val: V,
    ) -> Result<bool, Error> {
        prop.set_property(self, this, val)
    }

    /// Check if a property on an object.
    pub fn has_property<T: HasProperty>(&self, this: &Value, prop: T) -> Result<bool, Error> {
        prop.has_property(self, this)
    }

    /// Delete a property on an object.
    ///
    /// It returns a `bool` indicating whether or not the property was successfully deleted.
    pub fn delete_property<T: DeleteProperty>(&self, this: &Value, prop: T) -> Result<bool, Error> {
        prop.delete_property(self, this)
    }

    /// Defines a new property directly on an object, or modifies an existing property on an object.
    pub fn define_property<T: DefineProperty>(
        &self,
        this: &Value,
        prop: T,
        val: Option<Value>,
        getter: Option<&Value>,
        setter: Option<&Value>,
        flags: Prop,
    ) -> Result<bool, Error> {
        prop.define_property(self, this, val, getter, setter, flags)
    }

    /// Defines a new property with value directly on an object,
    /// or modifies an existing property on an object.
    pub fn define_property_value<T: DefinePropertyValue, V: NewValue>(
        &self,
        this: &Value,
        prop: T,
        val: V,
        flags: Prop,
    ) -> Result<bool, Error> {
        prop.define_property(self, this, val, flags)
    }

    /// Defines a new property with getter and setter directly on an object,
    /// or modifies an existing property on an object.
    pub fn define_property_get_set<T: DefinePropertyGetSet>(
        &self,
        this: &Value,
        prop: T,
        getter: Option<&Value>,
        setter: Option<&Value>,
        flags: Prop,
    ) -> Result<bool, Error> {
        prop.define_property(self, this, getter, setter, flags)
    }

    /// Check if an object is extensible (whether it can have new properties added to it).
    pub fn is_extensible(&self, obj: &Value) -> Result<bool, Error> {
        self.check_bool(unsafe { ffi::JS_IsExtensible(self.as_ptr(), obj.raw()) })
    }

    /// Prevents new properties from ever being added to an object (i.e. prevents future extensions to the object).
    pub fn prevent_extensions(&self, obj: &Value) -> Result<bool, Error> {
        self.check_bool(unsafe { ffi::JS_PreventExtensions(self.as_ptr(), obj.raw()) })
    }
}

#[cfg(test)]
mod tests {
    use crate::{Context, ErrorKind, Eval, Runtime};

    #[test]
    fn set_property() {
        let _ = pretty_env_logger::try_init();

        let rt = Runtime::new();
        let ctxt = Context::new(&rt);

        let obj = ctxt
            .eval_script("new Object();", "<evalScript>", Eval::GLOBAL)
            .unwrap();

        assert!(!obj.has_property("foo").unwrap());
        assert!(obj.get_property("foo").is_none());

        assert!(obj.set_property("foo", "bar").unwrap());
        assert!(obj.has_property("foo").unwrap());
        assert_eq!(obj.get_property("foo").unwrap().to_string(), "bar");

        assert_eq!(
            obj.get_own_property_names()
                .unwrap()
                .unwrap()
                .into_iter()
                .map(|name| name.to_cstr().to_string_lossy().to_string())
                .collect::<Vec<_>>(),
            vec!["foo"]
        );

        let desc = obj.get_own_property_descriptor("foo").unwrap().unwrap();
        assert!(desc.writable);
        assert_eq!(desc.value.unwrap().to_string(), "bar");
        assert!(desc.getter.is_none());
        assert!(desc.setter.is_none());
        assert!(desc.configurable);
        assert!(desc.enumerable);

        assert!(obj.delete_property("foo").unwrap());
        assert!(!obj.has_property("foo").unwrap());
    }

    #[test]
    fn extensible() {
        let _ = pretty_env_logger::try_init();

        let rt = Runtime::new();
        let ctxt = Context::new(&rt);

        let obj = ctxt
            .eval_script("new Object();", "<evalScript>", Eval::GLOBAL)
            .unwrap();

        assert!(obj.is_extensible().unwrap());
        assert!(obj.prevent_extensions().unwrap());
        assert!(!obj.is_extensible().unwrap());

        assert_eq!(
            obj.set_property("foo", "bar")
                .unwrap_err()
                .downcast::<ErrorKind>()
                .unwrap(),
            ErrorKind::TypeError("object is not extensible".into(), None)
        );
    }
}