RegExpConstructor.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }
  22. RegExpConstructor::~RegExpConstructor()
  23. {
  24. }
  25. Value RegExpConstructor::call()
  26. {
  27. return construct(*this);
  28. }
  29. Value RegExpConstructor::construct(Function&)
  30. {
  31. auto& vm = this->vm();
  32. String pattern = "";
  33. String flags = "";
  34. if (!vm.argument(0).is_undefined()) {
  35. pattern = vm.argument(0).to_string(global_object());
  36. if (vm.exception())
  37. return {};
  38. }
  39. if (!vm.argument(1).is_undefined()) {
  40. flags = vm.argument(1).to_string(global_object());
  41. if (vm.exception())
  42. return {};
  43. }
  44. return RegExpObject::create(global_object(), pattern, flags);
  45. }
  46. }