FunctionConstructor.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/ECMAScriptFunctionObject.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/FunctionConstructor.h>
  13. #include <LibJS/Runtime/FunctionObject.h>
  14. #include <LibJS/Runtime/GlobalObject.h>
  15. namespace JS {
  16. FunctionConstructor::FunctionConstructor(GlobalObject& global_object)
  17. : NativeFunction(vm().names.Function.as_string(), *global_object.function_prototype())
  18. {
  19. }
  20. void FunctionConstructor::initialize(GlobalObject& global_object)
  21. {
  22. auto& vm = this->vm();
  23. NativeFunction::initialize(global_object);
  24. // 20.2.2.2 Function.prototype, https://tc39.es/ecma262/#sec-function.prototype
  25. define_direct_property(vm.names.prototype, global_object.function_prototype(), 0);
  26. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  27. }
  28. FunctionConstructor::~FunctionConstructor()
  29. {
  30. }
  31. // 20.2.1.1.1 CreateDynamicFunction ( constructor, newTarget, kind, args ), https://tc39.es/ecma262/#sec-createdynamicfunction
  32. ThrowCompletionOr<RefPtr<FunctionExpression>> FunctionConstructor::create_dynamic_function_node(GlobalObject& global_object, FunctionObject&, FunctionKind kind)
  33. {
  34. auto& vm = global_object.vm();
  35. String parameters_source = "";
  36. String body_source = "";
  37. if (vm.argument_count() == 1)
  38. body_source = TRY(vm.argument(0).to_string(global_object));
  39. if (vm.argument_count() > 1) {
  40. Vector<String> parameters;
  41. for (size_t i = 0; i < vm.argument_count() - 1; ++i)
  42. parameters.append(TRY(vm.argument(i).to_string(global_object)));
  43. StringBuilder parameters_builder;
  44. parameters_builder.join(',', parameters);
  45. parameters_source = parameters_builder.build();
  46. body_source = TRY(vm.argument(vm.argument_count() - 1).to_string(global_object));
  47. }
  48. auto is_generator = kind == FunctionKind::Generator;
  49. auto is_async = kind == FunctionKind::Async;
  50. auto source = String::formatted("{}function{} anonymous({}\n) {{\n{}\n}}", is_async ? "async " : "", is_generator ? "*" : "", parameters_source, body_source);
  51. auto parser = Parser(Lexer(source));
  52. auto function = parser.parse_function_node<FunctionExpression>();
  53. if (parser.has_errors()) {
  54. auto error = parser.errors()[0];
  55. return vm.throw_completion<SyntaxError>(global_object, error.to_string());
  56. }
  57. return function;
  58. }
  59. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  60. ThrowCompletionOr<Value> FunctionConstructor::call()
  61. {
  62. return TRY(construct(*this));
  63. }
  64. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  65. ThrowCompletionOr<Object*> FunctionConstructor::construct(FunctionObject& new_target)
  66. {
  67. auto& vm = this->vm();
  68. auto function = TRY(create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular));
  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. auto result = function->execute(*interpreter, global_object());
  77. if (auto* exception = vm.exception())
  78. return throw_completion(exception->value());
  79. VERIFY(result.is_object() && is<ECMAScriptFunctionObject>(result.as_object()));
  80. return &result.as_object();
  81. }
  82. }