-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathjv.c
1983 lines (1703 loc) · 50.8 KB
/
jv.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
/*
* Portions Copyright (c) 2016 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdint.h>
#include <stddef.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <math.h>
#include <float.h>
#include "jv_alloc.h"
#include "jv.h"
#include "jv_unicode.h"
#include "util.h"
/*
* Internal refcounting helpers
*/
typedef struct jv_refcnt {
int count;
} jv_refcnt;
static const jv_refcnt JV_REFCNT_INIT = {1};
static void jvp_refcnt_inc(jv_refcnt* c) {
c->count++;
}
static int jvp_refcnt_dec(jv_refcnt* c) {
c->count--;
return c->count == 0;
}
static int jvp_refcnt_unshared(jv_refcnt* c) {
assert(c->count > 0);
return c->count == 1;
}
#define KIND_MASK 0xF
#define PFLAGS_MASK 0xF0
#define PTYPE_MASK 0x70
typedef enum {
JVP_PAYLOAD_NONE = 0,
JVP_PAYLOAD_ALLOCATED = 0x80,
} payload_flags;
#define JVP_MAKE_PFLAGS(ptype, allocated) ((((ptype) << 4) & PTYPE_MASK) | ((allocated) ? JVP_PAYLOAD_ALLOCATED : 0))
#define JVP_MAKE_FLAGS(kind, pflags) ((kind & KIND_MASK) | (pflags & PFLAGS_MASK))
#define JVP_FLAGS(j) ((j).kind_flags)
#define JVP_KIND(j) (JVP_FLAGS(j) & KIND_MASK)
#define JVP_HAS_FLAGS(j, flags) (JVP_FLAGS(j) == flags)
#define JVP_HAS_KIND(j, kind) (JVP_KIND(j) == kind)
#define JVP_IS_ALLOCATED(j) (j.kind_flags & JVP_PAYLOAD_ALLOCATED)
#define JVP_FLAGS_NULL JVP_MAKE_FLAGS(JV_KIND_NULL, JVP_PAYLOAD_NONE)
#define JVP_FLAGS_INVALID JVP_MAKE_FLAGS(JV_KIND_INVALID, JVP_PAYLOAD_NONE)
#define JVP_FLAGS_FALSE JVP_MAKE_FLAGS(JV_KIND_FALSE, JVP_PAYLOAD_NONE)
#define JVP_FLAGS_TRUE JVP_MAKE_FLAGS(JV_KIND_TRUE, JVP_PAYLOAD_NONE)
jv_kind jv_get_kind(jv x) {
return JVP_KIND(x);
}
const char* jv_kind_name(jv_kind k) {
switch (k) {
case JV_KIND_INVALID: return "<invalid>";
case JV_KIND_NULL: return "null";
case JV_KIND_FALSE: return "boolean";
case JV_KIND_TRUE: return "boolean";
case JV_KIND_NUMBER: return "number";
case JV_KIND_STRING: return "string";
case JV_KIND_ARRAY: return "array";
case JV_KIND_OBJECT: return "object";
}
assert(0 && "invalid kind");
return "<unknown>";
}
const jv JV_NULL = {JVP_FLAGS_NULL, 0, 0, 0, {0}};
const jv JV_INVALID = {JVP_FLAGS_INVALID, 0, 0, 0, {0}};
const jv JV_FALSE = {JVP_FLAGS_FALSE, 0, 0, 0, {0}};
const jv JV_TRUE = {JVP_FLAGS_TRUE, 0, 0, 0, {0}};
jv jv_true() {
return JV_TRUE;
}
jv jv_false() {
return JV_FALSE;
}
jv jv_null() {
return JV_NULL;
}
jv jv_bool(int x) {
return x ? JV_TRUE : JV_FALSE;
}
/*
* Invalid objects, with optional error messages
*/
#define JVP_FLAGS_INVALID_MSG JVP_MAKE_FLAGS(JV_KIND_INVALID, JVP_PAYLOAD_ALLOCATED)
typedef struct {
jv_refcnt refcnt;
jv errmsg;
} jvp_invalid;
jv jv_invalid_with_msg(jv err) {
jvp_invalid* i = jv_mem_alloc(sizeof(jvp_invalid));
i->refcnt = JV_REFCNT_INIT;
i->errmsg = err;
jv x = {JVP_FLAGS_INVALID_MSG, 0, 0, 0, {&i->refcnt}};
return x;
}
jv jv_invalid() {
return JV_INVALID;
}
jv jv_invalid_get_msg(jv inv) {
assert(JVP_HAS_KIND(inv, JV_KIND_INVALID));
jv x;
if (JVP_HAS_FLAGS(inv, JVP_FLAGS_INVALID_MSG)) {
x = jv_copy(((jvp_invalid*)inv.u.ptr)->errmsg);
}
else {
x = jv_null();
}
jv_free(inv);
return x;
}
int jv_invalid_has_msg(jv inv) {
assert(JVP_HAS_KIND(inv, JV_KIND_INVALID));
int r = JVP_HAS_FLAGS(inv, JVP_FLAGS_INVALID_MSG);
jv_free(inv);
return r;
}
static void jvp_invalid_free(jv x) {
assert(JVP_HAS_KIND(x, JV_KIND_INVALID));
if (JVP_HAS_FLAGS(x, JVP_FLAGS_INVALID_MSG) && jvp_refcnt_dec(x.u.ptr)) {
jv_free(((jvp_invalid*)x.u.ptr)->errmsg);
jv_mem_free(x.u.ptr);
}
}
/*
* Numbers
*/
#ifdef USE_DECNUM
#include "jv_dtoa.h"
#include "jv_dtoa_tsd.h"
// we will manage the space for the struct
#define DECNUMDIGITS 1
#include "decNumber/decNumber.h"
enum {
JVP_NUMBER_NATIVE = 0,
JVP_NUMBER_DECIMAL = 1
};
#define JV_NUMBER_SIZE_INIT (0)
#define JV_NUMBER_SIZE_CONVERTED (1)
#define JVP_FLAGS_NUMBER_NATIVE JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_NATIVE, 0))
#define JVP_FLAGS_NUMBER_LITERAL JVP_MAKE_FLAGS(JV_KIND_NUMBER, JVP_MAKE_PFLAGS(JVP_NUMBER_DECIMAL, 1))
// the decimal precision of binary double
#define DEC_NUMBER_DOUBLE_PRECISION (17)
#define DEC_NUMBER_STRING_GUARD (14)
#define DEC_NUMBER_DOUBLE_EXTRA_UNITS ((DEC_NUMBER_DOUBLE_PRECISION - DECNUMDIGITS + DECDPUN - 1)/DECDPUN)
#include "jv_thread.h"
#ifdef WIN32
#ifndef __MINGW32__
/* Copied from Heimdal: thread-specific keys; see lib/base/dll.c in Heimdal */
/*
* This is an implementation of thread-specific storage with
* destructors. WIN32 doesn't quite have this. Instead it has
* DllMain(), an entry point in every DLL that gets called to notify the
* DLL of thread/process "attach"/"detach" events.
*
* We use __thread (or __declspec(thread)) for the thread-local itself
* and DllMain() DLL_THREAD_DETACH events to drive destruction of
* thread-local values.
*
* When building in maintainer mode on non-Windows pthread systems this
* uses a single pthread key instead to implement multiple keys. This
* keeps the code from rotting when modified by non-Windows developers.
*/
/* Logical array of keys that grows lock-lessly */
typedef struct tls_keys tls_keys;
struct tls_keys {
void (**keys_dtors)(void *); /* array of destructors */
size_t keys_start_idx; /* index of first destructor */
size_t keys_num;
tls_keys *keys_next;
};
/*
* Well, not quite locklessly. We need synchronization primitives to do
* this locklessly. An atomic CAS will do.
*/
static pthread_mutex_t tls_key_defs_lock = PTHREAD_MUTEX_INITIALIZER;
static tls_keys *tls_key_defs;
/* Logical array of values (per-thread; no locking needed here) */
struct tls_values {
void **values; /* realloc()ed */
size_t values_num;
};
#ifdef _MSC_VER
static __declspec(thread) struct nomem_handler nomem_handler;
#else
static __thread struct tls_values values;
#endif
#define DEAD_KEY ((void *)8)
static void
w32_service_thread_detach(void *unused)
{
tls_keys *key_defs;
void (*dtor)(void*);
size_t i;
pthread_mutex_lock(&tls_key_defs_lock);
key_defs = tls_key_defs;
pthread_mutex_unlock(&tls_key_defs_lock);
if (key_defs == NULL)
return;
for (i = 0; i < values.values_num; i++) {
assert(i >= key_defs->keys_start_idx);
if (i >= key_defs->keys_start_idx + key_defs->keys_num) {
pthread_mutex_lock(&tls_key_defs_lock);
key_defs = key_defs->keys_next;
pthread_mutex_unlock(&tls_key_defs_lock);
assert(key_defs != NULL);
assert(i >= key_defs->keys_start_idx);
assert(i < key_defs->keys_start_idx + key_defs->keys_num);
}
dtor = key_defs->keys_dtors[i - key_defs->keys_start_idx];
if (values.values[i] != NULL && dtor != NULL && dtor != DEAD_KEY)
dtor(values.values[i]);
values.values[i] = NULL;
}
}
extern void jv_tsd_dtoa_ctx_init();
extern void jv_tsd_dtoa_ctx_fini();
void jv_tsd_dec_ctx_fini();
void jv_tsd_dec_ctx_init();
BOOL WINAPI DllMain(HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
/*create_pt_key();*/
jv_tsd_dtoa_ctx_init();
jv_tsd_dec_ctx_init();
return TRUE;
case DLL_PROCESS_DETACH:
jv_tsd_dtoa_ctx_fini();
jv_tsd_dec_ctx_fini();
return TRUE;
case DLL_THREAD_ATTACH: return 0;
case DLL_THREAD_DETACH:
w32_service_thread_detach(NULL);
return TRUE;
default: return TRUE;
}
}
int
pthread_key_create(pthread_key_t *key, void (*dtor)(void *))
{
tls_keys *key_defs, *new_key_defs;
size_t i, k;
int ret = ENOMEM;
pthread_mutex_lock(&tls_key_defs_lock);
if (tls_key_defs == NULL) {
/* First key */
new_key_defs = calloc(1, sizeof(*new_key_defs));
if (new_key_defs == NULL) {
pthread_mutex_unlock(&tls_key_defs_lock);
return ENOMEM;
}
new_key_defs->keys_num = 8;
new_key_defs->keys_dtors = calloc(new_key_defs->keys_num,
sizeof(*new_key_defs->keys_dtors));
if (new_key_defs->keys_dtors == NULL) {
pthread_mutex_unlock(&tls_key_defs_lock);
free(new_key_defs);
return ENOMEM;
}
tls_key_defs = new_key_defs;
new_key_defs->keys_dtors[0] = dtor;
for (i = 1; i < new_key_defs->keys_num; i++)
new_key_defs->keys_dtors[i] = NULL;
pthread_mutex_unlock(&tls_key_defs_lock);
return 0;
}
for (key_defs = tls_key_defs;
key_defs != NULL;
key_defs = key_defs->keys_next) {
k = key_defs->keys_start_idx;
for (i = 0; i < key_defs->keys_num; i++, k++) {
if (key_defs->keys_dtors[i] == NULL) {
/* Found free slot; use it */
key_defs->keys_dtors[i] = dtor;
*key = k;
pthread_mutex_unlock(&tls_key_defs_lock);
return 0;
}
}
if (key_defs->keys_next != NULL)
continue;
/* Grow the registration array */
/* XXX DRY */
new_key_defs = calloc(1, sizeof(*new_key_defs));
if (new_key_defs == NULL)
break;
new_key_defs->keys_dtors =
calloc(key_defs->keys_num + key_defs->keys_num / 2,
sizeof(*new_key_defs->keys_dtors));
if (new_key_defs->keys_dtors == NULL) {
free(new_key_defs);
break;
}
new_key_defs->keys_start_idx = key_defs->keys_start_idx +
key_defs->keys_num;
new_key_defs->keys_num = key_defs->keys_num + key_defs->keys_num / 2;
new_key_defs->keys_dtors[i] = dtor;
for (i = 1; i < new_key_defs->keys_num; i++)
new_key_defs->keys_dtors[i] = NULL;
key_defs->keys_next = new_key_defs;
ret = 0;
break;
}
pthread_mutex_unlock(&tls_key_defs_lock);
return ret;
}
static void
key_lookup(pthread_key_t key, tls_keys **kd,
size_t *dtor_idx, void (**dtor)(void *))
{
tls_keys *key_defs;
if (kd != NULL)
*kd = NULL;
if (dtor_idx != NULL)
*dtor_idx = 0;
if (dtor != NULL)
*dtor = NULL;
pthread_mutex_lock(&tls_key_defs_lock);
key_defs = tls_key_defs;
pthread_mutex_unlock(&tls_key_defs_lock);
while (key_defs != NULL) {
if (key >= key_defs->keys_start_idx &&
key < key_defs->keys_start_idx + key_defs->keys_num) {
if (kd != NULL)
*kd = key_defs;
if (dtor_idx != NULL)
*dtor_idx = key - key_defs->keys_start_idx;
if (dtor != NULL)
*dtor = key_defs->keys_dtors[key - key_defs->keys_start_idx];
return;
}
pthread_mutex_lock(&tls_key_defs_lock);
key_defs = key_defs->keys_next;
pthread_mutex_unlock(&tls_key_defs_lock);
assert(key_defs != NULL);
assert(key >= key_defs->keys_start_idx);
}
}
int
pthread_setspecific(pthread_key_t key, void *value)
{
void **new_values;
size_t new_num;
void (*dtor)(void *);
size_t i;
key_lookup(key, NULL, NULL, &dtor);
if (dtor == NULL)
return EINVAL;
if (key >= values.values_num) {
if (values.values_num == 0) {
values.values = NULL;
new_num = 8;
} else {
new_num = (values.values_num + values.values_num / 2);
}
new_values = realloc(values.values, sizeof(void *) * new_num);
if (new_values == NULL)
return ENOMEM;
for (i = values.values_num; i < new_num; i++)
new_values[i] = NULL;
values.values = new_values;
values.values_num = new_num;
}
assert(key < values.values_num);
if (values.values[key] != NULL && dtor != NULL && dtor != DEAD_KEY)
dtor(values.values[key]);
values.values[key] = value;
return 0;
}
void *
pthread_getspecific(pthread_key_t key)
{
if (key >= values.values_num)
return NULL;
return values.values[key];
}
#else
#include <pthread.h>
#endif
#else
#include <pthread.h>
#endif
static pthread_key_t dec_ctx_key;
static pthread_once_t dec_ctx_once = PTHREAD_ONCE_INIT;
#define DEC_CONTEXT() tsd_dec_ctx_get(&dec_ctx_key)
// atexit finalizer to clean up the tsd dec contexts if main() exits
// without having called pthread_exit()
void jv_tsd_dec_ctx_fini() {
jv_mem_free(pthread_getspecific(dec_ctx_key));
pthread_setspecific(dec_ctx_key, NULL);
}
void jv_tsd_dec_ctx_init() {
if (pthread_key_create(&dec_ctx_key, jv_mem_free) != 0) {
fprintf(stderr, "error: cannot create thread specific key");
abort();
}
atexit(jv_tsd_dec_ctx_fini);
}
static decContext* tsd_dec_ctx_get(pthread_key_t *key) {
pthread_once(&dec_ctx_once, jv_tsd_dec_ctx_init); // cannot fail
decContext *ctx = (decContext*)pthread_getspecific(*key);
if (ctx) {
return ctx;
}
ctx = malloc(sizeof(decContext));
if (ctx) {
if (key == &dec_ctx_key)
{
decContextDefault(ctx, DEC_INIT_BASE);
// make sure (Int)D2U(rhs->exponent-lhs->exponent) does not overflow
ctx->digits = MIN(DEC_MAX_DIGITS,
INT32_MAX - (DECDPUN - 1) - (ctx->emax - ctx->emin - 1));
ctx->traps = 0; /*no errors*/
}
if (pthread_setspecific(*key, ctx) != 0) {
fprintf(stderr, "error: cannot store thread specific data");
abort();
}
}
return ctx;
}
typedef struct {
jv_refcnt refcnt;
double num_double;
char * literal_data;
decNumber num_decimal; // must be the last field in the structure for memory management
} jvp_literal_number;
typedef struct {
decNumber number;
decNumberUnit units[DEC_NUMBER_DOUBLE_EXTRA_UNITS];
} decNumberDoublePrecision;
static inline int jvp_number_is_literal(jv n) {
assert(JVP_HAS_KIND(n, JV_KIND_NUMBER));
return JVP_HAS_FLAGS(n, JVP_FLAGS_NUMBER_LITERAL);
}
static jvp_literal_number* jvp_literal_number_ptr(jv j) {
assert(JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL));
return (jvp_literal_number*)j.u.ptr;
}
static decNumber* jvp_dec_number_ptr(jv j) {
assert(JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL));
return &(((jvp_literal_number*)j.u.ptr)->num_decimal);
}
static jvp_literal_number* jvp_literal_number_alloc(unsigned literal_length) {
/* The number of units needed is ceil(DECNUMDIGITS/DECDPUN) */
int units = ((literal_length+DECDPUN-1)/DECDPUN);
jvp_literal_number* n = jv_mem_alloc(
sizeof(jvp_literal_number)
+ sizeof(decNumberUnit) * units
);
return n;
}
static jv jvp_literal_number_new(const char * literal) {
jvp_literal_number * n = jvp_literal_number_alloc(strlen(literal));
n->refcnt = JV_REFCNT_INIT;
n->literal_data = NULL;
decContext *ctx = DEC_CONTEXT();
decContextClearStatus(ctx, DEC_Conversion_syntax);
decNumberFromString(&n->num_decimal, literal, ctx);
n->num_double = NAN;
if (ctx->status & DEC_Conversion_syntax) {
jv_mem_free(n);
return JV_INVALID;
}
jv r = {JVP_FLAGS_NUMBER_LITERAL, 0, 0, JV_NUMBER_SIZE_INIT, {&n->refcnt}};
return r;
}
static double jvp_literal_number_to_double(jv j) {
assert(JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL));
decContext dblCtx;
// init as decimal64 but change digits to allow conversion to binary64 (double)
decContextDefault(&dblCtx, DEC_INIT_DECIMAL64);
dblCtx.digits = DEC_NUMBER_DOUBLE_PRECISION;
decNumber *p_dec_number = jvp_dec_number_ptr(j);
decNumberDoublePrecision dec_double;
char literal[DEC_NUMBER_DOUBLE_PRECISION + DEC_NUMBER_STRING_GUARD + 1];
// reduce the number to the shortest possible form
// that fits into the 64 bit floating point representation
decNumberReduce(&dec_double.number, p_dec_number, &dblCtx);
decNumberToString(&dec_double.number, literal);
char *end;
return jvp_strtod(tsd_dtoa_context_get(), literal, &end);
}
static const char* jvp_literal_number_literal(jv n) {
assert(JVP_HAS_FLAGS(n, JVP_FLAGS_NUMBER_LITERAL));
decNumber *pdec = jvp_dec_number_ptr(n);
jvp_literal_number* plit = jvp_literal_number_ptr(n);
if (decNumberIsNaN(pdec)) {
return "null";
}
if (decNumberIsInfinite(pdec)) {
// We cannot preserve the literal data of numbers outside the limited
// range of exponent. Since `decNumberToString` returns "Infinity"
// (or "-Infinity"), and to reduce stack allocations as possible, we
// normalize infinities in the callers instead of printing the maximum
// (or minimum) double here.
return NULL;
}
if (plit->literal_data == NULL) {
int len = jvp_dec_number_ptr(n)->digits + 15 /* 14 + NUL */;
plit->literal_data = jv_mem_alloc(len);
// Preserve the actual precision as we have parsed it
// don't do decNumberTrim(pdec);
decNumberToString(pdec, plit->literal_data);
}
return plit->literal_data;
}
int jv_number_has_literal(jv n) {
assert(JVP_HAS_KIND(n, JV_KIND_NUMBER));
return JVP_HAS_FLAGS(n, JVP_FLAGS_NUMBER_LITERAL);
}
const char* jv_number_get_literal(jv n) {
assert(JVP_HAS_KIND(n, JV_KIND_NUMBER));
if (JVP_HAS_FLAGS(n, JVP_FLAGS_NUMBER_LITERAL)) {
return jvp_literal_number_literal(n);
} else {
return NULL;
}
}
jv jv_number_with_literal(const char * literal) {
return jvp_literal_number_new(literal);
}
#endif /* USE_DECNUM */
jv jv_number(double x) {
jv j = {
#ifdef USE_DECNUM
JVP_FLAGS_NUMBER_NATIVE,
#else
JV_KIND_NUMBER,
#endif
0, 0, 0, {.number = x}
};
return j;
}
static void jvp_number_free(jv j) {
assert(JVP_HAS_KIND(j, JV_KIND_NUMBER));
#ifdef USE_DECNUM
if (JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL) && jvp_refcnt_dec(j.u.ptr)) {
jvp_literal_number* n = jvp_literal_number_ptr(j);
if (n->literal_data) {
jv_mem_free(n->literal_data);
}
jv_mem_free(n);
}
#endif
}
double jv_number_value(jv j) {
assert(JVP_HAS_KIND(j, JV_KIND_NUMBER));
#ifdef USE_DECNUM
if (JVP_HAS_FLAGS(j, JVP_FLAGS_NUMBER_LITERAL)) {
jvp_literal_number* n = jvp_literal_number_ptr(j);
if (j.size != JV_NUMBER_SIZE_CONVERTED) {
n->num_double = jvp_literal_number_to_double(j);
j.size = JV_NUMBER_SIZE_CONVERTED;
}
return n->num_double;
}
#endif
return j.u.number;
}
int jv_is_integer(jv j){
if (!JVP_HAS_KIND(j, JV_KIND_NUMBER)){
return 0;
}
double x = jv_number_value(j);
double ipart;
double fpart = modf(x, &ipart);
return fabs(fpart) < DBL_EPSILON;
}
int jvp_number_is_nan(jv n) {
assert(JVP_HAS_KIND(n, JV_KIND_NUMBER));
#ifdef USE_DECNUM
if (JVP_HAS_FLAGS(n, JVP_FLAGS_NUMBER_LITERAL)) {
decNumber *pdec = jvp_dec_number_ptr(n);
return decNumberIsNaN(pdec);
}
#endif
return n.u.number != n.u.number;
}
int jvp_number_cmp(jv a, jv b) {
assert(JVP_HAS_KIND(a, JV_KIND_NUMBER));
assert(JVP_HAS_KIND(b, JV_KIND_NUMBER));
#ifdef USE_DECNUM
if (JVP_HAS_FLAGS(a, JVP_FLAGS_NUMBER_LITERAL) && JVP_HAS_FLAGS(b, JVP_FLAGS_NUMBER_LITERAL)) {
struct {
decNumber number;
decNumberUnit units[1];
} res;
decNumberCompare(&res.number,
jvp_dec_number_ptr(a),
jvp_dec_number_ptr(b),
DEC_CONTEXT()
);
if (decNumberIsZero(&res.number)) {
return 0;
} else if (decNumberIsNegative(&res.number)) {
return -1;
} else {
return 1;
}
}
#endif
double da = jv_number_value(a), db = jv_number_value(b);
if (da < db) {
return -1;
} else if (da == db) {
return 0;
} else {
return 1;
}
}
static int jvp_number_equal(jv a, jv b) {
return jvp_number_cmp(a, b) == 0;
}
/*
* Arrays (internal helpers)
*/
#define ARRAY_SIZE_ROUND_UP(n) (((n)*3)/2)
#define JVP_FLAGS_ARRAY JVP_MAKE_FLAGS(JV_KIND_ARRAY, JVP_PAYLOAD_ALLOCATED)
static int imax(int a, int b) {
if (a>b) return a;
else return b;
}
//FIXME signed vs unsigned
typedef struct {
jv_refcnt refcnt;
int length, alloc_length;
jv elements[];
} jvp_array;
static jvp_array* jvp_array_ptr(jv a) {
assert(JVP_HAS_KIND(a, JV_KIND_ARRAY));
return (jvp_array*)a.u.ptr;
}
static jvp_array* jvp_array_alloc(unsigned size) {
jvp_array* a = jv_mem_alloc(sizeof(jvp_array) + sizeof(jv) * size);
a->refcnt.count = 1;
a->length = 0;
a->alloc_length = size;
return a;
}
static jv jvp_array_new(unsigned size) {
jv r = {JVP_FLAGS_ARRAY, 0, 0, 0, {&jvp_array_alloc(size)->refcnt}};
return r;
}
static void jvp_array_free(jv a) {
assert(JVP_HAS_KIND(a, JV_KIND_ARRAY));
if (jvp_refcnt_dec(a.u.ptr)) {
jvp_array* array = jvp_array_ptr(a);
for (int i=0; i<array->length; i++) {
jv_free(array->elements[i]);
}
jv_mem_free(array);
}
}
static int jvp_array_length(jv a) {
assert(JVP_HAS_KIND(a, JV_KIND_ARRAY));
return a.size;
}
static int jvp_array_offset(jv a) {
assert(JVP_HAS_KIND(a, JV_KIND_ARRAY));
return a.offset;
}
static jv* jvp_array_read(jv a, int i) {
assert(JVP_HAS_KIND(a, JV_KIND_ARRAY));
if (i >= 0 && i < jvp_array_length(a)) {
jvp_array* array = jvp_array_ptr(a);
assert(i + jvp_array_offset(a) < array->length);
return &array->elements[i + jvp_array_offset(a)];
} else {
return 0;
}
}
static jv* jvp_array_write(jv* a, int i) {
assert(i >= 0);
jvp_array* array = jvp_array_ptr(*a);
int pos = i + jvp_array_offset(*a);
if (pos < array->alloc_length && jvp_refcnt_unshared(a->u.ptr)) {
// use existing array space
for (int j = array->length; j <= pos; j++) {
array->elements[j] = JV_NULL;
}
array->length = imax(pos + 1, array->length);
a->size = imax(i + 1, a->size);
return &array->elements[pos];
} else {
// allocate a new array
int new_length = imax(i + 1, jvp_array_length(*a));
jvp_array* new_array = jvp_array_alloc(ARRAY_SIZE_ROUND_UP(new_length));
int j;
for (j = 0; j < jvp_array_length(*a); j++) {
new_array->elements[j] =
jv_copy(array->elements[j + jvp_array_offset(*a)]);
}
for (; j < new_length; j++) {
new_array->elements[j] = JV_NULL;
}
new_array->length = new_length;
jvp_array_free(*a);
jv new_jv = {JVP_FLAGS_ARRAY, 0, 0, new_length, {&new_array->refcnt}};
*a = new_jv;
return &new_array->elements[i];
}
}
static int jvp_array_equal(jv a, jv b) {
if (jvp_array_length(a) != jvp_array_length(b))
return 0;
if (jvp_array_ptr(a) == jvp_array_ptr(b) &&
jvp_array_offset(a) == jvp_array_offset(b))
return 1;
for (int i=0; i<jvp_array_length(a); i++) {
if (!jv_equal(jv_copy(*jvp_array_read(a, i)),
jv_copy(*jvp_array_read(b, i))))
return 0;
}
return 1;
}
static void jvp_clamp_slice_params(int len, int *pstart, int *pend)
{
if (*pstart < 0) *pstart = len + *pstart;
if (*pend < 0) *pend = len + *pend;
if (*pstart < 0) *pstart = 0;
if (*pstart > len) *pstart = len;
if (*pend > len) *pend = len;
if (*pend < *pstart) *pend = *pstart;
}
static int jvp_array_contains(jv a, jv b) {
int r = 1;
jv_array_foreach(b, bi, belem) {
int ri = 0;
jv_array_foreach(a, ai, aelem) {
if (jv_contains(aelem, jv_copy(belem))) {
ri = 1;
break;
}
}
jv_free(belem);
if (!ri) {
r = 0;
break;
}
}
return r;
}
/*
* Public
*/
static jv jvp_array_slice(jv a, int start, int end) {
assert(JVP_HAS_KIND(a, JV_KIND_ARRAY));
int len = jvp_array_length(a);
jvp_clamp_slice_params(len, &start, &end);
assert(0 <= start && start <= end && end <= len);
// FIXME: maybe slice should reallocate if the slice is small enough
if (start == end) {
jv_free(a);
return jv_array();
}
if (a.offset + start >= 1 << (sizeof(a.offset) * CHAR_BIT)) {
jv r = jv_array_sized(end - start);
for (int i = start; i < end; i++)
r = jv_array_append(r, jv_array_get(jv_copy(a), i));
jv_free(a);
return r;
} else {
a.offset += start;
a.size = end - start;
return a;
}
}
/*
* Arrays (public interface)
*/
jv jv_array_sized(int n) {
return jvp_array_new(n);
}
jv jv_array() {
return jv_array_sized(16);
}
int jv_array_length(jv j) {
assert(JVP_HAS_KIND(j, JV_KIND_ARRAY));
int len = jvp_array_length(j);
jv_free(j);
return len;
}
jv jv_array_get(jv j, int idx) {
assert(JVP_HAS_KIND(j, JV_KIND_ARRAY));
jv* slot = jvp_array_read(j, idx);
jv val;
if (slot) {
val = jv_copy(*slot);
} else {
val = jv_invalid();
}
jv_free(j);
return val;
}
jv jv_array_set(jv j, int idx, jv val) {
assert(JVP_HAS_KIND(j, JV_KIND_ARRAY));
if (idx < 0)
idx = jvp_array_length(j) + idx;
if (idx < 0) {
jv_free(j);
jv_free(val);
return jv_invalid_with_msg(jv_string("Out of bounds negative array index"));
}
// copy/free of val,j coalesced
jv* slot = jvp_array_write(&j, idx);
jv_free(*slot);
*slot = val;
return j;
}