StringObject.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Utf16View.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/PrimitiveString.h>
  11. #include <LibJS/Runtime/PropertyDescriptor.h>
  12. #include <LibJS/Runtime/StringObject.h>
  13. namespace JS {
  14. // 10.4.3.4 StringCreate ( value, prototype ), https://tc39.es/ecma262/#sec-stringcreate
  15. NonnullGCPtr<StringObject> StringObject::create(Realm& realm, PrimitiveString& primitive_string, Object& prototype)
  16. {
  17. // 1. Let S be MakeBasicObject(« [[Prototype]], [[Extensible]], [[StringData]] »).
  18. // 2. Set S.[[Prototype]] to prototype.
  19. // 3. Set S.[[StringData]] to value.
  20. // 4. Set S.[[GetOwnProperty]] as specified in 10.4.3.1.
  21. // 5. Set S.[[DefineOwnProperty]] as specified in 10.4.3.2.
  22. // 6. Set S.[[OwnPropertyKeys]] as specified in 10.4.3.3.
  23. // 7. Let length be the length of value.
  24. // 8. Perform ! DefinePropertyOrThrow(S, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
  25. // 9. Return S.
  26. return realm.heap().allocate<StringObject>(realm, primitive_string, prototype);
  27. }
  28. StringObject::StringObject(PrimitiveString& string, Object& prototype)
  29. : Object(ConstructWithPrototypeTag::Tag, prototype)
  30. , m_string(string)
  31. {
  32. }
  33. void StringObject::initialize(Realm& realm)
  34. {
  35. auto& vm = this->vm();
  36. Base::initialize(realm);
  37. define_direct_property(vm.names.length, Value(m_string->utf16_string_view().length_in_code_units()), 0);
  38. }
  39. void StringObject::visit_edges(Cell::Visitor& visitor)
  40. {
  41. Base::visit_edges(visitor);
  42. visitor.visit(m_string);
  43. }
  44. // 10.4.3.5 StringGetOwnProperty ( S, P ), https://tc39.es/ecma262/#sec-stringgetownproperty
  45. static ThrowCompletionOr<Optional<PropertyDescriptor>> string_get_own_property(StringObject const& string, PropertyKey const& property_key)
  46. {
  47. VERIFY(property_key.is_valid());
  48. auto& vm = string.vm();
  49. // 1. If Type(P) is not String, return undefined.
  50. // NOTE: The spec only uses string and symbol keys, and later coerces to numbers -
  51. // this is not the case for PropertyKey, so '!property_key.is_string()' would be wrong.
  52. if (property_key.is_symbol())
  53. return Optional<PropertyDescriptor> {};
  54. // 2. Let index be CanonicalNumericIndexString(P).
  55. auto index = canonical_numeric_index_string(property_key, CanonicalIndexMode::IgnoreNumericRoundtrip);
  56. // 3. If index is undefined, return undefined.
  57. // 4. If IsIntegralNumber(index) is false, return undefined.
  58. // 5. If index is -0𝔽, return undefined.
  59. if (!index.is_index())
  60. return Optional<PropertyDescriptor> {};
  61. // 6. Let str be S.[[StringData]].
  62. // 7. Assert: Type(str) is String.
  63. auto str = string.primitive_string().utf16_string_view();
  64. // 8. Let len be the length of str.
  65. auto length = str.length_in_code_units();
  66. // 9. If ℝ(index) < 0 or len ≤ ℝ(index), return undefined.
  67. if (length <= index.as_index())
  68. return Optional<PropertyDescriptor> {};
  69. // 10. Let resultStr be the String value of length 1, containing one code unit from str, specifically the code unit at index ℝ(index).
  70. auto result_str = PrimitiveString::create(vm, Utf16String::create(str.substring_view(index.as_index(), 1)));
  71. // 11. Return the PropertyDescriptor { [[Value]]: resultStr, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false }.
  72. return PropertyDescriptor {
  73. .value = result_str,
  74. .writable = false,
  75. .enumerable = true,
  76. .configurable = false,
  77. };
  78. }
  79. // 10.4.3.1 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-string-exotic-objects-getownproperty-p
  80. ThrowCompletionOr<Optional<PropertyDescriptor>> StringObject::internal_get_own_property(PropertyKey const& property_key) const
  81. {
  82. VERIFY(property_key.is_valid());
  83. // 1. Let desc be OrdinaryGetOwnProperty(S, P).
  84. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  85. // 2. If desc is not undefined, return desc.
  86. if (descriptor.has_value())
  87. return descriptor;
  88. // 3. Return StringGetOwnProperty(S, P).
  89. return string_get_own_property(*this, property_key);
  90. }
  91. // 10.4.3.2 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-string-exotic-objects-defineownproperty-p-desc
  92. ThrowCompletionOr<bool> StringObject::internal_define_own_property(PropertyKey const& property_key, PropertyDescriptor const& property_descriptor)
  93. {
  94. VERIFY(property_key.is_valid());
  95. // 1. Let stringDesc be StringGetOwnProperty(S, P).
  96. auto string_descriptor = TRY(string_get_own_property(*this, property_key));
  97. // 2. If stringDesc is not undefined, then
  98. if (string_descriptor.has_value()) {
  99. // a. Let extensible be S.[[Extensible]].
  100. auto extensible = m_is_extensible;
  101. // b. Return IsCompatiblePropertyDescriptor(extensible, Desc, stringDesc).
  102. return is_compatible_property_descriptor(extensible, property_descriptor, string_descriptor);
  103. }
  104. // 3. Return ! OrdinaryDefineOwnProperty(S, P, Desc).
  105. return Object::internal_define_own_property(property_key, property_descriptor);
  106. }
  107. // 10.4.3.3 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-string-exotic-objects-ownpropertykeys
  108. ThrowCompletionOr<MarkedVector<Value>> StringObject::internal_own_property_keys() const
  109. {
  110. auto& vm = this->vm();
  111. // 1. Let keys be a new empty List.
  112. auto keys = MarkedVector<Value> { heap() };
  113. // 2. Let str be O.[[StringData]].
  114. auto str = m_string->utf16_string_view();
  115. // 3. Assert: Type(str) is String.
  116. // 4. Let len be the length of str.
  117. auto length = str.length_in_code_units();
  118. // 5. For each integer i starting with 0 such that i < len, in ascending order, do
  119. for (size_t i = 0; i < length; ++i) {
  120. // a. Add ! ToString(𝔽(i)) as the last element of keys.
  121. keys.append(PrimitiveString::create(vm, MUST(String::number(i))));
  122. }
  123. // 6. For each own property key P of O such that P is an array index and ! ToIntegerOrInfinity(P) ≥ len, in ascending numeric index order, do
  124. for (auto& entry : indexed_properties()) {
  125. if (entry.index() >= length) {
  126. // a. Add P as the last element of keys.
  127. keys.append(PrimitiveString::create(vm, MUST(String::number(entry.index()))));
  128. }
  129. }
  130. // 7. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do
  131. for (auto& it : shape().property_table()) {
  132. if (it.key.is_string()) {
  133. // a. Add P as the last element of keys.
  134. keys.append(it.key.to_value(vm));
  135. }
  136. }
  137. // 8. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  138. for (auto& it : shape().property_table()) {
  139. if (it.key.is_symbol()) {
  140. // a. Add P as the last element of keys.
  141. keys.append(it.key.to_value(vm));
  142. }
  143. }
  144. // 9. Return keys.
  145. return { move(keys) };
  146. }
  147. }