RegExpObject.cpp 6.1 KB

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