NativeFunction.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Badge.h>
  9. #include <AK/Optional.h>
  10. #include <LibJS/Heap/HeapFunction.h>
  11. #include <LibJS/Runtime/Completion.h>
  12. #include <LibJS/Runtime/FunctionObject.h>
  13. #include <LibJS/Runtime/PropertyKey.h>
  14. namespace JS {
  15. class NativeFunction : public FunctionObject {
  16. JS_OBJECT(NativeFunction, FunctionObject);
  17. JS_DECLARE_ALLOCATOR(NativeFunction);
  18. public:
  19. static NonnullGCPtr<NativeFunction> create(Realm&, Function<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> = {}, Optional<Object*> prototype = {}, Optional<StringView> const& prefix = {});
  20. static NonnullGCPtr<NativeFunction> create(Realm&, DeprecatedFlyString const& name, Function<ThrowCompletionOr<Value>(VM&)>);
  21. virtual ~NativeFunction() override = default;
  22. virtual ThrowCompletionOr<Value> internal_call(Value this_argument, ReadonlySpan<Value> arguments_list) override;
  23. virtual ThrowCompletionOr<NonnullGCPtr<Object>> internal_construct(ReadonlySpan<Value> arguments_list, FunctionObject& new_target) override;
  24. // Used for [[Call]] / [[Construct]]'s "...result of evaluating F in a manner that conforms to the specification of F".
  25. // Needs to be overridden by all NativeFunctions without an m_native_function.
  26. virtual ThrowCompletionOr<Value> call();
  27. virtual ThrowCompletionOr<NonnullGCPtr<Object>> construct(FunctionObject& new_target);
  28. virtual DeprecatedFlyString const& name() const override { return m_name; }
  29. virtual bool is_strict_mode() const override;
  30. virtual bool has_constructor() const override { return false; }
  31. virtual Realm* realm() const override { return m_realm; }
  32. Optional<DeprecatedFlyString> const& initial_name() const { return m_initial_name; }
  33. void set_initial_name(Badge<FunctionObject>, DeprecatedFlyString initial_name) { m_initial_name = move(initial_name); }
  34. protected:
  35. NativeFunction(DeprecatedFlyString name, Object& prototype);
  36. NativeFunction(JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object* prototype, Realm& realm);
  37. NativeFunction(DeprecatedFlyString name, JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object& prototype);
  38. explicit NativeFunction(Object& prototype);
  39. virtual void initialize(Realm&) override;
  40. virtual void visit_edges(Cell::Visitor& visitor) override;
  41. private:
  42. virtual bool is_native_function() const final { return true; }
  43. DeprecatedFlyString m_name;
  44. GCPtr<PrimitiveString> m_name_string;
  45. Optional<DeprecatedFlyString> m_initial_name; // [[InitialName]]
  46. JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>> m_native_function;
  47. GCPtr<Realm> m_realm;
  48. };
  49. template<>
  50. inline bool Object::fast_is<NativeFunction>() const { return is_native_function(); }
  51. }