PropertyDescriptor.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Error.h>
  7. #include <LibJS/Runtime/FunctionObject.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibJS/Runtime/PropertyDescriptor.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS {
  13. // 6.2.5.1 IsAccessorDescriptor ( Desc ), https://tc39.es/ecma262/#sec-isaccessordescriptor
  14. bool PropertyDescriptor::is_accessor_descriptor() const
  15. {
  16. // 1. If Desc is undefined, return false.
  17. // 2. If both Desc.[[Get]] and Desc.[[Set]] are absent, return false.
  18. if (!get.has_value() && !set.has_value())
  19. return false;
  20. // 3. Return true.
  21. return true;
  22. }
  23. // 6.2.5.2 IsDataDescriptor ( Desc ), https://tc39.es/ecma262/#sec-isdatadescriptor
  24. bool PropertyDescriptor::is_data_descriptor() const
  25. {
  26. // 1. If Desc is undefined, return false.
  27. // 2. If both Desc.[[Value]] and Desc.[[Writable]] are absent, return false.
  28. if (!value.has_value() && !writable.has_value())
  29. return false;
  30. // 3. Return true.
  31. return true;
  32. }
  33. // 6.2.5.3 IsGenericDescriptor ( Desc ), https://tc39.es/ecma262/#sec-isgenericdescriptor
  34. bool PropertyDescriptor::is_generic_descriptor() const
  35. {
  36. // 1. If Desc is undefined, return false.
  37. // 2. If IsAccessorDescriptor(Desc) and IsDataDescriptor(Desc) are both false, return true.
  38. if (!is_accessor_descriptor() && !is_data_descriptor())
  39. return true;
  40. // 3. Return false.
  41. return false;
  42. }
  43. // 6.2.5.4 FromPropertyDescriptor ( Desc ), https://tc39.es/ecma262/#sec-frompropertydescriptor
  44. Value from_property_descriptor(GlobalObject& global_object, Optional<PropertyDescriptor> const& property_descriptor)
  45. {
  46. if (!property_descriptor.has_value())
  47. return js_undefined();
  48. auto& vm = global_object.vm();
  49. auto* object = Object::create(global_object, global_object.object_prototype());
  50. if (property_descriptor->value.has_value())
  51. MUST(object->create_data_property_or_throw(vm.names.value, *property_descriptor->value));
  52. if (property_descriptor->writable.has_value())
  53. MUST(object->create_data_property_or_throw(vm.names.writable, Value(*property_descriptor->writable)));
  54. if (property_descriptor->get.has_value())
  55. MUST(object->create_data_property_or_throw(vm.names.get, *property_descriptor->get ? Value(*property_descriptor->get) : js_undefined()));
  56. if (property_descriptor->set.has_value())
  57. MUST(object->create_data_property_or_throw(vm.names.set, *property_descriptor->set ? Value(*property_descriptor->set) : js_undefined()));
  58. if (property_descriptor->enumerable.has_value())
  59. MUST(object->create_data_property_or_throw(vm.names.enumerable, Value(*property_descriptor->enumerable)));
  60. if (property_descriptor->configurable.has_value())
  61. MUST(object->create_data_property_or_throw(vm.names.configurable, Value(*property_descriptor->configurable)));
  62. return object;
  63. }
  64. // 6.2.5.5 ToPropertyDescriptor ( Obj ), https://tc39.es/ecma262/#sec-topropertydescriptor
  65. ThrowCompletionOr<PropertyDescriptor> to_property_descriptor(GlobalObject& global_object, Value argument)
  66. {
  67. auto& vm = global_object.vm();
  68. // 1. If Type(Obj) is not Object, throw a TypeError exception.
  69. if (!argument.is_object())
  70. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, argument.to_string_without_side_effects());
  71. auto& object = argument.as_object();
  72. // 2. Let desc be a new Property Descriptor that initially has no fields.
  73. PropertyDescriptor descriptor;
  74. // 3. Let hasEnumerable be ? HasProperty(Obj, "enumerable").
  75. auto has_enumerable = TRY(object.has_property(vm.names.enumerable));
  76. // 4. If hasEnumerable is true, then
  77. if (has_enumerable) {
  78. // a. Let enumerable be ! ToBoolean(? Get(Obj, "enumerable")).
  79. auto enumerable = TRY(object.get(vm.names.enumerable)).to_boolean();
  80. // b. Set desc.[[Enumerable]] to enumerable.
  81. descriptor.enumerable = enumerable;
  82. }
  83. // 5. Let hasConfigurable be ? HasProperty(Obj, "configurable").
  84. auto has_configurable = TRY(object.has_property(vm.names.configurable));
  85. // 6. If hasConfigurable is true, then
  86. if (has_configurable) {
  87. // a. Let configurable be ! ToBoolean(? Get(Obj, "configurable")).
  88. auto configurable = TRY(object.get(vm.names.configurable)).to_boolean();
  89. // b. Set desc.[[Configurable]] to configurable.
  90. descriptor.configurable = configurable;
  91. }
  92. // 7. Let hasValue be ? HasProperty(Obj, "value").
  93. auto has_value = TRY(object.has_property(vm.names.value));
  94. // 8. If hasValue is true, then
  95. if (has_value) {
  96. // a. Let value be ? Get(Obj, "value").
  97. auto value = TRY(object.get(vm.names.value));
  98. // b. Set desc.[[Value]] to value.
  99. descriptor.value = value;
  100. }
  101. // 9. Let hasWritable be ? HasProperty(Obj, "writable").
  102. auto has_writable = TRY(object.has_property(vm.names.writable));
  103. // 10. If hasWritable is true, then
  104. if (has_writable) {
  105. // a. Let writable be ! ToBoolean(? Get(Obj, "writable")).
  106. auto writable = TRY(object.get(vm.names.writable)).to_boolean();
  107. // b. Set desc.[[Writable]] to writable.
  108. descriptor.writable = writable;
  109. }
  110. // 11. Let hasGet be ? HasProperty(Obj, "get").
  111. auto has_get = TRY(object.has_property(vm.names.get));
  112. // 12. If hasGet is true, then
  113. if (has_get) {
  114. // a. Let getter be ? Get(Obj, "get").
  115. auto getter = TRY(object.get(vm.names.get));
  116. // b. If IsCallable(getter) is false and getter is not undefined, throw a TypeError exception.
  117. if (!getter.is_function() && !getter.is_undefined())
  118. return vm.throw_completion<TypeError>(global_object, ErrorType::AccessorBadField, "get");
  119. // c. Set desc.[[Get]] to getter.
  120. descriptor.get = getter.is_function() ? &getter.as_function() : nullptr;
  121. }
  122. // 13. Let hasSet be ? HasProperty(Obj, "set").
  123. auto has_set = TRY(object.has_property(vm.names.set));
  124. // 14. If hasSet is true, then
  125. if (has_set) {
  126. // a. Let setter be ? Get(Obj, "set").
  127. auto setter = TRY(object.get(vm.names.set));
  128. // b. If IsCallable(setter) is false and setter is not undefined, throw a TypeError exception.
  129. if (!setter.is_function() && !setter.is_undefined())
  130. return vm.throw_completion<TypeError>(global_object, ErrorType::AccessorBadField, "set");
  131. // c. Set desc.[[Set]] to setter.
  132. descriptor.set = setter.is_function() ? &setter.as_function() : nullptr;
  133. }
  134. // 15. If desc.[[Get]] is present or desc.[[Set]] is present, then
  135. if (descriptor.get.has_value() || descriptor.set.has_value()) {
  136. // a. If desc.[[Value]] is present or desc.[[Writable]] is present, throw a TypeError exception.
  137. if (descriptor.value.has_value() || descriptor.writable.has_value())
  138. return vm.throw_completion<TypeError>(global_object, ErrorType::AccessorValueOrWritable);
  139. }
  140. // 16. Return desc.
  141. return descriptor;
  142. }
  143. // 6.2.5.6 CompletePropertyDescriptor ( Desc ), https://tc39.es/ecma262/#sec-completepropertydescriptor
  144. void PropertyDescriptor::complete()
  145. {
  146. if (is_generic_descriptor() || is_data_descriptor()) {
  147. if (!value.has_value())
  148. value = Value {};
  149. if (!writable.has_value())
  150. writable = false;
  151. } else {
  152. if (!get.has_value())
  153. get = nullptr;
  154. if (!set.has_value())
  155. set = nullptr;
  156. }
  157. if (!enumerable.has_value())
  158. enumerable = false;
  159. if (!configurable.has_value())
  160. configurable = false;
  161. }
  162. // Non-standard, just a convenient way to get from three Optional<bool> to PropertyAttributes.
  163. PropertyAttributes PropertyDescriptor::attributes() const
  164. {
  165. u8 attributes = 0;
  166. if (writable.value_or(false))
  167. attributes |= Attribute::Writable;
  168. if (enumerable.value_or(false))
  169. attributes |= Attribute::Enumerable;
  170. if (configurable.value_or(false))
  171. attributes |= Attribute::Configurable;
  172. return { attributes };
  173. }
  174. }