ECMAScriptFunctionObject.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibJS/AST.h>
  9. #include <LibJS/Bytecode/Generator.h>
  10. #include <LibJS/Runtime/ExecutionContext.h>
  11. #include <LibJS/Runtime/FunctionObject.h>
  12. namespace JS {
  13. void async_block_start(VM&, NonnullRefPtr<Statement> const& parse_node, PromiseCapability const&, ExecutionContext&);
  14. // 10.2 ECMAScript Function Objects, https://tc39.es/ecma262/#sec-ecmascript-function-objects
  15. class ECMAScriptFunctionObject final : public FunctionObject {
  16. JS_OBJECT(ECMAScriptFunctionObject, FunctionObject);
  17. public:
  18. enum class ConstructorKind : u8 {
  19. Base,
  20. Derived,
  21. };
  22. enum class ThisMode : u8 {
  23. Lexical,
  24. Strict,
  25. Global,
  26. };
  27. static ECMAScriptFunctionObject* create(GlobalObject&, FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind, bool is_strict, bool might_need_arguments_object = true, bool contains_direct_call_to_eval = true, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {});
  28. static ECMAScriptFunctionObject* create(GlobalObject&, FlyString name, Object& prototype, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind, bool is_strict, bool might_need_arguments_object = true, bool contains_direct_call_to_eval = true, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {});
  29. ECMAScriptFunctionObject(FlyString name, String source_text, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_environment, PrivateEnvironment* private_environment, Object& prototype, FunctionKind, bool is_strict, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name);
  30. virtual void initialize(Realm&) override;
  31. virtual ~ECMAScriptFunctionObject() override = default;
  32. virtual ThrowCompletionOr<Value> internal_call(Value this_argument, MarkedVector<Value> arguments_list) override;
  33. virtual ThrowCompletionOr<Object*> internal_construct(MarkedVector<Value> arguments_list, FunctionObject& new_target) override;
  34. void make_method(Object& home_object);
  35. Statement const& ecmascript_code() const { return m_ecmascript_code; }
  36. Vector<FunctionNode::Parameter> const& formal_parameters() const { return m_formal_parameters; };
  37. virtual FlyString const& name() const override { return m_name; };
  38. void set_name(FlyString const& name);
  39. void set_is_class_constructor() { m_is_class_constructor = true; };
  40. auto& bytecode_executable() const { return m_bytecode_executable; }
  41. Environment* environment() { return m_environment; }
  42. virtual Realm* realm() const override { return m_realm; }
  43. ConstructorKind constructor_kind() const { return m_constructor_kind; };
  44. void set_constructor_kind(ConstructorKind constructor_kind) { m_constructor_kind = constructor_kind; }
  45. ThisMode this_mode() const { return m_this_mode; }
  46. Object* home_object() const { return m_home_object; }
  47. void set_home_object(Object* home_object) { m_home_object = home_object; }
  48. String const& source_text() const { return m_source_text; }
  49. void set_source_text(String source_text) { m_source_text = move(source_text); }
  50. Vector<ClassFieldDefinition> const& fields() const { return m_fields; }
  51. void add_field(ClassFieldDefinition field) { m_fields.append(move(field)); }
  52. Vector<PrivateElement> const& private_methods() const { return m_private_methods; }
  53. void add_private_method(PrivateElement method) { m_private_methods.append(move(method)); };
  54. // This is for IsSimpleParameterList (static semantics)
  55. bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
  56. // Equivalent to absence of [[Construct]]
  57. virtual bool has_constructor() const override { return m_kind == FunctionKind::Normal && !m_is_arrow_function; }
  58. FunctionKind kind() const { return m_kind; }
  59. // This is used by LibWeb to disassociate event handler attribute callback functions from the nearest script on the call stack.
  60. // https://html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler Step 3.11
  61. void set_script_or_module(ScriptOrModule script_or_module) { m_script_or_module = move(script_or_module); }
  62. Variant<PropertyKey, PrivateName, Empty> const& class_field_initializer_name() const { return m_class_field_initializer_name; }
  63. protected:
  64. virtual bool is_strict_mode() const final { return m_strict; }
  65. virtual Completion ordinary_call_evaluate_body();
  66. private:
  67. virtual bool is_ecmascript_function_object() const override { return true; }
  68. virtual void visit_edges(Visitor&) override;
  69. ThrowCompletionOr<void> prepare_for_ordinary_call(ExecutionContext& callee_context, Object* new_target);
  70. void ordinary_call_bind_this(ExecutionContext&, Value this_argument);
  71. void async_function_start(PromiseCapability const&);
  72. ThrowCompletionOr<void> function_declaration_instantiation(Interpreter*);
  73. FlyString m_name;
  74. OwnPtr<Bytecode::Executable> m_bytecode_executable;
  75. Vector<OwnPtr<Bytecode::Executable>> m_default_parameter_bytecode_executables;
  76. i32 m_function_length { 0 };
  77. // Internal Slots of ECMAScript Function Objects, https://tc39.es/ecma262/#table-internal-slots-of-ecmascript-function-objects
  78. Environment* m_environment { nullptr }; // [[Environment]]
  79. PrivateEnvironment* m_private_environment { nullptr }; // [[PrivateEnvironment]]
  80. Vector<FunctionNode::Parameter> const m_formal_parameters; // [[FormalParameters]]
  81. NonnullRefPtr<Statement> m_ecmascript_code; // [[ECMAScriptCode]]
  82. Realm* m_realm { nullptr }; // [[Realm]]
  83. ScriptOrModule m_script_or_module; // [[ScriptOrModule]]
  84. Object* m_home_object { nullptr }; // [[HomeObject]]
  85. String m_source_text; // [[SourceText]]
  86. Vector<ClassFieldDefinition> m_fields; // [[Fields]]
  87. Vector<PrivateElement> m_private_methods; // [[PrivateMethods]]
  88. Variant<PropertyKey, PrivateName, Empty> m_class_field_initializer_name; // [[ClassFieldInitializerName]]
  89. ConstructorKind m_constructor_kind : 1 { ConstructorKind::Base }; // [[ConstructorKind]]
  90. bool m_strict : 1 { false }; // [[Strict]]
  91. bool m_is_class_constructor : 1 { false }; // [[IsClassConstructor]]
  92. ThisMode m_this_mode : 2 { ThisMode::Global }; // [[ThisMode]]
  93. bool m_might_need_arguments_object : 1 { true };
  94. bool m_contains_direct_call_to_eval : 1 { true };
  95. bool m_is_arrow_function : 1 { false };
  96. bool m_has_simple_parameter_list : 1 { false };
  97. FunctionKind m_kind : 3 { FunctionKind::Normal };
  98. };
  99. template<>
  100. inline bool Object::fast_is<ECMAScriptFunctionObject>() const { return is_ecmascript_function_object(); }
  101. }