DisplayNamesConstructor.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/DisplayNames.h>
  9. #include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
  10. namespace JS::Intl {
  11. // 12.2 The Intl.DisplayNames Constructor, https://tc39.es/ecma402/#sec-intl-displaynames-constructor
  12. DisplayNamesConstructor::DisplayNamesConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.DisplayNames.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void DisplayNamesConstructor::initialize(GlobalObject& global_object)
  17. {
  18. NativeFunction::initialize(global_object);
  19. auto& vm = this->vm();
  20. // 12.3.1 Intl.DisplayNames.prototype, https://tc39.es/ecma402/#sec-Intl.DisplayNames.prototype
  21. define_direct_property(vm.names.prototype, global_object.intl_display_names_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  23. }
  24. // 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
  25. Value DisplayNamesConstructor::call()
  26. {
  27. // 1. If NewTarget is undefined, throw a TypeError exception.
  28. vm().throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.DisplayNames");
  29. return {};
  30. }
  31. // 12.2.1 Intl.DisplayNames ( locales, options ), https://tc39.es/ecma402/#sec-Intl.DisplayNames
  32. Value DisplayNamesConstructor::construct(FunctionObject& new_target)
  33. {
  34. auto& vm = this->vm();
  35. auto& global_object = this->global_object();
  36. // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%", « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[Fields]] »).
  37. auto* display_names = ordinary_create_from_constructor<DisplayNames>(global_object, new_target, &GlobalObject::intl_display_names_prototype);
  38. if (vm.exception())
  39. return {};
  40. // 28. Return displayNames.
  41. return display_names;
  42. }
  43. }