PrimitiveString.cpp 8.8 KB

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