RegExpObject.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. RegExpObject::~RegExpObject()
  96. {
  97. }
  98. void RegExpObject::initialize(GlobalObject& global_object)
  99. {
  100. auto& vm = this->vm();
  101. Object::initialize(global_object);
  102. define_direct_property(vm.names.lastIndex, {}, Attribute::Writable);
  103. }
  104. // 22.2.3.2.4 RegExpCreate ( P, F ), https://tc39.es/ecma262/#sec-regexpcreate
  105. RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value flags)
  106. {
  107. auto& vm = global_object.vm();
  108. String p;
  109. if (pattern.is_undefined()) {
  110. p = String::empty();
  111. } else {
  112. p = pattern.to_string(global_object);
  113. if (vm.exception())
  114. return {};
  115. }
  116. String f;
  117. if (flags.is_undefined()) {
  118. f = String::empty();
  119. } else {
  120. f = flags.to_string(global_object);
  121. if (vm.exception())
  122. return {};
  123. }
  124. auto* object = RegExpObject::create(global_object, move(p), move(f));
  125. object->set(vm.names.lastIndex, Value(0), true);
  126. if (vm.exception())
  127. return {};
  128. return object;
  129. }
  130. }