Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/reflect/xptcall/xptcall.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
/* Public declarations for xptcall. */
7
8
#ifndef xptcall_h___
9
#define xptcall_h___
10
11
#include "nscore.h"
12
#include "nsISupports.h"
13
#include "xptinfo.h"
14
#include "js/Value.h"
15
#include "mozilla/Attributes.h"
16
#include "mozilla/MemoryReporting.h"
17
18
struct nsXPTCMiniVariant
19
{
20
// No ctors or dtors so that we can use arrays of these on the stack
21
// with no penalty.
22
    union Union
23
    {
24
        int8_t    i8;
25
        int16_t   i16;
26
        int32_t   i32;
27
        int64_t   i64;
28
        uint8_t   u8;
29
        uint16_t  u16;
30
        uint32_t  u32;
31
        uint64_t  u64;
32
        float     f;
33
        double    d;
34
        bool      b;
35
        char      c;
36
        char16_t wc;
37
        void*     p;
38
    };
39
40
    Union val;
41
};
42
43
static_assert(offsetof(nsXPTCMiniVariant, val) == 0,
44
              "nsXPTCMiniVariant must be a thin wrapper");
45
46
struct nsXPTCVariant
47
{
48
    union ExtendedVal
49
    {
50
    // ExtendedVal is an extension on nsXPTCMiniVariant. It contains types
51
    // unknown to the assembly implementations which must be passed by indirect
52
    // semantics.
53
    //
54
    // nsXPTCVariant contains enough space to store ExtendedVal inline, which
55
    // can be used to store these types when IsIndirect() is true.
56
        nsXPTCMiniVariant mini;
57
58
        nsCString  nscstr;
59
        nsString   nsstr;
60
        JS::Value  jsval;
61
        xpt::detail::UntypedTArray array;
62
63
        // This type contains non-standard-layout types, so needs an explicit
64
        // Ctor/Dtor - we'll just delete them.
65
        ExtendedVal() = delete;
66
        ~ExtendedVal() = delete;
67
    };
68
69
    union
70
    {
71
        // The `val` field from nsXPTCMiniVariant.
72
        nsXPTCMiniVariant::Union val;
73
74
        // Storage for any extended variants.
75
        ExtendedVal ext;
76
    };
77
78
    nsXPTType type;
79
    uint8_t   flags;
80
81
    // Clear to a valid, null state.
82
    nsXPTCVariant() {
83
        memset(this, 0, sizeof(nsXPTCVariant));
84
        type = nsXPTType::T_VOID;
85
    }
86
87
    enum
88
    {
89
        //
90
        // Bitflag definitions
91
        //
92
93
        // Indicates that we &val.p should be passed n the stack, i.e. that
94
        // val should be passed by reference.
95
        IS_INDIRECT    = 0x1,
96
    };
97
98
0
    void ClearFlags()         {flags = 0;}
99
    void SetIndirect()        {flags |= IS_INDIRECT;}
100
101
43.8M
    bool IsIndirect()         const  {return 0 != (flags & IS_INDIRECT);}
102
103
    // Implicitly convert to nsXPTCMiniVariant.
104
0
    operator nsXPTCMiniVariant&() {
105
0
        return *(nsXPTCMiniVariant*) &val;
106
0
    }
107
0
    operator const nsXPTCMiniVariant&() const {
108
0
        return *(const nsXPTCMiniVariant*) &val;
109
0
    }
110
111
    // As this type contains an anonymous union, we need to provide an explicit
112
    // destructor.
113
    ~nsXPTCVariant() { }
114
};
115
116
static_assert(offsetof(nsXPTCVariant, val) == offsetof(nsXPTCVariant, ext),
117
              "nsXPTCVariant::{ext,val} must have matching offsets");
118
119
// static_assert that nsXPTCVariant::ExtendedVal is large enough and
120
// well-aligned enough for every XPT-supported type.
121
#define XPT_CHECK_SIZEOF(xpt, type) \
122
    static_assert(sizeof(nsXPTCVariant::ExtendedVal) >= sizeof(type), \
123
                  "nsXPTCVariant::ext not big enough for " #xpt " (" #type ")"); \
124
    static_assert(MOZ_ALIGNOF(nsXPTCVariant::ExtendedVal) >= MOZ_ALIGNOF(type), \
125
                  "nsXPTCVariant::ext not aligned enough for " #xpt " (" #type ")");
126
XPT_FOR_EACH_TYPE(XPT_CHECK_SIZEOF)
127
#undef XPT_CHECK_SIZEOF
128
129
class nsIXPTCProxy : public nsISupports
130
{
131
public:
132
    NS_IMETHOD CallMethod(uint16_t aMethodIndex,
133
                          const nsXPTMethodInfo *aInfo,
134
                          nsXPTCMiniVariant *aParams) = 0;
135
};
136
137
/**
138
 * This is a typedef to avoid confusion between the canonical
139
 * nsISupports* that provides object identity and an interface pointer
140
 * for inheriting interfaces that aren't known at compile-time.
141
 */
142
typedef nsISupports nsISomeInterface;
143
144
/**
145
 * Get a proxy object to implement the specified interface.
146
 *
147
 * @param aIID    The IID of the interface to implement.
148
 * @param aOuter  An object to receive method calls from the proxy object.
149
 *                The stub forwards QueryInterface/AddRef/Release to the
150
 *                outer object. The proxy object does not hold a reference to
151
 *                the outer object; it is the caller's responsibility to
152
 *                ensure that this pointer remains valid until the stub has
153
 *                been destroyed.
154
 * @param aStub   Out parameter for the new proxy object. The object is
155
 *                not addrefed. The object never destroys itself. It must be
156
 *                explicitly destroyed by calling
157
 *                NS_DestroyXPTCallStub when it is no longer needed.
158
 */
159
XPCOM_API(nsresult)
160
NS_GetXPTCallStub(REFNSIID aIID, nsIXPTCProxy* aOuter,
161
                  nsISomeInterface* *aStub);
162
163
/**
164
 * Destroys an XPTCall stub previously created with NS_GetXPTCallStub.
165
 */
166
XPCOM_API(void)
167
NS_DestroyXPTCallStub(nsISomeInterface* aStub);
168
169
/**
170
 * Measures the size of an XPTCall stub previously created with NS_GetXPTCallStub.
171
 */
172
XPCOM_API(size_t)
173
NS_SizeOfIncludingThisXPTCallStub(const nsISomeInterface* aStub, mozilla::MallocSizeOf aMallocSizeOf);
174
175
// this is extern "C" because on some platforms it is implemented in assembly
176
extern "C" nsresult
177
NS_InvokeByIndex(nsISupports* that, uint32_t methodIndex,
178
                 uint32_t paramCount, nsXPTCVariant* params);
179
180
#endif /* xptcall_h___ */