ECMAScriptFunctionObject.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibJS/Bytecode/Generator.h>
  10. #include <LibJS/Bytecode/Interpreter.h>
  11. #include <LibJS/Runtime/ClassFieldDefinition.h>
  12. #include <LibJS/Runtime/ExecutionContext.h>
  13. #include <LibJS/Runtime/FunctionObject.h>
  14. namespace JS {
  15. template<typename T>
  16. void async_block_start(VM&, T const& async_body, PromiseCapability const&, ExecutionContext&);
  17. template<typename T>
  18. void async_function_start(VM&, PromiseCapability const&, T const& async_function_body);
  19. // 10.2 ECMAScript Function Objects, https://tc39.es/ecma262/#sec-ecmascript-function-objects
  20. class ECMAScriptFunctionObject final : public FunctionObject {
  21. JS_OBJECT(ECMAScriptFunctionObject, FunctionObject);
  22. JS_DECLARE_ALLOCATOR(ECMAScriptFunctionObject);
  23. public:
  24. enum class ConstructorKind : u8 {
  25. Base,
  26. Derived,
  27. };
  28. enum class ThisMode : u8 {
  29. Lexical,
  30. Strict,
  31. Global,
  32. };
  33. static NonnullGCPtr<ECMAScriptFunctionObject> create(Realm&, DeprecatedFlyString name, ByteString source_text, Statement const& ecmascript_code, Vector<FunctionParameter> parameters, i32 m_function_length, Vector<DeprecatedFlyString> local_variables_names, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind, bool is_strict, FunctionParsingInsights, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {});
  34. static NonnullGCPtr<ECMAScriptFunctionObject> create(Realm&, DeprecatedFlyString name, Object& prototype, ByteString source_text, Statement const& ecmascript_code, Vector<FunctionParameter> parameters, i32 m_function_length, Vector<DeprecatedFlyString> local_variables_names, Environment* parent_environment, PrivateEnvironment* private_environment, FunctionKind, bool is_strict, FunctionParsingInsights, bool is_arrow_function = false, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name = {});
  35. virtual void initialize(Realm&) override;
  36. virtual ~ECMAScriptFunctionObject() override = default;
  37. virtual ThrowCompletionOr<Value> internal_call(Value this_argument, ReadonlySpan<Value> arguments_list) override;
  38. virtual ThrowCompletionOr<NonnullGCPtr<Object>> internal_construct(ReadonlySpan<Value> arguments_list, FunctionObject& new_target) override;
  39. void make_method(Object& home_object);
  40. [[nodiscard]] bool is_module_wrapper() const { return m_is_module_wrapper; }
  41. void set_is_module_wrapper(bool b) { m_is_module_wrapper = b; }
  42. Statement const& ecmascript_code() const { return m_ecmascript_code; }
  43. Vector<FunctionParameter> const& formal_parameters() const override { return m_formal_parameters; }
  44. virtual DeprecatedFlyString const& name() const override { return m_name; }
  45. void set_name(DeprecatedFlyString const& name);
  46. void set_is_class_constructor() { m_is_class_constructor = true; }
  47. auto& bytecode_executable() const { return m_bytecode_executable; }
  48. Environment* environment() { return m_environment; }
  49. virtual Realm* realm() const override { return m_realm; }
  50. ConstructorKind constructor_kind() const { return m_constructor_kind; }
  51. void set_constructor_kind(ConstructorKind constructor_kind) { m_constructor_kind = constructor_kind; }
  52. ThisMode this_mode() const { return m_this_mode; }
  53. Object* home_object() const { return m_home_object; }
  54. void set_home_object(Object* home_object) { m_home_object = home_object; }
  55. ByteString const& source_text() const { return m_source_text; }
  56. void set_source_text(ByteString source_text) { m_source_text = move(source_text); }
  57. Vector<ClassFieldDefinition> const& fields() const { return m_fields; }
  58. void add_field(ClassFieldDefinition field) { m_fields.append(move(field)); }
  59. Vector<PrivateElement> const& private_methods() const { return m_private_methods; }
  60. void add_private_method(PrivateElement method) { m_private_methods.append(move(method)); }
  61. // This is for IsSimpleParameterList (static semantics)
  62. bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
  63. // Equivalent to absence of [[Construct]]
  64. virtual bool has_constructor() const override { return m_kind == FunctionKind::Normal && !m_is_arrow_function; }
  65. virtual Vector<DeprecatedFlyString> const& local_variables_names() const override { return m_local_variables_names; }
  66. FunctionKind kind() const { return m_kind; }
  67. // This is used by LibWeb to disassociate event handler attribute callback functions from the nearest script on the call stack.
  68. // https://html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler Step 3.11
  69. void set_script_or_module(ScriptOrModule script_or_module) { m_script_or_module = move(script_or_module); }
  70. Variant<PropertyKey, PrivateName, Empty> const& class_field_initializer_name() const { return m_class_field_initializer_name; }
  71. bool allocates_function_environment() const { return m_function_environment_needed; }
  72. friend class Bytecode::Generator;
  73. protected:
  74. virtual bool is_strict_mode() const final { return m_strict; }
  75. virtual Completion ordinary_call_evaluate_body();
  76. private:
  77. ECMAScriptFunctionObject(DeprecatedFlyString name, ByteString source_text, Statement const& ecmascript_code, Vector<FunctionParameter> parameters, i32 m_function_length, Vector<DeprecatedFlyString> local_variables_names, Environment* parent_environment, PrivateEnvironment* private_environment, Object& prototype, FunctionKind, bool is_strict, FunctionParsingInsights, bool is_arrow_function, Variant<PropertyKey, PrivateName, Empty> class_field_initializer_name);
  78. virtual bool is_ecmascript_function_object() const override { return true; }
  79. virtual void visit_edges(Visitor&) override;
  80. ThrowCompletionOr<void> prepare_for_ordinary_call(ExecutionContext& callee_context, Object* new_target);
  81. void ordinary_call_bind_this(ExecutionContext&, Value this_argument);
  82. DeprecatedFlyString m_name;
  83. GCPtr<PrimitiveString> m_name_string;
  84. GCPtr<Bytecode::Executable> m_bytecode_executable;
  85. i32 m_function_length { 0 };
  86. Vector<DeprecatedFlyString> m_local_variables_names;
  87. // Internal Slots of ECMAScript Function Objects, https://tc39.es/ecma262/#table-internal-slots-of-ecmascript-function-objects
  88. GCPtr<Environment> m_environment; // [[Environment]]
  89. GCPtr<PrivateEnvironment> m_private_environment; // [[PrivateEnvironment]]
  90. Vector<FunctionParameter> const m_formal_parameters; // [[FormalParameters]]
  91. NonnullRefPtr<Statement const> m_ecmascript_code; // [[ECMAScriptCode]]
  92. GCPtr<Realm> m_realm; // [[Realm]]
  93. ScriptOrModule m_script_or_module; // [[ScriptOrModule]]
  94. GCPtr<Object> m_home_object; // [[HomeObject]]
  95. ByteString m_source_text; // [[SourceText]]
  96. Vector<ClassFieldDefinition> m_fields; // [[Fields]]
  97. Vector<PrivateElement> m_private_methods; // [[PrivateMethods]]
  98. Variant<PropertyKey, PrivateName, Empty> m_class_field_initializer_name; // [[ClassFieldInitializerName]]
  99. ConstructorKind m_constructor_kind : 1 { ConstructorKind::Base }; // [[ConstructorKind]]
  100. bool m_strict : 1 { false }; // [[Strict]]
  101. bool m_is_class_constructor : 1 { false }; // [[IsClassConstructor]]
  102. ThisMode m_this_mode : 2 { ThisMode::Global }; // [[ThisMode]]
  103. bool m_might_need_arguments_object : 1 { true };
  104. bool m_contains_direct_call_to_eval : 1 { true };
  105. bool m_is_arrow_function : 1 { false };
  106. bool m_has_simple_parameter_list : 1 { false };
  107. FunctionKind m_kind : 3 { FunctionKind::Normal };
  108. struct VariableNameToInitialize {
  109. Identifier const& identifier;
  110. bool parameter_binding { false };
  111. bool function_name { false };
  112. };
  113. bool m_has_parameter_expressions { false };
  114. bool m_has_duplicates { false };
  115. enum class ParameterIsLocal {
  116. No,
  117. Yes,
  118. };
  119. HashMap<DeprecatedFlyString, ParameterIsLocal> m_parameter_names;
  120. Vector<FunctionDeclaration const&> m_functions_to_initialize;
  121. bool m_arguments_object_needed { false };
  122. bool m_is_module_wrapper { false };
  123. bool m_function_environment_needed { false };
  124. bool m_uses_this { false };
  125. Vector<VariableNameToInitialize> m_var_names_to_initialize_binding;
  126. Vector<DeprecatedFlyString> m_function_names_to_initialize_binding;
  127. size_t m_function_environment_bindings_count { 0 };
  128. size_t m_var_environment_bindings_count { 0 };
  129. size_t m_lex_environment_bindings_count { 0 };
  130. };
  131. template<>
  132. inline bool Object::fast_is<ECMAScriptFunctionObject>() const { return is_ecmascript_function_object(); }
  133. }