Back to index

lightning-sunbird  0.9+nobinonly
xptcstubs.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* Implement shared vtbl methods. */
00039 
00040 #include "xptcprivate.h"
00041 
00042 #ifndef WIN32
00043 #error "This code is for Win32 only"
00044 #endif
00045 
00046 extern "C" {
00047 
00048 static nsresult __stdcall
00049 PrepareAndDispatch(nsXPTCStubBase* self, PRUint32 methodIndex,
00050                    PRUint32* args, PRUint32* stackBytesToPop)
00051 {
00052 #define PARAM_BUFFER_COUNT     16
00053 
00054     nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT];
00055     nsXPTCMiniVariant* dispatchParams = NULL;
00056     nsIInterfaceInfo* iface_info = NULL;
00057     const nsXPTMethodInfo* info = NULL;
00058     PRUint8 paramCount;
00059     PRUint8 i;
00060     nsresult result = NS_ERROR_FAILURE;
00061 
00062     // If anything fails before stackBytesToPop can be set then
00063     // the failure is completely catastrophic!
00064 
00065     NS_ASSERTION(self,"no self");
00066 
00067     self->GetInterfaceInfo(&iface_info);
00068     NS_ASSERTION(iface_info,"no interface info");
00069 
00070     iface_info->GetMethodInfo(PRUint16(methodIndex), &info);
00071     NS_ASSERTION(info,"no interface info");
00072 
00073     paramCount = info->GetParamCount();
00074 
00075     // setup variant array pointer
00076     if(paramCount > PARAM_BUFFER_COUNT)
00077         dispatchParams = new nsXPTCMiniVariant[paramCount];
00078     else
00079         dispatchParams = paramBuffer;
00080     NS_ASSERTION(dispatchParams,"no place for params");
00081 
00082     PRUint32* ap = args;
00083     for(i = 0; i < paramCount; i++, ap++)
00084     {
00085         const nsXPTParamInfo& param = info->GetParam(i);
00086         const nsXPTType& type = param.GetType();
00087         nsXPTCMiniVariant* dp = &dispatchParams[i];
00088 
00089         if(param.IsOut() || !type.IsArithmetic())
00090         {
00091             dp->val.p = (void*) *ap;
00092             continue;
00093         }
00094         // else
00095         switch(type)
00096         {
00097         case nsXPTType::T_I8     : dp->val.i8  = *((PRInt8*)  ap);       break;
00098         case nsXPTType::T_I16    : dp->val.i16 = *((PRInt16*) ap);       break;
00099         case nsXPTType::T_I32    : dp->val.i32 = *((PRInt32*) ap);       break;
00100         case nsXPTType::T_I64    : dp->val.i64 = *((PRInt64*) ap); ap++; break;
00101         case nsXPTType::T_U8     : dp->val.u8  = *((PRUint8*) ap);       break;
00102         case nsXPTType::T_U16    : dp->val.u16 = *((PRUint16*)ap);       break;
00103         case nsXPTType::T_U32    : dp->val.u32 = *((PRUint32*)ap);       break;
00104         case nsXPTType::T_U64    : dp->val.u64 = *((PRUint64*)ap); ap++; break;
00105         case nsXPTType::T_FLOAT  : dp->val.f   = *((float*)   ap);       break;
00106         case nsXPTType::T_DOUBLE : dp->val.d   = *((double*)  ap); ap++; break;
00107         case nsXPTType::T_BOOL   : dp->val.b   = *((PRBool*)  ap);       break;
00108         case nsXPTType::T_CHAR   : dp->val.c   = *((char*)    ap);       break;
00109         case nsXPTType::T_WCHAR  : dp->val.wc  = *((wchar_t*) ap);       break;
00110         default:
00111             NS_ASSERTION(0, "bad type");
00112             break;
00113         }
00114     }
00115     *stackBytesToPop = ((PRUint32)ap) - ((PRUint32)args);
00116 
00117     result = self->CallMethod((PRUint16)methodIndex, info, dispatchParams);
00118 
00119     NS_RELEASE(iface_info);
00120 
00121     if(dispatchParams != paramBuffer)
00122         delete [] dispatchParams;
00123 
00124     return result;
00125 }
00126 
00127 } // extern "C"
00128 
00129 // declspec(naked) is broken in gcc
00130 #ifndef __GNUC__
00131 static 
00132 __declspec(naked)
00133 void SharedStub(void)
00134 {
00135     __asm {
00136         push ebp            // set up simple stack frame
00137         mov  ebp, esp       // stack has: ebp/vtbl_index/retaddr/this/args
00138         push ecx            // make room for a ptr
00139         lea  eax, [ebp-4]   // pointer to stackBytesToPop
00140         push eax
00141         lea  eax, [ebp+12]  // pointer to args
00142         push eax
00143         push ecx            // vtbl_index
00144         mov  eax, [ebp+8]   // this
00145         push eax
00146         call PrepareAndDispatch
00147         mov  edx, [ebp+4]   // return address
00148         mov  ecx, [ebp-4]   // stackBytesToPop
00149         add  ecx, 8         // for 'this' and return address
00150         mov  esp, ebp
00151         pop  ebp
00152         add  esp, ecx       // fix up stack pointer
00153         jmp  edx            // simulate __stdcall return
00154     }
00155 }
00156 
00157 // these macros get expanded (many times) in the file #included below
00158 #define STUB_ENTRY(n) \
00159 __declspec(naked) nsresult __stdcall nsXPTCStubBase::Stub##n() \
00160 { __asm mov ecx, n __asm jmp SharedStub }
00161 
00162 #else
00163 
00164 #define STUB_ENTRY(n) \
00165 nsresult __stdcall nsXPTCStubBase::Stub##n() \
00166 { \
00167   PRUint32 *args, stackBytesToPop = 0; \
00168   nsresult result = 0; \
00169   nsXPTCStubBase *obj; \
00170   __asm__ __volatile__ ( \
00171     "leal   0x0c(%%ebp), %%ecx\n\t"    /* args */ \
00172     "movl   0x08(%%ebp), %%edx\n\t"    /* this */ \
00173     : "=c" (args), \
00174       "=d" (obj)); \
00175   result = PrepareAndDispatch(obj, n, args, &stackBytesToPop); \
00176   return result; \
00177 }   
00178 
00179 #endif /* __GNUC__ */
00180 
00181 #define SENTINEL_ENTRY(n) \
00182 nsresult __stdcall nsXPTCStubBase::Sentinel##n() \
00183 { \
00184     NS_ASSERTION(0,"nsXPTCStubBase::Sentinel called"); \
00185     return NS_ERROR_NOT_IMPLEMENTED; \
00186 }
00187 
00188 #ifdef _MSC_VER
00189 #pragma warning(disable : 4035) // OK to have no return value
00190 #endif
00191 #include "xptcstubsdef.inc"
00192 #ifdef _MSC_VER
00193 #pragma warning(default : 4035) // restore default
00194 #endif
00195 
00196 void
00197 #ifdef __GNUC__
00198 __cdecl
00199 #endif
00200 xptc_dummy()
00201 {
00202 }