Created
January 18, 2014 13:05
-
-
Save urasandesu/8490242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <Urasandesu/Swathe/Swathe.h> | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
using namespace Urasandesu::Swathe; | |
using namespace Urasandesu::Swathe::Hosting; | |
using namespace Urasandesu::Swathe::Metadata; | |
using namespace Urasandesu::Swathe::StrongNaming; | |
using Urasandesu::CppAnonym::Utilities::AutoPtr; | |
using Urasandesu::CppAnonym::Utilities::Empty; | |
using std::vector; | |
using std::wstring; | |
auto const *pHost = HostInfo::CreateHost(); | |
auto const *pRuntime = pHost->GetRuntime(L"v2.0.50727"); | |
auto const *pMetaInfo = pRuntime->GetInfo<MetadataInfo>(); | |
auto *pMetaDisp = pMetaInfo->CreateDispenser(); | |
auto const *pMSCorLib = pMetaDisp->GetAssembly(L"mscorlib, Version=2.0.0.0, Culture=neutral, " | |
L"PublicKeyToken=b77a5c561934e089, processorArchitecture=x86"); | |
auto const *pMSCorLibDll = pMSCorLib->GetModule(L"CommonLanguageRuntimeLibrary"); | |
auto const *pVoid = pMSCorLibDll->GetType(L"System.Void"); | |
auto const *pInt32 = pMSCorLibDll->GetType(L"System.Int32"); | |
auto const *pString = pMSCorLibDll->GetType(L"System.String"); | |
auto const *pStreamReader = pMSCorLibDll->GetType(L"System.IO.StreamReader"); | |
auto const *pChar = pMSCorLibDll->GetType(L"System.Char"); | |
auto const *pIOException = pMSCorLibDll->GetType(L"System.IO.IOException"); | |
auto const *pTextReader = pMSCorLibDll->GetType(L"System.IO.TextReader"); | |
auto const *pException = pMSCorLibDll->GetType(L"System.Exception"); | |
auto const *pConsole = pMSCorLibDll->GetType(L"System.Console"); | |
auto const *pObject = pMSCorLibDll->GetType(L"System.Object"); | |
auto const *pStreamReader_ctor_string = static_cast<IMethod *>(nullptr); | |
{ | |
auto params = vector<IType const *>(); | |
params.push_back(pString); | |
pStreamReader_ctor_string = pStreamReader->GetConstructor(params); | |
} | |
auto const *pTextReader_ReadBlock_CharArr_Int32_Int32 = static_cast<IMethod *>(nullptr); | |
{ | |
auto params = vector<IType const *>(); | |
params.push_back(pChar->MakeArrayType()); | |
params.push_back(pInt32); | |
params.push_back(pInt32); | |
pTextReader_ReadBlock_CharArr_Int32_Int32 = pTextReader->GetMethod(L"ReadBlock", params); | |
} | |
auto const *pTextReader_Close = pTextReader->GetMethod(L"Close"); | |
auto const *pException_get_Message = pException->GetMethod(L"get_Message"); | |
auto const *pConsole_WriteLine_string_object_object = static_cast<IMethod *>(nullptr); | |
{ | |
auto params = vector<IType const *>(); | |
params.push_back(pString); | |
params.push_back(pObject); | |
params.push_back(pObject); | |
pConsole_WriteLine_string_object_object = pConsole->GetMethod(L"WriteLine", params); | |
} | |
auto const *pSnInfo = pRuntime->GetInfo<StrongNameInfo>(); | |
auto pSnKey = pSnInfo->NewStrongNameKey(L"..\\Test.prig\\mscorlib.Prig.snk"); | |
auto *pMSCorLibEHGen = pMetaDisp->DefineAssemblyWithPartialName(L"mscorlib.EH"); | |
pMSCorLibEHGen->SetStrongNameKey(pSnKey); | |
auto *pMSCorLibEHDllGen = pMSCorLibEHGen->DefineModule(L"mscorlib.EH.dll"); | |
auto *pEHClassGen = pMSCorLibEHDllGen->DefineType(L"mscorlib.Prig.EHClass", | |
TypeAttributes::TA_PUBLIC | | |
TypeAttributes::TA_AUTO_CLASS | | |
TypeAttributes::TA_ANSI_CLASS | | |
TypeAttributes::TA_BEFORE_FIELD_INIT); | |
auto *pEHClassGen_ReadFile = static_cast<MethodGenerator *>(nullptr); | |
{ | |
auto params = vector<IType const *>(); | |
params.push_back(pString); | |
params.push_back(pInt32); | |
pEHClassGen_ReadFile = pEHClassGen->DefineMethod(L"ReadFile", | |
MethodAttributes::MA_PUBLIC | | |
MethodAttributes::MA_HIDE_BY_SIG, | |
CallingConventions::CC_STANDARD, | |
pVoid, | |
params); | |
} | |
{ | |
auto *pBodyGen = pEHClassGen_ReadFile->DefineMethodBody(); | |
auto *pLocal0_file = pBodyGen->DefineLocal(pStreamReader); | |
auto *pLocal1_buffer = pBodyGen->DefineLocal(pChar->MakeArrayType()); | |
auto *pLocal2_e = pBodyGen->DefineLocal(pIOException); | |
auto label0 = pBodyGen->DefineLabel(); | |
pBodyGen->Emit(OpCodes::Ldnull); | |
pBodyGen->Emit(OpCodes::Stloc_0); | |
pBodyGen->Emit(OpCodes::Ldc_I4_S, static_cast<BYTE>(10)); | |
pBodyGen->Emit(OpCodes::Newarr, pChar); | |
pBodyGen->Emit(OpCodes::Stloc_1); | |
pBodyGen->BeginExceptionBlock(); | |
{ | |
pBodyGen->Emit(OpCodes::Ldarg_1); | |
pBodyGen->Emit(OpCodes::Newobj, pStreamReader_ctor_string); | |
pBodyGen->Emit(OpCodes::Stloc_0); | |
pBodyGen->Emit(OpCodes::Ldloc_0); | |
pBodyGen->Emit(OpCodes::Ldloc_1); | |
pBodyGen->Emit(OpCodes::Ldarg_2); | |
pBodyGen->Emit(OpCodes::Ldloc_1); | |
pBodyGen->Emit(OpCodes::Ldlen); | |
pBodyGen->Emit(OpCodes::Conv_I4); | |
pBodyGen->Emit(OpCodes::Callvirt, pTextReader_ReadBlock_CharArr_Int32_Int32); | |
pBodyGen->Emit(OpCodes::Pop); | |
} | |
pBodyGen->BeginCatchBlock(pIOException); | |
{ | |
pBodyGen->Emit(OpCodes::Stloc_2); | |
pBodyGen->Emit(OpCodes::Ldstr, L"Error reading from {0}. Message = {1}"); | |
pBodyGen->Emit(OpCodes::Ldarg_1); | |
pBodyGen->Emit(OpCodes::Ldloc_2); | |
pBodyGen->Emit(OpCodes::Callvirt, pException_get_Message); | |
pBodyGen->Emit(OpCodes::Call, pConsole_WriteLine_string_object_object); | |
} | |
pBodyGen->BeginFinallyBlock(); | |
{ | |
pBodyGen->Emit(OpCodes::Ldloc_0); | |
pBodyGen->Emit(OpCodes::Brfalse_S, label0); | |
pBodyGen->Emit(OpCodes::Ldloc_0); | |
pBodyGen->Emit(OpCodes::Callvirt, pTextReader_Close); | |
pBodyGen->MarkLabel(label0); | |
} | |
pBodyGen->EndExceptionBlock(); | |
pBodyGen->Emit(OpCodes::Ret); | |
} | |
pMSCorLibEHGen->Save(PortableExecutableKinds::PEK_IL_ONLY, ImageFileMachine::IFM_I386); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment