ProxyConstructor.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Array.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/ProxyConstructor.h>
  10. #include <LibJS/Runtime/ProxyObject.h>
  11. namespace JS {
  12. ProxyConstructor::ProxyConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.Proxy, *global_object.function_prototype())
  14. {
  15. }
  16. void ProxyConstructor::initialize(GlobalObject& global_object)
  17. {
  18. auto& vm = this->vm();
  19. NativeFunction::initialize(global_object);
  20. define_property(vm.names.length, Value(2), Attribute::Configurable);
  21. }
  22. ProxyConstructor::~ProxyConstructor()
  23. {
  24. }
  25. Value ProxyConstructor::call()
  26. {
  27. auto& vm = this->vm();
  28. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Proxy);
  29. return {};
  30. }
  31. Value ProxyConstructor::construct(Function&)
  32. {
  33. auto& vm = this->vm();
  34. if (vm.argument_count() < 2) {
  35. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyTwoArguments);
  36. return {};
  37. }
  38. auto target = vm.argument(0);
  39. auto handler = vm.argument(1);
  40. if (!target.is_object()) {
  41. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyConstructorBadType, "target", target.to_string_without_side_effects());
  42. return {};
  43. }
  44. if (!handler.is_object()) {
  45. vm.throw_exception<TypeError>(global_object(), ErrorType::ProxyConstructorBadType, "handler", handler.to_string_without_side_effects());
  46. return {};
  47. }
  48. return ProxyObject::create(global_object(), target.as_object(), handler.as_object());
  49. }
  50. }