RegExpObject.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibJS/Heap/Heap.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. #include <LibJS/Runtime/PrimitiveString.h>
  31. #include <LibJS/Runtime/RegExpObject.h>
  32. #include <LibJS/Runtime/Value.h>
  33. namespace JS {
  34. static Flags options_from(const String& flags, VM& vm, GlobalObject& global_object)
  35. {
  36. bool g = false, i = false, m = false, s = false, u = false, y = false;
  37. Flags options {
  38. { (regex::ECMAScriptFlags)regex::AllFlags::Global }, // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful".
  39. {},
  40. };
  41. for (auto ch : flags) {
  42. switch (ch) {
  43. case 'g':
  44. if (g)
  45. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  46. g = true;
  47. options.effective_flags |= regex::ECMAScriptFlags::Global;
  48. options.declared_flags |= regex::ECMAScriptFlags::Global;
  49. break;
  50. case 'i':
  51. if (i)
  52. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  53. i = true;
  54. options.effective_flags |= regex::ECMAScriptFlags::Insensitive;
  55. options.declared_flags |= regex::ECMAScriptFlags::Insensitive;
  56. break;
  57. case 'm':
  58. if (m)
  59. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  60. m = true;
  61. options.effective_flags |= regex::ECMAScriptFlags::Multiline;
  62. options.declared_flags |= regex::ECMAScriptFlags::Multiline;
  63. break;
  64. case 's':
  65. if (s)
  66. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  67. s = true;
  68. options.effective_flags |= regex::ECMAScriptFlags::SingleLine;
  69. options.declared_flags |= regex::ECMAScriptFlags::SingleLine;
  70. break;
  71. case 'u':
  72. if (u)
  73. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  74. u = true;
  75. options.effective_flags |= regex::ECMAScriptFlags::Unicode;
  76. options.declared_flags |= regex::ECMAScriptFlags::Unicode;
  77. break;
  78. case 'y':
  79. if (y)
  80. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  81. y = true;
  82. // Now for the more interesting flag, 'sticky' actually unsets 'global', part of which is the default.
  83. options.effective_flags.reset_flag(regex::ECMAScriptFlags::Global);
  84. // "What's the difference between sticky and global, then", that's simple.
  85. // all the other flags imply 'global', and the "global" flag implies 'stateful';
  86. // however, the "sticky" flag does *not* imply 'global', only 'stateful'.
  87. options.effective_flags |= (regex::ECMAScriptFlags)regex::AllFlags::Internal_Stateful;
  88. options.effective_flags |= regex::ECMAScriptFlags::Sticky;
  89. options.declared_flags |= regex::ECMAScriptFlags::Sticky;
  90. break;
  91. default:
  92. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectBadFlag, ch);
  93. return options;
  94. }
  95. }
  96. return options;
  97. }
  98. RegExpObject* RegExpObject::create(GlobalObject& global_object, String pattern, String flags)
  99. {
  100. return global_object.heap().allocate<RegExpObject>(global_object, pattern, flags, *global_object.regexp_prototype());
  101. }
  102. RegExpObject::RegExpObject(String pattern, String flags, Object& prototype)
  103. : Object(prototype)
  104. , m_pattern(pattern)
  105. , m_flags(flags)
  106. , m_active_flags(options_from(m_flags, this->vm(), this->global_object()))
  107. , m_regex(pattern, m_active_flags.effective_flags)
  108. {
  109. if (m_regex.parser_result.error != regex::Error::NoError) {
  110. vm().throw_exception<SyntaxError>(global_object(), ErrorType::RegExpCompileError, m_regex.error_string());
  111. }
  112. }
  113. void RegExpObject::initialize(GlobalObject& global_object)
  114. {
  115. auto& vm = this->vm();
  116. Object::initialize(global_object);
  117. define_native_property(vm.names.lastIndex, last_index, set_last_index, Attribute::Writable);
  118. }
  119. RegExpObject::~RegExpObject()
  120. {
  121. }
  122. static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_object)
  123. {
  124. auto* this_object = vm.this_value(global_object).to_object(global_object);
  125. if (!this_object)
  126. return nullptr;
  127. if (!is<RegExpObject>(this_object)) {
  128. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "RegExp");
  129. return nullptr;
  130. }
  131. return static_cast<RegExpObject*>(this_object);
  132. }
  133. JS_DEFINE_NATIVE_GETTER(RegExpObject::last_index)
  134. {
  135. auto regexp_object = regexp_object_from(vm, global_object);
  136. if (!regexp_object)
  137. return {};
  138. return Value((unsigned)regexp_object->regex().start_offset);
  139. }
  140. JS_DEFINE_NATIVE_SETTER(RegExpObject::set_last_index)
  141. {
  142. auto regexp_object = regexp_object_from(vm, global_object);
  143. if (!regexp_object)
  144. return;
  145. auto index = value.to_i32(global_object);
  146. if (vm.exception())
  147. return;
  148. if (index < 0)
  149. index = 0;
  150. regexp_object->regex().start_offset = index;
  151. }
  152. }