PropertyDescriptor.cpp 8.1 KB

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