StringObject.cpp 6.7 KB

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