FunctionConstructor.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = vm.argument(0).to_string(global_object);
  38. if (vm.exception())
  39. return {};
  40. }
  41. if (vm.argument_count() > 1) {
  42. Vector<String> parameters;
  43. for (size_t i = 0; i < vm.argument_count() - 1; ++i) {
  44. parameters.append(vm.argument(i).to_string(global_object));
  45. if (vm.exception())
  46. return {};
  47. }
  48. StringBuilder parameters_builder;
  49. parameters_builder.join(',', parameters);
  50. parameters_source = parameters_builder.build();
  51. body_source = vm.argument(vm.argument_count() - 1).to_string(global_object);
  52. if (vm.exception())
  53. return {};
  54. }
  55. auto is_generator = kind == FunctionKind::Generator;
  56. auto source = String::formatted("function{} anonymous({}\n) {{\n{}\n}}", is_generator ? "*" : "", parameters_source, body_source);
  57. auto parser = Parser(Lexer(source));
  58. auto function = parser.parse_function_node<FunctionExpression>();
  59. if (parser.has_errors()) {
  60. auto error = parser.errors()[0];
  61. vm.throw_exception<SyntaxError>(global_object, error.to_string());
  62. return {};
  63. }
  64. return function;
  65. }
  66. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  67. Value FunctionConstructor::call()
  68. {
  69. return construct(*this);
  70. }
  71. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  72. Value FunctionConstructor::construct(FunctionObject& new_target)
  73. {
  74. auto function = create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular);
  75. if (!function)
  76. return {};
  77. OwnPtr<Interpreter> local_interpreter;
  78. Interpreter* interpreter = vm().interpreter_if_exists();
  79. if (!interpreter) {
  80. local_interpreter = Interpreter::create_with_existing_realm(*realm());
  81. interpreter = local_interpreter.ptr();
  82. }
  83. VM::InterpreterExecutionScope scope(*interpreter);
  84. return function->execute(*interpreter, global_object());
  85. }
  86. }