-
Notifications
You must be signed in to change notification settings - Fork 316
/
compiler.go
1137 lines (1007 loc) · 30.6 KB
/
compiler.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
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
/*
* Copyright 2021 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package decoder
import (
`encoding/json`
`fmt`
`reflect`
`sort`
`strconv`
`strings`
`unsafe`
`github.com/bytedance/sonic/internal/caching`
`github.com/bytedance/sonic/internal/resolver`
`github.com/bytedance/sonic/internal/rt`
`github.com/bytedance/sonic/option`
)
type _Op uint8
const (
_OP_any _Op = iota + 1
_OP_dyn
_OP_str
_OP_bin
_OP_bool
_OP_num
_OP_i8
_OP_i16
_OP_i32
_OP_i64
_OP_u8
_OP_u16
_OP_u32
_OP_u64
_OP_f32
_OP_f64
_OP_unquote
_OP_nil_1
_OP_nil_2
_OP_nil_3
_OP_deref
_OP_index
_OP_is_null
_OP_is_null_quote
_OP_map_init
_OP_map_key_i8
_OP_map_key_i16
_OP_map_key_i32
_OP_map_key_i64
_OP_map_key_u8
_OP_map_key_u16
_OP_map_key_u32
_OP_map_key_u64
_OP_map_key_f32
_OP_map_key_f64
_OP_map_key_str
_OP_map_key_utext
_OP_map_key_utext_p
_OP_array_skip
_OP_array_clear
_OP_array_clear_p
_OP_slice_init
_OP_slice_append
_OP_object_skip
_OP_object_next
_OP_struct_field
_OP_unmarshal
_OP_unmarshal_p
_OP_unmarshal_text
_OP_unmarshal_text_p
_OP_lspace
_OP_match_char
_OP_check_char
_OP_load
_OP_save
_OP_drop
_OP_drop_2
_OP_recurse
_OP_goto
_OP_switch
_OP_check_bool
_OP_check_bytes
_OP_check_num
_OP_check_char_0
_OP_dismatch_err
)
const (
_INT_SIZE = 32 << (^uint(0) >> 63)
_PTR_SIZE = 32 << (^uintptr(0) >> 63)
)
const (
_MAX_ILBUF = 100000 // cutoff at 100k of IL instructions
_MAX_FIELDS = 50 // cutoff at 50 fields struct
)
var _OpNames = [256]string {
_OP_any : "any",
_OP_dyn : "dyn",
_OP_str : "str",
_OP_bin : "bin",
_OP_bool : "bool",
_OP_num : "num",
_OP_i8 : "i8",
_OP_i16 : "i16",
_OP_i32 : "i32",
_OP_i64 : "i64",
_OP_u8 : "u8",
_OP_u16 : "u16",
_OP_u32 : "u32",
_OP_u64 : "u64",
_OP_f32 : "f32",
_OP_f64 : "f64",
_OP_unquote : "unquote",
_OP_nil_1 : "nil_1",
_OP_nil_2 : "nil_2",
_OP_nil_3 : "nil_3",
_OP_deref : "deref",
_OP_index : "index",
_OP_is_null : "is_null",
_OP_is_null_quote : "is_null_quote",
_OP_map_init : "map_init",
_OP_map_key_i8 : "map_key_i8",
_OP_map_key_i16 : "map_key_i16",
_OP_map_key_i32 : "map_key_i32",
_OP_map_key_i64 : "map_key_i64",
_OP_map_key_u8 : "map_key_u8",
_OP_map_key_u16 : "map_key_u16",
_OP_map_key_u32 : "map_key_u32",
_OP_map_key_u64 : "map_key_u64",
_OP_map_key_f32 : "map_key_f32",
_OP_map_key_f64 : "map_key_f64",
_OP_map_key_str : "map_key_str",
_OP_map_key_utext : "map_key_utext",
_OP_map_key_utext_p : "map_key_utext_p",
_OP_array_skip : "array_skip",
_OP_slice_init : "slice_init",
_OP_slice_append : "slice_append",
_OP_object_skip : "object_skip",
_OP_object_next : "object_next",
_OP_struct_field : "struct_field",
_OP_unmarshal : "unmarshal",
_OP_unmarshal_p : "unmarshal_p",
_OP_unmarshal_text : "unmarshal_text",
_OP_unmarshal_text_p : "unmarshal_text_p",
_OP_lspace : "lspace",
_OP_match_char : "match_char",
_OP_check_char : "check_char",
_OP_load : "load",
_OP_save : "save",
_OP_drop : "drop",
_OP_drop_2 : "drop_2",
_OP_recurse : "recurse",
_OP_goto : "goto",
_OP_switch : "switch",
_OP_check_bool : "check_bool",
_OP_check_bytes : "check_bytes",
_OP_check_num : "check_num",
_OP_check_char_0 : "check_char_0",
_OP_dismatch_err : "dismatch_err",
}
func (self _Op) String() string {
if ret := _OpNames[self]; ret != "" {
return ret
} else {
return "<invalid>"
}
}
func _OP_int() _Op {
switch _INT_SIZE {
case 32: return _OP_i32
case 64: return _OP_i64
default: panic("unsupported int size")
}
}
func _OP_uint() _Op {
switch _INT_SIZE {
case 32: return _OP_u32
case 64: return _OP_u64
default: panic("unsupported uint size")
}
}
func _OP_uintptr() _Op {
switch _PTR_SIZE {
case 32: return _OP_u32
case 64: return _OP_u64
default: panic("unsupported pointer size")
}
}
func _OP_map_key_int() _Op {
switch _INT_SIZE {
case 32: return _OP_map_key_i32
case 64: return _OP_map_key_i64
default: panic("unsupported int size")
}
}
func _OP_map_key_uint() _Op {
switch _INT_SIZE {
case 32: return _OP_map_key_u32
case 64: return _OP_map_key_u64
default: panic("unsupported uint size")
}
}
func _OP_map_key_uintptr() _Op {
switch _PTR_SIZE {
case 32: return _OP_map_key_u32
case 64: return _OP_map_key_u64
default: panic("unsupported pointer size")
}
}
type _Instr struct {
u uint64 // union {op: 8, vb: 8, vi: 48}, iv maybe int or len([]int)
p unsafe.Pointer // maybe GoSlice.Data, *GoType or *caching.FieldMap
}
func packOp(op _Op) uint64 {
return uint64(op) << 56
}
func newInsOp(op _Op) _Instr {
return _Instr{u: packOp(op)}
}
func newInsVi(op _Op, vi int) _Instr {
return _Instr{u: packOp(op) | rt.PackInt(vi)}
}
func newInsVb(op _Op, vb byte) _Instr {
return _Instr{u: packOp(op) | (uint64(vb) << 48)}
}
func newInsVs(op _Op, vs []int) _Instr {
return _Instr {
u: packOp(op) | rt.PackInt(len(vs)),
p: (*rt.GoSlice)(unsafe.Pointer(&vs)).Ptr,
}
}
func newInsVt(op _Op, vt reflect.Type) _Instr {
return _Instr {
u: packOp(op),
p: unsafe.Pointer(rt.UnpackType(vt)),
}
}
func newInsVf(op _Op, vf *caching.FieldMap) _Instr {
return _Instr {
u: packOp(op),
p: unsafe.Pointer(vf),
}
}
func (self _Instr) op() _Op {
return _Op(self.u >> 56)
}
func (self _Instr) vi() int {
return rt.UnpackInt(self.u)
}
func (self _Instr) vb() byte {
return byte(self.u >> 48)
}
func (self _Instr) vs() (v []int) {
(*rt.GoSlice)(unsafe.Pointer(&v)).Ptr = self.p
(*rt.GoSlice)(unsafe.Pointer(&v)).Cap = self.vi()
(*rt.GoSlice)(unsafe.Pointer(&v)).Len = self.vi()
return
}
func (self _Instr) vf() *caching.FieldMap {
return (*caching.FieldMap)(self.p)
}
func (self _Instr) vk() reflect.Kind {
return (*rt.GoType)(self.p).Kind()
}
func (self _Instr) vt() reflect.Type {
return (*rt.GoType)(self.p).Pack()
}
func (self _Instr) i64() int64 {
return int64(self.vi())
}
func (self _Instr) vlen() int {
return int((*rt.GoType)(self.p).Size)
}
func (self _Instr) isBranch() bool {
switch self.op() {
case _OP_goto : fallthrough
case _OP_switch : fallthrough
case _OP_is_null : fallthrough
case _OP_is_null_quote : fallthrough
case _OP_check_char : return true
default : return false
}
}
func (self _Instr) disassemble() string {
switch self.op() {
case _OP_dyn : fallthrough
case _OP_deref : fallthrough
case _OP_map_key_i8 : fallthrough
case _OP_map_key_i16 : fallthrough
case _OP_map_key_i32 : fallthrough
case _OP_map_key_i64 : fallthrough
case _OP_map_key_u8 : fallthrough
case _OP_map_key_u16 : fallthrough
case _OP_map_key_u32 : fallthrough
case _OP_map_key_u64 : fallthrough
case _OP_map_key_f32 : fallthrough
case _OP_map_key_f64 : fallthrough
case _OP_map_key_str : fallthrough
case _OP_map_key_utext : fallthrough
case _OP_map_key_utext_p : fallthrough
case _OP_slice_init : fallthrough
case _OP_slice_append : fallthrough
case _OP_unmarshal : fallthrough
case _OP_unmarshal_p : fallthrough
case _OP_unmarshal_text : fallthrough
case _OP_unmarshal_text_p : fallthrough
case _OP_recurse : return fmt.Sprintf("%-18s%s", self.op(), self.vt())
case _OP_goto : fallthrough
case _OP_is_null_quote : fallthrough
case _OP_is_null : return fmt.Sprintf("%-18sL_%d", self.op(), self.vi())
case _OP_index : fallthrough
case _OP_array_clear : fallthrough
case _OP_array_clear_p : return fmt.Sprintf("%-18s%d", self.op(), self.vi())
case _OP_switch : return fmt.Sprintf("%-18s%s", self.op(), self.formatSwitchLabels())
case _OP_struct_field : return fmt.Sprintf("%-18s%s", self.op(), self.formatStructFields())
case _OP_match_char : return fmt.Sprintf("%-18s%s", self.op(), strconv.QuoteRune(rune(self.vb())))
case _OP_check_char : return fmt.Sprintf("%-18sL_%d, %s", self.op(), self.vi(), strconv.QuoteRune(rune(self.vb())))
default : return self.op().String()
}
}
func (self _Instr) formatSwitchLabels() string {
var i int
var v int
var m []string
/* format each label */
for i, v = range self.vs() {
m = append(m, fmt.Sprintf("%d=L_%d", i, v))
}
/* join them with "," */
return strings.Join(m, ", ")
}
func (self _Instr) formatStructFields() string {
var i uint64
var r []string
var m []struct{i int; n string}
/* extract all the fields */
for i = 0; i < self.vf().N; i++ {
if v := self.vf().At(i); v.Hash != 0 {
m = append(m, struct{i int; n string}{i: v.ID, n: v.Name})
}
}
/* sort by field name */
sort.Slice(m, func(i, j int) bool {
return m[i].n < m[j].n
})
/* format each field */
for _, v := range m {
r = append(r, fmt.Sprintf("%s=%d", v.n, v.i))
}
/* join them with "," */
return strings.Join(r, ", ")
}
type (
_Program []_Instr
)
func (self _Program) pc() int {
return len(self)
}
func (self _Program) tag(n int) {
if n >= _MaxStack {
panic("type nesting too deep")
}
}
func (self _Program) pin(i int) {
v := &self[i]
v.u &= 0xffff000000000000
v.u |= rt.PackInt(self.pc())
}
func (self _Program) rel(v []int) {
for _, i := range v {
self.pin(i)
}
}
func (self *_Program) add(op _Op) {
*self = append(*self, newInsOp(op))
}
func (self *_Program) int(op _Op, vi int) {
*self = append(*self, newInsVi(op, vi))
}
func (self *_Program) chr(op _Op, vb byte) {
*self = append(*self, newInsVb(op, vb))
}
func (self *_Program) tab(op _Op, vs []int) {
*self = append(*self, newInsVs(op, vs))
}
func (self *_Program) rtt(op _Op, vt reflect.Type) {
*self = append(*self, newInsVt(op, vt))
}
func (self *_Program) fmv(op _Op, vf *caching.FieldMap) {
*self = append(*self, newInsVf(op, vf))
}
func (self _Program) disassemble() string {
nb := len(self)
tab := make([]bool, nb + 1)
ret := make([]string, 0, nb + 1)
/* prescan to get all the labels */
for _, ins := range self {
if ins.isBranch() {
if ins.op() != _OP_switch {
tab[ins.vi()] = true
} else {
for _, v := range ins.vs() {
tab[v] = true
}
}
}
}
/* disassemble each instruction */
for i, ins := range self {
if !tab[i] {
ret = append(ret, "\t" + ins.disassemble())
} else {
ret = append(ret, fmt.Sprintf("L_%d:\n\t%s", i, ins.disassemble()))
}
}
/* add the last label, if needed */
if tab[nb] {
ret = append(ret, fmt.Sprintf("L_%d:", nb))
}
/* add an "end" indicator, and join all the strings */
return strings.Join(append(ret, "\tend"), "\n")
}
type _Compiler struct {
opts option.CompileOptions
tab map[reflect.Type]bool
rec map[reflect.Type]bool
}
func newCompiler() *_Compiler {
return &_Compiler {
opts: option.DefaultCompileOptions(),
tab: map[reflect.Type]bool{},
rec: map[reflect.Type]bool{},
}
}
func (self *_Compiler) apply(opts option.CompileOptions) *_Compiler {
self.opts = opts
return self
}
func (self *_Compiler) rescue(ep *error) {
if val := recover(); val != nil {
if err, ok := val.(error); ok {
*ep = err
} else {
panic(val)
}
}
}
func (self *_Compiler) compile(vt reflect.Type) (ret _Program, err error) {
defer self.rescue(&err)
self.compileOne(&ret, 0, vt)
return
}
func (self *_Compiler) compileOne(p *_Program, sp int, vt reflect.Type) {
/* check for recursive nesting */
ok := self.tab[vt]
if ok {
p.rtt(_OP_recurse, vt)
return
}
pt := reflect.PtrTo(vt)
/* check for `json.Unmarshaler` with pointer receiver */
if pt.Implements(jsonUnmarshalerType) {
p.rtt(_OP_unmarshal_p, pt)
return
}
/* check for `json.Unmarshaler` */
if vt.Implements(jsonUnmarshalerType) {
p.add(_OP_lspace)
self.compileUnmarshalJson(p, vt)
return
}
/* check for `encoding.TextMarshaler` with pointer receiver */
if pt.Implements(encodingTextUnmarshalerType) {
p.add(_OP_lspace)
self.compileUnmarshalTextPtr(p, pt)
return
}
/* check for `encoding.TextUnmarshaler` */
if vt.Implements(encodingTextUnmarshalerType) {
p.add(_OP_lspace)
self.compileUnmarshalText(p, vt)
return
}
/* enter the recursion */
p.add(_OP_lspace)
self.tab[vt] = true
self.compileOps(p, sp, vt)
delete(self.tab, vt)
}
func (self *_Compiler) compileOps(p *_Program, sp int, vt reflect.Type) {
// check first char mathes the type
skip := self.checkType(p, vt)
switch vt.Kind() {
case reflect.Bool : self.compilePrimitive (p, _OP_bool)
case reflect.Int : self.compilePrimitive (p, _OP_int())
case reflect.Int8 : self.compilePrimitive (p, _OP_i8)
case reflect.Int16 : self.compilePrimitive (p, _OP_i16)
case reflect.Int32 : self.compilePrimitive (p, _OP_i32)
case reflect.Int64 : self.compilePrimitive (p, _OP_i64)
case reflect.Uint : self.compilePrimitive (p, _OP_uint())
case reflect.Uint8 : self.compilePrimitive (p, _OP_u8)
case reflect.Uint16 : self.compilePrimitive (p, _OP_u16)
case reflect.Uint32 : self.compilePrimitive (p, _OP_u32)
case reflect.Uint64 : self.compilePrimitive (p, _OP_u64)
case reflect.Uintptr : self.compilePrimitive (p, _OP_uintptr())
case reflect.Float32 : self.compilePrimitive (p, _OP_f32)
case reflect.Float64 : self.compilePrimitive (p, _OP_f64)
case reflect.String : self.compileString (p, vt)
case reflect.Array : self.compileArray (p, sp, vt)
case reflect.Interface : self.compileInterface (p, vt)
case reflect.Map : self.compileMap (p, sp, vt)
case reflect.Ptr : self.compilePtr (p, sp, vt)
case reflect.Slice : self.compileSlice (p, sp, vt.Elem())
case reflect.Struct : self.compileStruct (p, sp, vt)
default : panic (&json.UnmarshalTypeError{Type: vt})
}
if skip >= 0 {
p.pin(skip)
}
}
func (self *_Compiler) compileMap(p *_Program, sp int, vt reflect.Type) {
if reflect.PtrTo(vt.Key()).Implements(encodingTextUnmarshalerType) {
self.compileMapOp(p, sp, vt, _OP_map_key_utext_p)
} else if vt.Key().Implements(encodingTextUnmarshalerType) {
self.compileMapOp(p, sp, vt, _OP_map_key_utext)
} else {
self.compileMapUt(p, sp, vt)
}
}
func (self *_Compiler) compileMapUt(p *_Program, sp int, vt reflect.Type) {
switch vt.Key().Kind() {
case reflect.Int : self.compileMapOp(p, sp, vt, _OP_map_key_int())
case reflect.Int8 : self.compileMapOp(p, sp, vt, _OP_map_key_i8)
case reflect.Int16 : self.compileMapOp(p, sp, vt, _OP_map_key_i16)
case reflect.Int32 : self.compileMapOp(p, sp, vt, _OP_map_key_i32)
case reflect.Int64 : self.compileMapOp(p, sp, vt, _OP_map_key_i64)
case reflect.Uint : self.compileMapOp(p, sp, vt, _OP_map_key_uint())
case reflect.Uint8 : self.compileMapOp(p, sp, vt, _OP_map_key_u8)
case reflect.Uint16 : self.compileMapOp(p, sp, vt, _OP_map_key_u16)
case reflect.Uint32 : self.compileMapOp(p, sp, vt, _OP_map_key_u32)
case reflect.Uint64 : self.compileMapOp(p, sp, vt, _OP_map_key_u64)
case reflect.Uintptr : self.compileMapOp(p, sp, vt, _OP_map_key_uintptr())
case reflect.Float32 : self.compileMapOp(p, sp, vt, _OP_map_key_f32)
case reflect.Float64 : self.compileMapOp(p, sp, vt, _OP_map_key_f64)
case reflect.String : self.compileMapOp(p, sp, vt, _OP_map_key_str)
default : panic(&json.UnmarshalTypeError{Type: vt})
}
}
func (self *_Compiler) compileMapOp(p *_Program, sp int, vt reflect.Type, op _Op) {
i := p.pc()
p.add(_OP_is_null)
p.tag(sp + 1)
p.add(_OP_save)
p.chr(_OP_match_char, '{')
p.add(_OP_map_init)
p.add(_OP_save)
p.add(_OP_lspace)
j := p.pc()
p.chr(_OP_check_char, '}')
p.chr(_OP_match_char, '"')
p.rtt(op, vt)
/* match the closing quote if needed */
if op != _OP_map_key_str && op != _OP_map_key_utext && op != _OP_map_key_utext_p {
p.chr(_OP_match_char, '"')
}
/* match the value separator */
p.add(_OP_lspace)
p.chr(_OP_match_char, ':')
self.compileOne(p, sp + 2, vt.Elem())
p.add(_OP_load)
k0 := p.pc()
p.add(_OP_lspace)
k1 := p.pc()
p.chr(_OP_check_char, '}')
p.chr(_OP_match_char, ',')
p.add(_OP_lspace)
p.chr(_OP_match_char, '"')
p.rtt(op, vt)
/* match the closing quote if needed */
if op != _OP_map_key_str && op != _OP_map_key_utext && op != _OP_map_key_utext_p {
p.chr(_OP_match_char, '"')
}
/* match the value separator */
p.add(_OP_lspace)
p.chr(_OP_match_char, ':')
self.compileOne(p, sp + 2, vt.Elem())
p.add(_OP_load)
p.int(_OP_goto, k0)
p.pin(j)
p.pin(k1)
p.add(_OP_drop_2)
x := p.pc()
p.add(_OP_goto)
p.pin(i)
p.add(_OP_nil_1)
p.pin(x)
}
func (self *_Compiler) compilePtr(p *_Program, sp int, et reflect.Type) {
i := p.pc()
p.add(_OP_is_null)
/* dereference all the way down */
for et.Kind() == reflect.Ptr {
et = et.Elem()
p.rtt(_OP_deref, et)
}
/* compile the element type */
self.compileOne(p, sp + 1, et)
j := p.pc()
p.add(_OP_goto)
p.pin(i)
p.add(_OP_nil_1)
p.pin(j)
}
func (self *_Compiler) compileArray(p *_Program, sp int, vt reflect.Type) {
x := p.pc()
p.add(_OP_is_null)
p.tag(sp)
p.chr(_OP_match_char, '[')
p.add(_OP_save)
p.add(_OP_lspace)
v := []int{p.pc()}
p.chr(_OP_check_char, ']')
/* decode every item */
for i := 1; i <= vt.Len(); i++ {
self.compileOne(p, sp + 1, vt.Elem())
p.add(_OP_load)
p.int(_OP_index, i * int(vt.Elem().Size()))
p.add(_OP_lspace)
v = append(v, p.pc())
p.chr(_OP_check_char, ']')
p.chr(_OP_match_char, ',')
}
/* drop rest of the array */
p.add(_OP_array_skip)
w := p.pc()
p.add(_OP_goto)
p.rel(v)
/* check for pointer data */
if rt.UnpackType(vt.Elem()).PtrData == 0 {
p.int(_OP_array_clear, int(vt.Size()))
} else {
p.int(_OP_array_clear_p, int(vt.Size()))
}
/* restore the stack */
p.pin(w)
p.add(_OP_drop)
p.pin(x)
}
func (self *_Compiler) compileSlice(p *_Program, sp int, et reflect.Type) {
if et.Kind() == byteType.Kind() {
self.compileSliceBin(p, sp, et)
} else {
self.compileSliceList(p, sp, et)
}
}
func (self *_Compiler) compileSliceBin(p *_Program, sp int, et reflect.Type) {
i := p.pc()
p.add(_OP_is_null)
j := p.pc()
p.chr(_OP_check_char, '[')
p.chr(_OP_match_char, '"')
k := p.pc()
p.chr(_OP_check_char, '"')
p.add(_OP_bin)
x := p.pc()
p.add(_OP_goto)
p.pin(j)
self.compileSliceBody(p, sp, et)
y := p.pc()
p.add(_OP_goto)
p.pin(i)
p.pin(k)
p.add(_OP_nil_3)
p.pin(x)
p.pin(y)
}
func (self *_Compiler) compileSliceList(p *_Program, sp int, et reflect.Type) {
i := p.pc()
p.add(_OP_is_null)
p.tag(sp)
p.chr(_OP_match_char, '[')
self.compileSliceBody(p, sp, et)
x := p.pc()
p.add(_OP_goto)
p.pin(i)
p.add(_OP_nil_3)
p.pin(x)
}
func (self *_Compiler) compileSliceBody(p *_Program, sp int, et reflect.Type) {
p.rtt(_OP_slice_init, et)
p.add(_OP_save)
p.add(_OP_lspace)
j := p.pc()
p.chr(_OP_check_char, ']')
p.rtt(_OP_slice_append, et)
self.compileOne(p, sp + 1, et)
p.add(_OP_load)
k0 := p.pc()
p.add(_OP_lspace)
k1 := p.pc()
p.chr(_OP_check_char, ']')
p.chr(_OP_match_char, ',')
p.rtt(_OP_slice_append, et)
self.compileOne(p, sp + 1, et)
p.add(_OP_load)
p.int(_OP_goto, k0)
p.pin(j)
p.pin(k1)
p.add(_OP_drop)
}
func (self *_Compiler) compileString(p *_Program, vt reflect.Type) {
if vt == jsonNumberType {
self.compilePrimitive(p, _OP_num)
} else {
self.compileStringBody(p)
}
}
func (self *_Compiler) compileStringBody(p *_Program) {
i := p.pc()
p.add(_OP_is_null)
p.chr(_OP_match_char, '"')
p.add(_OP_str)
p.pin(i)
}
func (self *_Compiler) compileStruct(p *_Program, sp int, vt reflect.Type) {
if sp >= self.opts.MaxInlineDepth || p.pc() >= _MAX_ILBUF || (sp > 0 && vt.NumField() >= _MAX_FIELDS) {
p.rtt(_OP_recurse, vt)
if self.opts.RecursiveDepth > 0 {
self.rec[vt] = true
}
} else {
self.compileStructBody(p, sp, vt)
}
}
func (self *_Compiler) compileStructBody(p *_Program, sp int, vt reflect.Type) {
fv := resolver.ResolveStruct(vt)
fm, sw := caching.CreateFieldMap(len(fv)), make([]int, len(fv))
/* start of object */
p.tag(sp)
n := p.pc()
p.add(_OP_is_null)
p.chr(_OP_match_char, '{')
p.add(_OP_save)
p.add(_OP_lspace)
x := p.pc()
p.chr(_OP_check_char, '}')
p.chr(_OP_match_char, '"')
p.fmv(_OP_struct_field, fm)
p.add(_OP_lspace)
p.chr(_OP_match_char, ':')
p.tab(_OP_switch, sw)
p.add(_OP_object_next)
y0 := p.pc()
p.add(_OP_lspace)
y1 := p.pc()
p.chr(_OP_check_char, '}')
p.chr(_OP_match_char, ',')
/* special case of an empty struct */
if len(fv) == 0 {
p.add(_OP_object_skip)
goto end_of_object
}
/* match the remaining fields */
p.add(_OP_lspace)
p.chr(_OP_match_char, '"')
p.fmv(_OP_struct_field, fm)
p.add(_OP_lspace)
p.chr(_OP_match_char, ':')
p.tab(_OP_switch, sw)
p.add(_OP_object_next)
p.int(_OP_goto, y0)
/* process each field */
for i, f := range fv {
sw[i] = p.pc()
fm.Set(f.Name, i)
/* index to the field */
for _, o := range f.Path {
if p.int(_OP_index, int(o.Size)); o.Kind == resolver.F_deref {
p.rtt(_OP_deref, o.Type)
}
}
/* check for "stringnize" option */
if (f.Opts & resolver.F_stringize) == 0 {
self.compileOne(p, sp + 1, f.Type)
} else {
self.compileStructFieldStr(p, sp + 1, f.Type)
}
/* load the state, and try next field */
p.add(_OP_load)
p.int(_OP_goto, y0)
}
end_of_object:
p.pin(x)
p.pin(y1)
p.add(_OP_drop)
p.pin(n)
}
func (self *_Compiler) checkType(p *_Program, vt reflect.Type) int {
if k := vt.Kind(); k == reflect.Ptr {
return self.checkType(p, vt.Elem())
} else if k == reflect.Interface {
return -1
} else {
x := p.pc()
switch vt.Kind() {
case reflect.Bool : p.add(_OP_check_bool)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
p.chr(_OP_check_num, 1)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
p.chr(_OP_check_num, 0)
case reflect.String :
if vt == jsonNumberType {
p.chr(_OP_check_num, 2)
}else {
p.chr(_OP_check_char_0, '"')
}
case reflect.Array : p.chr(_OP_check_char_0, '[')
case reflect.Map : p.chr(_OP_check_char_0, '{')
case reflect.Slice :
if vt == bytesType {
p.chr(_OP_check_bytes, '"')
} else {
p.chr(_OP_check_char_0, '[')
}
case reflect.Struct : p.chr(_OP_check_char_0, '{')
default : panic(&json.UnmarshalTypeError{Type: vt})
}
p.rtt(_OP_dismatch_err, vt)
p.add(_OP_object_next)
y := p.pc()
p.add(_OP_goto)
p.pin(x)
return y
}
}
func (self *_Compiler) compileStructFieldStr(p *_Program, sp int, vt reflect.Type) {
n1 := -1
ft := vt
sv := false
/* dereference the pointer if needed */
if ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
/* check if it can be stringized */
switch ft.Kind() {
case reflect.Bool : sv = true
case reflect.Int : sv = true
case reflect.Int8 : sv = true
case reflect.Int16 : sv = true
case reflect.Int32 : sv = true
case reflect.Int64 : sv = true
case reflect.Uint : sv = true
case reflect.Uint8 : sv = true
case reflect.Uint16 : sv = true
case reflect.Uint32 : sv = true
case reflect.Uint64 : sv = true
case reflect.Uintptr : sv = true
case reflect.Float32 : sv = true
case reflect.Float64 : sv = true
case reflect.String : sv = true
}
/* if it's not, ignore the "string" and follow the regular path */
if !sv {
self.compileOne(p, sp, vt)
return
}
/* remove the leading space, and match the leading quote */
vk := vt.Kind()
p.add(_OP_lspace)
n0 := p.pc()
p.add(_OP_is_null)
p.chr(_OP_match_char, '"')
/* also check for inner "null" */
n1 = p.pc()
p.add(_OP_is_null_quote)
/* dereference the pointer only when it is not null */
if vk == reflect.Ptr {
vt = vt.Elem()
p.rtt(_OP_deref, vt)
}