FunctionObject.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <AK/String.h>
  8. #include <LibJS/Runtime/Object.h>
  9. namespace JS {
  10. class FunctionObject : public Object {
  11. JS_OBJECT(Function, Object);
  12. public:
  13. virtual ~FunctionObject();
  14. virtual void initialize(GlobalObject&) override { }
  15. virtual Value call() = 0;
  16. virtual Value construct(FunctionObject& new_target) = 0;
  17. virtual const FlyString& name() const = 0;
  18. virtual FunctionEnvironment* create_environment(FunctionObject&) = 0;
  19. BoundFunction* bind(Value bound_this_value, Vector<Value> arguments);
  20. Value bound_this() const { return m_bound_this; }
  21. const Vector<Value>& bound_arguments() const { return m_bound_arguments; }
  22. virtual bool is_strict_mode() const { return false; }
  23. // [[Environment]]
  24. // The Environment Record that the function was closed over.
  25. // Used as the outer environment when evaluating the code of the function.
  26. virtual Environment* environment() { return nullptr; }
  27. // [[Realm]]
  28. virtual Realm* realm() const { return nullptr; }
  29. // This is for IsSimpleParameterList (static semantics)
  30. bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
  31. protected:
  32. virtual void visit_edges(Visitor&) override;
  33. explicit FunctionObject(Object& prototype);
  34. FunctionObject(Value bound_this, Vector<Value> bound_arguments, Object& prototype);
  35. void set_has_simple_parameter_list(bool b) { m_has_simple_parameter_list = b; }
  36. private:
  37. virtual bool is_function() const override { return true; }
  38. Value m_bound_this;
  39. Vector<Value> m_bound_arguments;
  40. bool m_has_simple_parameter_list { false };
  41. };
  42. }