StringObject.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <andreas@ladybird.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. GC_DEFINE_ALLOCATOR(StringObject);
  15. // 10.4.3.4 StringCreate ( value, prototype ), https://tc39.es/ecma262/#sec-stringcreate
  16. GC::Ref<StringObject> StringObject::create(Realm& realm, PrimitiveString& primitive_string, Object& prototype)
  17. {
  18. // 1. Let S be MakeBasicObject(« [[Prototype]], [[Extensible]], [[StringData]] »).
  19. // 2. Set S.[[Prototype]] to prototype.
  20. // 3. Set S.[[StringData]] to value.
  21. // 4. Set S.[[GetOwnProperty]] as specified in 10.4.3.1.
  22. // 5. Set S.[[DefineOwnProperty]] as specified in 10.4.3.2.
  23. // 6. Set S.[[OwnPropertyKeys]] as specified in 10.4.3.3.
  24. // 7. Let length be the length of value.
  25. // 8. Perform ! DefinePropertyOrThrow(S, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
  26. // 9. Return S.
  27. return realm.create<StringObject>(primitive_string, prototype);
  28. }
  29. StringObject::StringObject(PrimitiveString& string, Object& prototype)
  30. : Object(ConstructWithPrototypeTag::Tag, prototype, MayInterfereWithIndexedPropertyAccess::Yes)
  31. , m_string(string)
  32. {
  33. }
  34. void StringObject::initialize(Realm& realm)
  35. {
  36. auto& vm = this->vm();
  37. Base::initialize(realm);
  38. define_direct_property(vm.names.length, Value(m_string->utf16_string_view().length_in_code_units()), 0);
  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. auto& vm = string.vm();
  49. // 1. If P is not a 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 index is not an integral Number, 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 substring of str from ℝ(index) to ℝ(index) + 1.
  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. // 1. Let desc be OrdinaryGetOwnProperty(S, P).
  83. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  84. // 2. If desc is not undefined, return desc.
  85. if (descriptor.has_value())
  86. return descriptor;
  87. // 3. Return StringGetOwnProperty(S, P).
  88. return string_get_own_property(*this, property_key);
  89. }
  90. // 10.4.3.2 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-string-exotic-objects-defineownproperty-p-desc
  91. ThrowCompletionOr<bool> StringObject::internal_define_own_property(PropertyKey const& property_key, PropertyDescriptor const& property_descriptor, Optional<PropertyDescriptor>* precomputed_get_own_property)
  92. {
  93. // 1. Let stringDesc be StringGetOwnProperty(S, P).
  94. auto string_descriptor = TRY(string_get_own_property(*this, property_key));
  95. // 2. If stringDesc is not undefined, then
  96. if (string_descriptor.has_value()) {
  97. // a. Let extensible be S.[[Extensible]].
  98. auto extensible = m_is_extensible;
  99. // b. Return IsCompatiblePropertyDescriptor(extensible, Desc, stringDesc).
  100. return is_compatible_property_descriptor(extensible, property_descriptor, string_descriptor);
  101. }
  102. // 3. Return ! OrdinaryDefineOwnProperty(S, P, Desc).
  103. return Object::internal_define_own_property(property_key, property_descriptor, precomputed_get_own_property);
  104. }
  105. // 10.4.3.3 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-string-exotic-objects-ownpropertykeys
  106. ThrowCompletionOr<GC::RootVector<Value>> StringObject::internal_own_property_keys() const
  107. {
  108. auto& vm = this->vm();
  109. // 1. Let keys be a new empty List.
  110. auto keys = GC::RootVector<Value> { heap() };
  111. // 2. Let str be O.[[StringData]].
  112. auto str = m_string->utf16_string_view();
  113. // 3. Assert: str is a String.
  114. // 4. Let len be the length of str.
  115. auto length = str.length_in_code_units();
  116. // 5. For each integer i starting with 0 such that i < len, in ascending order, do
  117. for (size_t i = 0; i < length; ++i) {
  118. // a. Add ! ToString(𝔽(i)) as the last element of keys.
  119. keys.append(PrimitiveString::create(vm, String::number(i)));
  120. }
  121. // 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
  122. for (auto& entry : indexed_properties()) {
  123. if (entry.index() >= length) {
  124. // a. Add P as the last element of keys.
  125. keys.append(PrimitiveString::create(vm, String::number(entry.index())));
  126. }
  127. }
  128. // 7. For each own property key P of O such that P is a String and P is not an array index, in ascending chronological order of property creation, do
  129. for (auto& it : shape().property_table()) {
  130. if (it.key.is_string()) {
  131. // a. Add P as the last element of keys.
  132. keys.append(it.key.to_value(vm));
  133. }
  134. }
  135. // 8. For each own property key P of O such that P is a Symbol, in ascending chronological order of property creation, do
  136. for (auto& it : shape().property_table()) {
  137. if (it.key.is_symbol()) {
  138. // a. Add P as the last element of keys.
  139. keys.append(it.key.to_value(vm));
  140. }
  141. }
  142. // 9. Return keys.
  143. return { move(keys) };
  144. }
  145. }