PrimitiveString.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/Utf16View.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibJS/Runtime/AbstractOperations.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/PrimitiveString.h>
  13. #include <LibJS/Runtime/PropertyKey.h>
  14. #include <LibJS/Runtime/VM.h>
  15. #include <LibJS/Runtime/Value.h>
  16. namespace JS {
  17. PrimitiveString::PrimitiveString(PrimitiveString& lhs, PrimitiveString& rhs)
  18. : m_is_rope(true)
  19. , m_lhs(&lhs)
  20. , m_rhs(&rhs)
  21. {
  22. }
  23. PrimitiveString::PrimitiveString(DeprecatedString string)
  24. : m_has_utf8_string(true)
  25. , m_utf8_string(move(string))
  26. {
  27. }
  28. PrimitiveString::PrimitiveString(Utf16String string)
  29. : m_has_utf16_string(true)
  30. , m_utf16_string(move(string))
  31. {
  32. }
  33. PrimitiveString::~PrimitiveString()
  34. {
  35. vm().string_cache().remove(m_utf8_string);
  36. }
  37. void PrimitiveString::visit_edges(Cell::Visitor& visitor)
  38. {
  39. Cell::visit_edges(visitor);
  40. if (m_is_rope) {
  41. visitor.visit(m_lhs);
  42. visitor.visit(m_rhs);
  43. }
  44. }
  45. bool PrimitiveString::is_empty() const
  46. {
  47. if (m_is_rope) {
  48. // NOTE: We never make an empty rope string.
  49. return false;
  50. }
  51. if (m_has_utf16_string)
  52. return m_utf16_string.is_empty();
  53. if (m_has_utf8_string)
  54. return m_utf8_string.is_empty();
  55. VERIFY_NOT_REACHED();
  56. }
  57. DeprecatedString const& PrimitiveString::deprecated_string() const
  58. {
  59. resolve_rope_if_needed();
  60. if (!m_has_utf8_string) {
  61. m_utf8_string = m_utf16_string.to_utf8();
  62. m_has_utf8_string = true;
  63. }
  64. return m_utf8_string;
  65. }
  66. Utf16String const& PrimitiveString::utf16_string() const
  67. {
  68. resolve_rope_if_needed();
  69. if (!m_has_utf16_string) {
  70. m_utf16_string = Utf16String(m_utf8_string);
  71. m_has_utf16_string = true;
  72. }
  73. return m_utf16_string;
  74. }
  75. Utf16View PrimitiveString::utf16_string_view() const
  76. {
  77. return utf16_string().view();
  78. }
  79. Optional<Value> PrimitiveString::get(VM& vm, PropertyKey const& property_key) const
  80. {
  81. if (property_key.is_symbol())
  82. return {};
  83. if (property_key.is_string()) {
  84. if (property_key.as_string() == vm.names.length.as_string()) {
  85. auto length = utf16_string().length_in_code_units();
  86. return Value(static_cast<double>(length));
  87. }
  88. }
  89. auto index = canonical_numeric_index_string(property_key, CanonicalIndexMode::IgnoreNumericRoundtrip);
  90. if (!index.is_index())
  91. return {};
  92. auto str = utf16_string_view();
  93. auto length = str.length_in_code_units();
  94. if (length <= index.as_index())
  95. return {};
  96. return create(vm, str.substring_view(index.as_index(), 1));
  97. }
  98. NonnullGCPtr<PrimitiveString> PrimitiveString::create(VM& vm, Utf16View const& view)
  99. {
  100. return create(vm, Utf16String(view));
  101. }
  102. NonnullGCPtr<PrimitiveString> PrimitiveString::create(VM& vm, Utf16String string)
  103. {
  104. if (string.is_empty())
  105. return vm.empty_string();
  106. if (string.length_in_code_units() == 1) {
  107. u16 code_unit = string.code_unit_at(0);
  108. if (is_ascii(code_unit))
  109. return vm.single_ascii_character_string(static_cast<u8>(code_unit));
  110. }
  111. return *vm.heap().allocate_without_realm<PrimitiveString>(move(string));
  112. }
  113. NonnullGCPtr<PrimitiveString> PrimitiveString::create(VM& vm, DeprecatedString string)
  114. {
  115. if (string.is_empty())
  116. return vm.empty_string();
  117. if (string.length() == 1) {
  118. auto ch = static_cast<u8>(string.characters()[0]);
  119. if (is_ascii(ch))
  120. return vm.single_ascii_character_string(ch);
  121. }
  122. auto& string_cache = vm.string_cache();
  123. auto it = string_cache.find(string);
  124. if (it == string_cache.end()) {
  125. auto* new_string = vm.heap().allocate_without_realm<PrimitiveString>(string);
  126. string_cache.set(move(string), new_string);
  127. return *new_string;
  128. }
  129. return *it->value;
  130. }
  131. NonnullGCPtr<PrimitiveString> PrimitiveString::create(VM& vm, PrimitiveString& lhs, PrimitiveString& rhs)
  132. {
  133. // We're here to concatenate two strings into a new rope string.
  134. // However, if any of them are empty, no rope is required.
  135. bool lhs_empty = lhs.is_empty();
  136. bool rhs_empty = rhs.is_empty();
  137. if (lhs_empty && rhs_empty)
  138. return vm.empty_string();
  139. if (lhs_empty)
  140. return rhs;
  141. if (rhs_empty)
  142. return lhs;
  143. return *vm.heap().allocate_without_realm<PrimitiveString>(lhs, rhs);
  144. }
  145. void PrimitiveString::resolve_rope_if_needed() const
  146. {
  147. if (!m_is_rope)
  148. return;
  149. // NOTE: Special case for two concatenated UTF-16 strings.
  150. // This is here as an optimization, although I'm unsure how valuable it is.
  151. if (m_lhs->has_utf16_string() && m_rhs->has_utf16_string()) {
  152. auto const& lhs_string = m_lhs->utf16_string();
  153. auto const& rhs_string = m_rhs->utf16_string();
  154. Vector<u16, 1> combined;
  155. combined.ensure_capacity(lhs_string.length_in_code_units() + rhs_string.length_in_code_units());
  156. combined.extend(lhs_string.string());
  157. combined.extend(rhs_string.string());
  158. m_utf16_string = Utf16String(move(combined));
  159. m_has_utf16_string = true;
  160. m_is_rope = false;
  161. m_lhs = nullptr;
  162. m_rhs = nullptr;
  163. return;
  164. }
  165. // This vector will hold all the pieces of the rope that need to be assembled
  166. // into the resolved string.
  167. Vector<PrimitiveString const*> pieces;
  168. // NOTE: We traverse the rope tree without using recursion, since we'd run out of
  169. // stack space quickly when handling a long sequence of unresolved concatenations.
  170. Vector<PrimitiveString const*> stack;
  171. stack.append(m_rhs);
  172. stack.append(m_lhs);
  173. while (!stack.is_empty()) {
  174. auto* current = stack.take_last();
  175. if (current->m_is_rope) {
  176. stack.append(current->m_rhs);
  177. stack.append(current->m_lhs);
  178. continue;
  179. }
  180. pieces.append(current);
  181. }
  182. // Now that we have all the pieces, we can concatenate them using a StringBuilder.
  183. StringBuilder builder;
  184. // We keep track of the previous piece in order to handle surrogate pairs spread across two pieces.
  185. PrimitiveString const* previous = nullptr;
  186. for (auto const* current : pieces) {
  187. if (!previous) {
  188. // This is the very first piece, just append it and continue.
  189. builder.append(current->deprecated_string());
  190. previous = current;
  191. continue;
  192. }
  193. // Get the UTF-8 representations for both strings.
  194. auto const& previous_string_as_utf8 = previous->deprecated_string();
  195. auto const& current_string_as_utf8 = current->deprecated_string();
  196. // NOTE: Now we need to look at the end of the previous string and the start
  197. // of the current string, to see if they should be combined into a surrogate.
  198. // Surrogates encoded as UTF-8 are 3 bytes.
  199. if ((previous_string_as_utf8.length() < 3) || (current_string_as_utf8.length() < 3)) {
  200. builder.append(current->deprecated_string());
  201. previous = current;
  202. continue;
  203. }
  204. // Might the previous string end with a UTF-8 encoded surrogate?
  205. if ((static_cast<u8>(previous_string_as_utf8[previous_string_as_utf8.length() - 3]) & 0xf0) != 0xe0) {
  206. // If not, just append the current string and continue.
  207. builder.append(current->deprecated_string());
  208. previous = current;
  209. continue;
  210. }
  211. // Might the current string begin with a UTF-8 encoded surrogate?
  212. if ((static_cast<u8>(current_string_as_utf8[0]) & 0xf0) != 0xe0) {
  213. // If not, just append the current string and continue.
  214. builder.append(current->deprecated_string());
  215. previous = current;
  216. continue;
  217. }
  218. auto high_surrogate = *Utf8View(previous_string_as_utf8.substring_view(previous_string_as_utf8.length() - 3)).begin();
  219. auto low_surrogate = *Utf8View(current_string_as_utf8).begin();
  220. if (!Utf16View::is_high_surrogate(high_surrogate) || !Utf16View::is_low_surrogate(low_surrogate)) {
  221. builder.append(current->deprecated_string());
  222. previous = current;
  223. continue;
  224. }
  225. // Remove 3 bytes from the builder and replace them with the UTF-8 encoded code point.
  226. builder.trim(3);
  227. builder.append_code_point(Utf16View::decode_surrogate_pair(high_surrogate, low_surrogate));
  228. // Append the remaining part of the current string.
  229. builder.append(current_string_as_utf8.substring_view(3));
  230. previous = current;
  231. }
  232. m_utf8_string = builder.to_deprecated_string();
  233. m_has_utf8_string = true;
  234. m_is_rope = false;
  235. m_lhs = nullptr;
  236. m_rhs = nullptr;
  237. }
  238. }