NativeFunction.h 2.7 KB

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