NativeFunction.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. public:
  18. static NonnullGCPtr<NativeFunction> create(Realm&, Function<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> = {}, Optional<Object*> prototype = {}, Optional<StringView> const& prefix = {});
  19. static NonnullGCPtr<NativeFunction> create(Realm&, DeprecatedFlyString const& name, Function<ThrowCompletionOr<Value>(VM&)>);
  20. virtual ~NativeFunction() override = default;
  21. virtual ThrowCompletionOr<Value> internal_call(Value this_argument, MarkedVector<Value> arguments_list) override;
  22. virtual ThrowCompletionOr<NonnullGCPtr<Object>> internal_construct(MarkedVector<Value> arguments_list, FunctionObject& new_target) override;
  23. // Used for [[Call]] / [[Construct]]'s "...result of evaluating F in a manner that conforms to the specification of F".
  24. // Needs to be overridden by all NativeFunctions without an m_native_function.
  25. virtual ThrowCompletionOr<Value> call();
  26. virtual ThrowCompletionOr<NonnullGCPtr<Object>> construct(FunctionObject& new_target);
  27. virtual DeprecatedFlyString const& name() const override { return m_name; }
  28. virtual bool is_strict_mode() const override;
  29. virtual bool has_constructor() const override { return false; }
  30. virtual Realm* realm() const override { return m_realm; }
  31. Optional<DeprecatedFlyString> const& initial_name() const { return m_initial_name; }
  32. void set_initial_name(Badge<FunctionObject>, DeprecatedFlyString initial_name) { m_initial_name = move(initial_name); }
  33. protected:
  34. NativeFunction(DeprecatedFlyString name, Object& prototype);
  35. NativeFunction(JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object* prototype, Realm& realm);
  36. NativeFunction(DeprecatedFlyString name, JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object& prototype);
  37. explicit NativeFunction(Object& prototype);
  38. virtual void visit_edges(Cell::Visitor& visitor) override
  39. {
  40. Base::visit_edges(visitor);
  41. visitor.visit(m_native_function);
  42. }
  43. private:
  44. virtual bool is_native_function() const final { return true; }
  45. DeprecatedFlyString m_name;
  46. Optional<DeprecatedFlyString> m_initial_name; // [[InitialName]]
  47. JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>> m_native_function;
  48. GCPtr<Realm> m_realm;
  49. };
  50. template<>
  51. inline bool Object::fast_is<NativeFunction>() const { return is_native_function(); }
  52. }