Array.cpp 9.7 KB

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