-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
sentry_value.c
1357 lines (1217 loc) · 34.5 KB
/
sentry_value.c
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
#include "sentry_boot.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4127) // conditional expression is constant
#endif
#include "../vendor/mpack.h"
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#include "sentry_alloc.h"
#include "sentry_core.h"
#include "sentry_json.h"
#include "sentry_slice.h"
#include "sentry_string.h"
#include "sentry_sync.h"
#include "sentry_utils.h"
#include "sentry_uuid.h"
#include "sentry_value.h"
/**
* Pointer Tagging of `sentry_value_t`
*
* We expect all of the pointers we deal with to be at least 4-byte aligned,
* which means we can use the least significant 2 bits for tagging.
* We only ever save pointers to `thing_t`, which has an `alignof >= 4`, and
* also both our own allocator, and the system allocator should give us
* properly aligned pointers.
*
* xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx00
* ||
* Pointer to a `thing_t`, a refcounted heap allocation - 00
* xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx - A `int32_t` shifted by 32 - 01
* CONST as below - 10
* false - 0010
* true - 0110
* null - 1010
*/
#define TAG_MASK 0x3
#define TAG_INT32 0x1
#define TAG_CONST 0x2
#define CONST_FALSE 0x2
#define CONST_TRUE 0x6
#define CONST_NULL 0xa
#define THING_TYPE_MASK 0x7f
#define THING_TYPE_FROZEN 0x80
#define THING_TYPE_LIST 0
#define THING_TYPE_OBJECT 1
#define THING_TYPE_STRING 2
#define THING_TYPE_DOUBLE 3
/* internal value helpers */
typedef struct {
union {
void *_ptr;
double _double;
} payload;
long refcount;
uint8_t type;
} thing_t;
typedef struct {
sentry_value_t *items;
size_t len;
size_t allocated;
} list_t;
typedef struct {
char *k;
sentry_value_t v;
} obj_pair_t;
typedef struct {
obj_pair_t *pairs;
size_t len;
size_t allocated;
} obj_t;
static const char *
level_as_string(sentry_level_t level)
{
switch (level) {
case SENTRY_LEVEL_DEBUG:
return "debug";
case SENTRY_LEVEL_WARNING:
return "warning";
case SENTRY_LEVEL_ERROR:
return "error";
case SENTRY_LEVEL_FATAL:
return "fatal";
case SENTRY_LEVEL_INFO:
default:
return "info";
}
}
static bool
reserve(void **buf, size_t item_size, size_t *allocated, size_t min_len)
{
if (*allocated >= min_len) {
return true;
}
size_t new_allocated = *allocated;
if (new_allocated == 0) {
new_allocated = 16;
}
while (new_allocated < min_len) {
new_allocated *= 2;
}
void *new_buf = sentry_malloc(new_allocated * item_size);
if (!new_buf) {
return false;
}
if (*buf) {
memcpy(new_buf, *buf, *allocated * item_size);
sentry_free(*buf);
}
*buf = new_buf;
*allocated = new_allocated;
return true;
}
static int
thing_get_type(const thing_t *thing)
{
return thing->type & (uint8_t)THING_TYPE_MASK;
}
static void
thing_free(thing_t *thing)
{
switch (thing_get_type(thing)) {
case THING_TYPE_LIST: {
list_t *list = thing->payload._ptr;
for (size_t i = 0; i < list->len; i++) {
sentry_value_decref(list->items[i]);
}
sentry_free(list->items);
sentry_free(list);
break;
}
case THING_TYPE_OBJECT: {
obj_t *obj = thing->payload._ptr;
for (size_t i = 0; i < obj->len; i++) {
sentry_free(obj->pairs[i].k);
sentry_value_decref(obj->pairs[i].v);
}
sentry_free(obj->pairs);
sentry_free(obj);
break;
}
case THING_TYPE_STRING: {
sentry_free(thing->payload._ptr);
break;
}
}
sentry_free(thing);
}
static int
thing_is_frozen(const thing_t *thing)
{
return (thing->type >> 7) != 0;
}
static void
thing_freeze(thing_t *thing)
{
if (thing_is_frozen(thing)) {
return;
}
thing->type |= 0x80;
switch (thing_get_type(thing)) {
case THING_TYPE_LIST: {
const list_t *l = thing->payload._ptr;
for (size_t i = 0; i < l->len; i++) {
sentry_value_freeze(l->items[i]);
}
break;
}
case THING_TYPE_OBJECT: {
const obj_t *o = thing->payload._ptr;
for (size_t i = 0; i < o->len; i++) {
sentry_value_freeze(o->pairs[i].v);
}
break;
}
default:;
}
}
static sentry_value_t
new_thing_value(void *ptr, uint8_t thing_type)
{
thing_t *thing = sentry_malloc(sizeof(thing_t));
if (!thing) {
return sentry_value_new_null();
}
thing->payload._ptr = ptr;
thing->refcount = 1;
thing->type = thing_type;
sentry_value_t rv;
rv._bits = (uint64_t)(size_t)thing;
return rv;
}
static thing_t *
value_as_thing(sentry_value_t value)
{
if (value._bits & TAG_MASK) {
return NULL;
}
return (thing_t *)(size_t)value._bits;
}
static thing_t *
value_as_unfrozen_thing(sentry_value_t value)
{
thing_t *thing = value_as_thing(value);
return thing && !thing_is_frozen(thing) ? thing : NULL;
}
/* public api implementations */
void
sentry_value_incref(sentry_value_t value)
{
thing_t *thing = value_as_thing(value);
if (thing) {
sentry__atomic_fetch_and_add(&thing->refcount, 1);
}
}
void
sentry_value_decref(sentry_value_t value)
{
thing_t *thing = value_as_thing(value);
if (thing && sentry__atomic_fetch_and_add(&thing->refcount, -1) == 1) {
thing_free(thing);
}
}
size_t
sentry_value_refcount(sentry_value_t value)
{
thing_t *thing = value_as_thing(value);
return thing ? (size_t)sentry__atomic_fetch(&thing->refcount) : 1;
}
void
sentry_value_freeze(sentry_value_t value)
{
thing_t *thing = value_as_thing(value);
if (thing) {
thing_freeze(thing);
}
}
int
sentry_value_is_frozen(sentry_value_t value)
{
const thing_t *thing = value_as_thing(value);
return thing ? thing_is_frozen(thing) : true;
}
sentry_value_t
sentry_value_new_null(void)
{
sentry_value_t rv;
rv._bits = (uint64_t)CONST_NULL;
return rv;
}
sentry_value_t
sentry_value_new_int32(int32_t value)
{
sentry_value_t rv;
rv._bits = ((uint64_t)(uint32_t)value) << 32 | TAG_INT32;
return rv;
}
sentry_value_t
sentry_value_new_double(double value)
{
thing_t *thing = sentry_malloc(sizeof(thing_t));
if (!thing) {
return sentry_value_new_null();
}
thing->payload._double = value;
thing->refcount = 1;
thing->type = (uint8_t)(THING_TYPE_DOUBLE | THING_TYPE_FROZEN);
sentry_value_t rv;
rv._bits = (uint64_t)(size_t)thing;
return rv;
}
sentry_value_t
sentry_value_new_bool(int value)
{
sentry_value_t rv;
rv._bits = (uint64_t)(value ? CONST_TRUE : CONST_FALSE);
return rv;
}
sentry_value_t
sentry_value_new_string_n(const char *value, size_t value_len)
{
char *s = sentry__string_clone_n(value, value_len);
if (!s) {
return sentry_value_new_null();
}
return sentry__value_new_string_owned(s);
}
sentry_value_t
sentry_value_new_string(const char *value)
{
return value ? sentry_value_new_string_n(value, strlen(value))
: sentry_value_new_null();
}
sentry_value_t
sentry_value_new_list(void)
{
list_t *l = SENTRY_MAKE(list_t);
if (l) {
memset(l, 0, sizeof(list_t));
sentry_value_t rv = new_thing_value(l, THING_TYPE_LIST);
if (sentry_value_is_null(rv)) {
sentry_free(l);
}
return rv;
} else {
return sentry_value_new_null();
}
}
sentry_value_t
sentry__value_new_list_with_size(size_t size)
{
list_t *l = SENTRY_MAKE(list_t);
if (l) {
memset(l, 0, sizeof(list_t));
l->allocated = size;
if (size) {
l->items = sentry_malloc(sizeof(sentry_value_t) * size);
if (!l->items) {
sentry_free(l);
return sentry_value_new_null();
}
}
sentry_value_t rv = new_thing_value(l, THING_TYPE_LIST);
if (sentry_value_is_null(rv)) {
sentry_free(l->items);
sentry_free(l);
}
return rv;
} else {
return sentry_value_new_null();
}
}
sentry_value_t
sentry_value_new_object(void)
{
obj_t *o = SENTRY_MAKE(obj_t);
if (o) {
memset(o, 0, sizeof(obj_t));
sentry_value_t rv = new_thing_value(o, THING_TYPE_OBJECT);
if (sentry_value_is_null(rv)) {
sentry_free(o);
}
return rv;
} else {
return sentry_value_new_null();
}
}
sentry_value_t
sentry__value_new_object_with_size(size_t size)
{
obj_t *o = SENTRY_MAKE(obj_t);
if (o) {
memset(o, 0, sizeof(obj_t));
o->allocated = size;
if (size) {
o->pairs = sentry_malloc(sizeof(obj_pair_t) * size);
if (!o->pairs) {
sentry_free(o);
return sentry_value_new_null();
}
}
sentry_value_t rv = new_thing_value(o, THING_TYPE_OBJECT);
if (sentry_value_is_null(rv)) {
sentry_free(o->pairs);
sentry_free(o);
}
return rv;
} else {
return sentry_value_new_null();
}
}
sentry_value_type_t
sentry_value_get_type(sentry_value_t value)
{
if (sentry_value_is_null(value)) {
return SENTRY_VALUE_TYPE_NULL;
}
const thing_t *thing = value_as_thing(value);
if (thing) {
switch (thing_get_type(thing)) {
case THING_TYPE_STRING:
return SENTRY_VALUE_TYPE_STRING;
case THING_TYPE_LIST:
return SENTRY_VALUE_TYPE_LIST;
case THING_TYPE_OBJECT:
return SENTRY_VALUE_TYPE_OBJECT;
case THING_TYPE_DOUBLE:
return SENTRY_VALUE_TYPE_DOUBLE;
}
assert(!"unreachable");
} else if ((value._bits & TAG_MASK) == TAG_CONST) {
return SENTRY_VALUE_TYPE_BOOL;
} else if ((value._bits & TAG_MASK) == TAG_INT32) {
return SENTRY_VALUE_TYPE_INT32;
}
assert(!"unreachable");
return SENTRY_VALUE_TYPE_NULL;
}
int
sentry_value_set_by_key_n(
sentry_value_t value, const char *k, size_t k_len, sentry_value_t v)
{
if (!k) {
goto fail;
}
sentry_slice_t k_slice = { k, k_len };
thing_t *thing = value_as_unfrozen_thing(value);
if (!thing || thing_get_type(thing) != THING_TYPE_OBJECT) {
goto fail;
}
obj_t *o = thing->payload._ptr;
for (size_t i = 0; i < o->len; i++) {
obj_pair_t *pair = &o->pairs[i];
if (sentry__slice_eqs(k_slice, pair->k)) {
sentry_value_decref(pair->v);
pair->v = v;
return 0;
}
}
if (!reserve((void **)&o->pairs, sizeof(o->pairs[0]), &o->allocated,
o->len + 1)) {
goto fail;
}
obj_pair_t pair;
pair.k = sentry__slice_to_owned(k_slice);
if (!pair.k) {
goto fail;
}
pair.v = v;
o->pairs[o->len++] = pair;
return 0;
fail:
sentry_value_decref(v);
return 1;
}
int
sentry_value_set_by_key(sentry_value_t value, const char *k, sentry_value_t v)
{
if (k) {
return sentry_value_set_by_key_n(value, k, strlen(k), v);
}
sentry_value_decref(v);
return 1;
}
int
sentry_value_remove_by_key_n(sentry_value_t value, const char *k, size_t k_len)
{
if (!k) {
return 1;
}
sentry_slice_t k_slice = { k, k_len };
thing_t *thing = value_as_unfrozen_thing(value);
if (!thing || thing_get_type(thing) != THING_TYPE_OBJECT) {
return 1;
}
obj_t *o = thing->payload._ptr;
for (size_t i = 0; i < o->len; i++) {
obj_pair_t *pair = &o->pairs[i];
if (sentry__slice_eqs(k_slice, pair->k)) {
sentry_free(pair->k);
sentry_value_decref(pair->v);
memmove(o->pairs + i, o->pairs + i + 1,
(o->len - i - 1) * sizeof(o->pairs[0]));
o->len--;
return 0;
}
}
return 1;
}
int
sentry_value_remove_by_key(sentry_value_t value, const char *k)
{
if (k) {
return sentry_value_remove_by_key_n(value, k, strlen(k));
}
return 1;
}
int
sentry_value_append(sentry_value_t value, sentry_value_t v)
{
thing_t *thing = value_as_unfrozen_thing(value);
if (!thing || thing_get_type(thing) != THING_TYPE_LIST) {
goto fail;
}
list_t *l = thing->payload._ptr;
if (!reserve((void **)&l->items, sizeof(l->items[0]), &l->allocated,
l->len + 1)) {
goto fail;
}
l->items[l->len++] = v;
return 0;
fail:
sentry_value_decref(v);
return 1;
}
sentry_uuid_t
sentry__value_as_uuid(sentry_value_t value)
{
const char *val = sentry_value_as_string(value);
if (val) {
return sentry_uuid_from_string(val);
} else {
return sentry_uuid_nil();
}
}
char *
sentry__value_stringify(sentry_value_t value)
{
switch (sentry_value_get_type(value)) {
case SENTRY_VALUE_TYPE_LIST:
case SENTRY_VALUE_TYPE_OBJECT:
case SENTRY_VALUE_TYPE_NULL:
return sentry__string_clone("");
case SENTRY_VALUE_TYPE_BOOL:
return sentry__string_clone(
sentry_value_is_true(value) ? "true" : "false");
case SENTRY_VALUE_TYPE_STRING:
return sentry__string_clone(sentry_value_as_string(value));
default: {
char buf[24];
size_t written = (size_t)sentry__snprintf_c(
buf, sizeof(buf), "%g", sentry_value_as_double(value));
if (written >= sizeof(buf)) {
return sentry__string_clone("");
}
buf[written] = '\0';
return sentry__string_clone(buf);
}
}
}
sentry_value_t
sentry__value_clone(sentry_value_t value)
{
const thing_t *thing = value_as_thing(value);
if (!thing) {
return value;
}
switch (thing_get_type(thing)) {
case THING_TYPE_LIST: {
const list_t *list = thing->payload._ptr;
sentry_value_t rv = sentry__value_new_list_with_size(list->len);
for (size_t i = 0; i < list->len; i++) {
sentry_value_incref(list->items[i]);
sentry_value_append(rv, list->items[i]);
}
return rv;
}
case THING_TYPE_OBJECT: {
const obj_t *obj = thing->payload._ptr;
sentry_value_t rv = sentry__value_new_object_with_size(obj->len);
for (size_t i = 0; i < obj->len; i++) {
sentry_value_incref(obj->pairs[i].v);
sentry_value_set_by_key(rv, obj->pairs[i].k, obj->pairs[i].v);
}
return rv;
}
case THING_TYPE_STRING:
case THING_TYPE_DOUBLE:
sentry_value_incref(value);
return value;
default:
return sentry_value_new_null();
}
}
int
sentry__value_append_bounded(sentry_value_t value, sentry_value_t v, size_t max)
{
thing_t *thing = value_as_unfrozen_thing(value);
if (!thing || thing_get_type(thing) != THING_TYPE_LIST) {
goto fail;
}
list_t *l = thing->payload._ptr;
if (l->len < max) {
return sentry_value_append(value, v);
}
// len: 120
// max: 100
// move to 0
// move 99 items (len - 1)
// from 20
size_t to_move = max >= 1 ? max - 1 : 0;
size_t to_shift = l->len - to_move;
for (size_t i = 0; i < to_shift; i++) {
sentry_value_decref(l->items[i]);
}
if (to_move) {
memmove(l->items, l->items + to_shift, to_move * sizeof(l->items[0]));
}
if (max >= 1) {
l->items[max - 1] = v;
} else {
sentry_value_decref(v);
}
l->len = max;
return 0;
fail:
sentry_value_decref(v);
return 1;
}
int
sentry_value_set_by_index(sentry_value_t value, size_t index, sentry_value_t v)
{
thing_t *thing = value_as_unfrozen_thing(value);
if (!thing || thing_get_type(thing) != THING_TYPE_LIST) {
goto fail;
}
list_t *l = thing->payload._ptr;
if (!reserve(
(void *)&l->items, sizeof(l->items[0]), &l->allocated, index + 1)) {
goto fail;
}
if (index >= l->len) {
for (size_t i = l->len; i < index + 1; i++) {
l->items[i] = sentry_value_new_null();
}
l->len = index + 1;
}
sentry_value_decref(l->items[index]);
l->items[index] = v;
return 0;
fail:
sentry_value_decref(v);
return 1;
}
int
sentry_value_remove_by_index(sentry_value_t value, size_t index)
{
thing_t *thing = value_as_unfrozen_thing(value);
if (!thing || thing_get_type(thing) != THING_TYPE_LIST) {
return 1;
}
list_t *l = thing->payload._ptr;
if (index >= l->len) {
return 0;
}
sentry_value_decref(l->items[index]);
memmove(l->items + index, l->items + index + 1,
(l->len - index - 1) * sizeof(l->items[0]));
l->len--;
return 0;
}
sentry_value_t
sentry_value_get_by_key_n(sentry_value_t value, const char *k, size_t k_len)
{
if (!k) {
return sentry_value_new_null();
}
const thing_t *thing = value_as_thing(value);
if (thing && thing_get_type(thing) == THING_TYPE_OBJECT) {
obj_t *o = thing->payload._ptr;
for (size_t i = 0; i < o->len; i++) {
obj_pair_t *pair = &o->pairs[i];
if (sentry__slice_eqs((sentry_slice_t) { k, k_len }, pair->k)) {
return pair->v;
}
}
}
return sentry_value_new_null();
}
sentry_value_t
sentry_value_get_by_key(sentry_value_t value, const char *k)
{
const size_t k_len = k ? strlen(k) : 0;
return sentry_value_get_by_key_n(value, k, k_len);
}
sentry_value_t
sentry_value_get_by_key_owned_n(
sentry_value_t value, const char *k, size_t k_len)
{
sentry_value_t rv = sentry_value_get_by_key_n(value, k, k_len);
sentry_value_incref(rv);
return rv;
}
sentry_value_t
sentry_value_get_by_key_owned(sentry_value_t value, const char *k)
{
if (k) {
return sentry_value_get_by_key_owned_n(value, k, strlen(k));
}
sentry_value_t rv = sentry_value_new_null();
sentry_value_incref(rv);
return rv;
}
sentry_value_t
sentry_value_get_by_index(sentry_value_t value, size_t index)
{
const thing_t *thing = value_as_thing(value);
if (thing && thing_get_type(thing) == THING_TYPE_LIST) {
list_t *l = thing->payload._ptr;
if (index < l->len) {
return l->items[index];
}
}
return sentry_value_new_null();
}
sentry_value_t
sentry_value_get_by_index_owned(sentry_value_t value, size_t index)
{
sentry_value_t rv = sentry_value_get_by_index(value, index);
sentry_value_incref(rv);
return rv;
}
size_t
sentry_value_get_length(sentry_value_t value)
{
const thing_t *thing = value_as_thing(value);
if (thing) {
switch (thing_get_type(thing)) {
case THING_TYPE_STRING:
return strlen(thing->payload._ptr);
case THING_TYPE_LIST:
return ((const list_t *)thing->payload._ptr)->len;
case THING_TYPE_OBJECT:
return ((const obj_t *)thing->payload._ptr)->len;
}
}
return 0;
}
int32_t
sentry_value_as_int32(sentry_value_t value)
{
if ((value._bits & TAG_MASK) == TAG_INT32) {
return (int32_t)((int64_t)value._bits >> 32);
} else {
return 0;
}
}
double
sentry_value_as_double(sentry_value_t value)
{
if ((value._bits & TAG_MASK) == TAG_INT32) {
return (double)sentry_value_as_int32(value);
}
const thing_t *thing = value_as_thing(value);
if (thing && thing_get_type(thing) == THING_TYPE_DOUBLE) {
return thing->payload._double;
} else {
return NAN;
}
}
const char *
sentry_value_as_string(sentry_value_t value)
{
const thing_t *thing = value_as_thing(value);
if (thing && thing_get_type(thing) == THING_TYPE_STRING) {
return (const char *)thing->payload._ptr;
} else {
return "";
}
}
int
sentry_value_is_true(sentry_value_t value)
{
if (value._bits == CONST_TRUE) {
return 1;
}
switch (sentry_value_get_type(value)) {
case SENTRY_VALUE_TYPE_BOOL:
case SENTRY_VALUE_TYPE_NULL:
return 0;
case SENTRY_VALUE_TYPE_INT32:
return sentry_value_as_int32(value) != 0;
case SENTRY_VALUE_TYPE_DOUBLE:
return sentry_value_as_double(value) != 0.0;
default:
return sentry_value_get_length(value) > 0;
}
}
int
sentry_value_is_null(sentry_value_t value)
{
return value._bits == CONST_NULL;
}
int
sentry__value_merge_objects(sentry_value_t dst, sentry_value_t src)
{
if (sentry_value_is_null(src)) {
return 0;
}
if (sentry_value_get_type(dst) != SENTRY_VALUE_TYPE_OBJECT
|| sentry_value_get_type(src) != SENTRY_VALUE_TYPE_OBJECT
|| sentry_value_is_frozen(dst)) {
return 1;
}
thing_t *thing = value_as_thing(src);
if (!thing) {
return 1;
}
obj_t *obj = thing->payload._ptr;
for (size_t i = 0; i < obj->len; i++) {
char *key = obj->pairs[i].k;
sentry_value_t src_val = obj->pairs[i].v;
sentry_value_t dst_val = sentry_value_get_by_key(dst, key);
if (sentry_value_get_type(dst_val) == SENTRY_VALUE_TYPE_OBJECT
&& sentry_value_get_type(src_val) == SENTRY_VALUE_TYPE_OBJECT) {
if (sentry__value_merge_objects(dst_val, src_val) != 0) {
return 1;
}
} else {
if (sentry_value_set_by_key(dst, key, src_val) != 0) {
return 1;
}
sentry_value_incref(src_val);
}
}
return 0;
}
void
sentry__jsonwriter_write_value(sentry_jsonwriter_t *jw, sentry_value_t value)
{
switch (sentry_value_get_type(value)) {
case SENTRY_VALUE_TYPE_NULL:
sentry__jsonwriter_write_null(jw);
break;
case SENTRY_VALUE_TYPE_BOOL:
sentry__jsonwriter_write_bool(jw, sentry_value_is_true(value));
break;
case SENTRY_VALUE_TYPE_INT32:
sentry__jsonwriter_write_int32(jw, sentry_value_as_int32(value));
break;
case SENTRY_VALUE_TYPE_DOUBLE:
sentry__jsonwriter_write_double(jw, sentry_value_as_double(value));
break;
case SENTRY_VALUE_TYPE_STRING:
sentry__jsonwriter_write_str(jw, sentry_value_as_string(value));
break;
case SENTRY_VALUE_TYPE_LIST: {
const list_t *l = value_as_thing(value)->payload._ptr;
sentry__jsonwriter_write_list_start(jw);
for (size_t i = 0; i < l->len; i++) {
sentry__jsonwriter_write_value(jw, l->items[i]);
}
sentry__jsonwriter_write_list_end(jw);
break;
}
case SENTRY_VALUE_TYPE_OBJECT: {
const obj_t *o = value_as_thing(value)->payload._ptr;
sentry__jsonwriter_write_object_start(jw);
for (size_t i = 0; i < o->len; i++) {
sentry__jsonwriter_write_key(jw, o->pairs[i].k);
sentry__jsonwriter_write_value(jw, o->pairs[i].v);
}
sentry__jsonwriter_write_object_end(jw);
break;
}
}
}
char *
sentry_value_to_json(sentry_value_t value)
{
sentry_jsonwriter_t *jw = sentry__jsonwriter_new(NULL);
if (!jw) {
return NULL;
}
sentry__jsonwriter_write_value(jw, value);
return sentry__jsonwriter_into_string(jw, NULL);
}
static void
value_to_msgpack(mpack_writer_t *writer, sentry_value_t value)
{
switch (sentry_value_get_type(value)) {
case SENTRY_VALUE_TYPE_NULL:
mpack_write_nil(writer);
break;
case SENTRY_VALUE_TYPE_BOOL:
mpack_write_bool(writer, sentry_value_is_true(value) ? true : false);
break;
case SENTRY_VALUE_TYPE_INT32:
mpack_write_i32(writer, sentry_value_as_int32(value));
break;
case SENTRY_VALUE_TYPE_DOUBLE:
mpack_write_double(writer, sentry_value_as_double(value));
break;
case SENTRY_VALUE_TYPE_STRING: {
mpack_write_cstr_or_nil(writer, sentry_value_as_string(value));
break;
}
case SENTRY_VALUE_TYPE_LIST: {
const list_t *l = value_as_thing(value)->payload._ptr;
mpack_start_array(writer, (uint32_t)l->len);
for (size_t i = 0; i < l->len; i++) {
value_to_msgpack(writer, l->items[i]);
}
mpack_finish_array(writer);
break;
}
case SENTRY_VALUE_TYPE_OBJECT: {
const obj_t *o = value_as_thing(value)->payload._ptr;
mpack_start_map(writer, (uint32_t)o->len);
for (size_t i = 0; i < o->len; i++) {
mpack_write_cstr(writer, o->pairs[i].k);
value_to_msgpack(writer, o->pairs[i].v);
}
mpack_finish_map(writer);
break;
}
}
}
char *