RegExpObject.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <LibJS/Heap/Heap.h>
  28. #include <LibJS/Runtime/GlobalObject.h>
  29. #include <LibJS/Runtime/PrimitiveString.h>
  30. #include <LibJS/Runtime/RegExpObject.h>
  31. #include <LibJS/Runtime/Value.h>
  32. namespace JS {
  33. static Flags options_from(const String& flags, VM& vm, GlobalObject& global_object)
  34. {
  35. bool g = false, i = false, m = false, s = false, u = false, y = false;
  36. Flags options {
  37. // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful".
  38. // FIXME: Enable 'BrowserExtended' only if in a browser context.
  39. .effective_flags = { (regex::ECMAScriptFlags)regex::AllFlags::Global | (regex::ECMAScriptFlags)regex::AllFlags::SkipTrimEmptyMatches | regex::ECMAScriptFlags::BrowserExtended },
  40. .declared_flags = {},
  41. };
  42. for (auto ch : flags) {
  43. switch (ch) {
  44. case 'g':
  45. if (g)
  46. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  47. g = true;
  48. options.effective_flags |= regex::ECMAScriptFlags::Global;
  49. options.declared_flags |= regex::ECMAScriptFlags::Global;
  50. break;
  51. case 'i':
  52. if (i)
  53. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  54. i = true;
  55. options.effective_flags |= regex::ECMAScriptFlags::Insensitive;
  56. options.declared_flags |= regex::ECMAScriptFlags::Insensitive;
  57. break;
  58. case 'm':
  59. if (m)
  60. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  61. m = true;
  62. options.effective_flags |= regex::ECMAScriptFlags::Multiline;
  63. options.declared_flags |= regex::ECMAScriptFlags::Multiline;
  64. break;
  65. case 's':
  66. if (s)
  67. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  68. s = true;
  69. options.effective_flags |= regex::ECMAScriptFlags::SingleLine;
  70. options.declared_flags |= regex::ECMAScriptFlags::SingleLine;
  71. break;
  72. case 'u':
  73. if (u)
  74. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  75. u = true;
  76. options.effective_flags |= regex::ECMAScriptFlags::Unicode;
  77. options.declared_flags |= regex::ECMAScriptFlags::Unicode;
  78. break;
  79. case 'y':
  80. if (y)
  81. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  82. y = true;
  83. // Now for the more interesting flag, 'sticky' actually unsets 'global', part of which is the default.
  84. options.effective_flags.reset_flag(regex::ECMAScriptFlags::Global);
  85. // "What's the difference between sticky and global, then", that's simple.
  86. // all the other flags imply 'global', and the "global" flag implies 'stateful';
  87. // however, the "sticky" flag does *not* imply 'global', only 'stateful'.
  88. options.effective_flags |= (regex::ECMAScriptFlags)regex::AllFlags::Internal_Stateful;
  89. options.effective_flags |= regex::ECMAScriptFlags::Sticky;
  90. options.declared_flags |= regex::ECMAScriptFlags::Sticky;
  91. break;
  92. default:
  93. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectBadFlag, ch);
  94. return options;
  95. }
  96. }
  97. return options;
  98. }
  99. RegExpObject* RegExpObject::create(GlobalObject& global_object, String pattern, String flags)
  100. {
  101. return global_object.heap().allocate<RegExpObject>(global_object, pattern, flags, *global_object.regexp_prototype());
  102. }
  103. RegExpObject::RegExpObject(String pattern, String flags, Object& prototype)
  104. : Object(prototype)
  105. , m_pattern(pattern)
  106. , m_flags(flags)
  107. , m_active_flags(options_from(m_flags, this->vm(), this->global_object()))
  108. , m_regex(pattern, m_active_flags.effective_flags)
  109. {
  110. if (m_regex.parser_result.error != regex::Error::NoError) {
  111. vm().throw_exception<SyntaxError>(global_object(), ErrorType::RegExpCompileError, m_regex.error_string());
  112. }
  113. }
  114. void RegExpObject::initialize(GlobalObject& global_object)
  115. {
  116. auto& vm = this->vm();
  117. Object::initialize(global_object);
  118. define_native_property(vm.names.lastIndex, last_index, set_last_index, Attribute::Writable);
  119. }
  120. RegExpObject::~RegExpObject()
  121. {
  122. }
  123. static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_object)
  124. {
  125. auto* this_object = vm.this_value(global_object).to_object(global_object);
  126. if (!this_object)
  127. return nullptr;
  128. if (!is<RegExpObject>(this_object)) {
  129. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "RegExp");
  130. return nullptr;
  131. }
  132. return static_cast<RegExpObject*>(this_object);
  133. }
  134. JS_DEFINE_NATIVE_GETTER(RegExpObject::last_index)
  135. {
  136. auto regexp_object = regexp_object_from(vm, global_object);
  137. if (!regexp_object)
  138. return {};
  139. return Value((unsigned)regexp_object->regex().start_offset);
  140. }
  141. JS_DEFINE_NATIVE_SETTER(RegExpObject::set_last_index)
  142. {
  143. auto regexp_object = regexp_object_from(vm, global_object);
  144. if (!regexp_object)
  145. return;
  146. auto index = value.to_i32(global_object);
  147. if (vm.exception())
  148. return;
  149. if (index < 0)
  150. index = 0;
  151. regexp_object->regex().start_offset = index;
  152. }
  153. RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value flags)
  154. {
  155. // https://tc39.es/ecma262/#sec-regexpcreate
  156. String p;
  157. if (pattern.is_undefined()) {
  158. p = String::empty();
  159. } else {
  160. p = pattern.to_string(global_object);
  161. if (p.is_null())
  162. return nullptr;
  163. }
  164. String f;
  165. if (flags.is_undefined()) {
  166. f = String::empty();
  167. } else {
  168. f = flags.to_string(global_object);
  169. if (f.is_null())
  170. return nullptr;
  171. }
  172. // FIXME: This is awkward: the RegExpObject C++ constructor may throw a VM exception.
  173. auto* obj = RegExpObject::create(global_object, move(p), move(f));
  174. if (global_object.vm().exception())
  175. return nullptr;
  176. return obj;
  177. }
  178. }