All pastes #84276 Raw Edit

Stuff

public text v1 · immutable
·published 2006-07-10 19:47 UTC
rendered paste body
/*
 * Get the parameter information from the method.
 */
static ILParameter *_ILJitGetParameter(ILMethod *method, ILUInt32 param)
{
	ILParameter *parameter = 0;

	while((parameter = ILMethodNextParam(method, parameter)))
	{
		if(parameter->paramNum == param)
		{
			return parameter;
		}
	}
	return 0;
}

/*
 * Get the native type information for a method parameter.
 */
static ILUInt32 _ILJitGetNativeType(ILMethod *method, ILUInt32 param)
{
	ILParameter *parameter = _ILJitGetParameter(method, param);

	if(parameter)
	{
		ILFieldMarshal *marshal;

		marshal = ILFieldMarshalGetFromOwner(&(parameter->programItem));
		if(marshal)
		{
			unsigned long nativeTypeLen = 0;
			const unsigned char *nativeType;

			nativeType = (const unsigned char *)
				ILFieldMarshalGetType(marshal, &nativeTypeLen);
			if(nativeTypeLen > 0)
			{
				return (ILUInt32)(nativeType[0]);
			}
		}
	}
	return IL_META_NATIVETYPE_END;
}

/*
 * Determine the character set to use for strings and return the
 * corresponding marshaltype.
 */
static ILUInt32 _ILJitStringCharSet(ILPInvoke *pinvoke, ILMethod *method)
{
	/* Check the PInvoke record for character set information */
	if(pinvoke)
	{
		switch(pinvoke->member.attributes & IL_META_PINVOKE_CHAR_SET_MASK)
		{
			case IL_META_PINVOKE_CHAR_SET_ANSI:
			{
				return IL_META_NATIVETYPE_LPSTR;
			}
			/* Not reached */

			case IL_META_PINVOKE_CHAR_SET_AUTO:
			{
				return IL_META_NATIVETYPE_LPTSTR;
			}
			/* Not reached */
	
			case IL_META_PINVOKE_CHAR_SET_UNICODE:
			{
				return IL_META_NATIVETYPE_LPWSTR;
			}
			/* Not reached */
		}
	}

	/* Check the class for character set information */
	if(method)
	{
		switch(method->member.owner->attributes &
				IL_META_TYPEDEF_STRING_FORMAT_MASK)
		{
			case IL_META_TYPEDEF_UNICODE_CLASS:
			{
				return IL_META_NATIVETYPE_LPWSTR;
			}
			/* Not reached */

			case IL_META_TYPEDEF_ANSI_CLASS:
			{
				return IL_META_NATIVETYPE_LPSTR;
			}
			/* Not reached */

			default:
			{
				return IL_META_NATIVETYPE_LPTSTR;
			}
			/* Not reached */
		}
	}
	else
	{
		return IL_META_NATIVETYPE_LPTSTR;
	}
}

/*
 * Marshal an IL String to a native string.
 */
static ILJitValue _ILJitMarshalStringToNative(ILJitFunction func,
											  ILJitValue thread,
											  ILJitValue string,
											  ILUInt32 nativeType)
{
	ILJitValue returnValue;
	ILJitValue args[2];


	switch(nativeType)
	{
		case IL_META_NATIVETYPE_LPSTR:
		{
			/* Marshal the string to Ansi. */
			args[0] = thread;
			args[1] = string;

			returnValue = jit_insn_call_native(func,
											   "ILStringToAnsi",
											   ILStringToAnsi,
											   _ILJitSignature_ILStringToAnsi,
											   args, 2, JIT_CALL_NOTHROW);
			return returnValue;

		}
		break;

		case IL_META_NATIVETYPE_LPWSTR:
		{
			/* Marshal the string to a two byte unicode string. */
			args[0] = thread;
			args[1] = string;

			returnValue = jit_insn_call_native(func,
											   "ILStringToUTF16",
											   ILStringToUTF16,
											   _ILJitSignature_ILStringToUTF16,
											   args, 2, JIT_CALL_NOTHROW);
			return returnValue;

		}
		break;

		case IL_META_NATIVETYPE_LPTSTR:
		{
			/* Marshal the string to a string used by the unterlying platform. */
			args[0] = thread;
			args[1] = string;

		#if defined(IL_WIN32_PLATFORM)
			/* TODO: Check the version and convert to Ansi on Win98 and UTF16 */
			/* on WinNT and later. */
			returnValue = jit_insn_call_native(func,
											   "ILStringToUTF16",
											   ILStringToUTF16,
											   _ILJitSignature_ILStringToUTF16,
											   args, 2, JIT_CALL_NOTHROW);
		#else
			/* Use UTF8 ol all other OS/Archs. */
			returnValue = jit_insn_call_native(func,
											   "ILStringToUTF8",
											   ILStringToUTF8,
											   _ILJitSignature_ILStringToUTF8,
											   args, 2, JIT_CALL_NOTHROW);
		#endif
			return returnValue;
		}
		break;

		default:
		{
			/* Do no marshalling at all. Return string as is. */
			return string;
		}
	}
	return 0;
}