FunctionConstructor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Interpreter.h>
  8. #include <LibJS/Lexer.h>
  9. #include <LibJS/Parser.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/FunctionConstructor.h>
  12. #include <LibJS/Runtime/FunctionObject.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. namespace JS {
  15. FunctionConstructor::FunctionConstructor(GlobalObject& global_object)
  16. : NativeFunction(vm().names.Function.as_string(), *global_object.function_prototype())
  17. {
  18. }
  19. void FunctionConstructor::initialize(GlobalObject& global_object)
  20. {
  21. auto& vm = this->vm();
  22. NativeFunction::initialize(global_object);
  23. // 20.2.2.2 Function.prototype, https://tc39.es/ecma262/#sec-function.prototype
  24. define_direct_property(vm.names.prototype, global_object.function_prototype(), 0);
  25. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  26. }
  27. FunctionConstructor::~FunctionConstructor()
  28. {
  29. }
  30. // 20.2.1.1.1 CreateDynamicFunction ( constructor, newTarget, kind, args ), https://tc39.es/ecma262/#sec-createdynamicfunction
  31. RefPtr<FunctionExpression> FunctionConstructor::create_dynamic_function_node(GlobalObject& global_object, FunctionObject&, FunctionKind kind)
  32. {
  33. auto& vm = global_object.vm();
  34. String parameters_source = "";
  35. String body_source = "";
  36. if (vm.argument_count() == 1)
  37. body_source = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
  38. if (vm.argument_count() > 1) {
  39. Vector<String> parameters;
  40. for (size_t i = 0; i < vm.argument_count() - 1; ++i)
  41. parameters.append(TRY_OR_DISCARD(vm.argument(i).to_string(global_object)));
  42. StringBuilder parameters_builder;
  43. parameters_builder.join(',', parameters);
  44. parameters_source = parameters_builder.build();
  45. body_source = TRY_OR_DISCARD(vm.argument(vm.argument_count() - 1).to_string(global_object));
  46. }
  47. auto is_generator = kind == FunctionKind::Generator;
  48. auto source = String::formatted("function{} anonymous({}\n) {{\n{}\n}}", is_generator ? "*" : "", parameters_source, body_source);
  49. auto parser = Parser(Lexer(source));
  50. auto function = parser.parse_function_node<FunctionExpression>();
  51. if (parser.has_errors()) {
  52. auto error = parser.errors()[0];
  53. vm.throw_exception<SyntaxError>(global_object, error.to_string());
  54. return {};
  55. }
  56. return function;
  57. }
  58. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  59. Value FunctionConstructor::call()
  60. {
  61. return construct(*this);
  62. }
  63. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  64. Value FunctionConstructor::construct(FunctionObject& new_target)
  65. {
  66. auto function = create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular);
  67. if (!function)
  68. return {};
  69. OwnPtr<Interpreter> local_interpreter;
  70. Interpreter* interpreter = vm().interpreter_if_exists();
  71. if (!interpreter) {
  72. local_interpreter = Interpreter::create_with_existing_realm(*realm());
  73. interpreter = local_interpreter.ptr();
  74. }
  75. VM::InterpreterExecutionScope scope(*interpreter);
  76. return function->execute(*interpreter, global_object());
  77. }
  78. }