StringObject.cpp 6.6 KB

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