FunctionConstructor.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/Function.h>
  12. #include <LibJS/Runtime/FunctionConstructor.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. namespace JS {
  15. FunctionConstructor::FunctionConstructor(GlobalObject& global_object)
  16. : NativeFunction(vm().names.Function, *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. define_property(vm.names.prototype, global_object.function_prototype(), 0);
  24. define_property(vm.names.length, Value(1), Attribute::Configurable);
  25. }
  26. FunctionConstructor::~FunctionConstructor()
  27. {
  28. }
  29. // 20.2.1.1.1 CreateDynamicFunction ( constructor, newTarget, kind, args ), https://tc39.es/ecma262/#sec-createdynamicfunction
  30. RefPtr<FunctionExpression> FunctionConstructor::create_dynamic_function_node(GlobalObject& global_object, Function&, FunctionKind kind)
  31. {
  32. auto& vm = global_object.vm();
  33. String parameters_source = "";
  34. String body_source = "";
  35. if (vm.argument_count() == 1) {
  36. body_source = vm.argument(0).to_string(global_object);
  37. if (vm.exception())
  38. return {};
  39. }
  40. if (vm.argument_count() > 1) {
  41. Vector<String> parameters;
  42. for (size_t i = 0; i < vm.argument_count() - 1; ++i) {
  43. parameters.append(vm.argument(i).to_string(global_object));
  44. if (vm.exception())
  45. return {};
  46. }
  47. StringBuilder parameters_builder;
  48. parameters_builder.join(',', parameters);
  49. parameters_source = parameters_builder.build();
  50. body_source = vm.argument(vm.argument_count() - 1).to_string(global_object);
  51. if (vm.exception())
  52. return {};
  53. }
  54. auto is_generator = kind == FunctionKind::Generator;
  55. auto source = String::formatted("function{} anonymous({}\n) {{\n{}\n}}", is_generator ? "*" : "", parameters_source, body_source);
  56. auto parser = Parser(Lexer(source));
  57. auto function = parser.parse_function_node<FunctionExpression>();
  58. if (parser.has_errors()) {
  59. auto error = parser.errors()[0];
  60. vm.throw_exception<SyntaxError>(global_object, error.to_string());
  61. return {};
  62. }
  63. return function;
  64. }
  65. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  66. Value FunctionConstructor::call()
  67. {
  68. return construct(*this);
  69. }
  70. // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
  71. Value FunctionConstructor::construct(Function& new_target)
  72. {
  73. auto function = create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular);
  74. if (!function)
  75. return {};
  76. OwnPtr<Interpreter> local_interpreter;
  77. Interpreter* interpreter = vm().interpreter_if_exists();
  78. if (!interpreter) {
  79. local_interpreter = Interpreter::create_with_existing_global_object(global_object());
  80. interpreter = local_interpreter.ptr();
  81. }
  82. VM::InterpreterExecutionScope scope(*interpreter);
  83. return function->execute(*interpreter, global_object());
  84. }
  85. }