StringObject.cpp 6.7 KB

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