ILBool _IL_Object_Equals(ILExecThread *thread, ILObject *_this, ILObject *obj) { ILClass *classInfo; ILUInt32 size; /* Handle the easy cases first */ if(_this == obj) { return 1; } else if(!obj) { return 0; } /* Check to see if both are value types with the same type */ classInfo = GetObjectClass(_this); if(classInfo != GetObjectClass(obj) || !ILClassIsValueType(classInfo)) { return 0; } /* Perform a bitwise comparison on the values */ size = ILSizeOfType(thread, ILClassToType(classInfo)); if(!size) { return 1; } else { return !ILMemCmp((void *)_this, (void *)obj, size); } }
/* * Do the real delegate invokation. */ static void _DelegateInvoke(ILDelegateInvokeParams *params) { ILMethod *method; ILType *type; ILUInt32 size; PackDelegateUserData userData; /* If this is a multicast delegate, then execute "prev" first */ if(params->delegate->prev) { ILDelegateInvokeParams prevParams; prevParams.thread = params->thread; prevParams.cif = params->cif; prevParams.result = params->result; prevParams.args = params->args; prevParams.delegate = (System_Delegate *)(params->delegate->prev); ; _DelegateInvoke(&prevParams); if(_ILExecThreadHasException(params->thread)) { return; } } /* Extract the method from the delegate */ method = params->delegate->methodInfo; if(!method) { ILExecThreadThrowSystem(params->thread, "System.MissingMethodException", (const char *)0); return; } /* Call the method */ userData.args = params->args; userData.pinvokeInfo = (ILMethod *)ILTypeGetDelegateMethod (ILType_FromClass(GetObjectClass(params->delegate))); userData.needThis = 0; if(_ILCallMethod(params->thread, method, UnpackDelegateResult, params->result, 0, params->delegate->target, PackDelegateParams, &userData)) { /* An exception occurred, which is already stored in the thread */ type = ILMethod_Signature(method); type = ILTypeGetEnumType(ILTypeGetReturn(type)); if(type != ILType_Void) { /* Clear the native return value, because we cannot assume that the native caller knows how to handle exceptions */ size = ILSizeOfType(params->thread, type); ILMemZero(params->result, size); } } }
/* * Unpack the result of a delegate closure call. */ static void UnpackDelegateResult(ILExecThread *thread, ILMethod *method, int isCtor, void *result, void *userData) { ILMethod *pinvokeInfo = ((PackDelegateUserData *)userData)->pinvokeInfo; ILType *signature = ILMethod_Signature(method); ILType *paramType; ILUInt32 size, sizeInWords; ILNativeFloat tempFloat; ILUInt32 marshalType; char *customName; int customNameLen; char *customCookie; int customCookieLen; /* Marshal return types that need special handling */ marshalType = ILPInvokeGetMarshalType (0, pinvokeInfo, 0, &customName, &customNameLen, &customCookie, &customCookieLen, ILTypeGetReturn(signature)); if(marshalType != IL_META_MARSHAL_DIRECT) { switch(marshalType) { case IL_META_MARSHAL_ANSI_STRING: { /* Marshal an ANSI string back to the native world */ *((char **)result) = ILStringToAnsi (thread, (ILString *)(thread->stackTop[-1].ptrValue)); --(thread->stackTop); } return; case IL_META_MARSHAL_UTF8_STRING: { /* Marshal a UTF-8 string back to the native world */ *((char **)result) = ILStringToUTF8 (thread, (ILString *)(thread->stackTop[-1].ptrValue)); --(thread->stackTop); } return; case IL_META_MARSHAL_UTF16_STRING: { /* Marshal a UTF-16 string back to the native world */ *((ILUInt16 **)result) = ILStringToUTF16 (thread, (ILString *)(thread->stackTop[-1].ptrValue)); --(thread->stackTop); } return; case IL_META_MARSHAL_FNPTR: { /* Convert a delegate into a function closure pointer */ *((void **)result) = _ILDelegateGetClosure (thread, (ILObject *)(thread->stackTop[-1].ptrValue)); --(thread->stackTop); } return; case IL_META_MARSHAL_ARRAY: { /* Convert an array into a pointer to its first member */ void *array = thread->stackTop[-1].ptrValue; --(thread->stackTop); if(array) { *((void **)result) = ArrayToBuffer(array); } else { *((void **)result) = 0; } } return; case IL_META_MARSHAL_CUSTOM: { /* Marshal a custom value to the native world */ *((void **)result) = _ILObjectToCustom (thread, (ILObject *)(thread->stackTop[-1].ptrValue), customName, customNameLen, customCookie, customCookieLen); --(thread->stackTop); } return; } } /* Copy the return value into place */ paramType = ILTypeGetEnumType(ILTypeGetReturn(signature)); if(ILType_IsPrimitive(paramType)) { /* Process a primitive value */ switch(ILType_ToElement(paramType)) { case IL_META_ELEMTYPE_VOID: break; case IL_META_ELEMTYPE_BOOLEAN: case IL_META_ELEMTYPE_I1: case IL_META_ELEMTYPE_U1: case IL_META_ELEMTYPE_I2: case IL_META_ELEMTYPE_U2: case IL_META_ELEMTYPE_CHAR: case IL_META_ELEMTYPE_I4: case IL_META_ELEMTYPE_U4: #ifdef IL_NATIVE_INT32 case IL_META_ELEMTYPE_I: case IL_META_ELEMTYPE_U: #endif { *((ILInt32 *)result) = thread->stackTop[-1].intValue; --(thread->stackTop); } break; case IL_META_ELEMTYPE_I8: case IL_META_ELEMTYPE_U8: #ifdef IL_NATIVE_INT64 case IL_META_ELEMTYPE_I: case IL_META_ELEMTYPE_U: #endif { ILMemCpy(result, thread->stackTop - CVM_WORDS_PER_LONG, sizeof(ILInt64)); thread->stackTop -= CVM_WORDS_PER_LONG; } break; case IL_META_ELEMTYPE_R4: { ILMemCpy(&tempFloat, thread->stackTop - CVM_WORDS_PER_NATIVE_FLOAT, sizeof(ILNativeFloat)); *((ILFloat *)result) = (ILFloat)tempFloat; thread->stackTop -= CVM_WORDS_PER_NATIVE_FLOAT; } break; case IL_META_ELEMTYPE_R8: { ILMemCpy(&tempFloat, thread->stackTop - CVM_WORDS_PER_NATIVE_FLOAT, sizeof(ILNativeFloat)); *((ILDouble *)result) = (ILDouble)tempFloat; thread->stackTop -= CVM_WORDS_PER_NATIVE_FLOAT; } break; case IL_META_ELEMTYPE_R: { ILMemCpy(result, thread->stackTop - CVM_WORDS_PER_NATIVE_FLOAT, sizeof(ILNativeFloat)); thread->stackTop -= CVM_WORDS_PER_NATIVE_FLOAT; } break; case IL_META_ELEMTYPE_TYPEDBYREF: { ILMemCpy(result, thread->stackTop - CVM_WORDS_PER_TYPED_REF, sizeof(ILTypedRef)); thread->stackTop -= CVM_WORDS_PER_TYPED_REF; } break; } } else if(ILType_IsClass(paramType)) { /* Process an object reference */ *((void **)result) = thread->stackTop[-1].ptrValue; --(thread->stackTop); } else if(ILType_IsValueType(paramType)) { /* Process a value type */ size = ILSizeOfType(thread, paramType); sizeInWords = ((size + sizeof(CVMWord) - 1) / sizeof(CVMWord)); ILMemCpy(result, thread->stackTop - sizeInWords, size); thread->stackTop -= sizeInWords; } else { /* Assume that everything else is an object reference */ *((void **)result) = thread->stackTop[-1].ptrValue; --(thread->stackTop); } }
/* * Pack the parameters for a delegate closure call onto the CVM stack. */ static int PackDelegateParams(ILExecThread *thread, ILMethod *method, int isCtor, void *_this, void *userData) { void **args = ((PackDelegateUserData *)userData)->args; ILMethod *pinvokeInfo = ((PackDelegateUserData *)userData)->pinvokeInfo; ILType *signature = ILMethod_Signature(method); CVMWord *stacktop, *stacklimit; ILUInt32 param, numParams; ILType *paramType; void *ptr; ILUInt32 size, sizeInWords; ILNativeFloat tempFloat; ILUInt32 marshalType; char *customName; int customNameLen; char *customCookie; int customCookieLen; char *strValue; /* Get the top and extent of the stack */ stacktop = thread->stackTop; stacklimit = thread->stackLimit; /* Push the arguments onto the evaluation stack */ if(ILType_HasThis(signature)) { /* Push the "this" argument */ CHECK_SPACE(1); if(((PackDelegateUserData *)userData)->needThis) { /* We get the "this" value from the incoming arguments */ stacktop->ptrValue = *((void **)(*args)); ++args; } else { /* We get the "this" value from the delegate object */ stacktop->ptrValue = _this; } ++stacktop; } numParams = ILTypeNumParams(signature); for(param = 1; param <= numParams; ++param) { /* Marshal parameters that need special handling */ marshalType = ILPInvokeGetMarshalType(0, pinvokeInfo, param, &customName, &customNameLen, &customCookie, &customCookieLen, ILTypeGetParam(signature, param)); if(marshalType != IL_META_MARSHAL_DIRECT) { switch(marshalType) { case IL_META_MARSHAL_ANSI_STRING: { /* Marshal an ANSI string from the native world */ CHECK_SPACE(1); strValue = *((char **)(*args)); if(strValue) { stacktop->ptrValue = ILStringCreate(thread, strValue); /* Free the native string */ ILFreeNativeString(strValue); if(!(stacktop->ptrValue)) { return 1; } } else { stacktop->ptrValue = 0; } ++args; ++stacktop; } continue; case IL_META_MARSHAL_UTF8_STRING: { /* Marshal a UTF-8 string from the native world */ CHECK_SPACE(1); strValue = *((char **)(*args)); if(strValue) { stacktop->ptrValue = ILStringCreateUTF8(thread, strValue); /* Free the native string */ ILFreeNativeString(strValue); if(!(stacktop->ptrValue)) { return 1; } } else { stacktop->ptrValue = 0; } ++args; ++stacktop; } continue; case IL_META_MARSHAL_UTF16_STRING: { /* Marshal a UTF-16 string from the native world */ CHECK_SPACE(1); strValue = *((char **)(*args)); if(strValue) { stacktop->ptrValue = ILStringWCreate(thread, (ILUInt16 *)strValue); /* Free the native string */ ILFreeNativeString(strValue); if(!(stacktop->ptrValue)) { return 1; } } else { stacktop->ptrValue = 0; } ++args; ++stacktop; } continue; case IL_META_MARSHAL_CUSTOM: { /* Marshal a custom value from the native world */ CHECK_SPACE(1); stacktop->ptrValue = _ILCustomToObject (thread, *((void **)(*args)), customName, customNameLen, customCookie, customCookieLen); if(_ILExecThreadHasException(thread)) { return 1; } ++args; ++stacktop; } continue; } } /* Marshal the parameter directly */ paramType = ILTypeGetEnumType(ILTypeGetParam(signature, param)); if(ILType_IsPrimitive(paramType)) { /* Process a primitive value */ switch(ILType_ToElement(paramType)) { case IL_META_ELEMTYPE_VOID: break; case IL_META_ELEMTYPE_BOOLEAN: case IL_META_ELEMTYPE_I1: case IL_META_ELEMTYPE_U1: case IL_META_ELEMTYPE_I2: case IL_META_ELEMTYPE_U2: case IL_META_ELEMTYPE_CHAR: case IL_META_ELEMTYPE_I4: case IL_META_ELEMTYPE_U4: #ifdef IL_NATIVE_INT32 case IL_META_ELEMTYPE_I: case IL_META_ELEMTYPE_U: #endif { CHECK_SPACE(1); stacktop->intValue = *((ILInt32 *)(*args)); ++args; ++stacktop; } break; case IL_META_ELEMTYPE_I8: case IL_META_ELEMTYPE_U8: #ifdef IL_NATIVE_INT64 case IL_META_ELEMTYPE_I: case IL_META_ELEMTYPE_U: #endif { CHECK_SPACE(CVM_WORDS_PER_LONG); ILMemCpy(stacktop, *args, sizeof(ILInt64)); ++args; stacktop += CVM_WORDS_PER_LONG; } break; case IL_META_ELEMTYPE_R4: { CHECK_SPACE(CVM_WORDS_PER_NATIVE_FLOAT); tempFloat = (ILNativeFloat)(*((ILFloat *)(*args))); ILMemCpy(stacktop, &tempFloat, sizeof(ILNativeFloat)); ++args; stacktop += CVM_WORDS_PER_NATIVE_FLOAT; } break; case IL_META_ELEMTYPE_R8: { CHECK_SPACE(CVM_WORDS_PER_NATIVE_FLOAT); tempFloat = (ILNativeFloat)(*((ILDouble *)(*args))); ILMemCpy(stacktop, &tempFloat, sizeof(ILNativeFloat)); ++args; stacktop += CVM_WORDS_PER_NATIVE_FLOAT; } break; case IL_META_ELEMTYPE_R: { CHECK_SPACE(CVM_WORDS_PER_NATIVE_FLOAT); ILMemCpy(stacktop, *args, sizeof(ILNativeFloat)); ++args; stacktop += CVM_WORDS_PER_NATIVE_FLOAT; } break; case IL_META_ELEMTYPE_TYPEDBYREF: { CHECK_SPACE(CVM_WORDS_PER_TYPED_REF); ILMemCpy(stacktop, *args, sizeof(ILTypedRef)); ++args; stacktop += CVM_WORDS_PER_TYPED_REF; } break; } } else if(ILType_IsClass(paramType)) { /* Process an object reference */ CHECK_SPACE(1); stacktop->ptrValue = *((void **)(*args)); ++args; ++stacktop; } else if(ILType_IsValueType(paramType)) { /* Process a value type which was passed by value */ ptr = *args; ++args; size = ILSizeOfType(thread, paramType); sizeInWords = ((size + sizeof(CVMWord) - 1) / sizeof(CVMWord)); CHECK_SPACE(sizeInWords); ILMemCpy(stacktop, ptr, size); stacktop += sizeInWords; } else if(paramType != 0 && ILType_IsComplex(paramType) && (ILType_Kind(paramType) == IL_TYPE_COMPLEX_BYREF || ILType_Kind(paramType) == IL_TYPE_COMPLEX_PTR)) { /* Process a value that is being passed by reference */ CHECK_SPACE(1); stacktop->ptrValue = *((void **)(*args)); ++args; ++stacktop; } else { /* Assume that everything else is an object reference */ CHECK_SPACE(1); stacktop->ptrValue = *args; ++args; ++stacktop; } } /* Update the stack top */ thread->stackTop = stacktop; return 0; }