RegExpConstructor.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. namespace JS {
  11. RegExpConstructor::RegExpConstructor(GlobalObject& global_object)
  12. : NativeFunction(vm().names.RegExp, *global_object.function_prototype())
  13. {
  14. }
  15. void RegExpConstructor::initialize(GlobalObject& global_object)
  16. {
  17. auto& vm = this->vm();
  18. NativeFunction::initialize(global_object);
  19. define_property(vm.names.prototype, global_object.regexp_prototype(), 0);
  20. define_property(vm.names.length, Value(2), Attribute::Configurable);
  21. define_native_accessor(vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  22. }
  23. RegExpConstructor::~RegExpConstructor()
  24. {
  25. }
  26. Value RegExpConstructor::call()
  27. {
  28. return construct(*this);
  29. }
  30. Value RegExpConstructor::construct(Function&)
  31. {
  32. auto& vm = this->vm();
  33. String pattern = "";
  34. String flags = "";
  35. if (!vm.argument(0).is_undefined()) {
  36. pattern = vm.argument(0).to_string(global_object());
  37. if (vm.exception())
  38. return {};
  39. }
  40. if (!vm.argument(1).is_undefined()) {
  41. flags = vm.argument(1).to_string(global_object());
  42. if (vm.exception())
  43. return {};
  44. }
  45. return RegExpObject::create(global_object(), pattern, flags);
  46. }
  47. JS_DEFINE_NATIVE_GETTER(RegExpConstructor::symbol_species_getter)
  48. {
  49. return vm.this_value(global_object);
  50. }
  51. }