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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
package json
import "core:mem"
import "core:math/bits"
import "core:runtime"
import "core:strconv"
import "core:strings"
import "core:io"
Marshal_Data_Error :: enum {
None,
Unsupported_Type,
}
Marshal_Error :: union #shared_nil {
Marshal_Data_Error,
io.Error,
}
marshal :: proc(v: any, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) {
b := strings.make_builder(allocator)
defer if err != nil {
strings.destroy_builder(&b)
}
marshal_to_builder(&b, v) or_return
if len(b.buf) != 0 {
data = b.buf[:]
}
return data, nil
}
marshal_to_builder :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
return marshal_to_writer(strings.to_writer(b), v)
}
marshal_to_writer :: proc(w: io.Writer, v: any) -> (err: Marshal_Error) {
if v == nil {
io.write_string(w, "null") or_return
return
}
ti := runtime.type_info_base(type_info_of(v.id))
a := any{v.data, ti.id}
switch info in ti.variant {
case runtime.Type_Info_Named:
unreachable()
case runtime.Type_Info_Integer:
buf: [21]byte
u: u128
switch i in a {
case i8: u = u128(i)
case i16: u = u128(i)
case i32: u = u128(i)
case i64: u = u128(i)
case int: u = u128(i)
case u8: u = u128(i)
case u16: u = u128(i)
case u32: u = u128(i)
case u64: u = u128(i)
case u128: u = u128(i)
case uint: u = u128(i)
case uintptr: u = u128(i)
case i16le: u = u128(i)
case i32le: u = u128(i)
case i64le: u = u128(i)
case u16le: u = u128(i)
case u32le: u = u128(i)
case u64le: u = u128(i)
case u128le: u = u128(i)
case i16be: u = u128(i)
case i32be: u = u128(i)
case i64be: u = u128(i)
case u16be: u = u128(i)
case u32be: u = u128(i)
case u64be: u = u128(i)
case u128be: u = u128(i)
}
s := strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
io.write_string(w, s) or_return
case runtime.Type_Info_Rune:
r := a.(rune)
io.write_byte(w, '"') or_return
io.write_escaped_rune(w, r, '"', true) or_return
io.write_byte(w, '"') or_return
case runtime.Type_Info_Float:
switch f in a {
case f16: io.write_f16(w, f) or_return
case f32: io.write_f32(w, f) or_return
case f64: io.write_f64(w, f) or_return
case: return .Unsupported_Type
}
case runtime.Type_Info_Complex:
r, i: f64
switch z in a {
case complex32: r, i = f64(real(z)), f64(imag(z))
case complex64: r, i = f64(real(z)), f64(imag(z))
case complex128: r, i = f64(real(z)), f64(imag(z))
case: return .Unsupported_Type
}
io.write_byte(w, '[') or_return
io.write_f64(w, r) or_return
io.write_string(w, ", ") or_return
io.write_f64(w, i) or_return
io.write_byte(w, ']') or_return
case runtime.Type_Info_Quaternion:
return .Unsupported_Type
case runtime.Type_Info_String:
switch s in a {
case string: io.write_quoted_string(w, s) or_return
case cstring: io.write_quoted_string(w, string(s)) or_return
}
case runtime.Type_Info_Boolean:
val: bool
switch b in a {
case bool: val = bool(b)
case b8: val = bool(b)
case b16: val = bool(b)
case b32: val = bool(b)
case b64: val = bool(b)
}
io.write_string(w, val ? "true" : "false") or_return
case runtime.Type_Info_Any:
return .Unsupported_Type
case runtime.Type_Info_Type_Id:
return .Unsupported_Type
case runtime.Type_Info_Pointer:
return .Unsupported_Type
case runtime.Type_Info_Multi_Pointer:
return .Unsupported_Type
case runtime.Type_Info_Procedure:
return .Unsupported_Type
case runtime.Type_Info_Tuple:
return .Unsupported_Type
case runtime.Type_Info_Simd_Vector:
return .Unsupported_Type
case runtime.Type_Info_Relative_Pointer:
return .Unsupported_Type
case runtime.Type_Info_Relative_Slice:
return .Unsupported_Type
case runtime.Type_Info_Matrix:
return .Unsupported_Type
case runtime.Type_Info_Array:
io.write_byte(w, '[') or_return
for i in 0..<info.count {
if i > 0 { io.write_string(w, ", ") or_return }
data := uintptr(v.data) + uintptr(i*info.elem_size)
marshal_to_writer(w, any{rawptr(data), info.elem.id}) or_return
}
io.write_byte(w, ']') or_return
case runtime.Type_Info_Enumerated_Array:
index := runtime.type_info_base(info.index).variant.(runtime.Type_Info_Enum)
io.write_byte(w, '[') or_return
for i in 0..<info.count {
if i > 0 { io.write_string(w, ", ") or_return }
data := uintptr(v.data) + uintptr(i*info.elem_size)
marshal_to_writer(w, any{rawptr(data), info.elem.id}) or_return
}
io.write_byte(w, ']') or_return
case runtime.Type_Info_Dynamic_Array:
io.write_byte(w, '[') or_return
array := cast(^mem.Raw_Dynamic_Array)v.data
for i in 0..<array.len {
if i > 0 { io.write_string(w, ", ") or_return }
data := uintptr(array.data) + uintptr(i*info.elem_size)
marshal_to_writer(w, any{rawptr(data), info.elem.id}) or_return
}
io.write_byte(w, ']') or_return
case runtime.Type_Info_Slice:
io.write_byte(w, '[') or_return
slice := cast(^mem.Raw_Slice)v.data
for i in 0..<slice.len {
if i > 0 { io.write_string(w, ", ") or_return }
data := uintptr(slice.data) + uintptr(i*info.elem_size)
marshal_to_writer(w, any{rawptr(data), info.elem.id}) or_return
}
io.write_byte(w, ']') or_return
case runtime.Type_Info_Map:
m := (^mem.Raw_Map)(v.data)
io.write_byte(w, '{') or_return
if m != nil {
if info.generated_struct == nil {
return .Unsupported_Type
}
entries := &m.entries
gs := runtime.type_info_base(info.generated_struct).variant.(runtime.Type_Info_Struct)
ed := runtime.type_info_base(gs.types[1]).variant.(runtime.Type_Info_Dynamic_Array)
entry_type := ed.elem.variant.(runtime.Type_Info_Struct)
entry_size := ed.elem_size
for i in 0..<entries.len {
if i > 0 { io.write_string(w, ", ") or_return }
data := uintptr(entries.data) + uintptr(i*entry_size)
key := rawptr(data + entry_type.offsets[2])
value := rawptr(data + entry_type.offsets[3])
marshal_to_writer(w, any{key, info.key.id}) or_return
io.write_string(w, ": ") or_return
marshal_to_writer(w, any{value, info.value.id}) or_return
}
}
io.write_byte(w, '}') or_return
case runtime.Type_Info_Struct:
io.write_byte(w, '{') or_return
for name, i in info.names {
if i > 0 { io.write_string(w, ", ") or_return }
io.write_quoted_string(w, name) or_return
io.write_string(w, ": ") or_return
id := info.types[i].id
data := rawptr(uintptr(v.data) + info.offsets[i])
marshal_to_writer(w, any{data, id}) or_return
}
io.write_byte(w, '}') or_return
case runtime.Type_Info_Union:
tag_ptr := uintptr(v.data) + info.tag_offset
tag_any := any{rawptr(tag_ptr), info.tag_type.id}
tag: i64 = -1
switch i in tag_any {
case u8: tag = i64(i)
case i8: tag = i64(i)
case u16: tag = i64(i)
case i16: tag = i64(i)
case u32: tag = i64(i)
case i32: tag = i64(i)
case u64: tag = i64(i)
case i64: tag = i64(i)
case: panic("Invalid union tag type")
}
if v.data == nil || tag == 0 {
io.write_string(w, "null") or_return
} else {
id := info.variants[tag-1].id
return marshal_to_writer(w, any{v.data, id})
}
case runtime.Type_Info_Enum:
return marshal_to_writer(w, any{v.data, info.base.id})
case runtime.Type_Info_Bit_Set:
is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
if ti == nil {
return false
}
t := runtime.type_info_base(ti)
#partial switch info in t.variant {
case runtime.Type_Info_Integer:
switch info.endianness {
case .Platform: return false
case .Little: return ODIN_ENDIAN != .Little
case .Big: return ODIN_ENDIAN != .Big
}
}
return false
}
bit_data: u64
bit_size := u64(8*ti.size)
do_byte_swap := is_bit_set_different_endian_to_platform(info.underlying)
switch bit_size {
case 0: bit_data = 0
case 8:
x := (^u8)(v.data)^
bit_data = u64(x)
case 16:
x := (^u16)(v.data)^
if do_byte_swap {
x = bits.byte_swap(x)
}
bit_data = u64(x)
case 32:
x := (^u32)(v.data)^
if do_byte_swap {
x = bits.byte_swap(x)
}
bit_data = u64(x)
case 64:
x := (^u64)(v.data)^
if do_byte_swap {
x = bits.byte_swap(x)
}
bit_data = u64(x)
case: panic("unknown bit_size size")
}
io.write_u64(w, bit_data) or_return
return .Unsupported_Type
}
return
}
|