StringObject.cpp 6.4 KB

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