Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* Public declarations for xptcall. */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef xptcall_h___ |
michael@0 | 9 | #define xptcall_h___ |
michael@0 | 10 | |
michael@0 | 11 | #include "nscore.h" |
michael@0 | 12 | #include "nsISupports.h" |
michael@0 | 13 | #include "xpt_struct.h" |
michael@0 | 14 | #include "xptinfo.h" |
michael@0 | 15 | #include "js/Value.h" |
michael@0 | 16 | |
michael@0 | 17 | struct nsXPTCMiniVariant |
michael@0 | 18 | { |
michael@0 | 19 | // No ctors or dtors so that we can use arrays of these on the stack |
michael@0 | 20 | // with no penalty. |
michael@0 | 21 | union |
michael@0 | 22 | { |
michael@0 | 23 | int8_t i8; |
michael@0 | 24 | int16_t i16; |
michael@0 | 25 | int32_t i32; |
michael@0 | 26 | int64_t i64; |
michael@0 | 27 | uint8_t u8; |
michael@0 | 28 | uint16_t u16; |
michael@0 | 29 | uint32_t u32; |
michael@0 | 30 | uint64_t u64; |
michael@0 | 31 | float f; |
michael@0 | 32 | double d; |
michael@0 | 33 | bool b; |
michael@0 | 34 | char c; |
michael@0 | 35 | char16_t wc; |
michael@0 | 36 | void* p; |
michael@0 | 37 | |
michael@0 | 38 | // Types below here are unknown to the assembly implementations, and |
michael@0 | 39 | // therefore _must_ be passed with indirect semantics. We put them in |
michael@0 | 40 | // the union here for type safety, so that we can avoid void* tricks. |
michael@0 | 41 | JS::Value j; |
michael@0 | 42 | } val; |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | struct nsXPTCVariant : public nsXPTCMiniVariant |
michael@0 | 46 | { |
michael@0 | 47 | // No ctors or dtors so that we can use arrays of these on the stack |
michael@0 | 48 | // with no penalty. |
michael@0 | 49 | |
michael@0 | 50 | // inherits 'val' here |
michael@0 | 51 | void* ptr; |
michael@0 | 52 | nsXPTType type; |
michael@0 | 53 | uint8_t flags; |
michael@0 | 54 | |
michael@0 | 55 | enum |
michael@0 | 56 | { |
michael@0 | 57 | // |
michael@0 | 58 | // Bitflag definitions |
michael@0 | 59 | // |
michael@0 | 60 | |
michael@0 | 61 | // Indicates that ptr (above, and distinct from val.p) is the value that |
michael@0 | 62 | // should be passed on the stack. |
michael@0 | 63 | // |
michael@0 | 64 | // In theory, ptr could point anywhere. But in practice it always points |
michael@0 | 65 | // to &val. So this flag is used to pass 'val' by reference, letting us |
michael@0 | 66 | // avoid the extra allocation we would incur if we were to use val.p. |
michael@0 | 67 | // |
michael@0 | 68 | // Various parts of XPConnect assume that ptr==&val, so we enforce it |
michael@0 | 69 | // explicitly with SetIndirect() and IsIndirect(). |
michael@0 | 70 | // |
michael@0 | 71 | // Since ptr always points to &val, the semantics of this flag are kind of |
michael@0 | 72 | // dumb, since the ptr field is unnecessary. But changing them would |
michael@0 | 73 | // require changing dozens of assembly files, so they're likely to stay |
michael@0 | 74 | // the way they are. |
michael@0 | 75 | PTR_IS_DATA = 0x1, |
michael@0 | 76 | |
michael@0 | 77 | // Indicates that the value we hold requires some sort of cleanup (memory |
michael@0 | 78 | // deallocation, interface release, JS::Value unrooting, etc). The precise |
michael@0 | 79 | // cleanup that is performed depends on the 'type' field above. |
michael@0 | 80 | // If the value is an array, this flag specifies whether the elements |
michael@0 | 81 | // within the array require cleanup (we always clean up the array itself, |
michael@0 | 82 | // so this flag would be redundant for that purpose). |
michael@0 | 83 | VAL_NEEDS_CLEANUP = 0x2 |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | void ClearFlags() {flags = 0;} |
michael@0 | 87 | void SetIndirect() {ptr = &val; flags |= PTR_IS_DATA;} |
michael@0 | 88 | void SetValNeedsCleanup() {flags |= VAL_NEEDS_CLEANUP;} |
michael@0 | 89 | |
michael@0 | 90 | bool IsIndirect() const {return 0 != (flags & PTR_IS_DATA);} |
michael@0 | 91 | bool DoesValNeedCleanup() const {return 0 != (flags & VAL_NEEDS_CLEANUP);} |
michael@0 | 92 | |
michael@0 | 93 | // Internal use only. Use IsIndirect() instead. |
michael@0 | 94 | bool IsPtrData() const {return 0 != (flags & PTR_IS_DATA);} |
michael@0 | 95 | |
michael@0 | 96 | void Init(const nsXPTCMiniVariant& mv, const nsXPTType& t, uint8_t f) |
michael@0 | 97 | { |
michael@0 | 98 | type = t; |
michael@0 | 99 | flags = f; |
michael@0 | 100 | |
michael@0 | 101 | if(f & PTR_IS_DATA) |
michael@0 | 102 | { |
michael@0 | 103 | ptr = mv.val.p; |
michael@0 | 104 | val.p = nullptr; |
michael@0 | 105 | } |
michael@0 | 106 | else |
michael@0 | 107 | { |
michael@0 | 108 | ptr = nullptr; |
michael@0 | 109 | val.p = nullptr; // make sure 'val.p' is always initialized |
michael@0 | 110 | switch(t.TagPart()) { |
michael@0 | 111 | case nsXPTType::T_I8: val.i8 = mv.val.i8; break; |
michael@0 | 112 | case nsXPTType::T_I16: val.i16 = mv.val.i16; break; |
michael@0 | 113 | case nsXPTType::T_I32: val.i32 = mv.val.i32; break; |
michael@0 | 114 | case nsXPTType::T_I64: val.i64 = mv.val.i64; break; |
michael@0 | 115 | case nsXPTType::T_U8: val.u8 = mv.val.u8; break; |
michael@0 | 116 | case nsXPTType::T_U16: val.u16 = mv.val.u16; break; |
michael@0 | 117 | case nsXPTType::T_U32: val.u32 = mv.val.u32; break; |
michael@0 | 118 | case nsXPTType::T_U64: val.u64 = mv.val.u64; break; |
michael@0 | 119 | case nsXPTType::T_FLOAT: val.f = mv.val.f; break; |
michael@0 | 120 | case nsXPTType::T_DOUBLE: val.d = mv.val.d; break; |
michael@0 | 121 | case nsXPTType::T_BOOL: val.b = mv.val.b; break; |
michael@0 | 122 | case nsXPTType::T_CHAR: val.c = mv.val.c; break; |
michael@0 | 123 | case nsXPTType::T_WCHAR: val.wc = mv.val.wc; break; |
michael@0 | 124 | case nsXPTType::T_VOID: /* fall through */ |
michael@0 | 125 | case nsXPTType::T_IID: /* fall through */ |
michael@0 | 126 | case nsXPTType::T_DOMSTRING: /* fall through */ |
michael@0 | 127 | case nsXPTType::T_CHAR_STR: /* fall through */ |
michael@0 | 128 | case nsXPTType::T_WCHAR_STR: /* fall through */ |
michael@0 | 129 | case nsXPTType::T_INTERFACE: /* fall through */ |
michael@0 | 130 | case nsXPTType::T_INTERFACE_IS: /* fall through */ |
michael@0 | 131 | case nsXPTType::T_ARRAY: /* fall through */ |
michael@0 | 132 | case nsXPTType::T_PSTRING_SIZE_IS: /* fall through */ |
michael@0 | 133 | case nsXPTType::T_PWSTRING_SIZE_IS: /* fall through */ |
michael@0 | 134 | case nsXPTType::T_UTF8STRING: /* fall through */ |
michael@0 | 135 | case nsXPTType::T_CSTRING: /* fall through */ |
michael@0 | 136 | default: val.p = mv.val.p; break; |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | class nsIXPTCProxy : public nsISupports |
michael@0 | 143 | { |
michael@0 | 144 | public: |
michael@0 | 145 | NS_IMETHOD CallMethod(uint16_t aMethodIndex, |
michael@0 | 146 | const XPTMethodDescriptor *aInfo, |
michael@0 | 147 | nsXPTCMiniVariant *aParams) = 0; |
michael@0 | 148 | }; |
michael@0 | 149 | |
michael@0 | 150 | /** |
michael@0 | 151 | * This is a typedef to avoid confusion between the canonical |
michael@0 | 152 | * nsISupports* that provides object identity and an interface pointer |
michael@0 | 153 | * for inheriting interfaces that aren't known at compile-time. |
michael@0 | 154 | */ |
michael@0 | 155 | typedef nsISupports nsISomeInterface; |
michael@0 | 156 | |
michael@0 | 157 | /** |
michael@0 | 158 | * Get a proxy object to implement the specified interface. |
michael@0 | 159 | * |
michael@0 | 160 | * @param aIID The IID of the interface to implement. |
michael@0 | 161 | * @param aOuter An object to receive method calls from the proxy object. |
michael@0 | 162 | * The stub forwards QueryInterface/AddRef/Release to the |
michael@0 | 163 | * outer object. The proxy object does not hold a reference to |
michael@0 | 164 | * the outer object; it is the caller's responsibility to |
michael@0 | 165 | * ensure that this pointer remains valid until the stub has |
michael@0 | 166 | * been destroyed. |
michael@0 | 167 | * @param aStub Out parameter for the new proxy object. The object is |
michael@0 | 168 | * not addrefed. The object never destroys itself. It must be |
michael@0 | 169 | * explicitly destroyed by calling |
michael@0 | 170 | * NS_DestroyXPTCallStub when it is no longer needed. |
michael@0 | 171 | */ |
michael@0 | 172 | XPCOM_API(nsresult) |
michael@0 | 173 | NS_GetXPTCallStub(REFNSIID aIID, nsIXPTCProxy* aOuter, |
michael@0 | 174 | nsISomeInterface* *aStub); |
michael@0 | 175 | |
michael@0 | 176 | /** |
michael@0 | 177 | * Destroys an XPTCall stub previously created with NS_GetXPTCallStub. |
michael@0 | 178 | */ |
michael@0 | 179 | XPCOM_API(void) |
michael@0 | 180 | NS_DestroyXPTCallStub(nsISomeInterface* aStub); |
michael@0 | 181 | |
michael@0 | 182 | XPCOM_API(nsresult) |
michael@0 | 183 | NS_InvokeByIndex(nsISupports* that, uint32_t methodIndex, |
michael@0 | 184 | uint32_t paramCount, nsXPTCVariant* params); |
michael@0 | 185 | |
michael@0 | 186 | #endif /* xptcall_h___ */ |