StringObject.cpp 6.6 KB

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