| 1 | 'TODO: ローカルオブジェクト確保及び解放時にTryServiceに通知する必要がある。
|
|---|
| 2 |
|
|---|
| 3 | Class TryLayer
|
|---|
| 4 | Public
|
|---|
| 5 | Const addressOfCatch As VoidPtr
|
|---|
| 6 | Const basePtr As LONG_PTR
|
|---|
| 7 | Const stackPtr As LONG_PTR
|
|---|
| 8 | context As CONTEXT
|
|---|
| 9 | Const debugProcNum As DWord
|
|---|
| 10 |
|
|---|
| 11 | Sub TryLayer( addressOfCatch As VoidPtr, basePtr As LONG_PTR, stackPtr As LONG_PTR )
|
|---|
| 12 | This.addressOfCatch = addressOfCatch
|
|---|
| 13 | This.basePtr = basePtr
|
|---|
| 14 | This.stackPtr = stackPtr
|
|---|
| 15 |
|
|---|
| 16 | Dim tempContext As CONTEXT
|
|---|
| 17 |
|
|---|
| 18 | tempContext.ContextFlags = CONTEXT_CONTROL or CONTEXT_INTEGER
|
|---|
| 19 | If Thread.CurrentThread().__GetContext(tempContext) = 0 Then
|
|---|
| 20 | ' TODO: エラー処理
|
|---|
| 21 | debug
|
|---|
| 22 | End If
|
|---|
| 23 | context = tempContext
|
|---|
| 24 |
|
|---|
| 25 | #ifdef _DEBUG
|
|---|
| 26 | Dim ThreadNum As Long
|
|---|
| 27 | ThreadNum=_DebugSys_GetThread()
|
|---|
| 28 | If ThreadNum <> -1 Then
|
|---|
| 29 | debugProcNum = _DebugSys_ProcNum[ThreadNum]
|
|---|
| 30 | End If
|
|---|
| 31 | #endif
|
|---|
| 32 | End Sub
|
|---|
| 33 | Sub ~TryLayer()
|
|---|
| 34 | End Sub
|
|---|
| 35 | End Class
|
|---|
| 36 |
|
|---|
| 37 | Class ExceptionService
|
|---|
| 38 | ppTryLayers As **TryLayer
|
|---|
| 39 | nTryLayers As Long
|
|---|
| 40 |
|
|---|
| 41 | Sub FreeLocalObjects()
|
|---|
| 42 | 'TODO: 破棄されていないローカルオブジェクトを破棄
|
|---|
| 43 | End Sub
|
|---|
| 44 | Public
|
|---|
| 45 |
|
|---|
| 46 | Sub ExceptionService()
|
|---|
| 47 | ppTryLayers = _System_malloc( 1 )
|
|---|
| 48 | nTryLayers = 0
|
|---|
| 49 | End Sub
|
|---|
| 50 | Sub ~ExceptionService()
|
|---|
| 51 | Dim i As Long
|
|---|
| 52 | For i = 0 To ELM( nTryLayers )
|
|---|
| 53 | Delete ppTryLayers[i]
|
|---|
| 54 | Next
|
|---|
| 55 | _System_free( ppTryLayers )
|
|---|
| 56 | ppTryLayers = 0
|
|---|
| 57 | End Sub
|
|---|
| 58 |
|
|---|
| 59 | 'Try
|
|---|
| 60 | Function BeginTryScope( addressOfCatch As VoidPtr, basePtr As LONG_PTR, stackPtr As LONG_PTR ) As TryLayer
|
|---|
| 61 | ppTryLayers = _System_realloc( ppTryLayers, ( nTryLayers + 1 ) * SizeOf( *TryLayer ) )
|
|---|
| 62 | ppTryLayers[nTryLayers] = New TryLayer( addressOfCatch, basePtr, stackPtr )
|
|---|
| 63 | nTryLayers++
|
|---|
| 64 |
|
|---|
| 65 | Return ByVal ppTryLayers[nTryLayers-1]
|
|---|
| 66 | End Function
|
|---|
| 67 |
|
|---|
| 68 | 'End Try
|
|---|
| 69 | Sub EndTryScope()
|
|---|
| 70 | nTryLayers--
|
|---|
| 71 | Delete ppTryLayers[nTryLayers]
|
|---|
| 72 | End Sub
|
|---|
| 73 |
|
|---|
| 74 | 'Throw
|
|---|
| 75 | Sub _Throw()
|
|---|
| 76 | If nTryLayers <= 0 then
|
|---|
| 77 | '例外処理スコープ制御が無効なとき
|
|---|
| 78 |
|
|---|
| 79 | 'TODO: 適切なエラー処理
|
|---|
| 80 | MessageBox( NULL, "例外", "", MB_OK or MB_ICONEXCLAMATION )
|
|---|
| 81 |
|
|---|
| 82 | Return
|
|---|
| 83 | End If
|
|---|
| 84 |
|
|---|
| 85 | '未解放なローカルオブジェクトを解放する
|
|---|
| 86 | FreeLocalObjects()
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | '--------------------------------------------------
|
|---|
| 91 | ' スレッドのコンテキストを設定(Catchへ遷移する)
|
|---|
| 92 | '--------------------------------------------------
|
|---|
| 93 |
|
|---|
| 94 | 'レジスタ情報を取得
|
|---|
| 95 | Dim context = ppTryLayers[nTryLayers - 1]->context
|
|---|
| 96 |
|
|---|
| 97 | 'カレント スレッドを取得
|
|---|
| 98 | Dim thread = Thread.CurrentThread
|
|---|
| 99 |
|
|---|
| 100 | '新しいip, spをセット
|
|---|
| 101 | #ifdef _WIN64
|
|---|
| 102 | context.Rip = ppTryLayers[nTryLayers - 1]->addressOfCatch As QWord
|
|---|
| 103 | context.Rbp = ppTryLayers[nTryLayers - 1]->basePtr
|
|---|
| 104 | context.Rsp = ppTryLayers[nTryLayers - 1]->stackPtr
|
|---|
| 105 | #else
|
|---|
| 106 | context.Eip = ppTryLayers[nTryLayers - 1]->addressOfCatch As DWord
|
|---|
| 107 | context.Ebp = ppTryLayers[nTryLayers - 1]->basePtr
|
|---|
| 108 | context.Esp = ppTryLayers[nTryLayers - 1]->stackPtr
|
|---|
| 109 | #endif
|
|---|
| 110 |
|
|---|
| 111 | #ifdef _DEBUG
|
|---|
| 112 | Dim ThreadNum As Long
|
|---|
| 113 | ThreadNum=_DebugSys_GetThread()
|
|---|
| 114 | If ThreadNum <> -1 Then
|
|---|
| 115 | _DebugSys_ProcNum[ThreadNum] = ppTryLayers[nTryLayers - 1]->debugProcNum-1
|
|---|
| 116 | End If
|
|---|
| 117 | #endif
|
|---|
| 118 | debug
|
|---|
| 119 | If thread.__SetContext( context ) = 0 Then
|
|---|
| 120 | OutputDebugString(Ex"レジスタ情報の設定に失敗しました。\r\n")
|
|---|
| 121 | Return
|
|---|
| 122 | End If
|
|---|
| 123 | debug
|
|---|
| 124 | End Sub
|
|---|
| 125 |
|
|---|
| 126 | Sub _Throw( msg As String )
|
|---|
| 127 | _Throw()
|
|---|
| 128 | End Sub
|
|---|
| 129 |
|
|---|
| 130 | Static Sub AllocateObject( pObj As VoidPtr )
|
|---|
| 131 | Dim thread = Thread.CurrentThread
|
|---|
| 132 | 'TODO: ローカルオブジェクトを登録
|
|---|
| 133 | End Sub
|
|---|
| 134 |
|
|---|
| 135 | Static Sub DeallocateObject( pObj As VoidPtr )
|
|---|
| 136 | 'TODO: ローカルオブジェクトを破棄
|
|---|
| 137 | End Sub
|
|---|
| 138 | End Class
|
|---|
| 139 |
|
|---|