RegExpPrototype.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Function.h>
  28. #include <LibJS/Runtime/Array.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/RegExpObject.h>
  32. #include <LibJS/Runtime/RegExpPrototype.h>
  33. #include <LibJS/Token.h>
  34. namespace JS {
  35. RegExpPrototype::RegExpPrototype(GlobalObject& global_object)
  36. : RegExpObject({}, {}, *global_object.object_prototype())
  37. {
  38. }
  39. void RegExpPrototype::initialize(GlobalObject& global_object)
  40. {
  41. auto& vm = this->vm();
  42. Object::initialize(global_object);
  43. u8 attr = Attribute::Writable | Attribute::Configurable;
  44. define_native_function(vm.names.toString, to_string, 0, attr);
  45. define_native_function(vm.names.test, test, 1, attr);
  46. define_native_function(vm.names.exec, exec, 1, attr);
  47. u8 readable_attr = Attribute::Configurable;
  48. define_native_property(vm.names.flags, flags, nullptr, readable_attr);
  49. define_native_property(vm.names.source, source, nullptr, readable_attr);
  50. #define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \
  51. define_native_property(vm.names.flagName, flag_name, nullptr, readable_attr);
  52. JS_ENUMERATE_REGEXP_FLAGS
  53. #undef __JS_ENUMERATE
  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. static String escape_regexp_pattern(const RegExpObject& regexp_object)
  79. {
  80. auto pattern = regexp_object.pattern();
  81. if (pattern.is_empty())
  82. return "(?:)";
  83. // FIXME: Check u flag and escape accordingly
  84. pattern.replace("\n", "\\n", true);
  85. pattern.replace("\r", "\\r", true);
  86. pattern.replace(LINE_SEPARATOR, "\\u2028", true);
  87. pattern.replace(PARAGRAPH_SEPARATOR, "\\u2029", true);
  88. pattern.replace("/", "\\/", true);
  89. return pattern;
  90. }
  91. #define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \
  92. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flag_name) \
  93. { \
  94. auto regexp_object = regexp_object_from(vm, global_object); \
  95. if (!regexp_object) \
  96. return {}; \
  97. \
  98. return Value(regexp_object->declared_options().has_flag_set(ECMAScriptFlags::ECMAScriptFlagName)); \
  99. }
  100. JS_ENUMERATE_REGEXP_FLAGS
  101. #undef __JS_ENUMERATE
  102. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flags)
  103. {
  104. auto this_object = this_object_from(vm, global_object);
  105. if (!this_object)
  106. return {};
  107. StringBuilder builder(8);
  108. #define __JS_ENUMERATE(flagName, flag_name, flag_char, ECMAScriptFlagName) \
  109. auto flag_##flag_name = this_object->get(vm.names.flagName).value_or(js_undefined()); \
  110. if (vm.exception()) \
  111. return {}; \
  112. if (flag_##flag_name.to_boolean()) \
  113. builder.append(#flag_char);
  114. JS_ENUMERATE_REGEXP_FLAGS
  115. #undef __JS_ENUMERATE
  116. return js_string(vm, builder.to_string());
  117. }
  118. JS_DEFINE_NATIVE_GETTER(RegExpPrototype::source)
  119. {
  120. auto this_object = this_object_from(vm, global_object);
  121. if (!this_object)
  122. return {};
  123. // FIXME: This is obnoxious - we should have an easier way of looking up %RegExp.prototype%.
  124. auto& regexp_prototype = global_object.get(vm.names.RegExp).as_object().get(vm.names.prototype).as_object();
  125. if (this_object == &regexp_prototype)
  126. return js_string(vm, "(?:)");
  127. auto regexp_object = regexp_object_from(vm, global_object);
  128. if (!regexp_object)
  129. return {};
  130. return js_string(vm, escape_regexp_pattern(*regexp_object));
  131. }
  132. RegexResult RegExpPrototype::do_match(const Regex<ECMA262>& re, const StringView& subject)
  133. {
  134. auto result = re.match(subject);
  135. // The 'lastIndex' property is reset on failing tests (if 'global')
  136. if (!result.success && re.options().has_flag_set(ECMAScriptFlags::Global))
  137. re.start_offset = 0;
  138. return result;
  139. }
  140. JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::exec)
  141. {
  142. // FIXME: This should try using dynamic properties for 'lastIndex',
  143. // and internal slots [[RegExpMatcher]], [[OriginalFlags]], etc.
  144. auto regexp_object = regexp_object_from(vm, global_object);
  145. if (!regexp_object)
  146. return {};
  147. auto str = vm.argument(0).to_string(global_object);
  148. if (vm.exception())
  149. return {};
  150. StringView str_to_match = str;
  151. // RegExps without "global" and "sticky" always start at offset 0.
  152. if (!regexp_object->regex().options().has_flag_set((ECMAScriptFlags)regex::AllFlags::Internal_Stateful))
  153. regexp_object->regex().start_offset = 0;
  154. auto result = do_match(regexp_object->regex(), str_to_match);
  155. if (!result.success)
  156. return js_null();
  157. auto& match = result.matches[0];
  158. // FIXME: Do code point index correction if the Unicode flag is set.
  159. auto* array = Array::create(global_object);
  160. array->indexed_properties().set_array_like_size(result.n_capture_groups + 1);
  161. array->define_property(vm.names.index, Value((i32)match.column));
  162. array->define_property(vm.names.input, js_string(vm, str));
  163. array->indexed_properties().put(array, 0, js_string(vm, match.view.to_string()));
  164. for (size_t i = 0; i < result.n_capture_groups; ++i) {
  165. auto& capture = result.capture_group_matches[0][i];
  166. array->indexed_properties().put(array, i + 1, js_string(vm, capture.view.to_string()));
  167. }
  168. Value groups = js_undefined();
  169. if (result.n_named_capture_groups > 0) {
  170. auto groups_object = create_empty(global_object);
  171. for (auto& entry : result.named_capture_group_matches[0])
  172. groups_object->define_property(entry.key, js_string(vm, entry.value.view.to_string()));
  173. groups = move(groups_object);
  174. }
  175. array->define_property(vm.names.groups, groups);
  176. return array;
  177. }
  178. JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
  179. {
  180. // FIXME: This should try using dynamic properties for 'exec' first,
  181. // before falling back to builtin_exec.
  182. auto regexp_object = regexp_object_from(vm, global_object);
  183. if (!regexp_object)
  184. return {};
  185. auto str = vm.argument(0).to_string(global_object);
  186. if (vm.exception())
  187. return {};
  188. // RegExps without "global" and "sticky" always start at offset 0.
  189. if (!regexp_object->regex().options().has_flag_set((ECMAScriptFlags)regex::AllFlags::Internal_Stateful))
  190. regexp_object->regex().start_offset = 0;
  191. auto result = do_match(regexp_object->regex(), str);
  192. return Value(result.success);
  193. }
  194. JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string)
  195. {
  196. auto this_object = this_object_from(vm, global_object);
  197. if (!this_object)
  198. return {};
  199. auto source_attr = this_object->get(vm.names.source).value_or(js_undefined());
  200. if (vm.exception())
  201. return {};
  202. auto pattern = source_attr.to_string(global_object);
  203. if (vm.exception())
  204. return {};
  205. auto flags_attr = this_object->get(vm.names.flags).value_or(js_undefined());
  206. if (vm.exception())
  207. return {};
  208. auto flags = flags_attr.to_string(global_object);
  209. if (vm.exception())
  210. return {};
  211. return js_string(vm, String::formatted("/{}/{}", pattern, flags));
  212. }
  213. }