StringObject.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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. // 1. Assert: S is an Object that has a [[StringData]] internal slot.
  39. // 2. Assert: IsPropertyKey(P) is true.
  40. VERIFY(property_key.is_valid());
  41. // 3. 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 {};
  46. // 4. Let index be ! CanonicalNumericIndexString(P).
  47. auto index = canonical_numeric_index_string(property_key, CanonicalIndexMode::IgnoreNumericRoundtrip);
  48. // 5. If index is undefined, return undefined.
  49. // 6. If IsIntegralNumber(index) is false, return undefined.
  50. // 7. If index is -0𝔽, return undefined.
  51. if (!index.is_index())
  52. return {};
  53. // 8. Let str be S.[[StringData]].
  54. // 9. Assert: Type(str) is String.
  55. auto str = string.primitive_string().utf16_string_view();
  56. // 10. Let len be the length of str.
  57. auto length = str.length_in_code_units();
  58. // 11. If ℝ(index) < 0 or len ≤ ℝ(index), return undefined.
  59. if (length <= index.as_index())
  60. return {};
  61. // 12. 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 = js_string(string.vm(), str.substring_view(index.as_index(), 1));
  63. // 13. 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. // Assert: IsPropertyKey(P) is true.
  75. // 2. Let desc be OrdinaryGetOwnProperty(S, P).
  76. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  77. // 3. If desc is not undefined, return desc.
  78. if (descriptor.has_value())
  79. return descriptor;
  80. // 4. 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. // 1. Assert: IsPropertyKey(P) is true.
  87. VERIFY(property_key.is_valid());
  88. // 2. Let stringDesc be ! StringGetOwnProperty(S, P).
  89. auto string_descriptor = string_get_own_property(*this, property_key);
  90. // 3. If stringDesc is not undefined, then
  91. if (string_descriptor.has_value()) {
  92. // a. Let extensible be S.[[Extensible]].
  93. auto extensible = m_is_extensible;
  94. // b. Return ! IsCompatiblePropertyDescriptor(extensible, Desc, stringDesc).
  95. return is_compatible_property_descriptor(extensible, property_descriptor, string_descriptor);
  96. }
  97. // 4. Return ! OrdinaryDefineOwnProperty(S, P, Desc).
  98. return Object::internal_define_own_property(property_key, property_descriptor);
  99. }
  100. // 10.4.3.3 [[OwnPropertyKeys]] ( ), https://tc39.es/ecma262/#sec-string-exotic-objects-ownpropertykeys
  101. ThrowCompletionOr<MarkedVector<Value>> StringObject::internal_own_property_keys() const
  102. {
  103. auto& vm = this->vm();
  104. // 1. Let keys be a new empty List.
  105. auto keys = MarkedVector<Value> { heap() };
  106. // 2. Let str be O.[[StringData]].
  107. auto str = m_string.utf16_string_view();
  108. // 3. Assert: Type(str) is String.
  109. // 4. Let len be the length of str.
  110. auto length = str.length_in_code_units();
  111. // 5. For each integer i starting with 0 such that i < len, in ascending order, do
  112. for (size_t i = 0; i < length; ++i) {
  113. // a. Add ! ToString(𝔽(i)) as the last element of keys.
  114. keys.append(js_string(vm, String::number(i)));
  115. }
  116. // 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
  117. for (auto& entry : indexed_properties()) {
  118. if (entry.index() >= length) {
  119. // a. Add P as the last element of keys.
  120. keys.append(js_string(vm, String::number(entry.index())));
  121. }
  122. }
  123. // 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
  124. for (auto& it : shape().property_table_ordered()) {
  125. if (it.key.is_string()) {
  126. // a. Add P as the last element of keys.
  127. keys.append(it.key.to_value(vm));
  128. }
  129. }
  130. // 8. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
  131. for (auto& it : shape().property_table_ordered()) {
  132. if (it.key.is_symbol()) {
  133. // a. Add P as the last element of keys.
  134. keys.append(it.key.to_value(vm));
  135. }
  136. }
  137. // 9. Return keys.
  138. return { move(keys) };
  139. }
  140. }