StringObject.cpp 6.6 KB

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