StringObject.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. ThrowCompletionOr<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 MUST_OR_THROW_OOM(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. ThrowCompletionOr<void> StringObject::initialize(Realm& realm)
  34. {
  35. auto& vm = this->vm();
  36. MUST_OR_THROW_OOM(Base::initialize(realm));
  37. define_direct_property(vm.names.length, Value(MUST_OR_THROW_OOM(m_string->utf16_string_view()).length_in_code_units()), 0);
  38. return {};
  39. }
  40. void StringObject::visit_edges(Cell::Visitor& visitor)
  41. {
  42. Base::visit_edges(visitor);
  43. visitor.visit(m_string);
  44. }
  45. // 10.4.3.5 StringGetOwnProperty ( S, P ), https://tc39.es/ecma262/#sec-stringgetownproperty
  46. static ThrowCompletionOr<Optional<PropertyDescriptor>> string_get_own_property(StringObject const& string, PropertyKey const& property_key)
  47. {
  48. VERIFY(property_key.is_valid());
  49. auto& vm = string.vm();
  50. // 1. If Type(P) is not String, return undefined.
  51. // NOTE: The spec only uses string and symbol keys, and later coerces to numbers -
  52. // this is not the case for PropertyKey, so '!property_key.is_string()' would be wrong.
  53. if (property_key.is_symbol())
  54. return Optional<PropertyDescriptor> {};
  55. // 2. Let index be CanonicalNumericIndexString(P).
  56. auto index = MUST_OR_THROW_OOM(canonical_numeric_index_string(vm, property_key, CanonicalIndexMode::IgnoreNumericRoundtrip));
  57. // 3. If index is undefined, return undefined.
  58. // 4. If IsIntegralNumber(index) is false, return undefined.
  59. // 5. If index is -0𝔽, return undefined.
  60. if (!index.is_index())
  61. return Optional<PropertyDescriptor> {};
  62. // 6. Let str be S.[[StringData]].
  63. // 7. Assert: Type(str) is String.
  64. auto str = TRY(string.primitive_string().utf16_string_view());
  65. // 8. Let len be the length of str.
  66. auto length = str.length_in_code_units();
  67. // 9. If ℝ(index) < 0 or len ≤ ℝ(index), return undefined.
  68. if (length <= index.as_index())
  69. return Optional<PropertyDescriptor> {};
  70. // 10. Let resultStr be the String value of length 1, containing one code unit from str, specifically the code unit at index ℝ(index).
  71. auto result_str = PrimitiveString::create(vm, TRY(Utf16String::create(vm, str.substring_view(index.as_index(), 1))));
  72. // 11. Return the PropertyDescriptor { [[Value]]: resultStr, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false }.
  73. return PropertyDescriptor {
  74. .value = result_str,
  75. .writable = false,
  76. .enumerable = true,
  77. .configurable = false,
  78. };
  79. }
  80. // 10.4.3.1 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-string-exotic-objects-getownproperty-p
  81. ThrowCompletionOr<Optional<PropertyDescriptor>> StringObject::internal_get_own_property(PropertyKey const& property_key) const
  82. {
  83. VERIFY(property_key.is_valid());
  84. // 1. Let desc be OrdinaryGetOwnProperty(S, P).
  85. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  86. // 2. If desc is not undefined, return desc.
  87. if (descriptor.has_value())
  88. return descriptor;
  89. // 3. Return StringGetOwnProperty(S, P).
  90. return string_get_own_property(*this, property_key);
  91. }
  92. // 10.4.3.2 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-string-exotic-objects-defineownproperty-p-desc
  93. ThrowCompletionOr<bool> StringObject::internal_define_own_property(PropertyKey const& property_key, PropertyDescriptor const& property_descriptor)
  94. {
  95. VERIFY(property_key.is_valid());
  96. // 1. Let stringDesc be StringGetOwnProperty(S, P).
  97. auto string_descriptor = TRY(string_get_own_property(*this, property_key));
  98. // 2. If stringDesc is not undefined, then
  99. if (string_descriptor.has_value()) {
  100. // a. Let extensible be S.[[Extensible]].
  101. auto extensible = m_is_extensible;
  102. // b. Return IsCompatiblePropertyDescriptor(extensible, Desc, stringDesc).
  103. return is_compatible_property_descriptor(extensible, property_descriptor, string_descriptor);
  104. }
  105. // 3. Return ! OrdinaryDefineOwnProperty(S, P, Desc).
  106. return Object::internal_define_own_property(property_key, property_descriptor);
  107. }
  108. // 10.4.3.3 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-string-exotic-objects-ownpropertykeys
  109. ThrowCompletionOr<MarkedVector<Value>> StringObject::internal_own_property_keys() const
  110. {
  111. auto& vm = this->vm();
  112. // 1. Let keys be a new empty List.
  113. auto keys = MarkedVector<Value> { heap() };
  114. // 2. Let str be O.[[StringData]].
  115. auto str = TRY(m_string->utf16_string_view());
  116. // 3. Assert: Type(str) is String.
  117. // 4. Let len be the length of str.
  118. auto length = str.length_in_code_units();
  119. // 5. For each integer i starting with 0 such that i < len, in ascending order, do
  120. for (size_t i = 0; i < length; ++i) {
  121. // a. Add ! ToString(𝔽(i)) as the last element of keys.
  122. keys.append(PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::number(i))));
  123. }
  124. // 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
  125. for (auto& entry : indexed_properties()) {
  126. if (entry.index() >= length) {
  127. // a. Add P as the last element of keys.
  128. keys.append(PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::number(entry.index()))));
  129. }
  130. }
  131. // 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
  132. for (auto& it : shape().property_table_ordered()) {
  133. if (it.key.is_string()) {
  134. // a. Add P as the last element of keys.
  135. keys.append(it.key.to_value(vm));
  136. }
  137. }
  138. // 8. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  139. for (auto& it : shape().property_table_ordered()) {
  140. if (it.key.is_symbol()) {
  141. // a. Add P as the last element of keys.
  142. keys.append(it.key.to_value(vm));
  143. }
  144. }
  145. // 9. Return keys.
  146. return { move(keys) };
  147. }
  148. }