RegExpConstructor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Error.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/RegExpConstructor.h>
  9. #include <LibJS/Runtime/RegExpObject.h>
  10. #include <LibJS/Runtime/Value.h>
  11. namespace JS {
  12. RegExpConstructor::RegExpConstructor(Realm& realm)
  13. : NativeFunction(realm.vm().names.RegExp.as_string(), *realm.intrinsics().function_prototype())
  14. {
  15. }
  16. void RegExpConstructor::initialize(Realm& realm)
  17. {
  18. auto& vm = this->vm();
  19. NativeFunction::initialize(realm);
  20. // 22.2.4.1 RegExp.prototype, https://tc39.es/ecma262/#sec-regexp.prototype
  21. define_direct_property(vm.names.prototype, realm.intrinsics().regexp_prototype(), 0);
  22. define_native_accessor(realm, *vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  23. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  24. // Additional properties of the RegExp constructor, https://github.com/tc39/proposal-regexp-legacy-features#additional-properties-of-the-regexp-constructor
  25. define_native_accessor(realm, vm.names.input, input_getter, input_setter, Attribute::Configurable);
  26. define_native_accessor(realm, vm.names.inputAlias, input_alias_getter, input_alias_setter, Attribute::Configurable);
  27. define_native_accessor(realm, vm.names.lastMatch, last_match_getter, {}, Attribute::Configurable);
  28. define_native_accessor(realm, vm.names.lastMatchAlias, last_match_alias_getter, {}, Attribute::Configurable);
  29. define_native_accessor(realm, vm.names.lastParen, last_paren_getter, {}, Attribute::Configurable);
  30. define_native_accessor(realm, vm.names.lastParenAlias, last_paren_alias_getter, {}, Attribute::Configurable);
  31. define_native_accessor(realm, vm.names.leftContext, left_context_getter, {}, Attribute::Configurable);
  32. define_native_accessor(realm, vm.names.leftContextAlias, left_context_alias_getter, {}, Attribute::Configurable);
  33. define_native_accessor(realm, vm.names.rightContext, right_context_getter, {}, Attribute::Configurable);
  34. define_native_accessor(realm, vm.names.rightContextAlias, right_context_alias_getter, {}, Attribute::Configurable);
  35. define_native_accessor(realm, vm.names.$1, group_1_getter, {}, Attribute::Configurable);
  36. define_native_accessor(realm, vm.names.$2, group_2_getter, {}, Attribute::Configurable);
  37. define_native_accessor(realm, vm.names.$3, group_3_getter, {}, Attribute::Configurable);
  38. define_native_accessor(realm, vm.names.$4, group_4_getter, {}, Attribute::Configurable);
  39. define_native_accessor(realm, vm.names.$5, group_5_getter, {}, Attribute::Configurable);
  40. define_native_accessor(realm, vm.names.$6, group_6_getter, {}, Attribute::Configurable);
  41. define_native_accessor(realm, vm.names.$7, group_7_getter, {}, Attribute::Configurable);
  42. define_native_accessor(realm, vm.names.$8, group_8_getter, {}, Attribute::Configurable);
  43. define_native_accessor(realm, vm.names.$9, group_9_getter, {}, Attribute::Configurable);
  44. }
  45. // 22.2.3.1 RegExp ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp-pattern-flags
  46. ThrowCompletionOr<Value> RegExpConstructor::call()
  47. {
  48. auto& vm = this->vm();
  49. auto pattern = vm.argument(0);
  50. auto flags = vm.argument(1);
  51. // 1. Let patternIsRegExp be ? IsRegExp(pattern).
  52. bool pattern_is_regexp = TRY(pattern.is_regexp(vm));
  53. // 2. If NewTarget is undefined, then
  54. // a. Let newTarget be the active function object.
  55. auto& new_target = *this;
  56. // b. If patternIsRegExp is true and flags is undefined, then
  57. if (pattern_is_regexp && flags.is_undefined()) {
  58. // i. Let patternConstructor be ? Get(pattern, "constructor").
  59. auto pattern_constructor = TRY(pattern.as_object().get(vm.names.constructor));
  60. // ii. If SameValue(newTarget, patternConstructor) is true, return pattern.
  61. if (same_value(&new_target, pattern_constructor))
  62. return pattern;
  63. }
  64. return TRY(construct(new_target));
  65. }
  66. // 22.2.3.1 RegExp ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp-pattern-flags
  67. ThrowCompletionOr<NonnullGCPtr<Object>> RegExpConstructor::construct(FunctionObject& new_target)
  68. {
  69. auto& vm = this->vm();
  70. auto pattern = vm.argument(0);
  71. auto flags = vm.argument(1);
  72. // 1. Let patternIsRegExp be ? IsRegExp(pattern).
  73. bool pattern_is_regexp = TRY(pattern.is_regexp(vm));
  74. // NOTE: Step 2 is handled in call() above.
  75. // 3. Else, let newTarget be NewTarget.
  76. Value pattern_value;
  77. Value flags_value;
  78. // 4. If pattern is an Object and pattern has a [[RegExpMatcher]] internal slot, then
  79. if (pattern.is_object() && is<RegExpObject>(pattern.as_object())) {
  80. // a. Let P be pattern.[[OriginalSource]].
  81. auto& regexp_pattern = static_cast<RegExpObject&>(pattern.as_object());
  82. pattern_value = PrimitiveString::create(vm, regexp_pattern.pattern());
  83. // b. If flags is undefined, let F be pattern.[[OriginalFlags]].
  84. if (flags.is_undefined())
  85. flags_value = PrimitiveString::create(vm, regexp_pattern.flags());
  86. // c. Else, let F be flags.
  87. else
  88. flags_value = flags;
  89. }
  90. // 5. Else if patternIsRegExp is true, then
  91. else if (pattern_is_regexp) {
  92. // a. Let P be ? Get(pattern, "source").
  93. pattern_value = TRY(pattern.as_object().get(vm.names.source));
  94. // b. If flags is undefined, then
  95. if (flags.is_undefined()) {
  96. // i. Let F be ? Get(pattern, "flags").
  97. flags_value = TRY(pattern.as_object().get(vm.names.flags));
  98. }
  99. // c. Else, let F be flags.
  100. else {
  101. flags_value = flags;
  102. }
  103. }
  104. // 6. Else,
  105. else {
  106. // a. Let P be pattern.
  107. pattern_value = pattern;
  108. // b. Let F be flags.
  109. flags_value = flags;
  110. }
  111. // 7. Let O be ? RegExpAlloc(newTarget).
  112. auto regexp_object = TRY(regexp_alloc(vm, new_target));
  113. // 8. Return ? RegExpInitialize(O, P, F).
  114. return TRY(regexp_object->regexp_initialize(vm, pattern_value, flags_value));
  115. }
  116. // 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species
  117. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::symbol_species_getter)
  118. {
  119. // 1. Return the this value.
  120. return vm.this_value();
  121. }
  122. // get RegExp.input, https://github.com/tc39/proposal-regexp-legacy-features#get-regexpinput
  123. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::input_getter)
  124. {
  125. auto* regexp_constructor = vm.current_realm()->intrinsics().regexp_constructor();
  126. // 1. Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpInput]]).
  127. auto property_getter = &RegExpLegacyStaticProperties::input;
  128. return TRY(get_legacy_regexp_static_property(vm, *regexp_constructor, vm.this_value(), property_getter));
  129. }
  130. // get RegExp.$_, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp_
  131. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::input_alias_getter)
  132. {
  133. // Keep the same implementation with `get RegExp.input`
  134. return input_getter(vm);
  135. }
  136. // set RegExp.input, https://github.com/tc39/proposal-regexp-legacy-features#set-regexpinput--val
  137. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::input_setter)
  138. {
  139. auto* regexp_constructor = vm.current_realm()->intrinsics().regexp_constructor();
  140. // 1. Perform ? SetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpInput]], val).
  141. auto property_setter = &RegExpLegacyStaticProperties::set_input;
  142. TRY(set_legacy_regexp_static_property(vm, *regexp_constructor, vm.this_value(), property_setter, vm.argument(0)));
  143. return js_undefined();
  144. }
  145. // set RegExp.$_, https://github.com/tc39/proposal-regexp-legacy-features#set-regexp_---val
  146. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::input_alias_setter)
  147. {
  148. // Keep the same implementation with `set RegExp.input`
  149. return input_setter(vm);
  150. }
  151. // get RegExp.lastMatch, https://github.com/tc39/proposal-regexp-legacy-features#get-regexplastmatch
  152. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::last_match_getter)
  153. {
  154. auto* regexp_constructor = vm.current_realm()->intrinsics().regexp_constructor();
  155. // 1. Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLastMatch]]).
  156. auto property_getter = &RegExpLegacyStaticProperties::last_match;
  157. return TRY(get_legacy_regexp_static_property(vm, *regexp_constructor, vm.this_value(), property_getter));
  158. }
  159. // get RegExp.$&, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp
  160. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::last_match_alias_getter)
  161. {
  162. // Keep the same implementation with `get RegExp.lastMatch`
  163. return last_match_getter(vm);
  164. }
  165. // get RegExp.lastParen, https://github.com/tc39/proposal-regexp-legacy-features#get-regexplastparen
  166. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::last_paren_getter)
  167. {
  168. auto* regexp_constructor = vm.current_realm()->intrinsics().regexp_constructor();
  169. // 1. Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLastParen]]).
  170. auto property_getter = &RegExpLegacyStaticProperties::last_paren;
  171. return TRY(get_legacy_regexp_static_property(vm, *regexp_constructor, vm.this_value(), property_getter));
  172. }
  173. // get RegExp.$+, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp-1
  174. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::last_paren_alias_getter)
  175. {
  176. // Keep the same implementation with `get RegExp.lastParen`
  177. return last_paren_getter(vm);
  178. }
  179. // get RegExp.leftContext, https://github.com/tc39/proposal-regexp-legacy-features#get-regexpleftcontext
  180. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::left_context_getter)
  181. {
  182. auto* regexp_constructor = vm.current_realm()->intrinsics().regexp_constructor();
  183. // 1. Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLeftContext]]).
  184. auto property_getter = &RegExpLegacyStaticProperties::left_context;
  185. return TRY(get_legacy_regexp_static_property(vm, *regexp_constructor, vm.this_value(), property_getter));
  186. }
  187. // get RegExp.$`, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp-2
  188. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::left_context_alias_getter)
  189. {
  190. // Keep the same implementation with `get RegExp.leftContext`
  191. return left_context_getter(vm);
  192. }
  193. // get RegExp.rightContext, https://github.com/tc39/proposal-regexp-legacy-features#get-regexprightcontext
  194. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::right_context_getter)
  195. {
  196. auto* regexp_constructor = vm.current_realm()->intrinsics().regexp_constructor();
  197. // 1. Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpRightContext]]).
  198. auto property_getter = &RegExpLegacyStaticProperties::right_context;
  199. return TRY(get_legacy_regexp_static_property(vm, *regexp_constructor, vm.this_value(), property_getter));
  200. }
  201. // get RegExp.$', https://github.com/tc39/proposal-regexp-legacy-features#get-regexp-3
  202. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::right_context_alias_getter)
  203. {
  204. // Keep the same implementation with `get RegExp.rightContext`
  205. return right_context_getter(vm);
  206. }
  207. #define DEFINE_REGEXP_GROUP_GETTER(n) \
  208. JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::group_##n##_getter) \
  209. { \
  210. auto* regexp_constructor = vm.current_realm()->intrinsics().regexp_constructor(); \
  211. \
  212. /* 1. Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen##n##]]).*/ \
  213. auto property_getter = &RegExpLegacyStaticProperties::$##n; \
  214. return TRY(get_legacy_regexp_static_property(vm, *regexp_constructor, vm.this_value(), property_getter)); \
  215. }
  216. // get RegExp.$1, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp1
  217. DEFINE_REGEXP_GROUP_GETTER(1);
  218. // get RegExp.$2, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp2
  219. DEFINE_REGEXP_GROUP_GETTER(2);
  220. // get RegExp.$3, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp3
  221. DEFINE_REGEXP_GROUP_GETTER(3);
  222. // get RegExp.$4, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp4
  223. DEFINE_REGEXP_GROUP_GETTER(4);
  224. // get RegExp.$5, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp5
  225. DEFINE_REGEXP_GROUP_GETTER(5);
  226. // get RegExp.$6, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp6
  227. DEFINE_REGEXP_GROUP_GETTER(6);
  228. // get RegExp.$7, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp7
  229. DEFINE_REGEXP_GROUP_GETTER(7);
  230. // get RegExp.$8, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp8
  231. DEFINE_REGEXP_GROUP_GETTER(8);
  232. // get RegExp.$9, https://github.com/tc39/proposal-regexp-legacy-features#get-regexp9
  233. DEFINE_REGEXP_GROUP_GETTER(9);
  234. #undef DEFINE_REGEXP_GROUP_GETTER
  235. }