|
@@ -6,6 +6,7 @@
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
#include <AK/String.h>
|
|
|
+#include <LibJS/Runtime/Accessor.h>
|
|
|
#include <LibJS/Runtime/BooleanObject.h>
|
|
|
#include <LibJS/Runtime/Date.h>
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
@@ -37,6 +38,8 @@ void ObjectPrototype::initialize(GlobalObject& global_object)
|
|
|
define_native_function(vm.names.isPrototypeOf, is_prototype_of, 1, attr);
|
|
|
|
|
|
// Annex B
|
|
|
+ define_native_function(vm.names.__defineGetter__, define_getter, 2, attr);
|
|
|
+ define_native_function(vm.names.__defineSetter__, define_setter, 2, attr);
|
|
|
define_native_accessor(vm.names.__proto__, proto_getter, proto_setter, Attribute::Configurable);
|
|
|
}
|
|
|
|
|
@@ -147,6 +150,70 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::is_prototype_of)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// B.2.2.2 Object.prototype.__defineGetter__ ( P, getter ), https://tc39.es/ecma262/#sec-object.prototype.__defineGetter__
|
|
|
+JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::define_getter)
|
|
|
+{
|
|
|
+ auto object = vm.this_value(global_object).to_object(global_object);
|
|
|
+ if (vm.exception())
|
|
|
+ return {};
|
|
|
+
|
|
|
+ auto getter = vm.argument(1);
|
|
|
+ if (!getter.is_function()) {
|
|
|
+ vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, getter.to_string_without_side_effects());
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+
|
|
|
+ auto key = vm.argument(0).to_property_key(global_object);
|
|
|
+ if (vm.exception())
|
|
|
+ return {};
|
|
|
+
|
|
|
+ auto descriptor = Object::create(global_object, global_object.object_prototype());
|
|
|
+ descriptor->define_property(vm.names.get, getter);
|
|
|
+ descriptor->define_property(vm.names.enumerable, Value(true));
|
|
|
+ descriptor->define_property(vm.names.configurable, Value(true));
|
|
|
+
|
|
|
+ auto success = object->define_property(key, *descriptor);
|
|
|
+ if (vm.exception())
|
|
|
+ return {};
|
|
|
+ if (!success) {
|
|
|
+ vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ return js_undefined();
|
|
|
+}
|
|
|
+
|
|
|
+// B.2.2.3 Object.prototype.__defineSetter__ ( P, getter ), https://tc39.es/ecma262/#sec-object.prototype.__defineSetter__
|
|
|
+JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::define_setter)
|
|
|
+{
|
|
|
+ auto object = vm.this_value(global_object).to_object(global_object);
|
|
|
+ if (vm.exception())
|
|
|
+ return {};
|
|
|
+
|
|
|
+ auto setter = vm.argument(1);
|
|
|
+ if (!setter.is_function()) {
|
|
|
+ vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, setter.to_string_without_side_effects());
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+
|
|
|
+ auto key = vm.argument(0).to_property_key(global_object);
|
|
|
+ if (vm.exception())
|
|
|
+ return {};
|
|
|
+
|
|
|
+ auto descriptor = Object::create(global_object, global_object.object_prototype());
|
|
|
+ descriptor->define_property(vm.names.set, setter);
|
|
|
+ descriptor->define_property(vm.names.enumerable, Value(true));
|
|
|
+ descriptor->define_property(vm.names.configurable, Value(true));
|
|
|
+
|
|
|
+ auto success = object->define_property(key, *descriptor);
|
|
|
+ if (vm.exception())
|
|
|
+ return {};
|
|
|
+ if (!success) {
|
|
|
+ vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ return js_undefined();
|
|
|
+}
|
|
|
+
|
|
|
// B.2.2.1.1 get Object.prototype.__proto__, https://tc39.es/ecma262/#sec-get-object.prototype.__proto__
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::proto_getter)
|
|
|
{
|