ECMAScriptFunctionObject.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, FunctionKind, bool is_strict, bool is_arrow_function = false);
  16. ECMAScriptFunctionObject(GlobalObject&, const FlyString& name, const Statement& body, 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. const Statement& body() const { return m_body; }
  20. const Vector<FunctionNode::Parameter>& parameters() const { return m_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_is_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. FlyString m_name;
  37. NonnullRefPtr<Statement> m_body;
  38. const Vector<FunctionNode::Parameter> m_parameters;
  39. Optional<Bytecode::Executable> m_bytecode_executable;
  40. Environment* m_environment { nullptr };
  41. Realm* m_realm { nullptr };
  42. i32 m_function_length { 0 };
  43. FunctionKind m_kind { FunctionKind::Regular };
  44. bool m_is_strict { false };
  45. bool m_is_arrow_function { false };
  46. bool m_is_class_constructor { false };
  47. };
  48. }