FunctionConstructor.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 source = String::formatted("function{} anonymous({}\n) {{\n{}\n}}", is_generator ? "*" : "", parameters_source, body_source);
  50. auto parser = Parser(Lexer(source));
  51. auto function = parser.parse_function_node<FunctionExpression>();
  52. if (parser.has_errors()) {
  53. auto error = parser.errors()[0];
  54. return vm.throw_completion<SyntaxError>(global_object, error.to_string());
  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. ThrowCompletionOr<Value> FunctionConstructor::call()
  60. {
  61. return TRY(construct(*this));
  62. }
  63. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  64. ThrowCompletionOr<Object*> FunctionConstructor::construct(FunctionObject& new_target)
  65. {
  66. auto& vm = this->vm();
  67. auto function = TRY(create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular));
  68. OwnPtr<Interpreter> local_interpreter;
  69. Interpreter* interpreter = vm.interpreter_if_exists();
  70. if (!interpreter) {
  71. local_interpreter = Interpreter::create_with_existing_realm(*realm());
  72. interpreter = local_interpreter.ptr();
  73. }
  74. VM::InterpreterExecutionScope scope(*interpreter);
  75. auto result = function->execute(*interpreter, global_object());
  76. if (auto* exception = vm.exception())
  77. return throw_completion(exception->value());
  78. VERIFY(result.is_object() && is<ECMAScriptFunctionObject>(result.as_object()));
  79. return &result.as_object();
  80. }
  81. }