summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Runtime/Object.cpp
blob: 64f5e647d3623d57f58b61feb8face17dfae41cf (plain)
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
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <AK/String.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/NativeProperty.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Shape.h>
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/Value.h>

namespace JS {

PropertyDescriptor PropertyDescriptor::from_dictionary(Interpreter& interpreter, const Object& object)
{
    PropertyAttributes attributes;
    if (object.has_property("configurable")) {
        if (interpreter.exception())
            return {};
        attributes.set_has_configurable();
        if (object.get("configurable").value_or(Value(false)).to_boolean())
            attributes.set_configurable();
        if (interpreter.exception())
            return {};
    }
    if (object.has_property("enumerable")) {
        if (interpreter.exception())
            return {};
        attributes.set_has_enumerable();
        if (object.get("enumerable").value_or(Value(false)).to_boolean())
            attributes.set_enumerable();
        if (interpreter.exception())
            return {};
    }
    if (object.has_property("writable")) {
        if (interpreter.exception())
            return {};
        attributes.set_has_writable();
        if (object.get("writable").value_or(Value(false)).to_boolean())
            attributes.set_writable();
        if (interpreter.exception())
            return {};
    }
    PropertyDescriptor descriptor { attributes, object.get("value"), nullptr, nullptr };
    auto getter = object.get("get");
    if (getter.is_function())
        descriptor.getter = &getter.as_function();
    auto setter = object.get("set");
    if (setter.is_function())
        descriptor.setter = &setter.as_function();
    return descriptor;
}

Object* Object::create_empty(Interpreter&, GlobalObject& global_object)
{
    return global_object.heap().allocate<Object>(global_object.object_prototype());
}

Object::Object(Object* prototype)
{
    if (prototype) {
        m_shape = interpreter().global_object().empty_object_shape();
        set_prototype(prototype);
    } else {
        m_shape = interpreter().heap().allocate<Shape>();
    }
}

Object::~Object()
{
}

Object* Object::prototype()
{
    return shape().prototype();
}

const Object* Object::prototype() const
{
    return shape().prototype();
}

bool Object::set_prototype(Object* new_prototype)
{
    if (prototype() == new_prototype)
        return true;
    if (!m_is_extensible)
        return false;
    if (shape().is_unique()) {
        shape().set_prototype_without_transition(new_prototype);
        return true;
    }
    m_shape = m_shape->create_prototype_transition(new_prototype);
    return true;
}

bool Object::has_prototype(const Object* prototype) const
{
    for (auto* object = this->prototype(); object; object = object->prototype()) {
        if (object == prototype)
            return true;
    }
    return false;
}

bool Object::prevent_extensions()
{
    m_is_extensible = false;
    return true;
}

Value Object::get_own_property(const Object& this_object, PropertyName property_name) const
{
    Value value_here;

    if (property_name.is_number()) {
        auto existing_property = m_indexed_properties.get(nullptr, property_name.as_number(), false);
        if (!existing_property.has_value())
            return {};
        value_here = existing_property.value().value.value_or(js_undefined());
    } else {
        auto metadata = shape().lookup(property_name.as_string());
        if (!metadata.has_value())
            return {};
        value_here = m_storage[metadata.value().offset].value_or(js_undefined());
    }

    ASSERT(!value_here.is_empty());
    if (value_here.is_accessor()) {
        return value_here.as_accessor().call_getter(Value(const_cast<Object*>(this)));
    }
    if (value_here.is_object() && value_here.as_object().is_native_property())
        return call_native_property_getter(const_cast<Object*>(&this_object), value_here);
    return value_here;
}

Value Object::get_own_properties(const Object& this_object, GetOwnPropertyMode kind, bool only_enumerable_properties) const
{
    auto* properties_array = Array::create(interpreter().global_object());

    // FIXME: Support generic iterables
    if (this_object.is_string_object()) {
        auto str = static_cast<const StringObject&>(this_object).primitive_string().string();

        for (size_t i = 0; i < str.length(); ++i) {
            if (kind == GetOwnPropertyMode::Key) {
                properties_array->define_property(i, js_string(interpreter(), String::number(i)));
            } else if (kind == GetOwnPropertyMode::Value) {
                properties_array->define_property(i, js_string(interpreter(), String::format("%c", str[i])));
            } else {
                auto* entry_array = Array::create(interpreter().global_object());
                entry_array->define_property(0, js_string(interpreter(), String::number(i)));
                entry_array->define_property(1, js_string(interpreter(), String::format("%c", str[i])));
                properties_array->define_property(i, entry_array);
            }
        }

        return properties_array;
    }

    size_t property_index = 0;
    for (auto& entry : m_indexed_properties) {
        auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(&this_object));
        if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
            continue;

        if (kind == GetOwnPropertyMode::Key) {
            properties_array->define_property(property_index, js_string(interpreter(), String::number(entry.index())));
        } else if (kind == GetOwnPropertyMode::Value) {
            properties_array->define_property(property_index, value_and_attributes.value);
            if (interpreter().exception())
                return {};
        } else {
            auto* entry_array = Array::create(interpreter().global_object());
            entry_array->define_property(0, js_string(interpreter(), String::number(entry.index())));
            entry_array->define_property(1, value_and_attributes.value);
            if (interpreter().exception())
                return {};
            properties_array->define_property(property_index, entry_array);
        }

        ++property_index;
    }

    for (auto& it : this_object.shape().property_table_ordered()) {
        if (only_enumerable_properties && !it.value.attributes.is_enumerable())
            continue;

        size_t offset = it.value.offset + property_index;

        if (kind == GetOwnPropertyMode::Key) {
            properties_array->define_property(offset, js_string(interpreter(), it.key));
        } else if (kind == GetOwnPropertyMode::Value) {
            properties_array->define_property(offset, this_object.get(it.key));
            if (interpreter().exception())
                return {};
        } else {
            auto* entry_array = Array::create(interpreter().global_object());
            entry_array->define_property(0, js_string(interpreter(), it.key));
            entry_array->define_property(1, this_object.get(it.key));
            if (interpreter().exception())
                return {};
            properties_array->define_property(offset, entry_array);
        }
    }

    return properties_array;
}

Optional<PropertyDescriptor> Object::get_own_property_descriptor(PropertyName property_name) const
{
    Value value;
    PropertyAttributes attributes;

    if (property_name.is_number()) {
        auto existing_value = m_indexed_properties.get(nullptr, property_name.as_number(), false);
        if (!existing_value.has_value())
            return {};
        value = existing_value.value().value;
        attributes = existing_value.value().attributes;
        attributes = default_attributes;
    } else {
        auto metadata = shape().lookup(property_name.as_string());
        if (!metadata.has_value())
            return {};
        value = m_storage[metadata.value().offset];
        if (interpreter().exception())
            return {};
        attributes = metadata.value().attributes;
    }

    PropertyDescriptor descriptor { attributes, {}, nullptr, nullptr };
    if (value.is_object() && value.as_object().is_native_property()) {
        auto result = call_native_property_getter(const_cast<Object*>(this), value);
        descriptor.value = result.value_or(js_undefined());
    } else if (value.is_accessor()) {
        auto& pair = value.as_accessor();
        if (pair.getter())
            descriptor.getter = pair.getter();
        if (pair.setter())
            descriptor.setter = pair.setter();
    } else {
        descriptor.value = value.value_or(js_undefined());
    }

    return descriptor;
}

Value Object::get_own_property_descriptor_object(PropertyName property_name) const
{
    auto descriptor_opt = get_own_property_descriptor(property_name);
    if (!descriptor_opt.has_value())
        return js_undefined();
    auto descriptor = descriptor_opt.value();

    auto* descriptor_object = Object::create_empty(interpreter(), interpreter().global_object());
    descriptor_object->define_property("enumerable", Value(descriptor.attributes.is_enumerable()));
    descriptor_object->define_property("configurable", Value(descriptor.attributes.is_configurable()));
    if (descriptor.is_data_descriptor()) {
        descriptor_object->define_property("value", descriptor.value.value_or(js_undefined()));
        descriptor_object->define_property("writable", Value(descriptor.attributes.is_writable()));
    } else if (descriptor.is_accessor_descriptor()) {
        if (descriptor.getter) {
            descriptor_object->define_property("get", Value(descriptor.getter));
        }
        if (descriptor.setter)
            descriptor_object->define_property("set", Value(descriptor.setter));
    }
    return descriptor_object;
}

void Object::set_shape(Shape& new_shape)
{
    m_storage.resize(new_shape.property_count());
    m_shape = &new_shape;
}

bool Object::define_property(const FlyString& property_name, const Object& descriptor, bool throw_exceptions)
{
    bool is_accessor_property = descriptor.has_property("get") || descriptor.has_property("set");
    PropertyAttributes attributes;
    if (descriptor.has_property("configurable")) {
        if (interpreter().exception())
            return false;
        attributes.set_has_configurable();
        if (descriptor.get("configurable").value_or(Value(false)).to_boolean())
            attributes.set_configurable();
        if (interpreter().exception())
            return false;
    }
    if (descriptor.has_property("enumerable")) {
        if (interpreter().exception())
            return false;
        attributes.set_has_enumerable();
        if (descriptor.get("enumerable").value_or(Value(false)).to_boolean())
            attributes.set_enumerable();
        if (interpreter().exception())
            return false;
    }

    if (is_accessor_property) {
        if (descriptor.has_property("value") || descriptor.has_property("writable")) {
            if (throw_exceptions)
                interpreter().throw_exception<TypeError>("Accessor property descriptors cannot specify a value or writable key");
            return false;
        }

        auto getter = descriptor.get("get").value_or(js_undefined());
        if (interpreter().exception())
            return {};
        auto setter = descriptor.get("set").value_or(js_undefined());
        if (interpreter().exception())
            return {};

        Function* getter_function { nullptr };
        Function* setter_function { nullptr };

        if (getter.is_function()) {
            getter_function = &getter.as_function();
        } else if (!getter.is_undefined()) {
            interpreter().throw_exception<TypeError>("Accessor descriptor's 'get' field must be a function or undefined");
            return false;
        }

        if (setter.is_function()) {
            setter_function = &setter.as_function();
        } else if (!setter.is_undefined()) {
            interpreter().throw_exception<TypeError>("Accessor descriptor's 'set' field must be a function or undefined");
            return false;
        }

        dbg() << "Defining new property " << property_name << " with accessor descriptor { attributes=" << attributes << ", "
              << "getter=" << getter.to_string_without_side_effects() << ", "
              << "setter=" << setter.to_string_without_side_effects() << "}";

        return define_property(property_name, Accessor::create(interpreter(), getter_function, setter_function), attributes, throw_exceptions);
    }

    auto value = descriptor.get("value");
    if (interpreter().exception())
        return {};
    if (descriptor.has_property("writable")) {
        if (interpreter().exception())
            return false;
        attributes.set_has_writable();
        if (descriptor.get("writable").value_or(Value(false)).to_boolean())
            attributes.set_writable();
        if (interpreter().exception())
            return false;
    }
    if (interpreter().exception())
        return {};

    dbg() << "Defining new property " << property_name << " with data descriptor { attributes=" << attributes
          << ", value=" << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects()) << " }";

    return define_property(property_name, value, attributes, throw_exceptions);
}

bool Object::define_property(PropertyName property_name, Value value, PropertyAttributes attributes, bool throw_exceptions)
{
    if (property_name.is_number())
        return put_own_property_by_index(*this, property_name.as_number(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
    bool ok;
    i32 property_index = property_name.as_string().to_int(ok);
    if (ok && property_index >= 0)
        return put_own_property_by_index(*this, property_index, value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
    return put_own_property(*this, property_name.as_string(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
}

bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
{
    ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));

    if (!is_extensible()) {
        dbg() << "Disallow define_property of non-extensible object";
        if (throw_exceptions && interpreter().in_strict_mode())
            interpreter().throw_exception<TypeError>(String::format("Cannot define property %s on non-extensible object", property_name.characters()));
        return false;
    }

    if (value.is_accessor()) {
        auto& accessor = value.as_accessor();
        if (accessor.getter())
            attributes.set_has_getter();
        if (accessor.setter())
            attributes.set_has_setter();
    }

    auto metadata = shape().lookup(property_name);
    bool new_property = !metadata.has_value();

    if (new_property) {
        if (!m_shape->is_unique() && shape().property_count() > 100) {
            // If you add more than 100 properties to an object, let's stop doing
            // transitions to avoid filling up the heap with shapes.
            ensure_shape_is_unique();
        }

        if (m_shape->is_unique()) {
            m_shape->add_property_to_unique_shape(property_name, attributes);
            m_storage.resize(m_shape->property_count());
        } else {
            set_shape(*m_shape->create_put_transition(property_name, attributes));
        }
        metadata = shape().lookup(property_name);
        ASSERT(metadata.has_value());
    }

    if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable() && attributes != metadata.value().attributes) {
        dbg() << "Disallow reconfig of non-configurable property";
        if (throw_exceptions)
            interpreter().throw_exception<TypeError>(String::format("Cannot change attributes of non-configurable property '%s'", property_name.characters()));
        return false;
    }

    if (mode == PutOwnPropertyMode::DefineProperty && attributes != metadata.value().attributes) {
        if (m_shape->is_unique()) {
            m_shape->reconfigure_property_in_unique_shape(property_name, attributes);
        } else {
            set_shape(*m_shape->create_configure_transition(property_name, attributes));
        }
        metadata = shape().lookup(property_name);

        dbg() << "Reconfigured property " << property_name << ", new shape says offset is " << metadata.value().offset << " and my storage capacity is " << m_storage.size();
    }

    auto value_here = m_storage[metadata.value().offset];
    if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
        dbg() << "Disallow write to non-writable property";
        return false;
    }

    if (value.is_empty())
        return true;

    if (value_here.is_object() && value_here.as_object().is_native_property()) {
        call_native_property_setter(const_cast<Object*>(&this_object), value_here, value);
    } else {
        m_storage[metadata.value().offset] = value;
    }
    return true;
}

bool Object::put_own_property_by_index(Object& this_object, u32 property_index, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
{
    ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));

    if (!is_extensible()) {
        dbg() << "Disallow define_property of non-extensible object";
        if (throw_exceptions && interpreter().in_strict_mode())
            interpreter().throw_exception<TypeError>(String::format("Cannot define property %d on non-extensible object", property_index));
        return false;
    }

    if (value.is_accessor()) {
        auto& accessor = value.as_accessor();
        if (accessor.getter())
            attributes.set_has_getter();
        if (accessor.setter())
            attributes.set_has_setter();
    }

    auto existing_property = m_indexed_properties.get(nullptr, property_index, false);
    auto new_property = !existing_property.has_value();
    PropertyAttributes existing_attributes = new_property ? 0 : existing_property.value().attributes;

    if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
        dbg() << "Disallow reconfig of non-configurable property";
        if (throw_exceptions)
            interpreter().throw_exception<TypeError>(String::format("Cannot change attributes of non-configurable property %d", property_index));
        return false;
    }

    auto value_here = new_property ? Value() : existing_property.value().value;
    if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
        dbg() << "Disallow write to non-writable property";
        return false;
    }

    if (value.is_empty())
        return true;

    if (value_here.is_object() && value_here.as_object().is_native_property()) {
        call_native_property_setter(const_cast<Object*>(&this_object), value_here, value);
    } else {
        m_indexed_properties.put(&this_object, property_index, value, attributes, mode == PutOwnPropertyMode::Put);
    }
    return true;
}

Value Object::delete_property(PropertyName property_name)
{
    ASSERT(property_name.is_valid());
    if (property_name.is_number())
        return Value(m_indexed_properties.remove(property_name.as_number()));
    auto metadata = shape().lookup(property_name.as_string());
    if (!metadata.has_value())
        return Value(true);
    if (!metadata.value().attributes.is_configurable())
        return Value(false);

    size_t deleted_offset = metadata.value().offset;

    ensure_shape_is_unique();

    shape().remove_property_from_unique_shape(property_name.as_string(), deleted_offset);
    m_storage.remove(deleted_offset);
    return Value(true);
}

void Object::ensure_shape_is_unique()
{
    if (shape().is_unique())
        return;

    m_shape = m_shape->create_unique_clone();
}

Value Object::get_by_index(u32 property_index) const
{
    const Object* object = this;
    while (object) {
        if (is_string_object()) {
            auto& string = static_cast<const StringObject*>(this)->primitive_string().string();
            if (property_index < string.length())
                return js_string(heap(), string.substring(property_index, 1));
            return js_undefined();
        }
        if (static_cast<size_t>(property_index) < object->m_indexed_properties.array_like_size()) {
            auto result = object->m_indexed_properties.get(const_cast<Object*>(this), property_index);
            if (interpreter().exception())
                return {};
            if (result.has_value() && !result.value().value.is_empty())
                return result.value().value;
            return {};
        }
        object = object->prototype();
    }
    return {};
}

Value Object::get(PropertyName property_name) const
{
    if (property_name.is_number())
        return get_by_index(property_name.as_number());

    auto property_string = property_name.to_string();
    bool ok;
    i32 property_index = property_string.to_int(ok);
    if (ok && property_index >= 0)
        return get_by_index(property_index);

    const Object* object = this;
    while (object) {
        auto value = object->get_own_property(*this, property_name);
        if (!value.is_empty())
            return value;
        object = object->prototype();
    }
    return {};
}

bool Object::put_by_index(u32 property_index, Value value)
{
    ASSERT(!value.is_empty());

    // If there's a setter in the prototype chain, we go to the setter.
    // Otherwise, it goes in the own property storage.
    Object* object = this;
    while (object) {
        auto existing_value = object->m_indexed_properties.get(nullptr, property_index, false);
        if (existing_value.has_value()) {
            auto value_here = existing_value.value();
            if (value_here.value.is_accessor()) {
                value_here.value.as_accessor().call_setter(object, value);
                return true;
            }
            if (value_here.value.is_object() && value_here.value.as_object().is_native_property()) {
                call_native_property_setter(const_cast<Object*>(this), value_here.value, value);
                return true;
            }
        }
        object = object->prototype();
    }
    return put_own_property_by_index(*this, property_index, value, default_attributes, PutOwnPropertyMode::Put);
}

bool Object::put(PropertyName property_name, Value value)
{
    if (property_name.is_number())
        return put_by_index(property_name.as_number(), value);

    ASSERT(!value.is_empty());

    auto property_string = property_name.to_string();
    bool ok;
    i32 property_index = property_string.to_int(ok);
    if (ok && property_index >= 0)
        return put_by_index(property_index, value);

    // If there's a setter in the prototype chain, we go to the setter.
    // Otherwise, it goes in the own property storage.
    Object* object = this;
    while (object) {
        auto metadata = object->shape().lookup(property_string);
        if (metadata.has_value()) {
            auto value_here = object->m_storage[metadata.value().offset];
            if (value_here.is_accessor()) {
                value_here.as_accessor().call_setter(Value(this), value);
                return true;
            }
            if (value_here.is_object() && value_here.as_object().is_native_property()) {
                call_native_property_setter(const_cast<Object*>(this), value_here, value);
                return true;
            }
        }
        object = object->prototype();
    }
    return put_own_property(*this, property_string, value, default_attributes, PutOwnPropertyMode::Put);
}

bool Object::define_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length, PropertyAttributes attribute)
{
    auto* function = NativeFunction::create(interpreter(), interpreter().global_object(), property_name, move(native_function));
    function->define_property("length", Value(length), Attribute::Configurable);
    function->define_property("name", js_string(heap(), property_name), Attribute::Configurable);
    return define_property(property_name, function, attribute);
}

bool Object::define_native_property(const FlyString& property_name, AK::Function<Value(Interpreter&)> getter, AK::Function<void(Interpreter&, Value)> setter, PropertyAttributes attribute)
{
    return define_property(property_name, heap().allocate<NativeProperty>(move(getter), move(setter)), attribute);
}

void Object::visit_children(Cell::Visitor& visitor)
{
    Cell::visit_children(visitor);
    visitor.visit(m_shape);

    for (auto& value : m_storage)
        visitor.visit(value);

    for (auto& value : m_indexed_properties.values_unordered())
        visitor.visit(value.value);
}

bool Object::has_property(PropertyName property_name) const
{
    const Object* object = this;
    while (object) {
        if (object->has_own_property(property_name))
            return true;
        object = object->prototype();
    }
    return false;
}

bool Object::has_own_property(PropertyName property_name) const
{
    auto has_indexed_property = [&](u32 index) -> bool {
        if (is_string_object())
            return index < static_cast<const StringObject*>(this)->primitive_string().string().length();
        return m_indexed_properties.has_index(index);
    };

    if (property_name.is_number())
        return has_indexed_property(property_name.as_number());

    bool ok;
    i32 property_index = property_name.as_string().to_int(ok);
    if (ok && property_index >= 0)
        return has_indexed_property(property_index);

    return shape().lookup(property_name.as_string()).has_value();
}

Value Object::to_primitive(Value::PreferredType preferred_type) const
{
    Value result = js_undefined();

    switch (preferred_type) {
    case Value::PreferredType::Default:
    case Value::PreferredType::Number: {
        result = value_of();
        if (result.is_object()) {
            result = to_string();
        }
        break;
    }
    case Value::PreferredType::String: {
        result = to_string();
        if (result.is_object())
            result = value_of();
        break;
    }
    }

    ASSERT(!result.is_object());
    return result;
}

Value Object::to_string() const
{
    auto to_string_property = get("toString");
    if (to_string_property.is_function()) {
        auto& to_string_function = to_string_property.as_function();
        auto& interpreter = const_cast<Object*>(this)->interpreter();
        auto to_string_result = interpreter.call(to_string_function, const_cast<Object*>(this));
        if (to_string_result.is_object())
            interpreter.throw_exception<TypeError>("Cannot convert object to string");
        if (interpreter.exception())
            return {};
        auto* string = to_string_result.to_primitive_string(interpreter);
        if (interpreter.exception())
            return {};
        return string;
    }
    return js_string(heap(), String::format("[object %s]", class_name()));
}

Value Object::invoke(const FlyString& property_name, Optional<MarkedValueList> arguments)
{
    auto& interpreter = this->interpreter();
    auto property = get(property_name).value_or(js_undefined());
    if (interpreter.exception())
        return {};
    if (!property.is_function()) {
        interpreter.throw_exception<TypeError>(String::format("%s is not a function", property.to_string_without_side_effects().characters()));
        return {};
    }
    return interpreter.call(property.as_function(), this, move(arguments));
}

Value Object::call_native_property_getter(Object* this_object, Value property) const
{
    ASSERT(property.is_object());
    ASSERT(property.as_object().is_native_property());
    auto& native_property = static_cast<NativeProperty&>(property.as_object());
    auto& call_frame = interpreter().push_call_frame();
    call_frame.this_value = this_object;
    auto result = native_property.get(interpreter());
    interpreter().pop_call_frame();
    return result;
}

void Object::call_native_property_setter(Object* this_object, Value property, Value value) const
{
    ASSERT(property.is_object());
    ASSERT(property.as_object().is_native_property());
    auto& native_property = static_cast<NativeProperty&>(property.as_object());
    auto& call_frame = interpreter().push_call_frame();
    call_frame.this_value = this_object;
    native_property.set(interpreter(), value);
    interpreter().pop_call_frame();
}

}