ECMAScriptFunctionObject.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/AST.h>
  8. #include <LibJS/Bytecode/Generator.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. namespace JS {
  11. // 10.2 ECMAScript Function Objects, https://tc39.es/ecma262/#sec-ecmascript-function-objects
  12. class ECMAScriptFunctionObject final : public FunctionObject {
  13. JS_OBJECT(ECMAScriptFunctionObject, FunctionObject);
  14. public:
  15. static ECMAScriptFunctionObject* create(GlobalObject&, FlyString name, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, FunctionKind, bool is_strict, bool is_arrow_function = false);
  16. ECMAScriptFunctionObject(GlobalObject&, FlyString name, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, Object& prototype, FunctionKind, bool is_strict, bool is_arrow_function = false);
  17. virtual void initialize(GlobalObject&) override;
  18. virtual ~ECMAScriptFunctionObject();
  19. Statement const& ecmascript_code() const { return m_ecmascript_code; }
  20. Vector<FunctionNode::Parameter> const& formal_parameters() const { return m_formal_parameters; };
  21. virtual Value call() override;
  22. virtual Value construct(FunctionObject& new_target) override;
  23. virtual const FlyString& name() const override { return m_name; };
  24. void set_name(const FlyString& name);
  25. void set_is_class_constructor() { m_is_class_constructor = true; };
  26. auto& bytecode_executable() const { return m_bytecode_executable; }
  27. virtual Environment* environment() override { return m_environment; }
  28. virtual Realm* realm() const override { return m_realm; }
  29. protected:
  30. virtual bool is_strict_mode() const final { return m_strict; }
  31. private:
  32. virtual bool is_ecmascript_function_object() const override { return true; }
  33. virtual FunctionEnvironment* create_environment(FunctionObject&) override;
  34. virtual void visit_edges(Visitor&) override;
  35. Value execute_function_body();
  36. // Internal Slots of ECMAScript Function Objects, https://tc39.es/ecma262/#table-internal-slots-of-ecmascript-function-objects
  37. Environment* m_environment { nullptr }; // [[Environment]]
  38. Vector<FunctionNode::Parameter> const m_formal_parameters; // [[FormalParameters]]
  39. NonnullRefPtr<Statement> m_ecmascript_code; // [[ECMAScriptCode]]
  40. Realm* m_realm { nullptr }; // [[Realm]]
  41. bool m_strict { false }; // [[Strict]]
  42. bool m_is_class_constructor { false }; // [[IsClassConstructor]]
  43. FlyString m_name;
  44. Optional<Bytecode::Executable> m_bytecode_executable;
  45. i32 m_function_length { 0 };
  46. FunctionKind m_kind { FunctionKind::Regular };
  47. bool m_is_arrow_function { false };
  48. };
  49. }