ScriptFunction.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2020, 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/Function.h>
  10. namespace JS {
  11. class ScriptFunction final : public Function {
  12. JS_OBJECT(ScriptFunction, Function);
  13. public:
  14. static ScriptFunction* create(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_strict, bool is_arrow_function = false);
  15. ScriptFunction(GlobalObject&, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_strict, bool is_arrow_function = false);
  16. virtual void initialize(GlobalObject&) override;
  17. virtual ~ScriptFunction();
  18. const Statement& body() const { return m_body; }
  19. const Vector<FunctionNode::Parameter>& parameters() const { return m_parameters; };
  20. virtual Value call() override;
  21. virtual Value construct(Function& new_target) override;
  22. virtual const FlyString& name() const override { return m_name; };
  23. void set_name(const FlyString& name) { m_name = name; };
  24. void set_is_class_constructor() { m_is_class_constructor = true; };
  25. protected:
  26. virtual bool is_strict_mode() const final { return m_is_strict; }
  27. private:
  28. virtual LexicalEnvironment* create_environment() override;
  29. virtual void visit_edges(Visitor&) override;
  30. Value execute_function_body();
  31. JS_DECLARE_NATIVE_GETTER(length_getter);
  32. JS_DECLARE_NATIVE_GETTER(name_getter);
  33. FlyString m_name;
  34. NonnullRefPtr<Statement> m_body;
  35. const Vector<FunctionNode::Parameter> m_parameters;
  36. Optional<Bytecode::ExecutionUnit> m_bytecode_execution_unit;
  37. ScopeObject* m_parent_scope { nullptr };
  38. i32 m_function_length { 0 };
  39. bool m_is_strict { false };
  40. bool m_is_arrow_function { false };
  41. bool m_is_class_constructor { false };
  42. };
  43. }