FunctionObject.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. enum class ConstructorKind {
  14. Base,
  15. Derived,
  16. };
  17. virtual ~FunctionObject();
  18. virtual void initialize(GlobalObject&) override { }
  19. virtual Value call() = 0;
  20. virtual Value construct(FunctionObject& new_target) = 0;
  21. virtual const FlyString& name() const = 0;
  22. virtual FunctionEnvironment* create_environment(FunctionObject&) = 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. Object* home_object() const { return m_home_object; }
  27. void set_home_object(Object* 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 Environment* environment() { return nullptr; }
  35. // [[Realm]]
  36. virtual Realm* realm() const { return nullptr; }
  37. enum class ThisMode : u8 {
  38. Lexical,
  39. Strict,
  40. Global,
  41. };
  42. // [[ThisMode]]
  43. ThisMode this_mode() const { return m_this_mode; }
  44. void set_this_mode(ThisMode this_mode) { m_this_mode = this_mode; }
  45. // This is for IsSimpleParameterList (static semantics)
  46. bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
  47. // [[Fields]]
  48. struct InstanceField {
  49. StringOrSymbol name;
  50. FunctionObject* initializer { nullptr };
  51. void define_field(VM& vm, Object& receiver) const;
  52. };
  53. Vector<InstanceField> const& fields() const { return m_fields; }
  54. void add_field(StringOrSymbol property_key, FunctionObject* initializer);
  55. protected:
  56. virtual void visit_edges(Visitor&) override;
  57. explicit FunctionObject(Object& prototype);
  58. FunctionObject(Value bound_this, Vector<Value> bound_arguments, Object& prototype);
  59. void set_has_simple_parameter_list(bool b) { m_has_simple_parameter_list = b; }
  60. private:
  61. virtual bool is_function() const override { return true; }
  62. Value m_bound_this;
  63. Vector<Value> m_bound_arguments;
  64. Object* m_home_object { nullptr };
  65. ConstructorKind m_constructor_kind = ConstructorKind::Base;
  66. ThisMode m_this_mode { ThisMode::Global };
  67. bool m_has_simple_parameter_list { false };
  68. Vector<InstanceField> m_fields;
  69. };
  70. }