FunctionConstructor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 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_OR_DISCARD(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_OR_DISCARD(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_OR_DISCARD(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. vm.throw_exception<SyntaxError>(global_object, error.to_string());
  55. return {};
  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 = create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular);
  69. if (auto* exception = vm.exception())
  70. return throw_completion(exception->value());
  71. OwnPtr<Interpreter> local_interpreter;
  72. Interpreter* interpreter = vm.interpreter_if_exists();
  73. if (!interpreter) {
  74. local_interpreter = Interpreter::create_with_existing_realm(*realm());
  75. interpreter = local_interpreter.ptr();
  76. }
  77. VM::InterpreterExecutionScope scope(*interpreter);
  78. auto result = function->execute(*interpreter, global_object());
  79. if (auto* exception = vm.exception())
  80. return throw_completion(exception->value());
  81. VERIFY(result.is_object() && is<ECMAScriptFunctionObject>(result.as_object()));
  82. return &result.as_object();
  83. }
  84. }