NativeFunction.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 visit_edges(Cell::Visitor& visitor) override
  40. {
  41. Base::visit_edges(visitor);
  42. visitor.visit(m_native_function);
  43. }
  44. private:
  45. virtual bool is_native_function() const final { return true; }
  46. DeprecatedFlyString m_name;
  47. Optional<DeprecatedFlyString> m_initial_name; // [[InitialName]]
  48. JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>> m_native_function;
  49. GCPtr<Realm> m_realm;
  50. };
  51. template<>
  52. inline bool Object::fast_is<NativeFunction>() const { return is_native_function(); }
  53. }