RegExpObject.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 d = false, 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 'd':
  26. if (d)
  27. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  28. d = true;
  29. break;
  30. case 'g':
  31. if (g)
  32. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  33. g = true;
  34. options.effective_flags |= regex::ECMAScriptFlags::Global;
  35. options.declared_flags |= regex::ECMAScriptFlags::Global;
  36. break;
  37. case 'i':
  38. if (i)
  39. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  40. i = true;
  41. options.effective_flags |= regex::ECMAScriptFlags::Insensitive;
  42. options.declared_flags |= regex::ECMAScriptFlags::Insensitive;
  43. break;
  44. case 'm':
  45. if (m)
  46. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  47. m = true;
  48. options.effective_flags |= regex::ECMAScriptFlags::Multiline;
  49. options.declared_flags |= regex::ECMAScriptFlags::Multiline;
  50. break;
  51. case 's':
  52. if (s)
  53. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  54. s = true;
  55. options.effective_flags |= regex::ECMAScriptFlags::SingleLine;
  56. options.declared_flags |= regex::ECMAScriptFlags::SingleLine;
  57. break;
  58. case 'u':
  59. if (u)
  60. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  61. u = true;
  62. options.effective_flags |= regex::ECMAScriptFlags::Unicode;
  63. options.declared_flags |= regex::ECMAScriptFlags::Unicode;
  64. break;
  65. case 'y':
  66. if (y)
  67. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
  68. y = true;
  69. // Now for the more interesting flag, 'sticky' actually unsets 'global', part of which is the default.
  70. options.effective_flags.reset_flag(regex::ECMAScriptFlags::Global);
  71. // "What's the difference between sticky and global, then", that's simple.
  72. // all the other flags imply 'global', and the "global" flag implies 'stateful';
  73. // however, the "sticky" flag does *not* imply 'global', only 'stateful'.
  74. options.effective_flags |= (regex::ECMAScriptFlags)regex::AllFlags::Internal_Stateful;
  75. options.effective_flags |= regex::ECMAScriptFlags::Sticky;
  76. options.declared_flags |= regex::ECMAScriptFlags::Sticky;
  77. break;
  78. default:
  79. vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectBadFlag, ch);
  80. return options;
  81. }
  82. }
  83. return options;
  84. }
  85. RegExpObject* RegExpObject::create(GlobalObject& global_object, String pattern, String flags)
  86. {
  87. return global_object.heap().allocate<RegExpObject>(global_object, pattern, flags, *global_object.regexp_prototype());
  88. }
  89. RegExpObject::RegExpObject(String pattern, String flags, Object& prototype)
  90. : Object(prototype)
  91. , m_pattern(pattern)
  92. , m_flags(flags)
  93. , m_active_flags(options_from(global_object(), m_flags))
  94. , m_regex(pattern, m_active_flags.effective_flags)
  95. {
  96. if (m_regex.parser_result.error != regex::Error::NoError) {
  97. vm().throw_exception<SyntaxError>(global_object(), ErrorType::RegExpCompileError, m_regex.error_string());
  98. }
  99. }
  100. RegExpObject::~RegExpObject()
  101. {
  102. }
  103. void RegExpObject::initialize(GlobalObject& global_object)
  104. {
  105. auto& vm = this->vm();
  106. Object::initialize(global_object);
  107. define_direct_property(vm.names.lastIndex, {}, Attribute::Writable);
  108. }
  109. // 22.2.3.2.4 RegExpCreate ( P, F ), https://tc39.es/ecma262/#sec-regexpcreate
  110. RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value flags)
  111. {
  112. auto& vm = global_object.vm();
  113. String p;
  114. if (pattern.is_undefined()) {
  115. p = String::empty();
  116. } else {
  117. p = pattern.to_string(global_object);
  118. if (vm.exception())
  119. return {};
  120. }
  121. String f;
  122. if (flags.is_undefined()) {
  123. f = String::empty();
  124. } else {
  125. f = flags.to_string(global_object);
  126. if (vm.exception())
  127. return {};
  128. }
  129. auto* object = RegExpObject::create(global_object, move(p), move(f));
  130. object->set(vm.names.lastIndex, Value(0), true);
  131. if (vm.exception())
  132. return {};
  133. return object;
  134. }
  135. }