-
Notifications
You must be signed in to change notification settings - Fork 47
/
fix_read3.go
211 lines (186 loc) · 5.28 KB
/
fix_read3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package cxcore
//Why do these functions need CXArgument as imput!?
func ReadData(fp int, inp *CXArgument, dataType int) interface{} {
elt := GetAssignmentElement(inp)
if elt.IsSlice {
return ReadSlice(fp, inp, dataType)
} else if elt.IsArray {
return ReadArray(fp, inp, dataType)
} else {
return ReadObject(fp, inp, dataType)
}
}
// ReadData_i8 ...
func ReadData_i8(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_i16 ...
func ReadData_i16(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_i32 ...
func ReadData_i32(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_i64 ...
func ReadData_i64(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_ui8 ...
func ReadData_ui8(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_ui16 ...
func ReadData_ui16(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_ui32 ...
func ReadData_ui32(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_ui64 ...
func ReadData_ui64(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_f32 ...
func ReadData_f32(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
// ReadData_f64 ...
func ReadData_f64(fp int, inp *CXArgument, dataType int) interface{} {
return ReadData(fp, inp, dataType)
}
//Note: Only called once and only by ReadData
// ReadObject ...
func ReadObject(fp int, inp *CXArgument, dataType int) interface{} {
offset := GetFinalOffset(fp, inp)
array := ReadMemory(offset, inp)
return readAtomic(inp, array)
}
//Note: I modified this to crash if invalid type was used
func readAtomic(inp *CXArgument, bytes []byte) interface{} {
switch inp.Type {
case TYPE_I8:
data := readDataI8(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_I16:
data := readDataI16(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_I32:
data := readDataI32(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_I64:
data := readDataI64(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_UI8:
data := readDataUI8(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_UI16:
data := readDataUI16(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_UI32:
data := readDataUI32(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_UI64:
data := readDataUI64(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_F32:
data := readDataF32(bytes)
if len(data) > 0 {
return interface{}(data)
}
case TYPE_F64:
data := readDataF64(bytes)
if len(data) > 0 {
return interface{}(data)
}
default:
data := readDataUI8(bytes)
if len(data) > 0 {
return interface{}(data)
}
}
//should this crash if it gets here?
panic(CX_RUNTIME_INVALID_ARGUMENT) //Note: modified this so it crashes if it gets here for some reason
return interface{}(nil)
}
// ReadSlice ...
func ReadSlice(fp int, inp *CXArgument, dataType int) interface{} {
sliceOffset := GetSliceOffset(fp, inp)
if sliceOffset >= 0 && (dataType < 0 || inp.Type == dataType) {
slice := GetSliceData(sliceOffset, GetAssignmentElement(inp).Size)
if slice != nil {
return readAtomic(inp, slice) //readData
}
} else {
panic(CX_RUNTIME_INVALID_ARGUMENT)
}
return interface{}(nil)
}
// ReadArray ...
func ReadArray(fp int, inp *CXArgument, dataType int) interface{} {
offset := GetFinalOffset(fp, inp)
if dataType < 0 || inp.Type == dataType {
array := ReadMemory(offset, inp)
return readAtomic(inp, array) //readData
}
panic(CX_RUNTIME_INVALID_ARGUMENT)
}
// ReadSliceBytes ...
func ReadSliceBytes(fp int, inp *CXArgument, dataType int) []byte {
sliceOffset := GetSliceOffset(fp, inp)
if sliceOffset >= 0 && (dataType < 0 || inp.Type == dataType) {
slice := GetSliceData(sliceOffset, GetAssignmentElement(inp).Size)
return slice
}
panic(CX_RUNTIME_INVALID_ARGUMENT)
}
// second section
// ReadBool ...
func ReadBool(fp int, inp *CXArgument) (out bool) {
offset := GetFinalOffset(fp, inp)
out = DeserializeBool(ReadMemory(offset, inp))
return
}
// ReadStr ...
func ReadStr(fp int, inp *CXArgument) (out string) {
var offset int32
off := GetFinalOffset(fp, inp)
if inp.Name == "" {
// Then it's a literal.
offset = int32(off)
} else {
offset = Deserialize_i32(PROGRAM.Memory[off : off+TYPE_POINTER_SIZE])
}
if offset == 0 {
// Then it's nil string.
out = ""
return
}
// We need to check if the string lives on the data segment or on the
// heap to know if we need to take into consideration the object header's size.
if int(offset) > PROGRAM.HeapStartsAt {
size := Deserialize_i32(PROGRAM.Memory[offset+OBJECT_HEADER_SIZE : offset+OBJECT_HEADER_SIZE+STR_HEADER_SIZE])
DeserializeRaw(PROGRAM.Memory[offset+OBJECT_HEADER_SIZE:offset+OBJECT_HEADER_SIZE+STR_HEADER_SIZE+size], &out)
} else {
size := Deserialize_i32(PROGRAM.Memory[offset : offset+STR_HEADER_SIZE])
DeserializeRaw(PROGRAM.Memory[offset:offset+STR_HEADER_SIZE+size], &out)
}
return out
}