Function.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2020, 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 Function : public Object {
  11. JS_OBJECT(Function, Object);
  12. public:
  13. enum class ConstructorKind {
  14. Base,
  15. Derived,
  16. };
  17. virtual ~Function();
  18. virtual void initialize(GlobalObject&) override { }
  19. virtual Value call() = 0;
  20. virtual Value construct(Function& new_target) = 0;
  21. virtual const FlyString& name() const = 0;
  22. virtual FunctionEnvironmentRecord* create_environment_record() = 0;
  23. BoundFunction* bind(Value bound_this_value, Vector<Value> arguments);
  24. Value bound_this() const { return m_bound_this; }
  25. const Vector<Value>& bound_arguments() const { return m_bound_arguments; }
  26. Value home_object() const { return m_home_object; }
  27. void set_home_object(Value home_object) { m_home_object = home_object; }
  28. ConstructorKind constructor_kind() const { return m_constructor_kind; };
  29. void set_constructor_kind(ConstructorKind constructor_kind) { m_constructor_kind = constructor_kind; }
  30. virtual bool is_strict_mode() const { return false; }
  31. // [[Environment]]
  32. // The Environment Record that the function was closed over.
  33. // Used as the outer environment when evaluating the code of the function.
  34. virtual EnvironmentRecord* environment() { return nullptr; }
  35. protected:
  36. virtual void visit_edges(Visitor&) override;
  37. explicit Function(Object& prototype);
  38. Function(Value bound_this, Vector<Value> bound_arguments, Object& prototype);
  39. private:
  40. virtual bool is_function() const override { return true; }
  41. Value m_bound_this;
  42. Vector<Value> m_bound_arguments;
  43. Value m_home_object;
  44. ConstructorKind m_constructor_kind = ConstructorKind::Base;
  45. };
  46. }