FunctionObject.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. Object* home_object() const { return m_home_object; }
  23. void set_home_object(Object* home_object) { m_home_object = home_object; }
  24. virtual bool is_strict_mode() const { return false; }
  25. // [[Environment]]
  26. // The Environment Record that the function was closed over.
  27. // Used as the outer environment when evaluating the code of the function.
  28. virtual Environment* environment() { return nullptr; }
  29. // [[Realm]]
  30. virtual Realm* realm() const { return nullptr; }
  31. // This is for IsSimpleParameterList (static semantics)
  32. bool has_simple_parameter_list() const { return m_has_simple_parameter_list; }
  33. // [[Fields]]
  34. struct InstanceField {
  35. StringOrSymbol name;
  36. FunctionObject* initializer { nullptr };
  37. void define_field(VM& vm, Object& receiver) const;
  38. };
  39. Vector<InstanceField> const& fields() const { return m_fields; }
  40. void add_field(StringOrSymbol property_key, FunctionObject* initializer);
  41. protected:
  42. virtual void visit_edges(Visitor&) override;
  43. explicit FunctionObject(Object& prototype);
  44. FunctionObject(Value bound_this, Vector<Value> bound_arguments, Object& prototype);
  45. void set_has_simple_parameter_list(bool b) { m_has_simple_parameter_list = b; }
  46. private:
  47. virtual bool is_function() const override { return true; }
  48. Value m_bound_this;
  49. Vector<Value> m_bound_arguments;
  50. Object* m_home_object { nullptr };
  51. bool m_has_simple_parameter_list { false };
  52. Vector<InstanceField> m_fields;
  53. };
  54. }