-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodule.ae
More file actions
1190 lines (1104 loc) · 34.6 KB
/
Copy pathmodule.ae
File metadata and controls
1190 lines (1104 loc) · 34.6 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// std.msgpack — MessagePack serialization and deserialization library (RFC 9562 equivalent / Portions copyright 3Hren/msgpack-rust).
// Import with: import std.msgpack
//
// Memory model
// ------------
// A MsgValue tree owns its string/binary payloads through a std.bytes
// buffer (s_bytes), NOT through a refcounted AetherString field. Storing
// an AetherString in a heap.new struct field leaks: heap.free(struct) frees
// only the struct, never the string, and manually releasing the field
// double-frees (the escape walker does not follow a string into a struct
// field). std.bytes is malloc-backed with an explicit bytes.free, which is
// leak-clean the same way the crypto modules' byte buffers are. So every
// str/bin/ext payload is an owned AetherBytes; free() reclaims it with
// bytes.free alongside the structs and lists. get_string / get_bin mint a
// fresh caller-owned AetherString from those bytes on demand (the caller
// releases that copy; the tree keeps its buffer). On unpack the cursor's
// read_slice already returns a freshly-owned AetherBytes, so the payload is
// adopted directly with no extra copy.
import std.bytes
import std.bytes.cursor
import std.string
import std.list
import std.heap
import std.mem
exports(
nil_value, boolean, from_int, num, str, bin, arr, map, ext,
get_type, get_bool, get_int, get_float, get_string, get_bin,
array_size, array_get, array_add,
map_size, map_get_key, map_get_value, map_get, map_set,
free, pack, unpack,
TYPE_NIL, TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_STR, TYPE_BIN, TYPE_ARRAY, TYPE_MAP, TYPE_EXT
)
const TYPE_NIL = 0
const TYPE_BOOL = 1
const TYPE_INT = 2
const TYPE_FLOAT = 3
const TYPE_STR = 4
const TYPE_BIN = 5
const TYPE_ARRAY = 6
const TYPE_MAP = 7
const TYPE_EXT = 8
struct MsgValue {
type_id: int
b_val: bool
i_val: long
f_val: float
s_bytes: ptr // owned AetherBytes for str/bin/ext payload (null if empty); freed in free()
s_len: int // payload byte length
ext_type: int
arr_val: ptr // list pointer (*std.list.List)
map_keys: ptr // list pointer (*std.list.List)
map_values: ptr // list pointer (*std.list.List)
}
// Reconstitute an IEEE-754 single-precision float from its 32 raw bits and
// widen it to a double. Pure Aether over std.mem's double bit-cast — no C
// binding needed (double<->bits and float64<->bits are already in std.mem).
fn float32_from_bits(val: long) -> float {
sign = (val >> 31) & 1
exp = (val >> 23) & 255
frac = val & 8388607
if exp == 0 {
if frac == 0 {
if sign == 1 { return -0.0 }
return 0.0
}
// subnormal single -> normalized double
while (frac & 8388608) == 0 {
frac = frac << 1
exp = exp - 1
}
exp = exp + 1
frac = frac & 8388607
real_exp = exp - 127 + 1023
dbits = ((sign as long) << 63) | ((real_exp as long) << 52) | ((frac as long) << 29)
return mem.float_from_bits(dbits)
}
if exp == 255 {
if frac == 0 {
if sign == 1 { return mem.float_from_bits(-4503599627370496 as long) }
return mem.float_from_bits(9218868437227405312 as long)
}
return mem.float_from_bits(9221120237041090560 as long)
}
real_exp = exp - 127 + 1023
dbits = ((sign as long) << 63) | ((real_exp as long) << 52) | ((frac as long) << 29)
return mem.float_from_bits(dbits)
}
// ---- Internal payload helpers ----
// Store `len` bytes of `src` (an AetherString) as a value's owned payload.
// A zero-length payload records s_bytes = null / s_len = 0 (no allocation).
fn set_payload(v: *MsgValue, src: string, len: int) {
if len <= 0 {
v.s_bytes = null
v.s_len = 0
return
}
let b = bytes.new(len)
bytes.copy_from_string(b, 0, src, len)
v.s_bytes = b
v.s_len = len
}
// Materialize a fresh caller-owned AetherString from a value's payload.
// Non-consuming: the value keeps owning its AetherBytes buffer.
fn payload_string(v: *MsgValue) -> string {
if v.s_bytes == null || v.s_len <= 0 {
return ""
}
return bytes.to_string(v.s_bytes, v.s_len)
}
// ---- Builders ----
nil_value() -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_NIL
return v as ptr
}
boolean(val: bool) -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_BOOL
v.b_val = val
return v as ptr
}
from_int(val: long) -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = val
return v as ptr
}
num(val: float) -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_FLOAT
v.f_val = val
return v as ptr
}
str(val: string) -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_STR
set_payload(v as *MsgValue, val, string.length(val))
return v as ptr
}
bin(val: string) -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_BIN
set_payload(v as *MsgValue, val, string.length(val))
return v as ptr
}
arr() -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_ARRAY
v.arr_val = list.list_new()
return v as ptr
}
map() -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_MAP
v.map_keys = list.list_new()
v.map_values = list.list_new()
return v as ptr
}
ext(type_id: int, data: string) -> ptr {
let v = heap.new(MsgValue)
v.type_id = TYPE_EXT
v.ext_type = type_id
set_payload(v as *MsgValue, data, string.length(data))
return v as ptr
}
// ---- Getters / Setters ----
get_type(val: ptr) -> int {
if val == null { return TYPE_NIL }
return (val as *MsgValue).type_id
}
get_bool(val: ptr) -> bool {
if val == null { return false }
return (val as *MsgValue).b_val
}
get_int(val: ptr) -> long {
if val == null { return 0 }
return (val as *MsgValue).i_val
}
get_float(val: ptr) -> float {
if val == null { return 0.0 }
return (val as *MsgValue).f_val
}
// Returns a fresh, caller-owned string copy of the payload. Release it
// like any other string.
get_string(val: ptr) -> string {
if val == null { return "" }
return payload_string(val as *MsgValue)
}
// Returns a fresh, caller-owned string copy of the binary payload.
get_bin(val: ptr) -> string {
if val == null { return "" }
return payload_string(val as *MsgValue)
}
array_size(val: ptr) -> int {
if val == null { return -1 }
let m = val as *MsgValue
if m.type_id != TYPE_ARRAY { return -1 }
return list.list_size(m.arr_val)
}
array_get(val: ptr, index: int) -> ptr {
if val == null { return null }
let m = val as *MsgValue
if m.type_id != TYPE_ARRAY { return null }
if index < 0 || index >= list.list_size(m.arr_val) { return null }
return list.list_get_raw(m.arr_val, index)
}
array_add(val: ptr, item: ptr) -> int {
if val == null || item == null { return 0 }
let m = val as *MsgValue
if m.type_id != TYPE_ARRAY { return 0 }
list.list_add_raw(m.arr_val, item)
return 1
}
map_size(val: ptr) -> int {
if val == null { return -1 }
let m = val as *MsgValue
if m.type_id != TYPE_MAP { return -1 }
return list.list_size(m.map_keys)
}
map_get_key(val: ptr, index: int) -> ptr {
if val == null { return null }
let m = val as *MsgValue
if m.type_id != TYPE_MAP { return null }
if index < 0 || index >= list.list_size(m.map_keys) { return null }
return list.list_get_raw(m.map_keys, index)
}
map_get_value(val: ptr, index: int) -> ptr {
if val == null { return null }
let m = val as *MsgValue
if m.type_id != TYPE_MAP { return null }
if index < 0 || index >= list.list_size(m.map_values) { return null }
return list.list_get_raw(m.map_values, index)
}
// Compare a string-typed key node's payload against `key` byte-for-byte.
// Mints a temporary string for the comparison and releases it, so no
// AetherString escapes.
fn key_matches(k: *MsgValue, key: string) -> bool {
if k == null || k.type_id != TYPE_STR { return false }
if k.s_len != string.length(key) { return false }
if k.s_len == 0 { return true }
let ks = payload_string(k)
let eq = ks == key
string.release(ks)
return eq
}
map_get(val: ptr, key: string) -> ptr {
if val == null { return null }
let m = val as *MsgValue
if m.type_id != TYPE_MAP { return null }
let sz = list.list_size(m.map_keys)
let i = 0
while i < sz {
let k = list.list_get_raw(m.map_keys, i) as *MsgValue
if key_matches(k, key) {
return list.list_get_raw(m.map_values, i)
}
i = i + 1
}
return null
}
map_set(val: ptr, key: string, value: ptr) -> int {
if val == null || value == null { return 0 }
let m = val as *MsgValue
if m.type_id != TYPE_MAP { return 0 }
let sz = list.list_size(m.map_keys)
let i = 0
while i < sz {
let k = list.list_get_raw(m.map_keys, i) as *MsgValue
if key_matches(k, key) {
let old_val = list.list_get_raw(m.map_values, i)
if old_val != null { free(old_val) }
list.list_set(m.map_values, i, value)
return 1
}
i = i + 1
}
let new_key = str(key)
list.list_add_raw(m.map_keys, new_key)
list.list_add_raw(m.map_values, value)
return 1
}
// ---- Recursive Free ----
free(val: ptr) {
if val == null { return }
let m = val as *MsgValue
if m.type_id == TYPE_ARRAY {
if m.arr_val != null {
let sz = list.list_size(m.arr_val)
let i = 0
while i < sz {
let item = list.list_get_raw(m.arr_val, i)
if item != null { free(item) }
i = i + 1
}
list.list_free(m.arr_val)
}
} else if m.type_id == TYPE_MAP {
if m.map_keys != null {
let sz = list.list_size(m.map_keys)
let i = 0
while i < sz {
let k = list.list_get_raw(m.map_keys, i)
let v = list.list_get_raw(m.map_values, i)
if k != null { free(k) }
if v != null { free(v) }
i = i + 1
}
list.list_free(m.map_keys)
list.list_free(m.map_values)
}
}
if m.s_bytes != null {
bytes.free(m.s_bytes)
m.s_bytes = null
}
heap.free(m)
}
// ---- Pack / Unpack ----
pack(val: ptr) -> string {
let b = bytes.new(64)
let end = pack_val(val, b, 0)
let res = bytes.finish(b, end)
return res
}
unpack(bytes_str: string) -> ptr! {
let ok = ""
let len = string.length(bytes_str)
if len == 0 {
return null, "empty input"
}
let b = bytes.new(len)
bytes.copy_from_string(b, 0, bytes_str, len)
defer bytes.free(b)
let c, err_c = cursor.new(b)
if err_c != "" {
return null, err_c
}
defer cursor.free(c)
let val, err = unpack_inner(c)
if err != "" {
return null, err
}
// unpack_inner's success path returns a fresh heap "" in the error
// slot; we discard it here (returning our own ok), so release it or
// it leaks one byte per unpack call.
string.release(err)
return val, ok
}
// ---- Packing implementation helper ----
// Write a value's str/bin/ext payload bytes into the output buffer.
fn pack_payload(m: *MsgValue, b: ptr, offset: int) {
if m.s_len > 0 && m.s_bytes != null {
bytes.copy_from_bytes(b, offset, m.s_bytes, 0, m.s_len)
}
}
fn pack_val(val: ptr, b: ptr, offset: int) -> int {
if val == null {
bytes.set(b, offset, 0xc0)
return offset + 1
}
let m = val as *MsgValue
if m.type_id == 0 { // TYPE_NIL
bytes.set(b, offset, 0xc0)
return offset + 1
}
if m.type_id == 1 { // TYPE_BOOL
if m.b_val {
bytes.set(b, offset, 0xc3)
} else {
bytes.set(b, offset, 0xc2)
}
return offset + 1
}
if m.type_id == 2 { // TYPE_INT
let i = m.i_val
if i >= 0 {
if i <= 127 {
bytes.set(b, offset, i)
return offset + 1
}
if i <= 255 {
bytes.set(b, offset, 0xcc)
bytes.set(b, offset + 1, i)
return offset + 2
}
if i <= 65535 {
bytes.set(b, offset, 0xcd)
bytes.set_be16(b, offset + 1, i)
return offset + 3
}
if i <= 4294967295 {
bytes.set(b, offset, 0xce)
bytes.set_be32(b, offset + 1, i)
return offset + 5
}
bytes.set(b, offset, 0xcf)
bytes.set_be64(b, offset + 1, i)
return offset + 9
} else {
if i >= -32 {
bytes.set(b, offset, 0xe0 | (i & 31))
return offset + 1
}
if i >= -128 {
bytes.set(b, offset, 0xd0)
bytes.set(b, offset + 1, i & 255)
return offset + 2
}
if i >= -32768 {
bytes.set(b, offset, 0xd1)
bytes.set_be16(b, offset + 1, i)
return offset + 3
}
if i >= -2147483648 {
bytes.set(b, offset, 0xd2)
bytes.set_be32(b, offset + 1, i)
return offset + 5
}
bytes.set(b, offset, 0xd3)
bytes.set_be64(b, offset + 1, i)
return offset + 9
}
}
if m.type_id == 3 { // TYPE_FLOAT
let bits = mem.bits_of_float(m.f_val)
bytes.set(b, offset, 0xcb)
bytes.set_be64(b, offset + 1, bits)
return offset + 9
}
if m.type_id == 4 { // TYPE_STR
let len = m.s_len
let next_off = offset
if len <= 31 {
bytes.set(b, offset, 0xa0 | len)
next_off = offset + 1
} else if len <= 255 {
bytes.set(b, offset, 0xd9)
bytes.set(b, offset + 1, len)
next_off = offset + 2
} else if len <= 65535 {
bytes.set(b, offset, 0xda)
bytes.set_be16(b, offset + 1, len)
next_off = offset + 3
} else {
bytes.set(b, offset, 0xdb)
bytes.set_be32(b, offset + 1, len)
next_off = offset + 5
}
pack_payload(m, b, next_off)
return next_off + len
}
if m.type_id == 5 { // TYPE_BIN
let len = m.s_len
let next_off = offset
if len <= 255 {
bytes.set(b, offset, 0xc4)
bytes.set(b, offset + 1, len)
next_off = offset + 2
} else if len <= 65535 {
bytes.set(b, offset, 0xc5)
bytes.set_be16(b, offset + 1, len)
next_off = offset + 3
} else {
bytes.set(b, offset, 0xc6)
bytes.set_be32(b, offset + 1, len)
next_off = offset + 5
}
pack_payload(m, b, next_off)
return next_off + len
}
if m.type_id == 6 { // TYPE_ARRAY
let sz = list.list_size(m.arr_val)
let next_off = offset
if sz <= 15 {
bytes.set(b, offset, 0x90 | sz)
next_off = offset + 1
} else if sz <= 65535 {
bytes.set(b, offset, 0xdc)
bytes.set_be16(b, offset + 1, sz)
next_off = offset + 3
} else {
bytes.set(b, offset, 0xdd)
bytes.set_be32(b, offset + 1, sz)
next_off = offset + 5
}
let i = 0
while i < sz {
let item = list.list_get_raw(m.arr_val, i)
next_off = pack_val(item, b, next_off)
i = i + 1
}
return next_off
}
if m.type_id == 7 { // TYPE_MAP
let sz = list.list_size(m.map_keys)
let next_off = offset
if sz <= 15 {
bytes.set(b, offset, 0x80 | sz)
next_off = offset + 1
} else if sz <= 65535 {
bytes.set(b, offset, 0xde)
bytes.set_be16(b, offset + 1, sz)
next_off = offset + 3
} else {
bytes.set(b, offset, 0xdf)
bytes.set_be32(b, offset + 1, sz)
next_off = offset + 5
}
let i = 0
while i < sz {
let k = list.list_get_raw(m.map_keys, i)
let v = list.list_get_raw(m.map_values, i)
next_off = pack_val(k, b, next_off)
next_off = pack_val(v, b, next_off)
i = i + 1
}
return next_off
}
if m.type_id == 8 { // TYPE_EXT
let len = m.s_len
let next_off = offset
if len == 1 {
bytes.set(b, offset, 0xd4)
bytes.set(b, offset + 1, m.ext_type & 255)
next_off = offset + 2
} else if len == 2 {
bytes.set(b, offset, 0xd5)
bytes.set(b, offset + 1, m.ext_type & 255)
next_off = offset + 2
} else if len == 4 {
bytes.set(b, offset, 0xd6)
bytes.set(b, offset + 1, m.ext_type & 255)
next_off = offset + 2
} else if len == 8 {
bytes.set(b, offset, 0xd7)
bytes.set(b, offset + 1, m.ext_type & 255)
next_off = offset + 2
} else if len == 16 {
bytes.set(b, offset, 0xd8)
bytes.set(b, offset + 1, m.ext_type & 255)
next_off = offset + 2
} else if len <= 255 {
bytes.set(b, offset, 0xc7)
bytes.set(b, offset + 1, len)
bytes.set(b, offset + 2, m.ext_type & 255)
next_off = offset + 3
} else if len <= 65535 {
bytes.set(b, offset, 0xc8)
bytes.set_be16(b, offset + 1, len)
bytes.set(b, offset + 3, m.ext_type & 255)
next_off = offset + 4
} else {
bytes.set(b, offset, 0xc9)
bytes.set_be32(b, offset + 1, len)
bytes.set(b, offset + 5, m.ext_type & 255)
next_off = offset + 6
}
pack_payload(m, b, next_off)
return next_off + len
}
return offset
}
// ---- Unpacking implementation helper ----
// Read `len` payload bytes from the cursor and adopt the resulting slice
// buffer as `v`'s owned payload (read_slice returns a freshly-owned
// AetherBytes, so no copy is needed). Records an empty payload for len==0.
// Returns an error string ("" on success); on failure the value's payload
// is left empty and the caller frees the value.
fn read_payload(v: *MsgValue, c: ptr, len: int, what: string) -> string {
if len <= 0 {
v.s_bytes = null
v.s_len = 0
return ""
}
let slice = cursor.read_slice(c, len)
if slice == null {
return what
}
v.s_bytes = slice
v.s_len = len
return ""
}
fn unpack_inner(c: ptr) -> (ptr, string) {
// Single tracked empty-error binding reused for every success return.
// A bare `""` literal in the error slot mints a fresh 1-byte heap
// string per call that the caller discards — leaking one byte per
// decoded value (see std.yaml / std.asn1 for the same pattern).
let ok = ""
let m = cursor.read_u8(c)
if m < 0 {
return null, "EOF"
}
// 1. Positive FixInt
if m >= 0x00 && m <= 0x7f {
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = m
return v as ptr, ok
}
// 2. FixMap
if m >= 0x80 && m <= 0x8f {
let sz = m & 0x0f
let v = heap.new(MsgValue)
v.type_id = TYPE_MAP
v.map_keys = list.list_new()
v.map_values = list.list_new()
let i = 0
while i < sz {
let key, err_k = unpack_inner(c)
if err_k != "" {
free(v)
return null, err_k
}
string.release(err_k) // success-path heap "" from the child; discard
let val, err_v = unpack_inner(c)
if err_v != "" {
free(key)
free(v)
return null, err_v
}
string.release(err_v)
list.list_add_raw(v.map_keys, key)
list.list_add_raw(v.map_values, val)
i = i + 1
}
return v as ptr, ok
}
// 3. FixArray
if m >= 0x90 && m <= 0x9f {
let sz = m & 0x0f
let v = heap.new(MsgValue)
v.type_id = TYPE_ARRAY
v.arr_val = list.list_new()
let i = 0
while i < sz {
let item, err = unpack_inner(c)
if err != "" {
free(v)
return null, err
}
string.release(err) // success-path heap "" from the child; discard
list.list_add_raw(v.arr_val, item)
i = i + 1
}
return v as ptr, ok
}
// 4. FixStr
if m >= 0xa0 && m <= 0xbf {
let len = m & 0x1f
let v = heap.new(MsgValue)
v.type_id = TYPE_STR
let perr = read_payload(v as *MsgValue, c, len, "EOF reading FixStr data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 5. Nil
if m == 0xc0 {
let v = heap.new(MsgValue)
v.type_id = TYPE_NIL
return v as ptr, ok
}
// Reserved
if m == 0xc1 {
return null, "reserved marker 0xc1 encountered"
}
// 6. False
if m == 0xc2 {
let v = heap.new(MsgValue)
v.type_id = TYPE_BOOL
v.b_val = false
return v as ptr, ok
}
// 7. True
if m == 0xc3 {
let v = heap.new(MsgValue)
v.type_id = TYPE_BOOL
v.b_val = true
return v as ptr, ok
}
// 8. Bin 8
if m == 0xc4 {
if cursor.remaining(c) < 1 { return null, "EOF reading bin 8 length" }
let len = cursor.read_u8(c)
let v = heap.new(MsgValue)
v.type_id = TYPE_BIN
let perr = read_payload(v as *MsgValue, c, len, "EOF reading bin 8 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 9. Bin 16
if m == 0xc5 {
if cursor.remaining(c) < 2 { return null, "EOF reading bin 16 length" }
let len = cursor.read_be_u16(c)
let v = heap.new(MsgValue)
v.type_id = TYPE_BIN
let perr = read_payload(v as *MsgValue, c, len, "EOF reading bin 16 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 10. Bin 32
if m == 0xc6 {
if cursor.remaining(c) < 4 { return null, "EOF reading bin 32 length" }
let len = cursor.read_be_u32(c)
let v = heap.new(MsgValue)
v.type_id = TYPE_BIN
let perr = read_payload(v as *MsgValue, c, len, "EOF reading bin 32 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 11. Ext 8
if m == 0xc7 {
if cursor.remaining(c) < 2 { return null, "EOF reading ext 8 length/type" }
let len = cursor.read_u8(c)
let et = cursor.read_u8(c)
// ext_type is signed i8
let ext_type = et
if ext_type >= 128 { ext_type = ext_type - 256 }
let v = heap.new(MsgValue)
v.type_id = TYPE_EXT
v.ext_type = ext_type
let perr = read_payload(v as *MsgValue, c, len, "EOF reading ext 8 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 12. Ext 16
if m == 0xc8 {
if cursor.remaining(c) < 3 { return null, "EOF reading ext 16 length/type" }
let len = cursor.read_be_u16(c)
let et = cursor.read_u8(c)
let ext_type = et
if ext_type >= 128 { ext_type = ext_type - 256 }
let v = heap.new(MsgValue)
v.type_id = TYPE_EXT
v.ext_type = ext_type
let perr = read_payload(v as *MsgValue, c, len, "EOF reading ext 16 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 13. Ext 32
if m == 0xc9 {
if cursor.remaining(c) < 5 { return null, "EOF reading ext 32 length/type" }
let len = cursor.read_be_u32(c)
let et = cursor.read_u8(c)
let ext_type = et
if ext_type >= 128 { ext_type = ext_type - 256 }
let v = heap.new(MsgValue)
v.type_id = TYPE_EXT
v.ext_type = ext_type
let perr = read_payload(v as *MsgValue, c, len, "EOF reading ext 32 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 14. Float 32
if m == 0xca {
if cursor.remaining(c) < 4 { return null, "EOF reading float 32" }
let raw = cursor.read_be_u32(c)
// Mask to 32 unsigned bits: read_be_u32 returns an int, and widening
// an int with bit 31 set to long would sign-extend, corrupting the
// float's sign/exponent. float32_from_bits wants the raw 32 bits.
let bits: long = (raw as long) & 4294967295
let f = float32_from_bits(bits)
let v = heap.new(MsgValue)
v.type_id = TYPE_FLOAT
v.f_val = f
return v as ptr, ok
}
// 15. Float 64
if m == 0xcb {
if cursor.remaining(c) < 8 { return null, "EOF reading float 64" }
let bits = cursor.read_be_u64(c)
let f = mem.float_from_bits(bits)
let v = heap.new(MsgValue)
v.type_id = TYPE_FLOAT
v.f_val = f
return v as ptr, ok
}
// 16. U8
if m == 0xcc {
if cursor.remaining(c) < 1 { return null, "EOF reading u8" }
let i = cursor.read_u8(c)
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = i
return v as ptr, ok
}
// 17. U16
if m == 0xcd {
if cursor.remaining(c) < 2 { return null, "EOF reading u16" }
let i = cursor.read_be_u16(c)
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = i
return v as ptr, ok
}
// 18. U32
if m == 0xce {
if cursor.remaining(c) < 4 { return null, "EOF reading u32" }
let i = cursor.read_be_u32(c)
let val: long = i
if val < 0 {
val = val + 4294967296
}
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = val
return v as ptr, ok
}
// 19. U64
if m == 0xcf {
if cursor.remaining(c) < 8 { return null, "EOF reading u64" }
let i = cursor.read_be_u64(c)
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = i
return v as ptr, ok
}
// 20. I8
if m == 0xd0 {
if cursor.remaining(c) < 1 { return null, "EOF reading i8" }
let i = cursor.read_u8(c)
if i >= 128 { i = i - 256 }
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = i
return v as ptr, ok
}
// 21. I16
if m == 0xd1 {
if cursor.remaining(c) < 2 { return null, "EOF reading i16" }
let i = cursor.read_be_u16(c)
if i >= 32768 { i = i - 65536 }
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = i
return v as ptr, ok
}
// 22. I32
if m == 0xd2 {
if cursor.remaining(c) < 4 { return null, "EOF reading i32" }
let i = cursor.read_be_u32(c)
let val: long = i
if val >= 2147483648 { val = val - 4294967296 }
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = val
return v as ptr, ok
}
// 23. I64
if m == 0xd3 {
if cursor.remaining(c) < 8 { return null, "EOF reading i64" }
let i = cursor.read_be_u64(c)
let v = heap.new(MsgValue)
v.type_id = TYPE_INT
v.i_val = i
return v as ptr, ok
}
// 24. FixExt 1
if m == 0xd4 {
if cursor.remaining(c) < 2 { return null, "EOF reading fixext 1" }
let et = cursor.read_u8(c)
let ext_type = et
if ext_type >= 128 { ext_type = ext_type - 256 }
let v = heap.new(MsgValue)
v.type_id = TYPE_EXT
v.ext_type = ext_type
let perr = read_payload(v as *MsgValue, c, 1, "EOF reading fixext 1 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 25. FixExt 2
if m == 0xd5 {
if cursor.remaining(c) < 3 { return null, "EOF reading fixext 2" }
let et = cursor.read_u8(c)
let ext_type = et
if ext_type >= 128 { ext_type = ext_type - 256 }
let v = heap.new(MsgValue)
v.type_id = TYPE_EXT
v.ext_type = ext_type
let perr = read_payload(v as *MsgValue, c, 2, "EOF reading fixext 2 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}
// 26. FixExt 4
if m == 0xd6 {
if cursor.remaining(c) < 5 { return null, "EOF reading fixext 4" }
let et = cursor.read_u8(c)
let ext_type = et
if ext_type >= 128 { ext_type = ext_type - 256 }
let v = heap.new(MsgValue)
v.type_id = TYPE_EXT
v.ext_type = ext_type
let perr = read_payload(v as *MsgValue, c, 4, "EOF reading fixext 4 data")
if perr != "" {
heap.free(v)
return null, perr
}
return v as ptr, ok
}