ReneMiner
20-08-2014, 18:23
Here i have some script where i was trying out some stuff with heap & different subtypes.
There might be some new view of things, thats why it ended in this forums section, #MinVersion 1.9.12
The contained bug is strange, the script is currently modified so the bug does not occur.
To have it occur you can comment line 97 ("more As Long" within Type t_Type) as first, then run!
You can track it down backwards then in the next run when you comment line 43 ("GetUbound As Function" within Type own)
- then the script works again... has something to do with the amount of UDT-elements? really strange
' #######################################################################
Uses "console" , "TBGL" ' TBGL just for the built-in types...
Function EnumType( ByVal sType As String, _
Optional ByVal TestExist As Boolean = FALSE _
) As DWord
' this function will store all passed type-names in Ucase
' it will return a pointer to heap where the type-name
' can be read out. It will return 0 for non-existing if TestExist
Static allTypes(&H1FFF) As DWord
Static numTypes As Long
Static i As Long
If StrPtrLen(StrPtr(sType)) < 1 Then Return 0
If numTypes Then
For i = 1 To numTypes
If Ucase$(sType) = HEAP_Get(allTypes(i)) Then Return allTypes(i)
Next
EndIf
If TestExist Then Return 0
numTypes += 1
allTypes(numTypes) = HEAP_AllocByStr(Ucase$(sType))
Function = allTypes(numTypes)
End Function
' ----------------------------------------------------------------------
Type own
pData As DWord
pType As DWord
Is As Function
Be As Function
GetUbound As Function ' (<<< comment this later)
End Type
' - - -
Function own.Is() As String
Select Case HEAP_Get(Me.pType)
Case "HEAP", "STRING"
Function = HEAP_Get(Me.pData)
Case Else
Local data Like HEAP_Get(Me.pType) At Me.pData
Function = data
End Select
End Function
' - - -
Function own.Be(ByVal someData As String, _
Optional ByVal sType As String _
) As String
If Function_CParams = 2 Then Me.pType = EnumType(sType)
Select Case StrPtrLen(StrPtr(someData))
Case 0
If HEAP_Size(Me.pData) Then
Select Case HEAP_Get(Me.pType)
Case "HEAP", "STRING"
HEAP_Free(Me.pData)
Me.pData = 0
Case Else
Memory_Set( Me.pData, Repeat$(HEAP_Size(Me.pData), MKBYT$(0)) )
End Select
EndIf
Case HEAP_Size(Me.pData)
Memory_Set(Me.pData, someData)
Case Else
If HEAP_Size(Me.pData) Then HEAP_Free(Me.pData)
Me.pData = HEAP_AllocByStr(someData)
End Select
End Function
' - - -
Function own.GetUBound() As Long
Local lData Like HEAP_Get(Me.pType) At Me.pData
Function = HEAP_Size(Me.pData)/SizeOf(lData)
End Function
' - - - - - - - - - - - - - - - - -
Type t_Type
own
more As Long ' tB crashes if this not present... comment first
End Type
Dim foo As t_Type
foo.Be( MKBYT$(255,128,64,0), "TBGL_TRGBA" )
Local lData Like HEAP_Get(foo.pType) At foo.pData
PrintL "R:" & lData.R
PrintL "G:" & lData.G
PrintL "B:" & lData.B
PrintL "A:" & lData.A
PrintL
Local lVec As TBGL_TVECTOR3F
lVec.X = 1.23
lVec.Y = 2.34
lVec.Z = 3.45
foo.Be( lVec, "TBGL_tVector3F" )
String sResult = foo.Is()
Local vVec As TBGL_TVECTOR3F At StrPtr(sResult)
PrintL "X " & vVec.X
PrintL "Y " & vVec.Y
PrintL "Z " & vVec.Z
lVec.X = -3.45
lVec.Y = -2.34
lVec.Z = -1.23
foo.Be( lVec )
PrintL
SetAt( vVec, foo.pData )
PrintL "X " & vVec.X
PrintL "Y " & vVec.Y
PrintL "Z " & vVec.Z
PrintL
foo.Be("Hello DWord!", "STRING")
PrintL foo.Is()
foo.be("1234567890", "HEAP")
PrintL foo.Is()
' now loading up an array...
Long i, lArray(9)
For i = 1 To UBound(lArray)
' fill the array with something
lArray(i) = 10 - i
Next
foo.Be(Memory_Get(VarPtr(lArray(1)), SizeOf(lArray)) , "Long")
' now read out one-by-one:
Local surfer Like HEAP_Get(foo.pType) At foo.pData
While VarPtr(surfer) <= foo.pData + HEAP_Size(foo.pData) - SizeOf(surfer)
Print surfer & $TAB
SetAt( surfer, VarPtr(surfer) + SizeOf(surfer) )
Wend
PrintL
' or place virtual array upon:
' uncomment only if Function own.GetUBound() is present:
'Local vData(foo.GetUBound) Like HEAP_Get(foo.pType) At foo.pData
'For i = 1 To UBound(vData)
' Print vData(i) & $TAB
'Next
'PrintL
PrintL $CRLF & Repeat$(42, "*")
PrintL $CRLF & "Any key to end"
WaitKey
:unsure:
There might be some new view of things, thats why it ended in this forums section, #MinVersion 1.9.12
The contained bug is strange, the script is currently modified so the bug does not occur.
To have it occur you can comment line 97 ("more As Long" within Type t_Type) as first, then run!
You can track it down backwards then in the next run when you comment line 43 ("GetUbound As Function" within Type own)
- then the script works again... has something to do with the amount of UDT-elements? really strange
' #######################################################################
Uses "console" , "TBGL" ' TBGL just for the built-in types...
Function EnumType( ByVal sType As String, _
Optional ByVal TestExist As Boolean = FALSE _
) As DWord
' this function will store all passed type-names in Ucase
' it will return a pointer to heap where the type-name
' can be read out. It will return 0 for non-existing if TestExist
Static allTypes(&H1FFF) As DWord
Static numTypes As Long
Static i As Long
If StrPtrLen(StrPtr(sType)) < 1 Then Return 0
If numTypes Then
For i = 1 To numTypes
If Ucase$(sType) = HEAP_Get(allTypes(i)) Then Return allTypes(i)
Next
EndIf
If TestExist Then Return 0
numTypes += 1
allTypes(numTypes) = HEAP_AllocByStr(Ucase$(sType))
Function = allTypes(numTypes)
End Function
' ----------------------------------------------------------------------
Type own
pData As DWord
pType As DWord
Is As Function
Be As Function
GetUbound As Function ' (<<< comment this later)
End Type
' - - -
Function own.Is() As String
Select Case HEAP_Get(Me.pType)
Case "HEAP", "STRING"
Function = HEAP_Get(Me.pData)
Case Else
Local data Like HEAP_Get(Me.pType) At Me.pData
Function = data
End Select
End Function
' - - -
Function own.Be(ByVal someData As String, _
Optional ByVal sType As String _
) As String
If Function_CParams = 2 Then Me.pType = EnumType(sType)
Select Case StrPtrLen(StrPtr(someData))
Case 0
If HEAP_Size(Me.pData) Then
Select Case HEAP_Get(Me.pType)
Case "HEAP", "STRING"
HEAP_Free(Me.pData)
Me.pData = 0
Case Else
Memory_Set( Me.pData, Repeat$(HEAP_Size(Me.pData), MKBYT$(0)) )
End Select
EndIf
Case HEAP_Size(Me.pData)
Memory_Set(Me.pData, someData)
Case Else
If HEAP_Size(Me.pData) Then HEAP_Free(Me.pData)
Me.pData = HEAP_AllocByStr(someData)
End Select
End Function
' - - -
Function own.GetUBound() As Long
Local lData Like HEAP_Get(Me.pType) At Me.pData
Function = HEAP_Size(Me.pData)/SizeOf(lData)
End Function
' - - - - - - - - - - - - - - - - -
Type t_Type
own
more As Long ' tB crashes if this not present... comment first
End Type
Dim foo As t_Type
foo.Be( MKBYT$(255,128,64,0), "TBGL_TRGBA" )
Local lData Like HEAP_Get(foo.pType) At foo.pData
PrintL "R:" & lData.R
PrintL "G:" & lData.G
PrintL "B:" & lData.B
PrintL "A:" & lData.A
PrintL
Local lVec As TBGL_TVECTOR3F
lVec.X = 1.23
lVec.Y = 2.34
lVec.Z = 3.45
foo.Be( lVec, "TBGL_tVector3F" )
String sResult = foo.Is()
Local vVec As TBGL_TVECTOR3F At StrPtr(sResult)
PrintL "X " & vVec.X
PrintL "Y " & vVec.Y
PrintL "Z " & vVec.Z
lVec.X = -3.45
lVec.Y = -2.34
lVec.Z = -1.23
foo.Be( lVec )
PrintL
SetAt( vVec, foo.pData )
PrintL "X " & vVec.X
PrintL "Y " & vVec.Y
PrintL "Z " & vVec.Z
PrintL
foo.Be("Hello DWord!", "STRING")
PrintL foo.Is()
foo.be("1234567890", "HEAP")
PrintL foo.Is()
' now loading up an array...
Long i, lArray(9)
For i = 1 To UBound(lArray)
' fill the array with something
lArray(i) = 10 - i
Next
foo.Be(Memory_Get(VarPtr(lArray(1)), SizeOf(lArray)) , "Long")
' now read out one-by-one:
Local surfer Like HEAP_Get(foo.pType) At foo.pData
While VarPtr(surfer) <= foo.pData + HEAP_Size(foo.pData) - SizeOf(surfer)
Print surfer & $TAB
SetAt( surfer, VarPtr(surfer) + SizeOf(surfer) )
Wend
PrintL
' or place virtual array upon:
' uncomment only if Function own.GetUBound() is present:
'Local vData(foo.GetUBound) Like HEAP_Get(foo.pType) At foo.pData
'For i = 1 To UBound(vData)
' Print vData(i) & $TAB
'Next
'PrintL
PrintL $CRLF & Repeat$(42, "*")
PrintL $CRLF & "Any key to end"
WaitKey
:unsure: