FunctionObject.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // 10.2 ECMAScript Function Objects, https://tc39.es/ecma262/#sec-ecmascript-function-objects
  11. class FunctionObject : public Object {
  12. JS_OBJECT(Function, Object);
  13. public:
  14. enum class ConstructorKind {
  15. Base,
  16. Derived,
  17. };
  18. virtual ~FunctionObject();
  19. virtual void initialize(GlobalObject&) override { }
  20. virtual Value call() = 0;
  21. virtual Value construct(FunctionObject& new_target) = 0;
  22. virtual const FlyString& name() const = 0;
  23. virtual FunctionEnvironment* create_environment(FunctionObject&) = 0;
  24. BoundFunction* bind(Value bound_this_value, Vector<Value> arguments);
  25. Value bound_this() const { return m_bound_this; }
  26. const Vector<Value>& bound_arguments() const { return m_bound_arguments; }
  27. Object* home_object() const { return m_home_object; }
  28. void set_home_object(Object* home_object) { m_home_object = home_object; }
  29. ConstructorKind constructor_kind() const { return m_constructor_kind; };
  30. void set_constructor_kind(ConstructorKind constructor_kind) { m_constructor_kind = constructor_kind; }
  31. virtual bool is_strict_mode() const { return false; }
  32. // [[Environment]]
  33. // The Environment Record that the function was closed over.
  34. // Used as the outer environment when evaluating the code of the function.
  35. virtual Environment* environment() { return nullptr; }
  36. // [[Realm]]
  37. virtual Realm* realm() const { return nullptr; }
  38. enum class ThisMode : u8 {
  39. Lexical,
  40. Strict,
  41. Global,
  42. };
  43. // [[ThisMode]]
  44. ThisMode this_mode() const { return m_this_mode; }
  45. void set_this_mode(ThisMode this_mode) { m_this_mode = this_mode; }
  46. // This is for IsSimpleParameterList (static semantics)
  47. bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
  48. // [[Fields]]
  49. struct InstanceField {
  50. StringOrSymbol name;
  51. FunctionObject* initializer { nullptr };
  52. void define_field(VM& vm, Object& receiver) const;
  53. };
  54. Vector<InstanceField> const& fields() const { return m_fields; }
  55. void add_field(StringOrSymbol property_key, FunctionObject* initializer);
  56. protected:
  57. virtual void visit_edges(Visitor&) override;
  58. explicit FunctionObject(Object& prototype);
  59. FunctionObject(Value bound_this, Vector<Value> bound_arguments, Object& prototype);
  60. void set_has_simple_parameter_list(bool b) { m_has_simple_parameter_list = b; }
  61. private:
  62. virtual bool is_function() const override { return true; }
  63. Value m_bound_this;
  64. Vector<Value> m_bound_arguments;
  65. Object* m_home_object { nullptr };
  66. ConstructorKind m_constructor_kind = ConstructorKind::Base;
  67. ThisMode m_this_mode { ThisMode::Global };
  68. bool m_has_simple_parameter_list { false };
  69. Vector<InstanceField> m_fields;
  70. };
  71. }