michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Public declarations for xptcall. */ michael@0: michael@0: #ifndef xptcall_h___ michael@0: #define xptcall_h___ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsISupports.h" michael@0: #include "xpt_struct.h" michael@0: #include "xptinfo.h" michael@0: #include "js/Value.h" michael@0: michael@0: struct nsXPTCMiniVariant michael@0: { michael@0: // No ctors or dtors so that we can use arrays of these on the stack michael@0: // with no penalty. michael@0: union michael@0: { michael@0: int8_t i8; michael@0: int16_t i16; michael@0: int32_t i32; michael@0: int64_t i64; michael@0: uint8_t u8; michael@0: uint16_t u16; michael@0: uint32_t u32; michael@0: uint64_t u64; michael@0: float f; michael@0: double d; michael@0: bool b; michael@0: char c; michael@0: char16_t wc; michael@0: void* p; michael@0: michael@0: // Types below here are unknown to the assembly implementations, and michael@0: // therefore _must_ be passed with indirect semantics. We put them in michael@0: // the union here for type safety, so that we can avoid void* tricks. michael@0: JS::Value j; michael@0: } val; michael@0: }; michael@0: michael@0: struct nsXPTCVariant : public nsXPTCMiniVariant michael@0: { michael@0: // No ctors or dtors so that we can use arrays of these on the stack michael@0: // with no penalty. michael@0: michael@0: // inherits 'val' here michael@0: void* ptr; michael@0: nsXPTType type; michael@0: uint8_t flags; michael@0: michael@0: enum michael@0: { michael@0: // michael@0: // Bitflag definitions michael@0: // michael@0: michael@0: // Indicates that ptr (above, and distinct from val.p) is the value that michael@0: // should be passed on the stack. michael@0: // michael@0: // In theory, ptr could point anywhere. But in practice it always points michael@0: // to &val. So this flag is used to pass 'val' by reference, letting us michael@0: // avoid the extra allocation we would incur if we were to use val.p. michael@0: // michael@0: // Various parts of XPConnect assume that ptr==&val, so we enforce it michael@0: // explicitly with SetIndirect() and IsIndirect(). michael@0: // michael@0: // Since ptr always points to &val, the semantics of this flag are kind of michael@0: // dumb, since the ptr field is unnecessary. But changing them would michael@0: // require changing dozens of assembly files, so they're likely to stay michael@0: // the way they are. michael@0: PTR_IS_DATA = 0x1, michael@0: michael@0: // Indicates that the value we hold requires some sort of cleanup (memory michael@0: // deallocation, interface release, JS::Value unrooting, etc). The precise michael@0: // cleanup that is performed depends on the 'type' field above. michael@0: // If the value is an array, this flag specifies whether the elements michael@0: // within the array require cleanup (we always clean up the array itself, michael@0: // so this flag would be redundant for that purpose). michael@0: VAL_NEEDS_CLEANUP = 0x2 michael@0: }; michael@0: michael@0: void ClearFlags() {flags = 0;} michael@0: void SetIndirect() {ptr = &val; flags |= PTR_IS_DATA;} michael@0: void SetValNeedsCleanup() {flags |= VAL_NEEDS_CLEANUP;} michael@0: michael@0: bool IsIndirect() const {return 0 != (flags & PTR_IS_DATA);} michael@0: bool DoesValNeedCleanup() const {return 0 != (flags & VAL_NEEDS_CLEANUP);} michael@0: michael@0: // Internal use only. Use IsIndirect() instead. michael@0: bool IsPtrData() const {return 0 != (flags & PTR_IS_DATA);} michael@0: michael@0: void Init(const nsXPTCMiniVariant& mv, const nsXPTType& t, uint8_t f) michael@0: { michael@0: type = t; michael@0: flags = f; michael@0: michael@0: if(f & PTR_IS_DATA) michael@0: { michael@0: ptr = mv.val.p; michael@0: val.p = nullptr; michael@0: } michael@0: else michael@0: { michael@0: ptr = nullptr; michael@0: val.p = nullptr; // make sure 'val.p' is always initialized michael@0: switch(t.TagPart()) { michael@0: case nsXPTType::T_I8: val.i8 = mv.val.i8; break; michael@0: case nsXPTType::T_I16: val.i16 = mv.val.i16; break; michael@0: case nsXPTType::T_I32: val.i32 = mv.val.i32; break; michael@0: case nsXPTType::T_I64: val.i64 = mv.val.i64; break; michael@0: case nsXPTType::T_U8: val.u8 = mv.val.u8; break; michael@0: case nsXPTType::T_U16: val.u16 = mv.val.u16; break; michael@0: case nsXPTType::T_U32: val.u32 = mv.val.u32; break; michael@0: case nsXPTType::T_U64: val.u64 = mv.val.u64; break; michael@0: case nsXPTType::T_FLOAT: val.f = mv.val.f; break; michael@0: case nsXPTType::T_DOUBLE: val.d = mv.val.d; break; michael@0: case nsXPTType::T_BOOL: val.b = mv.val.b; break; michael@0: case nsXPTType::T_CHAR: val.c = mv.val.c; break; michael@0: case nsXPTType::T_WCHAR: val.wc = mv.val.wc; break; michael@0: case nsXPTType::T_VOID: /* fall through */ michael@0: case nsXPTType::T_IID: /* fall through */ michael@0: case nsXPTType::T_DOMSTRING: /* fall through */ michael@0: case nsXPTType::T_CHAR_STR: /* fall through */ michael@0: case nsXPTType::T_WCHAR_STR: /* fall through */ michael@0: case nsXPTType::T_INTERFACE: /* fall through */ michael@0: case nsXPTType::T_INTERFACE_IS: /* fall through */ michael@0: case nsXPTType::T_ARRAY: /* fall through */ michael@0: case nsXPTType::T_PSTRING_SIZE_IS: /* fall through */ michael@0: case nsXPTType::T_PWSTRING_SIZE_IS: /* fall through */ michael@0: case nsXPTType::T_UTF8STRING: /* fall through */ michael@0: case nsXPTType::T_CSTRING: /* fall through */ michael@0: default: val.p = mv.val.p; break; michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: class nsIXPTCProxy : public nsISupports michael@0: { michael@0: public: michael@0: NS_IMETHOD CallMethod(uint16_t aMethodIndex, michael@0: const XPTMethodDescriptor *aInfo, michael@0: nsXPTCMiniVariant *aParams) = 0; michael@0: }; michael@0: michael@0: /** michael@0: * This is a typedef to avoid confusion between the canonical michael@0: * nsISupports* that provides object identity and an interface pointer michael@0: * for inheriting interfaces that aren't known at compile-time. michael@0: */ michael@0: typedef nsISupports nsISomeInterface; michael@0: michael@0: /** michael@0: * Get a proxy object to implement the specified interface. michael@0: * michael@0: * @param aIID The IID of the interface to implement. michael@0: * @param aOuter An object to receive method calls from the proxy object. michael@0: * The stub forwards QueryInterface/AddRef/Release to the michael@0: * outer object. The proxy object does not hold a reference to michael@0: * the outer object; it is the caller's responsibility to michael@0: * ensure that this pointer remains valid until the stub has michael@0: * been destroyed. michael@0: * @param aStub Out parameter for the new proxy object. The object is michael@0: * not addrefed. The object never destroys itself. It must be michael@0: * explicitly destroyed by calling michael@0: * NS_DestroyXPTCallStub when it is no longer needed. michael@0: */ michael@0: XPCOM_API(nsresult) michael@0: NS_GetXPTCallStub(REFNSIID aIID, nsIXPTCProxy* aOuter, michael@0: nsISomeInterface* *aStub); michael@0: michael@0: /** michael@0: * Destroys an XPTCall stub previously created with NS_GetXPTCallStub. michael@0: */ michael@0: XPCOM_API(void) michael@0: NS_DestroyXPTCallStub(nsISomeInterface* aStub); michael@0: michael@0: XPCOM_API(nsresult) michael@0: NS_InvokeByIndex(nsISupports* that, uint32_t methodIndex, michael@0: uint32_t paramCount, nsXPTCVariant* params); michael@0: michael@0: #endif /* xptcall_h___ */