Coverage Report

Created: 2026-09-14 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibJS/Runtime/NativeFunction.h
Line
Count
Source
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
8
#pragma once
9
10
#include <AK/Badge.h>
11
#include <AK/Optional.h>
12
#include <LibJS/Heap/HeapFunction.h>
13
#include <LibJS/Runtime/Completion.h>
14
#include <LibJS/Runtime/FunctionObject.h>
15
#include <LibJS/Runtime/PropertyKey.h>
16
17
namespace JS {
18
19
class NativeFunction : public FunctionObject {
20
    JS_OBJECT(NativeFunction, FunctionObject);
21
    JS_DECLARE_ALLOCATOR(NativeFunction);
22
23
public:
24
    static NonnullGCPtr<NativeFunction> create(Realm&, ESCAPING Function<ThrowCompletionOr<Value>(VM&)> behaviour, i32 length, PropertyKey const& name, Optional<Realm*> = {}, Optional<Object*> prototype = {}, Optional<StringView> const& prefix = {});
25
    static NonnullGCPtr<NativeFunction> create(Realm&, DeprecatedFlyString const& name, ESCAPING Function<ThrowCompletionOr<Value>(VM&)>);
26
27
11.2k
    virtual ~NativeFunction() override = default;
28
29
    virtual ThrowCompletionOr<Value> internal_call(Value this_argument, ReadonlySpan<Value> arguments_list) override;
30
    virtual ThrowCompletionOr<NonnullGCPtr<Object>> internal_construct(ReadonlySpan<Value> arguments_list, FunctionObject& new_target) override;
31
32
    // Used for [[Call]] / [[Construct]]'s "...result of evaluating F in a manner that conforms to the specification of F".
33
    // Needs to be overridden by all NativeFunctions without an m_native_function.
34
    virtual ThrowCompletionOr<Value> call();
35
    virtual ThrowCompletionOr<NonnullGCPtr<Object>> construct(FunctionObject& new_target);
36
37
0
    virtual DeprecatedFlyString const& name() const override { return m_name; }
38
    virtual bool is_strict_mode() const override;
39
0
    virtual bool has_constructor() const override { return false; }
40
0
    virtual Realm* realm() const override { return m_realm; }
41
42
0
    Optional<DeprecatedFlyString> const& initial_name() const { return m_initial_name; }
43
11.1k
    void set_initial_name(Badge<FunctionObject>, DeprecatedFlyString initial_name) { m_initial_name = move(initial_name); }
44
45
protected:
46
    NativeFunction(DeprecatedFlyString name, Object& prototype);
47
    NativeFunction(JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object* prototype, Realm& realm);
48
    NativeFunction(DeprecatedFlyString name, JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>>, Object& prototype);
49
    explicit NativeFunction(Object& prototype);
50
51
    virtual void initialize(Realm&) override;
52
    virtual void visit_edges(Cell::Visitor& visitor) override;
53
54
private:
55
11.1k
    virtual bool is_native_function() const final { return true; }
56
57
    DeprecatedFlyString m_name;
58
    GCPtr<PrimitiveString> m_name_string;
59
    Optional<DeprecatedFlyString> m_initial_name; // [[InitialName]]
60
    JS::GCPtr<JS::HeapFunction<ThrowCompletionOr<Value>(VM&)>> m_native_function;
61
    GCPtr<Realm> m_realm;
62
};
63
64
template<>
65
11.1k
inline bool Object::fast_is<NativeFunction>() const { return is_native_function(); }
66
67
}