RegExpPrototype.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/Runtime/Array.h>
  28. #include <LibJS/Runtime/Error.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. #include <LibJS/Runtime/RegExpObject.h>
  31. #include <LibJS/Runtime/RegExpPrototype.h>
  32. namespace JS {
  33. RegExpPrototype::RegExpPrototype(GlobalObject& global_object)
  34. : RegExpObject({}, {}, *global_object.object_prototype())
  35. {
  36. }
  37. void RegExpPrototype::initialize(GlobalObject& global_object)
  38. {
  39. auto& vm = this->vm();
  40. Object::initialize(global_object);
  41. u8 attr = Attribute::Writable | Attribute::Configurable;
  42. define_native_function(vm.names.toString, to_string, 0, attr);
  43. define_native_function(vm.names.test, test, 1, attr);
  44. define_native_function(vm.names.exec, exec, 1, attr);
  45. u8 readable_attr = Attribute::Configurable;
  46. define_native_property(vm.names.dotAll, dot_all, nullptr, readable_attr);
  47. define_native_property(vm.names.flags, flags, nullptr, readable_attr);
  48. define_native_property(vm.names.global, global, nullptr, readable_attr);
  49. define_native_property(vm.names.ignoreCase, ignore_case, nullptr, readable_attr);
  50. define_native_property(vm.names.multiline, multiline, nullptr, readable_attr);
  51. define_native_property(vm.names.source, source, nullptr, readable_attr);
  52. define_native_property(vm.names.sticky, sticky, nullptr, readable_attr);
  53. define_native_property(vm.names.unicode, unicode, nullptr, readable_attr);
  54. }
  55. RegExpPrototype::~RegExpPrototype()
  56. {
  57. }
  58. static Object* this_object_from(VM& vm, GlobalObject& global_object)
  59. {
  60. auto this_value = vm.this_value(global_object);
  61. if (!this_value.is_object()) {
  62. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, this_value.to_string_without_side_effects());
  63. return {};
  64. }
  65. return &this_value.as_object();
  66. }
  67. static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_object)
  68. {
  69. auto* this_object = vm.this_value(global_object).to_object(global_object);
  70. if (!this_object)
  71. return nullptr;
  72. if (!this_object->is_regexp_object()) {
  73. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "RegExp");
  74. return nullptr;
  75. }
  76. return static_cast<RegExpObject*>(this_object);
  77. }
  78. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::dot_all)
  79. {
  80. auto regexp_object = regexp_object_from(vm, global_object);
  81. if (!regexp_object)
  82. return {};
  83. return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::SingleLine));
  84. }
  85. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flags)
  86. {
  87. auto this_object = this_object_from(vm, global_object);
  88. if (!this_object)
  89. return {};
  90. StringBuilder builder(8);
  91. #define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \
  92. auto flag_##flag_name = this_object->get(vm.names.flagName).value_or(js_undefined()); \
  93. if (vm.exception()) \
  94. return {}; \
  95. if (flag_##flag_name.to_boolean()) \
  96. builder.append(#flag_char);
  97. JS_ENUMERATE_REGEXP_FLAGS
  98. #undef __JS_ENUMERATE
  99. return js_string(vm, builder.to_string());
  100. }
  101. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::global)
  102. {
  103. auto regexp_object = regexp_object_from(vm, global_object);
  104. if (!regexp_object)
  105. return {};
  106. return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::Global)); // Note that this "Global" is actually "Global | Stateful"
  107. }
  108. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::ignore_case)
  109. {
  110. auto regexp_object = regexp_object_from(vm, global_object);
  111. if (!regexp_object)
  112. return {};
  113. return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::Insensitive));
  114. }
  115. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::multiline)
  116. {
  117. auto regexp_object = regexp_object_from(vm, global_object);
  118. if (!regexp_object)
  119. return {};
  120. return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::Multiline));
  121. }
  122. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::source)
  123. {
  124. auto regexp_object = regexp_object_from(vm, global_object);
  125. if (!regexp_object)
  126. return {};
  127. return js_string(vm, regexp_object->pattern());
  128. }
  129. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::sticky)
  130. {
  131. auto regexp_object = regexp_object_from(vm, global_object);
  132. if (!regexp_object)
  133. return {};
  134. return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::Sticky));
  135. }
  136. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::unicode)
  137. {
  138. auto regexp_object = regexp_object_from(vm, global_object);
  139. if (!regexp_object)
  140. return {};
  141. return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::Unicode));
  142. }
  143. RegexResult RegExpPrototype::do_match(const Regex<ECMA262>& re, const StringView& subject)
  144. {
  145. auto result = re.match(subject);
  146. // The 'lastIndex' property is reset on failing tests (if 'global')
  147. if (!result.success && re.options().has_flag_set(ECMAScriptFlags::Global))
  148. re.start_offset = 0;
  149. return result;
  150. }
  151. JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::exec)
  152. {
  153. // FIXME: This should try using dynamic properties for 'lastIndex',
  154. // and internal slots [[RegExpMatcher]], [[OriginalFlags]], etc.
  155. auto regexp_object = regexp_object_from(vm, global_object);
  156. if (!regexp_object)
  157. return {};
  158. auto str = vm.argument(0).to_string(global_object);
  159. if (vm.exception())
  160. return {};
  161. StringView str_to_match = str;
  162. // RegExps without "global" and "sticky" always start at offset 0.
  163. if (!regexp_object->regex().options().has_flag_set((ECMAScriptFlags)regex::AllFlags::Internal_Stateful))
  164. regexp_object->regex().start_offset = 0;
  165. auto result = do_match(regexp_object->regex(), str_to_match);
  166. if (!result.success)
  167. return js_null();
  168. auto& match = result.matches[0];
  169. // FIXME: Do code point index correction if the Unicode flag is set.
  170. auto* array = Array::create(global_object);
  171. array->indexed_properties().set_array_like_size(result.n_capture_groups + 1);
  172. array->define_property(vm.names.index, Value((i32)match.column));
  173. array->define_property(vm.names.input, js_string(vm, str));
  174. array->indexed_properties().put(array, 0, js_string(vm, match.view.to_string()));
  175. for (size_t i = 0; i < result.n_capture_groups; ++i) {
  176. auto& capture = result.capture_group_matches[0][i];
  177. array->indexed_properties().put(array, i + 1, js_string(vm, capture.view.to_string()));
  178. }
  179. Value groups = js_undefined();
  180. if (result.n_named_capture_groups > 0) {
  181. auto groups_object = create_empty(global_object);
  182. for (auto& entry : result.named_capture_group_matches[0])
  183. groups_object->define_property(entry.key, js_string(vm, entry.value.view.to_string()));
  184. groups = move(groups_object);
  185. }
  186. array->define_property(vm.names.groups, groups);
  187. return array;
  188. }
  189. JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
  190. {
  191. // FIXME: This should try using dynamic properties for 'exec' first,
  192. // before falling back to builtin_exec.
  193. auto regexp_object = regexp_object_from(vm, global_object);
  194. if (!regexp_object)
  195. return {};
  196. auto str = vm.argument(0).to_string(global_object);
  197. if (vm.exception())
  198. return {};
  199. // RegExps without "global" and "sticky" always start at offset 0.
  200. if (!regexp_object->regex().options().has_flag_set((ECMAScriptFlags)regex::AllFlags::Internal_Stateful))
  201. regexp_object->regex().start_offset = 0;
  202. auto result = do_match(regexp_object->regex(), str);
  203. return Value(result.success);
  204. }
  205. JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string)
  206. {
  207. auto* regexp_object = regexp_object_from(vm, global_object);
  208. if (!regexp_object)
  209. return {};
  210. return js_string(vm, String::formatted("/{}/{}", regexp_object->pattern(), regexp_object->flags()));
  211. }
  212. }