RegExpLegacyStaticProperties.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2022, LI YUBEI <leeight@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf16View.h>
  7. #include <LibJS/Runtime/RegExpConstructor.h>
  8. #include <LibJS/Runtime/RegExpLegacyStaticProperties.h>
  9. #include <LibJS/Runtime/VM.h>
  10. namespace JS {
  11. void RegExpLegacyStaticProperties::invalidate()
  12. {
  13. m_input = {};
  14. m_last_match = {};
  15. m_last_paren = {};
  16. m_left_context = {};
  17. m_right_context = {};
  18. m_$1 = {};
  19. m_$2 = {};
  20. m_$3 = {};
  21. m_$4 = {};
  22. m_$5 = {};
  23. m_$6 = {};
  24. m_$7 = {};
  25. m_$8 = {};
  26. m_$9 = {};
  27. }
  28. // GetLegacyRegExpStaticProperty( C, thisValue, internalSlotName ), https://github.com/tc39/proposal-regexp-legacy-features#getlegacyregexpstaticproperty-c-thisvalue-internalslotname-
  29. ThrowCompletionOr<Value> get_legacy_regexp_static_property(VM& vm, RegExpConstructor& constructor, Value this_value, Optional<Utf16String> const& (RegExpLegacyStaticProperties::*property_getter)() const)
  30. {
  31. // 1. Assert C is an object that has an internal slot named internalSlotName.
  32. // 2. If SameValue(C, thisValue) is false, throw a TypeError exception.
  33. if (!same_value(&constructor, this_value))
  34. return vm.throw_completion<TypeError>(ErrorType::GetLegacyRegExpStaticPropertyThisValueMismatch);
  35. // 3. Let val be the value of the internal slot of C named internalSlotName.
  36. auto val = (constructor.legacy_static_properties().*property_getter)();
  37. // 4. If val is empty, throw a TypeError exception.
  38. if (!val.has_value())
  39. return vm.throw_completion<TypeError>(ErrorType::GetLegacyRegExpStaticPropertyValueEmpty);
  40. // 5. Return val.
  41. return PrimitiveString::create(vm, val.release_value());
  42. }
  43. // SetLegacyRegExpStaticProperty( C, thisValue, internalSlotName, val ), https://github.com/tc39/proposal-regexp-legacy-features#setlegacyregexpstaticproperty-c-thisvalue-internalslotname-val-
  44. ThrowCompletionOr<void> set_legacy_regexp_static_property(VM& vm, RegExpConstructor& constructor, Value this_value, void (RegExpLegacyStaticProperties::*property_setter)(Utf16String), Value value)
  45. {
  46. // 1. Assert C is an object that has an internal slot named internalSlotName.
  47. // 2. If SameValue(C, thisValue) is false, throw a TypeError exception.
  48. if (!same_value(&constructor, this_value))
  49. return vm.throw_completion<TypeError>(ErrorType::SetLegacyRegExpStaticPropertyThisValueMismatch);
  50. // 3. Let strVal be ? ToString(val).
  51. auto str_value = TRY(value.to_utf16_string(vm));
  52. // 4. Set the value of the internal slot of C named internalSlotName to strVal.
  53. (constructor.legacy_static_properties().*property_setter)(str_value);
  54. return {};
  55. }
  56. // UpdateLegacyRegExpStaticProperties ( C, S, startIndex, endIndex, capturedValues ), https://github.com/tc39/proposal-regexp-legacy-features#updatelegacyregexpstaticproperties--c-s-startindex-endindex-capturedvalues-
  57. void update_legacy_regexp_static_properties(RegExpConstructor& constructor, Utf16String const& string, size_t start_index, size_t end_index, Vector<Utf16String> const& captured_values)
  58. {
  59. auto& legacy_static_properties = constructor.legacy_static_properties();
  60. // 1. Assert: C is an Object that has a [[RegExpInput]] internal slot.
  61. // 2. Assert: Type(S) is String.
  62. // 3. Let len be the number of code units in S.
  63. auto len = string.length_in_code_units();
  64. // 4. Assert: startIndex and endIndex are integers such that 0 ≤ startIndex ≤ endIndex ≤ len.
  65. VERIFY(start_index <= end_index);
  66. VERIFY(end_index <= len);
  67. // 5. Assert: capturedValues is a List of Strings.
  68. // 6. Let n be the number of elements in capturedValues.
  69. auto group_count = captured_values.size();
  70. // 7. Set the value of C’s [[RegExpInput]] internal slot to S.
  71. legacy_static_properties.set_input(string);
  72. // 8. Set the value of C’s [[RegExpLastMatch]] internal slot to a String whose length is endIndex - startIndex and containing the code units from S with indices startIndex through endIndex - 1, in ascending order.
  73. auto last_match = string.view().substring_view(start_index, end_index - start_index);
  74. legacy_static_properties.set_last_match(Utf16String::create(last_match));
  75. // 9. If n > 0, set the value of C’s [[RegExpLastParen]] internal slot to the last element of capturedValues.
  76. if (group_count > 0) {
  77. auto item = captured_values[group_count - 1];
  78. legacy_static_properties.set_last_paren(item);
  79. }
  80. // 10. Else, set the value of C’s [[RegExpLastParen]] internal slot to the empty String.
  81. else {
  82. legacy_static_properties.set_last_paren(Utf16String::create());
  83. }
  84. // 11. Set the value of C’s [[RegExpLeftContext]] internal slot to a String whose length is startIndex and containing the code units from S with indices 0 through startIndex - 1, in ascending order.
  85. auto left_context = string.view().substring_view(0, start_index);
  86. legacy_static_properties.set_left_context(Utf16String::create(left_context));
  87. // 12. Set the value of C’s [[RegExpRightContext]] internal slot to a String whose length is len - endIndex and containing the code units from S with indices endIndex through len - 1, in ascending order.
  88. auto right_context = string.view().substring_view(end_index, len - end_index);
  89. legacy_static_properties.set_right_context(Utf16String::create(right_context));
  90. // 13. For each integer i such that 1 ≤ i ≤ 9
  91. for (size_t i = 1; i <= 9; i++) {
  92. // i. If i ≤ n, set the value of C’s [[RegExpPareni]] internal slot to the ith element of capturedValues.
  93. // ii. Else, set the value of C’s [[RegExpPareni]] internal slot to the empty String.
  94. auto value = (i <= group_count) ? captured_values[i - 1] : Utf16String::create();
  95. if (i == 1) {
  96. legacy_static_properties.set_$1(move(value));
  97. } else if (i == 2) {
  98. legacy_static_properties.set_$2(move(value));
  99. } else if (i == 3) {
  100. legacy_static_properties.set_$3(move(value));
  101. } else if (i == 4) {
  102. legacy_static_properties.set_$4(move(value));
  103. } else if (i == 5) {
  104. legacy_static_properties.set_$5(move(value));
  105. } else if (i == 6) {
  106. legacy_static_properties.set_$6(move(value));
  107. } else if (i == 7) {
  108. legacy_static_properties.set_$7(move(value));
  109. } else if (i == 8) {
  110. legacy_static_properties.set_$8(move(value));
  111. } else if (i == 9) {
  112. legacy_static_properties.set_$9(move(value));
  113. }
  114. }
  115. }
  116. // InvalidateLegacyRegExpStaticProperties ( C ), https://github.com/tc39/proposal-regexp-legacy-features#invalidatelegacyregexpstaticproperties--c
  117. void invalidate_legacy_regexp_static_properties(RegExpConstructor& constructor)
  118. {
  119. // 1. Assert: C is an Object that has a [[RegExpInput]] internal slot.
  120. // 2. Set the value of the following internal slots of C to empty:
  121. constructor.legacy_static_properties().invalidate();
  122. }
  123. }