Array.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Function.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. namespace JS {
  13. // 10.4.2.2 ArrayCreate ( length [ , proto ] ), https://tc39.es/ecma262/#sec-arraycreate
  14. Array* Array::create(GlobalObject& global_object, size_t length, Object* prototype)
  15. {
  16. auto& vm = global_object.vm();
  17. if (length > NumericLimits<u32>::max()) {
  18. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
  19. return nullptr;
  20. }
  21. if (!prototype)
  22. prototype = global_object.array_prototype();
  23. auto* array = global_object.heap().allocate<Array>(global_object, *prototype);
  24. (void)array->internal_define_own_property(vm.names.length, { .value = Value(length), .writable = true, .enumerable = false, .configurable = false });
  25. return array;
  26. }
  27. // 7.3.17 CreateArrayFromList ( elements ), https://tc39.es/ecma262/#sec-createarrayfromlist
  28. Array* Array::create_from(GlobalObject& global_object, Vector<Value> const& elements)
  29. {
  30. // 1. Assert: elements is a List whose elements are all ECMAScript language values.
  31. // 2. Let array be ! ArrayCreate(0).
  32. auto* array = Array::create(global_object, 0);
  33. // 3. Let n be 0.
  34. // 4. For each element e of elements, do
  35. for (u32 n = 0; n < elements.size(); ++n) {
  36. // a. Perform ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(n)), e).
  37. array->create_data_property_or_throw(n, elements[n]);
  38. // b. Set n to n + 1.
  39. }
  40. // 5. Return array.
  41. return array;
  42. }
  43. Array::Array(Object& prototype)
  44. : Object(prototype)
  45. {
  46. }
  47. Array::~Array()
  48. {
  49. }
  50. // 10.4.2.4 ArraySetLength ( A, Desc ), https://tc39.es/ecma262/#sec-arraysetlength
  51. bool Array::set_length(PropertyDescriptor const& property_descriptor)
  52. {
  53. auto& global_object = this->global_object();
  54. auto& vm = this->vm();
  55. // 1. If Desc.[[Value]] is absent, then
  56. // a. Return OrdinaryDefineOwnProperty(A, "length", Desc).
  57. // 2. Let newLenDesc be a copy of Desc.
  58. // NOTE: Handled by step 16
  59. size_t new_length = indexed_properties().array_like_size();
  60. if (property_descriptor.value.has_value()) {
  61. // 3. Let newLen be ? ToUint32(Desc.[[Value]]).
  62. new_length = property_descriptor.value->to_u32(global_object);
  63. if (vm.exception())
  64. return {};
  65. // 4. Let numberLen be ? ToNumber(Desc.[[Value]]).
  66. auto number_length = property_descriptor.value->to_number(global_object);
  67. if (vm.exception())
  68. return {};
  69. // 5. If newLen is not the same value as numberLen, throw a RangeError exception.
  70. if (new_length != number_length.as_double()) {
  71. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
  72. return {};
  73. }
  74. }
  75. // 6. Set newLenDesc.[[Value]] to newLen.
  76. // 7. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
  77. // 8. Assert: ! IsDataDescriptor(oldLenDesc) is true.
  78. // 9. Assert: oldLenDesc.[[Configurable]] is false.
  79. // 10. Let oldLen be oldLenDesc.[[Value]].
  80. // 11. If newLen ≥ oldLen, then
  81. // a. Return OrdinaryDefineOwnProperty(A, "length", newLenDesc).
  82. // 12. If oldLenDesc.[[Writable]] is false, return false.
  83. // NOTE: Handled by step 16
  84. // 13. If newLenDesc.[[Writable]] is absent or has the value true, let newWritable be true.
  85. // 14. Else,
  86. // a. NOTE: Setting the [[Writable]] attribute to false is deferred in case any elements cannot be deleted.
  87. // b. Let newWritable be false.
  88. auto new_writable = property_descriptor.writable.value_or(true);
  89. // c. Set newLenDesc.[[Writable]] to true.
  90. // 15. Let succeeded be ! OrdinaryDefineOwnProperty(A, "length", newLenDesc).
  91. // 16. If succeeded is false, return false.
  92. // NOTE: Because the length property does not actually exist calling OrdinaryDefineOwnProperty
  93. // will result in unintended behavior, so instead we only implement here the small subset of
  94. // checks performed inside of it that would have mattered to us:
  95. // 10.1.6.3 ValidateAndApplyPropertyDescriptor ( O, P, extensible, Desc, current ), https://tc39.es/ecma262/#sec-validateandapplypropertydescriptor
  96. // 4. If current.[[Configurable]] is false, then
  97. // a. If Desc.[[Configurable]] is present and its value is true, return false.
  98. if (property_descriptor.configurable.has_value() && *property_descriptor.configurable)
  99. return false;
  100. // b. If Desc.[[Enumerable]] is present and ! SameValue(Desc.[[Enumerable]], current.[[Enumerable]]) is false, return false.
  101. if (property_descriptor.enumerable.has_value() && *property_descriptor.enumerable)
  102. return false;
  103. // 6. Else if ! SameValue(! IsDataDescriptor(current), ! IsDataDescriptor(Desc)) is false, then
  104. if (property_descriptor.is_accessor_descriptor()) {
  105. // a. If current.[[Configurable]] is false, return false.
  106. return false;
  107. }
  108. // 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both true, then
  109. // a. If current.[[Configurable]] is false and current.[[Writable]] is false, then
  110. if (!m_length_writable) {
  111. // i. If Desc.[[Writable]] is present and Desc.[[Writable]] is true, return false.
  112. if (property_descriptor.writable.has_value() && *property_descriptor.writable)
  113. return false;
  114. // ii. If Desc.[[Value]] is present and SameValue(Desc.[[Value]], current.[[Value]]) is false, return false.
  115. if (new_length != indexed_properties().array_like_size())
  116. return false;
  117. }
  118. // 17. For each own property key P of A that is an array index, whose numeric value is greater than or equal to newLen, in descending numeric index order, do
  119. // a. Let deleteSucceeded be ! A.[[Delete]](P).
  120. // b. If deleteSucceeded is false, then
  121. // i. Set newLenDesc.[[Value]] to ! ToUint32(P) + 1𝔽.
  122. bool success = indexed_properties().set_array_like_size(new_length);
  123. // ii. If newWritable is false, set newLenDesc.[[Writable]] to false.
  124. // iii. Perform ! OrdinaryDefineOwnProperty(A, "length", newLenDesc).
  125. // NOTE: Handled by step 18
  126. // 18. If newWritable is false, then
  127. // a. Set succeeded to ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Writable]]: false }).
  128. // b. Assert: succeeded is true.
  129. if (!new_writable)
  130. m_length_writable = false;
  131. // NOTE: Continuation of step #17
  132. // iv. Return false.
  133. if (!success)
  134. return false;
  135. // 19. Return true.
  136. return true;
  137. }
  138. // NON-STANDARD: Used to return the value of the ephemeral length property
  139. ThrowCompletionOr<Optional<PropertyDescriptor>> Array::internal_get_own_property(PropertyName const& property_name) const
  140. {
  141. auto& vm = this->vm();
  142. if (property_name.is_string() && property_name.as_string() == vm.names.length.as_string())
  143. return { PropertyDescriptor { .value = Value(indexed_properties().array_like_size()), .writable = m_length_writable, .enumerable = false, .configurable = false } };
  144. return Object::internal_get_own_property(property_name);
  145. }
  146. // 10.4.2.1 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-array-exotic-objects-defineownproperty-p-desc
  147. ThrowCompletionOr<bool> Array::internal_define_own_property(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
  148. {
  149. auto& vm = this->vm();
  150. // 1. Assert: IsPropertyKey(P) is true.
  151. VERIFY(property_name.is_valid());
  152. // 2. If P is "length", then
  153. if (property_name.is_string() && property_name.as_string() == vm.names.length.as_string()) {
  154. // a. Return ? ArraySetLength(A, Desc).
  155. auto success = set_length(property_descriptor);
  156. if (auto* exception = vm.exception())
  157. return throw_completion(exception->value());
  158. return success;
  159. }
  160. // 3. Else if P is an array index, then
  161. if (property_name.is_number()) {
  162. // a. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
  163. // b. Assert: ! IsDataDescriptor(oldLenDesc) is true.
  164. // c. Assert: oldLenDesc.[[Configurable]] is false.
  165. // d. Let oldLen be oldLenDesc.[[Value]].
  166. // e. Assert: oldLen is a non-negative integral Number.
  167. // f. Let index be ! ToUint32(P).
  168. // g. If index ≥ oldLen and oldLenDesc.[[Writable]] is false, return false.
  169. if (property_name.as_number() >= indexed_properties().array_like_size() && !m_length_writable)
  170. return false;
  171. // h. Let succeeded be ! OrdinaryDefineOwnProperty(A, P, Desc).
  172. auto succeeded = Object::internal_define_own_property(property_name, property_descriptor).release_value();
  173. // i. If succeeded is false, return false.
  174. if (!succeeded)
  175. return false;
  176. // j. If index ≥ oldLen, then
  177. // i. Set oldLenDesc.[[Value]] to index + 1𝔽.
  178. // ii. Set succeeded to OrdinaryDefineOwnProperty(A, "length", oldLenDesc).
  179. // iii. Assert: succeeded is true.
  180. // k. Return true.
  181. return true;
  182. }
  183. // 4. Return OrdinaryDefineOwnProperty(A, P, Desc).
  184. return Object::internal_define_own_property(property_name, property_descriptor);
  185. }
  186. // NON-STANDARD: Used to reject deletes to ephemeral (non-configurable) length property
  187. ThrowCompletionOr<bool> Array::internal_delete(PropertyName const& property_name)
  188. {
  189. auto& vm = this->vm();
  190. if (property_name.is_string() && property_name.as_string() == vm.names.length.as_string())
  191. return false;
  192. return Object::internal_delete(property_name);
  193. }
  194. // NON-STANDARD: Used to inject the ephemeral length property's key
  195. MarkedValueList Array::internal_own_property_keys() const
  196. {
  197. auto& vm = this->vm();
  198. auto keys = Object::internal_own_property_keys();
  199. if (vm.exception())
  200. return MarkedValueList { vm.heap() };
  201. // FIXME: This is pretty expensive, find a better way to do this
  202. keys.insert(indexed_properties().real_size(), js_string(vm, vm.names.length.as_string()));
  203. return keys;
  204. }
  205. }