| 1 | Namespace ActiveBasic
|
|---|
| 2 | Namespace Core
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | ' 中間的な実装(継承専用)
|
|---|
| 6 | Class TypeBaseImpl
|
|---|
| 7 | Inherits System.TypeInfo
|
|---|
| 8 |
|
|---|
| 9 | strNamespace As String
|
|---|
| 10 | name As String
|
|---|
| 11 | fullName As String
|
|---|
| 12 |
|
|---|
| 13 | ptrType As TypeBaseImpl
|
|---|
| 14 | ptrLevel As Long
|
|---|
| 15 |
|
|---|
| 16 | ' メンバ情報
|
|---|
| 17 | memberNames As *String ' 名前リスト
|
|---|
| 18 | memberTypeFullNames As *String ' 型名リスト
|
|---|
| 19 | memberOffsets As *LONG_PTR ' クラスの先頭ポインタからのオフセット値
|
|---|
| 20 | memberCounts As Long ' 個数
|
|---|
| 21 | memberInfosCache As System.Collections.Generic.List<System.Reflection.MemberInfo>
|
|---|
| 22 |
|
|---|
| 23 | Protected
|
|---|
| 24 |
|
|---|
| 25 | baseType As System.TypeInfo
|
|---|
| 26 | 'interfaces As f(^^;;;
|
|---|
| 27 |
|
|---|
| 28 | Sub TypeBaseImpl( strNamespace As String, name As String, fullName As String )
|
|---|
| 29 | This.strNamespace = strNamespace
|
|---|
| 30 | This.name = name
|
|---|
| 31 | This.fullName = fullName
|
|---|
| 32 | This.baseType = Nothing
|
|---|
| 33 |
|
|---|
| 34 | ptrType = Nothing
|
|---|
| 35 | ptrLevel = 0
|
|---|
| 36 | End Sub
|
|---|
| 37 |
|
|---|
| 38 | Sub ~TypeBaseImpl()
|
|---|
| 39 | End Sub
|
|---|
| 40 |
|
|---|
| 41 | Sub PtrLevelUp()
|
|---|
| 42 | ptrLevel ++
|
|---|
| 43 | fullName = "*" + fullName
|
|---|
| 44 | End Sub
|
|---|
| 45 |
|
|---|
| 46 | Function Clone() As TypeBaseImpl
|
|---|
| 47 | Dim result = New TypeBaseImpl( strNamespace, name, fullName )
|
|---|
| 48 | result.SetBaseType( baseType )
|
|---|
| 49 | result.SetMembers( memberNames, memberTypeFullNames, memberOffsets, memberCounts )
|
|---|
| 50 | result.memberInfosCache = This.memberInfosCache
|
|---|
| 51 | result.ptrLevel = This.ptrLevel
|
|---|
| 52 | Return result
|
|---|
| 53 | End Function
|
|---|
| 54 |
|
|---|
| 55 | Public
|
|---|
| 56 |
|
|---|
| 57 | Sub SetMembers( memberNames As *String, memberTypeFullNames As *String, memberOffsets As *LONG_PTR, num As Long )
|
|---|
| 58 | This.memberNames = memberNames
|
|---|
| 59 | This.memberTypeFullNames = memberTypeFullNames
|
|---|
| 60 | This.memberOffsets = memberOffsets
|
|---|
| 61 | This.memberCounts = num
|
|---|
| 62 |
|
|---|
| 63 | /*
|
|---|
| 64 | OutputDebugString( Ex"\r\n" )
|
|---|
| 65 | Dim i As Long
|
|---|
| 66 | For i=0 To ELM(num)
|
|---|
| 67 | OutputDebugString( memberNames[i] )
|
|---|
| 68 | OutputDebugString( ", " )
|
|---|
| 69 | OutputDebugString( memberTypeFullNames[i] )
|
|---|
| 70 | OutputDebugString( Ex"\r\n" )
|
|---|
| 71 | Next
|
|---|
| 72 | */
|
|---|
| 73 | End Sub
|
|---|
| 74 |
|
|---|
| 75 | Sub SetBaseType( baseType As System.TypeInfo )
|
|---|
| 76 | This.baseType = baseType
|
|---|
| 77 | End Sub
|
|---|
| 78 |
|
|---|
| 79 | Function GetPtrType() As TypeBaseImpl
|
|---|
| 80 | If Object.ReferenceEquals( ptrType, Nothing ) Then
|
|---|
| 81 | Dim clone = This.Clone()
|
|---|
| 82 |
|
|---|
| 83 | ptrType = clone
|
|---|
| 84 | ptrType.PtrLevelUp()
|
|---|
| 85 | End If
|
|---|
| 86 | Return ptrType
|
|---|
| 87 | End Function
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | '----------------------------------------------------------------
|
|---|
| 91 | ' Public properties
|
|---|
| 92 | '----------------------------------------------------------------
|
|---|
| 93 |
|
|---|
| 94 | Override Function BaseType() As System.TypeInfo
|
|---|
| 95 | Return baseType
|
|---|
| 96 | End Function
|
|---|
| 97 |
|
|---|
| 98 | Override Function FullName() As String
|
|---|
| 99 | Return fullName
|
|---|
| 100 | End Function
|
|---|
| 101 |
|
|---|
| 102 | Override Function IsArray() As Boolean
|
|---|
| 103 | Return False
|
|---|
| 104 | End Function
|
|---|
| 105 |
|
|---|
| 106 | Override Function IsByRef() As Boolean
|
|---|
| 107 | Return False
|
|---|
| 108 | End Function
|
|---|
| 109 |
|
|---|
| 110 | Override Function IsClass() As Boolean
|
|---|
| 111 | Return False
|
|---|
| 112 | End Function
|
|---|
| 113 |
|
|---|
| 114 | Override Function IsEnum() As Boolean
|
|---|
| 115 | Return False
|
|---|
| 116 | End Function
|
|---|
| 117 |
|
|---|
| 118 | Override Function IsInterface() As Boolean
|
|---|
| 119 | Return False
|
|---|
| 120 | End Function
|
|---|
| 121 |
|
|---|
| 122 | Override Function IsPointer() As Boolean
|
|---|
| 123 | Return ( ptrLevel > 0 )
|
|---|
| 124 | End Function
|
|---|
| 125 |
|
|---|
| 126 | Override Function IsValueType() As Boolean
|
|---|
| 127 | Return False
|
|---|
| 128 | End Function
|
|---|
| 129 |
|
|---|
| 130 | Override Function Name() As String
|
|---|
| 131 | Return name
|
|---|
| 132 | End Function
|
|---|
| 133 |
|
|---|
| 134 | Override Function Namespace() As String
|
|---|
| 135 | Return strNamespace
|
|---|
| 136 | End Function
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | '----------------------------------------------------------------
|
|---|
| 141 | ' Public methods
|
|---|
| 142 | '----------------------------------------------------------------
|
|---|
| 143 |
|
|---|
| 144 | Override Function GetMembers() As System.Collections.Generic.List<System.Reflection.MemberInfo>
|
|---|
| 145 | If Object.ReferenceEquals( memberInfosCache, Nothing ) Then
|
|---|
| 146 | ' キャッシュにないときは生成する
|
|---|
| 147 | memberInfosCache = New System.Collections.Generic.List
|
|---|
| 148 | Dim i As Long
|
|---|
| 149 | For i=0 To ELM(memberCounts)
|
|---|
| 150 | memberInfosCache.Add( New System.Reflection.MemberInfo( memberNames[i], _System_TypeBase_Search( memberTypeFullNames[i] ), memberOffsets[i] ) )
|
|---|
| 151 | Next
|
|---|
| 152 | End If
|
|---|
| 153 |
|
|---|
| 154 | Return memberInfosCache
|
|---|
| 155 | End Function
|
|---|
| 156 |
|
|---|
| 157 | End Class
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | ' 値型を管理するためのクラス
|
|---|
| 161 | Class _System_TypeForValueType
|
|---|
| 162 | Inherits TypeBaseImpl
|
|---|
| 163 | Public
|
|---|
| 164 | Sub _System_TypeForValueType( name As String )
|
|---|
| 165 | TypeBaseImpl( "", name, name )
|
|---|
| 166 | End Sub
|
|---|
| 167 |
|
|---|
| 168 | Override Function IsValueType() As Boolean
|
|---|
| 169 | Return True
|
|---|
| 170 | End Function
|
|---|
| 171 | End Class
|
|---|
| 172 |
|
|---|
| 173 | TypeDef _SYSTEM_CONSTRUCTOR_PROC = *Sub( pThis As *Object )
|
|---|
| 174 | TypeDef _SYSTEM_DESTRUCTOR_PROC = *Sub( pThis As *Object )
|
|---|
| 175 |
|
|---|
| 176 | ' クラスを管理するためのクラス
|
|---|
| 177 | Class _System_TypeForClass
|
|---|
| 178 | Inherits TypeBaseImpl
|
|---|
| 179 |
|
|---|
| 180 | Static Const _SYSTEM_OBJECT_HEAD_SIZE = SizeOf(LONG_PTR) * 4
|
|---|
| 181 |
|
|---|
| 182 | size As SIZE_T
|
|---|
| 183 | comVtbl As VoidPtr
|
|---|
| 184 | vtblList As VoidPtr
|
|---|
| 185 | pDefaultConstructor As _SYSTEM_CONSTRUCTOR_PROC
|
|---|
| 186 | pDestructor As _SYSTEM_DESTRUCTOR_PROC
|
|---|
| 187 |
|
|---|
| 188 | Public
|
|---|
| 189 | referenceOffsets As *Long
|
|---|
| 190 | numOfReference As Long
|
|---|
| 191 |
|
|---|
| 192 | Sub _System_TypeForClass( strNamespace As String, name As String, fullName As String, referenceOffsets As *Long, numOfReference As Long )
|
|---|
| 193 | TypeBaseImpl( strNamespace, name, fullName )
|
|---|
| 194 |
|
|---|
| 195 | This.referenceOffsets = referenceOffsets
|
|---|
| 196 | This.numOfReference = numOfReference
|
|---|
| 197 | End Sub
|
|---|
| 198 | Sub _System_TypeForClass( strNamespace As String, name As String, fullName As String )
|
|---|
| 199 | TypeBaseImpl( strNamespace, name, fullName )
|
|---|
| 200 | End Sub
|
|---|
| 201 | Sub ~_System_TypeForClass()
|
|---|
| 202 | End Sub
|
|---|
| 203 |
|
|---|
| 204 | Sub SetClassInfo( size As SIZE_T, comVtbl As VoidPtr, vtblList As VoidPtr, pDefaultConstructor As _SYSTEM_CONSTRUCTOR_PROC, pDestructor As _SYSTEM_DESTRUCTOR_PROC )
|
|---|
| 205 | This.size = size
|
|---|
| 206 | This.comVtbl = comVtbl
|
|---|
| 207 | This.vtblList = vtblList
|
|---|
| 208 | This.pDefaultConstructor = pDefaultConstructor
|
|---|
| 209 | This.pDestructor = pDestructor
|
|---|
| 210 | End Sub
|
|---|
| 211 |
|
|---|
| 212 | Override Function IsClass() As Boolean
|
|---|
| 213 | Return True
|
|---|
| 214 | End Function
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 | Static Function _System_New( typeForClass As ActiveBasic.Core._System_TypeForClass ) As Object
|
|---|
| 218 | If typeForClass.pDefaultConstructor = NULL Then
|
|---|
| 219 | Throw New System.SystemException( "デフォルトコンストラクタを持たないクラスに対して_System_New関数は使えません。" )
|
|---|
| 220 | End If
|
|---|
| 221 |
|
|---|
| 222 | ' まずはメモリを確保
|
|---|
| 223 | Dim pPtr = _System_GC_malloc_ForObject( typeForClass.size + _SYSTEM_OBJECT_HEAD_SIZE ) As VoidPtr
|
|---|
| 224 |
|
|---|
| 225 | ' pPtr[0]=オブジェクトの個数
|
|---|
| 226 | Set_LONG_PTR( pPtr, 1 )
|
|---|
| 227 | pPtr += SizeOf(LONG_PTR)
|
|---|
| 228 |
|
|---|
| 229 | ' pPtr[1]=オブジェクトのサイズ
|
|---|
| 230 | Set_LONG_PTR( pPtr, typeForClass.size )
|
|---|
| 231 | pPtr += SizeOf(LONG_PTR)
|
|---|
| 232 |
|
|---|
| 233 | ' pPtr[2]=デストラクタの関数ポインタ
|
|---|
| 234 | Set_LONG_PTR( pPtr, typeForClass.pDestructor As LONG_PTR )
|
|---|
| 235 | pPtr += SizeOf(LONG_PTR)
|
|---|
| 236 |
|
|---|
| 237 | ' pPtr[3]=reserve
|
|---|
| 238 | pPtr += SizeOf(LONG_PTR)
|
|---|
| 239 |
|
|---|
| 240 | Dim pObject = pPtr As *Object
|
|---|
| 241 |
|
|---|
| 242 | ' com_vtblをセット
|
|---|
| 243 | Set_LONG_PTR( pObject, typeForClass.comVtbl As LONG_PTR )
|
|---|
| 244 |
|
|---|
| 245 | ' vtbl_listをセット
|
|---|
| 246 | Set_LONG_PTR( pObject + SizeOf(LONG_PTR), typeForClass.vtblList As LONG_PTR )
|
|---|
| 247 |
|
|---|
| 248 | ' 動的型情報をセットする
|
|---|
| 249 | pObject->_System_SetType( typeForClass )
|
|---|
| 250 |
|
|---|
| 251 | ' コンストラクタを呼び出す
|
|---|
| 252 | Dim proc = typeForClass.pDefaultConstructor
|
|---|
| 253 | proc( pObject )
|
|---|
| 254 |
|
|---|
| 255 | Return ByVal pObject
|
|---|
| 256 | End Function
|
|---|
| 257 | End Class
|
|---|
| 258 |
|
|---|
| 259 | ' インターフェイスを管理するためのクラス
|
|---|
| 260 | Class _System_TypeForInterface
|
|---|
| 261 | Inherits TypeBaseImpl
|
|---|
| 262 | Public
|
|---|
| 263 | End Class
|
|---|
| 264 |
|
|---|
| 265 | ' 列挙体を管理するためのクラス
|
|---|
| 266 | Class _System_TypeForEnum
|
|---|
| 267 | Inherits TypeBaseImpl
|
|---|
| 268 | Public
|
|---|
| 269 | End Class
|
|---|
| 270 |
|
|---|
| 271 | ' デリゲートを管理するためのクラス
|
|---|
| 272 | Class _System_TypeForDelegate
|
|---|
| 273 | Inherits TypeBaseImpl
|
|---|
| 274 | Public
|
|---|
| 275 | End Class
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 | '--------------------------------------------------------------------
|
|---|
| 279 | ' プロセスに存在するすべての型を管理する
|
|---|
| 280 | '--------------------------------------------------------------------
|
|---|
| 281 | Class _System_TypeBase
|
|---|
| 282 | Static types As System.Collections.Generic.Dictionary<String, TypeBaseImpl>
|
|---|
| 283 |
|
|---|
| 284 | Static isReady = False
|
|---|
| 285 |
|
|---|
| 286 | Static Sub Add( typeInfo As TypeBaseImpl )
|
|---|
| 287 | types.Add( typeInfo.FullName, typeInfo )
|
|---|
| 288 | End Sub
|
|---|
| 289 |
|
|---|
| 290 | Static Sub InitializeValueType()
|
|---|
| 291 | types = New System.Collections.Generic.Dictionary<String, TypeBaseImpl>(8191)
|
|---|
| 292 |
|
|---|
| 293 | ' 値型の追加
|
|---|
| 294 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Byte", fullName = "Byte" ] )
|
|---|
| 295 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "SByte", fullName = "SByte" ] )
|
|---|
| 296 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Word", fullName = "Word" ] )
|
|---|
| 297 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Integer", fullName = "Integer" ] )
|
|---|
| 298 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "DWord", fullName = "DWord" ] )
|
|---|
| 299 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Long", fullName = "Long" ] )
|
|---|
| 300 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "QWord", fullName = "QWord" ] )
|
|---|
| 301 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Int64", fullName = "Int64" ] )
|
|---|
| 302 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Boolean", fullName = "Boolean" ] )
|
|---|
| 303 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Single", fullName = "Single" ] )
|
|---|
| 304 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "Double", fullName = "Double" ] )
|
|---|
| 305 | Add( _System_Static_New _System_TypeForValueType[ strNamespace = "", name = "VoidPtr", fullName = "VoidPtr" ] )
|
|---|
| 306 | End Sub
|
|---|
| 307 |
|
|---|
| 308 | Static Sub InitializeUserTypes()
|
|---|
| 309 | ' このメソッドの実装はコンパイラが自動生成する
|
|---|
| 310 |
|
|---|
| 311 | '例:
|
|---|
| 312 | 'Add( New _System_TypeForClass( "System", "String", "System.String", [__offsets...], __numOfOffsets ) )
|
|---|
| 313 | '...
|
|---|
| 314 | End Sub
|
|---|
| 315 | Static Sub InitializeUserTypesForBaseType()
|
|---|
| 316 | ' このメソッドの実装はコンパイラが自動生成する
|
|---|
| 317 |
|
|---|
| 318 | '例:
|
|---|
| 319 | 'Search( "System.String" ).SetBaseType( Search( "System.Object" ) )
|
|---|
| 320 | '...
|
|---|
| 321 | End Sub
|
|---|
| 322 |
|
|---|
| 323 | Public
|
|---|
| 324 | Static Sub Initialize()
|
|---|
| 325 | ' 値型を初期化
|
|---|
| 326 | InitializeValueType()
|
|---|
| 327 |
|
|---|
| 328 | ' Class / Interface / Enum / Delegate を初期化
|
|---|
| 329 | InitializeUserTypes()
|
|---|
| 330 |
|
|---|
| 331 | isReady = True
|
|---|
| 332 |
|
|---|
| 333 | ' 基底クラスを登録
|
|---|
| 334 | InitializeUserTypesForBaseType()
|
|---|
| 335 |
|
|---|
| 336 | selfTypeInfo = _System_TypeBase.Search( "System.TypeInfo" ) As System.TypeInfo
|
|---|
| 337 |
|
|---|
| 338 | _System_DebugOnly_OutputDebugString( Ex"ready dynamic meta datas!\r\n" )
|
|---|
| 339 | End Sub
|
|---|
| 340 |
|
|---|
| 341 | Static Sub _NextPointerForGC()
|
|---|
| 342 | ' TODO: 実装
|
|---|
| 343 | End Sub
|
|---|
| 344 |
|
|---|
| 345 | Static Function Search( fullName As String ) As TypeBaseImpl
|
|---|
| 346 | If Object.ReferenceEquals(types, Nothing) Then
|
|---|
| 347 | Return Nothing
|
|---|
| 348 | End If
|
|---|
| 349 |
|
|---|
| 350 | If isReady = False Then
|
|---|
| 351 | Return Nothing
|
|---|
| 352 | End If
|
|---|
| 353 |
|
|---|
| 354 | If fullName[0] = &H2A Then ' fullName[0] = '*'
|
|---|
| 355 | Dim result = Search( fullName.Substring( 1 ) )
|
|---|
| 356 | Return result.GetPtrType()
|
|---|
| 357 | End If
|
|---|
| 358 |
|
|---|
| 359 | If Not types.TryGetValue(fullName, Search) Then
|
|---|
| 360 | OutputDebugString(Ex"TypeSearch Failed: \r\n")
|
|---|
| 361 | If Not ActiveBasic.IsNothing(fullName) Then
|
|---|
| 362 | OutputDebugStringW(StrPtr(fullName) As PWSTR)
|
|---|
| 363 | OutputDebugString(Ex"\r\n")
|
|---|
| 364 | OutputDebugStringA(StrPtr(fullName) As PSTR)
|
|---|
| 365 | End If
|
|---|
| 366 | OutputDebugString(Ex"\r\n")
|
|---|
| 367 | Debug
|
|---|
| 368 | End If
|
|---|
| 369 | End Function
|
|---|
| 370 |
|
|---|
| 371 | Static Function IsReady() As Boolean
|
|---|
| 372 | Return isReady
|
|---|
| 373 | End Function
|
|---|
| 374 |
|
|---|
| 375 | Static selfTypeInfo As System.TypeInfo
|
|---|
| 376 |
|
|---|
| 377 | End Class
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 | End Namespace
|
|---|
| 381 | End Namespace
|
|---|
| 382 |
|
|---|
| 383 | Function _System_TypeBase_Search( fullName As String ) As ActiveBasic.Core.TypeBaseImpl
|
|---|
| 384 | Return ActiveBasic.Core._System_TypeBase.Search( fullName )
|
|---|
| 385 | End Function
|
|---|