HRESULT Library_corlib_native_System_RuntimeType::GetName( CLR_RT_HeapBlock& arg, bool fFullName, CLR_RT_HeapBlock& res )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_TypeDef_Instance td;
    CLR_UINT32              levels;
    char                    rgBuffer[ 256 ];
    LPSTR                   szBuffer;
    size_t                  iBuffer;

    TINYCLR_CHECK_HRESULT(GetTypeDescriptor( arg, td, &levels ));
    
    szBuffer = rgBuffer;
    iBuffer  = MAXSTRLEN(rgBuffer);

    TINYCLR_CHECK_HRESULT(g_CLR_RT_TypeSystem.BuildTypeName( td, szBuffer, iBuffer, fFullName ? CLR_RT_TypeSystem::TYPENAME_FLAGS_FULL : 0, levels ));

    TINYCLR_SET_AND_LEAVE(CLR_RT_HeapBlock_String::CreateInstance( res, rgBuffer ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_BitConverter::ToString___STATIC__STRING__SZARRAY_U1(CLR_RT_StackFrame& stack)
{
	NATIVE_PROFILE_CLR_CORE();
	TINYCLR_HEADER();

	CLR_RT_HeapBlock_Array* pArray = stack.Arg0().DereferenceArray();
	FAULT_ON_NULL_ARG(pArray);

	if (pArray->m_numOfElements == 0)
	{
		TINYCLR_CHECK_HRESULT(stack.SetResult_String(""));
	}
	else
	{
		BYTE* p = pArray->GetFirstElement();
		char* pOutput = ByteArrayToHex(p, 0, pArray->m_numOfElements);
		TINYCLR_CHECK_HRESULT(stack.SetResult_String(pOutput));
		delete[] pOutput;
	}

	TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_Time_native_Microsoft_SPOT_Time_TimeService::get_Settings___STATIC__MicrosoftSPOTTimeTimeServiceSettings( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER(); 

    TimeService_Settings    settings;
    CLR_RT_HeapBlock&       top = stack.PushValueAndClear();
    CLR_RT_HeapBlock*       managedSettings = NULL;

    TINYCLR_CHECK_HRESULT(TimeService_LoadSettings(&settings));    

    TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( top, g_CLR_RT_WellKnownTypes.m_TimeServiceSettings ));
    managedSettings = top.Dereference();

    managedSettings[ ManagedSettings::FIELD__PrimaryServerIP     ].SetInteger( settings.PrimaryServerIP );
    managedSettings[ ManagedSettings::FIELD__AlternateServerIP   ].SetInteger( settings.AlternateServerIP );
    managedSettings[ ManagedSettings::FIELD__RefreshTime         ].SetInteger( settings.RefreshTime );
    managedSettings[ ManagedSettings::FIELD__Tolerance           ].SetInteger( settings.Tolerance );
    managedSettings[ ManagedSettings::FIELD__ForceSyncAtWakeUp   ].SetBoolean( settings.Flags & TimeService_Settings_Flags_ForceSyncAtWakeUp );
    managedSettings[ ManagedSettings::FIELD__AutoDayLightSavings ].SetBoolean( settings.Flags & TimeService_Settings_Flags_AutoDST );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_AppDomain::CreateDomain___STATIC__SystemAppDomain__STRING( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();
   
    CLR_RT_AppDomain*    appDomain   = NULL;        
    CLR_RT_HeapBlock&    pArgs       = stack.Arg0();    
    CLR_RT_HeapBlock     res; res.SetObjectReference( NULL );
    CLR_RT_ProtectFromGC gc( res );
    LPCSTR               szName;

    szName = pArgs.RecoverString(); FAULT_ON_NULL(szName);
    
    TINYCLR_CHECK_HRESULT(CLR_RT_AppDomain::CreateInstance( szName, appDomain ));

    //load mscorlib
    TINYCLR_CHECK_HRESULT(appDomain->LoadAssembly( g_CLR_RT_TypeSystem.m_assemblyMscorlib ));        
    //load Native
    TINYCLR_CHECK_HRESULT(appDomain->LoadAssembly( g_CLR_RT_TypeSystem.m_assemblyNative   ));
    
    TINYCLR_CHECK_HRESULT(appDomain->GetManagedObject( res ));

    //Marshal the new AD to the calling AD.
    TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.GetCurrentAppDomain()->MarshalObject( res, res, appDomain ));

    stack.PushValueAndAssign( res );

    TINYCLR_CLEANUP();

    if(FAILED(hr))
    {
        if(appDomain)
        {
            appDomain->DestroyInstance();
        }        
    }

    TINYCLR_CLEANUP_END();
}
HRESULT Library_corlib_native_System_Threading_WaitHandle::WaitMultiple___STATIC__I4__SZARRAY_SystemThreadingWaitHandle__I4__BOOLEAN__BOOLEAN( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* arr = stack.Arg0().DereferenceArray();  FAULT_ON_NULL(arr);

    TINYCLR_CHECK_HRESULT(Wait( stack, stack.Arg1(), stack.Arg2(), (CLR_RT_HeapBlock*)arr->GetFirstElement(), arr->m_numOfElements, stack.Arg3().NumericByRef().s1 == 1 ));
    
    stack.SetResult_I4( stack.m_owningThread->m_waitForObject_Result );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_io_native_Microsoft_SPOT_IO_NativeFindFile::GetFileInfo___STATIC__MicrosoftSPOTIONativeFileInfo__STRING( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_IO();
    TINYCLR_HEADER();

    FS_FILEINFO                  fileInfo;
    BOOL                         found;
    UnicodeString                pathW;
    FileSystemVolume*            driver;

    CLR_RT_HeapBlock&            top = stack.PushValueAndClear();

    TINYCLR_CHECK_HRESULT(Library_spot_io_native_Microsoft_SPOT_IO_NativeIO::FindVolume( stack.Arg0(), driver, pathW ));

    TINYCLR_CHECK_HRESULT(driver->GetFileInfo( pathW, &fileInfo, &found ));

    if(found)
    {
        CLR_RT_HeapBlock* managedFi;

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( top, g_CLR_RT_WellKnownTypes.m_NativeFileInfo ));

        managedFi = top.Dereference();

        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Attributes     ].SetInteger( (CLR_UINT32)fileInfo.Attributes );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__CreationTime   ].SetInteger( (CLR_INT64)fileInfo.CreationTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastAccessTime ].SetInteger( (CLR_INT64)fileInfo.LastAccessTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastWriteTime  ].SetInteger( (CLR_INT64)fileInfo.LastWriteTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Size           ].SetInteger( (CLR_INT64)fileInfo.Size );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__FileName       ].SetObjectReference( NULL );
    }
    else
    {
        top.SetObjectReference( NULL );
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_io_native_Microsoft_SPOT_IO_NativeFindFile::GetNext___MicrosoftSPOTIONativeFileInfo( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_IO();
    TINYCLR_HEADER();

    CLR_RT_FindFile* ff;
    FS_FILEINFO*     fi;
    BOOL             found;
    
    CLR_RT_HeapBlock& top = stack.PushValueAndClear();

    TINYCLR_CHECK_HRESULT(GetFindFile( stack, ff ));

    TINYCLR_CHECK_HRESULT(ff->GetNext( &fi, &found ));

    if(found)
    {
        CLR_RT_HeapBlock* managedFi;

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( top, g_CLR_RT_WellKnownTypes.m_NativeFileInfo ));

        managedFi = top.Dereference();

        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Attributes     ].SetInteger( (CLR_UINT32)fi->Attributes );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__CreationTime   ].SetInteger( (CLR_INT64)fi->CreationTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastAccessTime ].SetInteger( (CLR_INT64)fi->LastAccessTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__LastWriteTime  ].SetInteger( (CLR_INT64)fi->LastWriteTime );
        managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__Size           ].SetInteger( (CLR_INT64)fi->Size );

        TINYCLR_CHECK_HRESULT(ff->CreateFilenameString( managedFi[ Library_spot_io_native_Microsoft_SPOT_IO_NativeFileInfo::FIELD__FileName ] ));
    }
    else
    {
        top.SetObjectReference( NULL );
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Cryptoki::GetSlotsInternal___STATIC__SZARRAY_MicrosoftSPOTCryptokiSlot( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CK_ULONG                i;
    CK_SLOT_ID              slots[NETMF_CRYPTOKI_MAX_SLOTS];
    CK_ULONG                count = NETMF_CRYPTOKI_MAX_SLOTS;
    CLR_RT_HeapBlock_Array* pSlots;
    CLR_RT_HeapBlock        ref;
    CLR_RT_HeapBlock*       pSlotRef;

    CRYPTOKI_CHECK_RESULT(stack, C_GetSlotList(CK_FALSE, slots, &count));

    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(ref, (CLR_UINT32)count, g_CLR_RT_WellKnownTypes.m_CryptokiSlot));

    pSlots = ref.DereferenceArray();

    pSlotRef = (CLR_RT_HeapBlock*)pSlots->GetFirstElement();
            
    for(i=0; i<count; i++)
    {
        CLR_RT_HeapBlock* pSlot;
        
        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( pSlotRef[i], g_CLR_RT_WellKnownTypes.m_CryptokiSlot ));

        pSlot = pSlotRef[i].Dereference();
      
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_slotIndex    ].SetInteger        ((CLR_INT32)slots[i]);
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_disposed     ].SetBoolean        (false              );
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_evtDispatcher].SetObjectReference(NULL               );
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_slotEvent    ].SetObjectReference(NULL               );
        pSlot[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Slot::FIELD__m_slotInfo     ].SetObjectReference(NULL               );
    }

    stack.SetResult_Object(pSlots);

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_BitConverter::GetBytes___STATIC__SZARRAY_U1__I4( CLR_RT_StackFrame& stack )
{
	NATIVE_PROFILE_CLR_CORE();
	TINYCLR_HEADER();

	int input = stack.Arg0().NumericByRefConst().s4;

	TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(stack.PushValueAndClear(), 4, g_CLR_RT_WellKnownTypes.m_UInt8));
	BYTE* p;
	p  = stack.TopValue().DereferenceArray()->GetFirstElement();
	*reinterpret_cast<int*>(p) = input;

	TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::_ctor___VOID__MicrosoftSPOTHardwareCpuPin__BOOLEAN__BOOLEAN__MicrosoftSPOTHardwarePortResistorMode( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();
    {
        CLR_RT_HeapBlock* pThis = stack.This();  FAULT_ON_NULL(pThis);
        CLR_UINT32    portId       =                stack.Arg1().NumericByRef().u4;
        bool          initialState =                stack.Arg2().NumericByRef().u1 != 0;
        bool          glitchFilter =                stack.Arg3().NumericByRef().u1 != 0;
        GPIO_RESISTOR resistorMode = (GPIO_RESISTOR)stack.Arg4().NumericByRef().u4;

        TINYCLR_CHECK_HRESULT(Microsoft_SPOT_Hardware_Port_Construct( pThis, portId, glitchFilter, resistorMode, GPIO_INT_NONE, initialState, false ));
    }
    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_HardwareProvider::NativeIsSupportedBaudRate___BOOLEAN__I4__BYREF_U4( CLR_RT_StackFrame& stack )
{
	TINYCLR_HEADER();
    CLR_RT_HeapBlock  hbBR;
    CLR_UINT32        port;
   
    CLR_UINT32        SupportedBaudrate;

    port              = stack.Arg1().NumericByRef().u4;

    TINYCLR_CHECK_HRESULT(hbBR.LoadFromReference( stack.Arg2() ));
    SupportedBaudrate = hbBR.NumericByRef().u4;

    if (CPU_USART_IsBaudrateSupported(port,SupportedBaudrate) == TRUE)
        stack.SetResult_Boolean( true );
    else
        stack.SetResult_Boolean( false );

    hbBR.SetInteger( (CLR_UINT32)SupportedBaudrate );
    TINYCLR_CHECK_HRESULT(hbBR.StoreToReference( stack.Arg2(), 0 ));        

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_NativeEventDispatcher::Dispose___VOID__BOOLEAN( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();
        
    CLR_RT_HeapBlock_NativeEventDispatcher *pNativeDisp = NULL;
    
    CLR_RT_HeapBlock*  pThis = stack.This();  FAULT_ON_NULL(pThis);
    
    TINYCLR_CHECK_HRESULT(GetEventDispatcher( stack, pNativeDisp ));
    
    // Cleanup the HAL queue from the instance of assiciated CLR_RT_HeapBlock_NativeEventDispatcher 
    pNativeDisp->RemoveFromHALQueue();
    
    // Calls driver to enable interrupts.  Consider that there could be no driver 
    // associated to this object so check that the driver methods are set 
    // we will be tolerant in this case and not throw any exception
    if(pNativeDisp->m_DriverMethods != NULL)
    {
        TINYCLR_CHECK_HRESULT(pNativeDisp->m_DriverMethods->m_CleanupProc( pNativeDisp )); 
    }
    
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_MethodBase::get_Name___STRING( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_MethodDef_Instance md;
    CLR_RT_HeapBlock*         hbMeth  = stack.Arg0().Dereference();

    TINYCLR_CHECK_HRESULT(GetMethodDescriptor( stack, *hbMeth, md ));

    TINYCLR_SET_AND_LEAVE(CLR_RT_HeapBlock_String::CreateInstance( stack.PushValue(), md.m_target->name, md.m_assm ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Object::GetType___SystemType( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_TypeDescriptor desc;
    CLR_RT_ReflectionDef_Index idx;
    CLR_RT_HeapBlock& arg0 = stack.Arg0();
    CLR_RT_HeapBlock* pObj;

    TINYCLR_CHECK_HRESULT(desc.InitializeFromObject( arg0 ));

    pObj = arg0.Dereference();

    if(pObj && pObj->DataType() == DATATYPE_REFLECTION)
    {
        idx.m_kind               = REFLECTION_TYPE;
        idx.m_levels             = 0;
        idx.m_data.m_type.m_data = desc.m_handlerCls.m_data;
    }
    else
    {
        idx = desc.m_reflex;
    }

    {
        CLR_RT_HeapBlock& top = stack.PushValue();
        CLR_RT_HeapBlock* hbObj;

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex(top, g_CLR_RT_WellKnownTypes.m_TypeStatic));
        
        hbObj = top.Dereference();
        hbObj->SetReflection( idx );
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::BindConnectHelper( CLR_RT_StackFrame& stack, bool fBind )
{
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock* socket = stack.Arg0().Dereference();
    CLR_INT32 handle;
    SOCK_sockaddr addr;
    CLR_UINT32 addrLen = sizeof(addr);
    CLR_INT32 ret;
    bool fThrowOnWouldBlock = false;

    FAULT_ON_NULL(socket);
    handle = socket[ FIELD__m_Handle ].NumericByRef().s4;

    TINYCLR_CHECK_HRESULT(MarshalSockAddress( &addr, addrLen, stack.Arg1() ));

    if(fBind)
    {
        ret = SOCK_bind( handle, &addr, addrLen );
    }
    else
    {
        ret = SOCK_connect( handle, &addr, addrLen );
        
        fThrowOnWouldBlock = (stack.Arg2().NumericByRefConst().s4 != 0);
        
        if(!fThrowOnWouldBlock && SOCK_getlasterror() == SOCK_EWOULDBLOCK)
        {
            TINYCLR_SET_AND_LEAVE(S_OK);            
        }
    }

    TINYCLR_CHECK_HRESULT(ThrowOnError( stack, ret ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_Time_native_Microsoft_SPOT_Time_TimeService::Update___STATIC__MicrosoftSPOTTimeTimeServiceStatus__U4__U4( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER(); 

    CLR_RT_HeapBlock* managedStatus = NULL;  

    UINT32 serverIP  = stack.Arg0().NumericByRef().u4;
    UINT32 tolerance = stack.Arg1().NumericByRef().u4;    

    TimeService_Status status;

    hr = TimeService_Update( serverIP, tolerance, &status );

    if(hr == CLR_E_RESCHEDULE) 
    {
        TINYCLR_LEAVE();
    }
    else 
    {
        CLR_RT_HeapBlock& top = stack.PushValueAndClear();

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex( top, g_CLR_RT_WellKnownTypes.m_TimeServiceStatus ));
        
        managedStatus = top.Dereference();

        managedStatus[ ManagedStatus::FIELD__Flags            ].SetInteger( status.Flags );
        managedStatus[ ManagedStatus::FIELD__SyncSourceServer ].SetInteger( status.ServerIP );
        managedStatus[ ManagedStatus::FIELD__SyncTimeOffset   ].SetInteger( status.SyncOffset );
        managedStatus[ ManagedStatus::FIELD__TimeUTC          ].SetInteger( status.CurrentTimeUTC );
    }

    TINYCLR_CLEANUP();

    // we are not throwing any exception, we are instead communicating the result through the TimeServiceStatus object we return
    // if the operation did not completed, we need to reschedule this call though
    switch(hr)
    {
        case CLR_E_RESCHEDULE:
            break;
        case CLR_E_TIMEOUT:
        case CLR_E_FAIL:
        default:
            // swallow error
            hr = S_OK; 
            break;
    }

    TINYCLR_CLEANUP_END();
}
HRESULT Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::MarshalSockAddress( CLR_RT_HeapBlock& blkDst, const struct SOCK_sockaddr* addrSrc, CLR_UINT32 addrLenSrc )
{
    NATIVE_PROFILE_CLR_NETWORK();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock_Array* arr = NULL;

    CLR_RT_HeapBlock blkArr; blkArr.SetObjectReference( NULL );
    CLR_RT_ProtectFromGC gc( blkArr );
        
    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( blkArr, addrLenSrc, g_CLR_RT_WellKnownTypes.m_UInt8 ));
    
    arr = blkArr.DereferenceArray();

    _ASSERTE(arr);

    memcpy( arr->GetFirstElement(), addrSrc, addrLenSrc );

    _ASSERTE(blkDst.DataType() == DATATYPE_BYREF || blkDst.DataType() == DATATYPE_ARRAY_BYREF);

    TINYCLR_CHECK_HRESULT(blkArr.StoreToReference( blkDst, 0 ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_Assembly::GetTypes___SZARRAY_SystemType( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_Assembly_Instance assm;
    CLR_RT_HeapBlock*        hbAsm = stack.Arg0().Dereference();

    TINYCLR_CHECK_HRESULT(GetTypeDescriptor( *hbAsm, assm ));

    {
        CLR_RT_Assembly*  pASSM = assm.m_assm;
        CLR_UINT32        num   = pASSM->m_pTablesSize[ TBL_TypeDef ];
        CLR_RT_HeapBlock& top   = stack.PushValue();
        CLR_RT_HeapBlock* hbObj;

        TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( top, num, g_CLR_RT_WellKnownTypes.m_TypeStatic ));

        if(num)
        {
            CLR_RT_HeapBlock* pArray = (CLR_RT_HeapBlock*)top.DereferenceArray()->GetFirstElement();

            for(CLR_UINT32 i=0; i<num; i++, pArray++)
            {
                CLR_RT_TypeDef_Index idx; idx.Set( pASSM->m_idx, i );

                TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex(*pArray, g_CLR_RT_WellKnownTypes.m_TypeStatic));
                
                hbObj = pArray->Dereference();
                hbObj->SetReflection( idx );
            }
        }
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Random::NextBytes___VOID__SZARRAY_U1( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();
    
    CLR_RT_Random* rand;
    CLR_RT_HeapBlock_Array* buffer;

    TINYCLR_CHECK_HRESULT(GetRandom( stack, rand ));

    buffer = stack.Arg1().DereferenceArray(); FAULT_ON_NULL(buffer);

    rand->NextBytes( buffer->GetFirstElement(), buffer->m_numOfElements );
        
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Random::GetRandom( CLR_RT_StackFrame& stack, CLR_RT_Random*& rand, bool create )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);

    if(create)
    {
        TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_BinaryBlob::CreateInstance( pThis[ FIELD___random ], sizeof(CLR_RT_Random), NULL, NULL, 0 ));
    }

    rand = (CLR_RT_Random*)pThis[ FIELD___random ].DereferenceBinaryBlob()->GetData();
    
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_TimeSpan::Equals___BOOLEAN__OBJECT( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    TINYCLR_CHECK_HRESULT(CompareTo___I4__OBJECT( stack ));

    {
        CLR_RT_HeapBlock& res = stack.TopValue();

        res.SetBoolean( res.NumericByRef().s4 == 0 );
    }

    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_Port::_ctor___VOID__MicrosoftSPOTHardwareCpuPin__BOOLEAN__MicrosoftSPOTHardwarePortResistorMode__MicrosoftSPOTHardwarePortInterruptMode( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_HARDWARE();
    TINYCLR_HEADER();
    {
        CLR_RT_HeapBlock*  pThis = stack.This();  FAULT_ON_NULL(pThis);
        CLR_UINT32     portId             =                stack.Arg1().NumericByRef().u4;
        bool           glitchFilterEnable =                stack.Arg2().NumericByRef().u1 != 0;
        GPIO_RESISTOR  resistorMode       = (GPIO_RESISTOR)stack.Arg3().NumericByRef().u4;
        GPIO_INT_EDGE  interruptMode      = (GPIO_INT_EDGE)stack.Arg4().NumericByRef().u4;

        TINYCLR_CHECK_HRESULT(Microsoft_SPOT_Hardware_Port_Construct( pThis, portId, glitchFilterEnable, resistorMode, interruptMode, false, false ));
    }
    TINYCLR_NOCLEANUP();
}
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_NativeEventDispatcher::_ctor___VOID__STRING__U8( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER(); 

    CLR_RT_DriverInterruptMethods*          pDriverMethods;
    const CLR_RT_NativeAssemblyData*        pNativeDriverData;
    CLR_RT_HeapBlock_NativeEventDispatcher* pNativeDisp = NULL;
    
    LPCSTR                                  lpszDriverName;    
    UINT64                                  driverData;
    
    CLR_RT_HeapBlock*  pThis = stack.This();  FAULT_ON_NULL(pThis);
    
    // Retrieve paramenters; 
    if (stack.Arg1().DataType() != DATATYPE_OBJECT) 
    {   TINYCLR_SET_AND_LEAVE(CLR_E_WRONG_TYPE);                      
    }
    lpszDriverName = stack.Arg1().RecoverString();  FAULT_ON_NULL(lpszDriverName);

    driverData = stack.Arg2().NumericByRef().u8;

    // Throw NULL exception if string is empty. 
    if(hal_strlen_s( lpszDriverName ) == 0)
    {  
        TINYCLR_CHECK_HRESULT(CLR_E_ARGUMENT_NULL);
    }
    
    // Retrives pointers to driver implemented functions.
    pNativeDriverData = GetAssemblyNativeData( lpszDriverName );
    
    // Validates check sum and presence of the structure.
    if(pNativeDriverData == NULL || pNativeDriverData->m_checkSum != DRIVER_INTERRUPT_METHODS_CHECKSUM)
    {
       TINYCLR_CHECK_HRESULT(CLR_E_DRIVER_NOT_REGISTERED);
    }

    // Get pointer to CLR_RT_DriverInterruptMethods
    pDriverMethods = (CLR_RT_DriverInterruptMethods *)pNativeDriverData->m_pNativeMethods;
    // Check that all methods are present:
    if(pDriverMethods->m_InitProc == NULL || pDriverMethods->m_EnableProc == NULL || pDriverMethods->m_CleanupProc == NULL)
    {
       TINYCLR_CHECK_HRESULT(CLR_E_DRIVER_NOT_REGISTERED);
    }

    // So we found driver by name and now we create instance of CLR_RT_HeapBlock_NativeEventDispatcher
    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_NativeEventDispatcher::CreateInstance( *pThis, pThis[ FIELD__m_NativeEventDispatcher ] ));

    // Initialize the driver with and provide the instance of CLR_RT_HeapBlock_NativeEventDispatcher 
    TINYCLR_CHECK_HRESULT(GetEventDispatcher( stack, pNativeDisp ));

    // Now call the driver. First save pointer to driver data.
    pNativeDisp->m_DriverMethods = pDriverMethods;
    TINYCLR_CHECK_HRESULT(pDriverMethods->m_InitProc( pNativeDisp, driverData ));
    
    TINYCLR_NOCLEANUP();
}
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::SignFinalInternal___SZARRAY_U1( CLR_RT_StackFrame& stack )
{
    TINYCLR_HEADER();

    CLR_RT_HeapBlock*       pThis    = stack.This();
    CLR_RT_HeapBlock_Array* pRes;
    CLR_RT_HeapBlock*       pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference();
    CK_SESSION_HANDLE       hSession;
    CLR_UINT32              sigLen   = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::FIELD__m_signatureLength].NumericByRef().u4;
    CLR_RT_HeapBlock        hbRef;

    FAULT_ON_NULL(pSession);

    hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4;

    if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED);

    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(hbRef, sigLen, g_CLR_RT_WellKnownTypes.m_UInt8));

    pRes = hbRef.DereferenceArray();

    CRYPTOKI_CHECK_RESULT(stack, C_SignFinal(hSession, pRes->GetFirstElement(), (CK_ULONG_PTR)&sigLen));

    if(sigLen < pRes->m_numOfElements)
    {
        TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(stack.PushValue(), sigLen, g_CLR_RT_WellKnownTypes.m_UInt8));

        memcpy(stack.TopValue().DereferenceArray()->GetFirstElement(), pRes->GetFirstElement(), sigLen);
    }
    else
    {
        stack.SetResult_Object(pRes);
    }

    TINYCLR_NOCLEANUP();
}
示例#25
0
HRESULT CLR_GFX_Bitmap::CreateInstanceGif( CLR_RT_HeapBlock& ref, const CLR_UINT8* data, const CLR_UINT32 size )
{
    NATIVE_PROFILE_CLR_GRAPHICS();
    TINYCLR_HEADER();

    GifDecoder*               decoder;
    CLR_GFX_Bitmap*           bitmap   = NULL;
    CLR_GFX_BitmapDescription bm;

    // Allocate the decoder
    decoder = (GifDecoder*)CLR_RT_Memory::Allocate( sizeof(GifDecoder) );  CHECK_ALLOCATION(decoder);

    // Initialize the decompression engine
    TINYCLR_CHECK_HRESULT( decoder->GifInitDecompress( data, size ) );

    // At this point decoder would have all the correct output dimension info, so we initialized the CLR_GFX_BitmapDescription
    // with that information, note that we currently only support decompression into the native bpp
    if(bm.BitmapDescription_Initialize( decoder->header.LogicScreenWidth, decoder->header.LogicScreenHeight, CLR_GFX_BitmapDescription::c_NativeBpp ) == false)
    {
        // if the resulting bitmap doesn't match our constraints, stop the decompression
        TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
    }

    // Allocate the memory that the decompressed bitmap would need
    TINYCLR_CHECK_HRESULT(CLR_GFX_Bitmap::CreateInstance( ref, bm ));

    TINYCLR_CHECK_HRESULT(CLR_GFX_Bitmap::GetInstanceFromHeapBlock (ref, bitmap));

    TINYCLR_CHECK_HRESULT(decoder->GifStartDecompress( bitmap ));

    TINYCLR_CLEANUP();

    if (decoder) CLR_RT_Memory::Release( decoder );

    TINYCLR_CLEANUP_END();
}
HRESULT Library_corlib_native_System_Text_UTF8Encoding::Helper__GetChars(CLR_RT_StackFrame& stack, bool fIndexed)
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    LPSTR szText;
    CLR_RT_HeapBlock ref;
    ref.SetObjectReference( NULL );
    CLR_RT_ProtectFromGC gc( ref );
    CLR_RT_HeapBlock_Array* pArrayBytes = stack.Arg1().DereferenceArray();
    CLR_INT32               byteIdx     = fIndexed ? stack.Arg2().NumericByRef().s4 : 0;
    CLR_INT32               byteCnt     = fIndexed ? stack.Arg3().NumericByRef().s4 : pArrayBytes->m_numOfElements;
    CLR_RT_HeapBlock_Array* pArrayBytesCopy;
    CLR_RT_HeapBlock_Array* arrTmp;
    int cBytesCopy;

    FAULT_ON_NULL(pArrayBytes);

    _ASSERTE(pArrayBytes->m_typeOfElement == DATATYPE_U1);

    if((byteIdx + byteCnt) > (CLR_INT32)pArrayBytes->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);

    cBytesCopy = byteCnt+1;

    /* Copy the array to a temporary buffer to create a zero-terminated string */
    TINYCLR_CHECK_HRESULT( CLR_RT_HeapBlock_Array::CreateInstance( ref, cBytesCopy, g_CLR_RT_WellKnownTypes.m_UInt8 ));

    pArrayBytesCopy = ref.DereferenceArray();
    szText = (LPSTR)pArrayBytesCopy->GetFirstElement();

    hal_strncpy_s( szText, cBytesCopy, (LPSTR)pArrayBytes->GetElement(byteIdx), byteCnt );

    TINYCLR_CHECK_HRESULT(Library_corlib_native_System_String::ConvertToCharArray( szText, stack.PushValueAndClear(), arrTmp, 0, -1 ));

    TINYCLR_NOCLEANUP();
}
示例#27
0
HRESULT CLR_RT_HeapBlock_XmlReader::Read()
{
    TINYCLR_HEADER();

    CLR_RT_XmlState* state = GetState();

    state->State.bufferStart = GetBuffer();
    state->State.buffer      = state->State.bufferStart + GetOffset();
    state->State.bufferEnd   = state->State.buffer + GetLength();
    state->State.reader      = this;

    if(state->State.initFn)
    {
        state->State.initFn( state );
    }

    while(true)
    {
        TINYCLR_CHECK_HRESULT(state->State.stateFn( state ));
    }

    TINYCLR_CLEANUP();

    if(hr == XML_E_NEED_MORE_DATA)
    {
        if(state->State.cleanUpFn)
        {
            state->State.cleanUpFn( state );
        }
        else
        {
            state->State.ShiftBuffer( state->State.buffer );
        }

        // if after buffer adjustment, there's no room for new data, then we know that
        // we've exceeded one of the system constraints (either the length of name or
        // attribute value is over buffer size)
        if(state->State.bufferEnd - state->State.bufferStart == XmlBufferSize)
        {
            hr = XML_E_LIMIT_EXCEEDED;
        }
    }

    SetOffset( state->State.buffer    - state->State.bufferStart );
    SetLength( state->State.bufferEnd - state->State.buffer      );

    TINYCLR_CLEANUP_END();
}
示例#28
0
HRESULT CLR_RT_HeapBlock_XmlNameTable::Grow()
{
    TINYCLR_HEADER();
    
    CLR_RT_HeapBlock_Array*              oldEntries;
    CLR_RT_HeapBlock                     newEntriesHB;
    CLR_RT_HeapBlock_Array*              newEntries;
    CLR_RT_HeapBlock_XmlNameTable_Entry* entry;
    CLR_RT_HeapBlock_XmlNameTable_Entry* next;
    CLR_RT_HeapBlock*                    newEntryHB;

    CLR_UINT32                           i;
    CLR_INT32                            newIndex;
    CLR_INT32                            newMask;

    newMask = GetMask() * 2 + 1;

    // allocate a new instance of Entry array
    TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( newEntriesHB, newMask + 1, g_CLR_RT_WellKnownTypes.m_XmlNameTable_Entry ));

    newEntries = newEntriesHB.DereferenceArray();
    oldEntries = GetEntries();

    // Go through the old buckets, and resort them
    for(i = 0; i < oldEntries->m_numOfElements; i++)
    {
        entry = (CLR_RT_HeapBlock_XmlNameTable_Entry*)((CLR_RT_HeapBlock*)oldEntries->GetElement( i ))->Dereference();
    
        while(entry != NULL)
        {
            newIndex = entry->GetHashCode() & newMask;
            next = entry->GetNext();
    
            newEntryHB = (CLR_RT_HeapBlock*)newEntries->GetElement( newIndex );
    
            entry->SetNext( (CLR_RT_HeapBlock_XmlNameTable_Entry*)newEntryHB->Dereference() );
    
            newEntryHB->SetObjectReference( entry );
    
            entry = next;
        }
    }

    SetEntries( newEntries );
    SetMask   ( newMask    );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_RuntimeFieldInfo::get_FieldType___SystemType( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_TypeDescriptor desc;
    CLR_RT_FieldDef_Instance fd;

    if(GetFieldDescriptor( stack, stack.Arg0(), fd ) == false) TINYCLR_SET_AND_LEAVE(CLR_E_NULL_REFERENCE);

    TINYCLR_CHECK_HRESULT(desc.InitializeFromFieldDefinition( fd ));

    stack.PushValue().SetReflection( desc.m_handlerCls );

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_RuntimeType::GetTypeDescriptor( CLR_RT_HeapBlock& arg, CLR_RT_TypeDef_Instance& inst )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_UINT32 levels;

    TINYCLR_CHECK_HRESULT(GetTypeDescriptor( arg, inst, &levels ));

    if(levels > 0)
    {
        inst.InitializeFromIndex( g_CLR_RT_WellKnownTypes.m_Array );
    }

    TINYCLR_NOCLEANUP();    
}