PropertyDescriptor.cpp 8.2 KB

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