forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduk_api_stack.c
More file actions
4658 lines (3865 loc) · 132 KB
/
duk_api_stack.c
File metadata and controls
4658 lines (3865 loc) · 132 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
/*
* API calls related to general value stack manipulation: resizing the value
* stack, pushing and popping values, type checking and reading values,
* coercing values, etc.
*
* Also contains internal functions (such as duk_get_tval()), defined
* in duk_api_internal.h, with semantics similar to the public API.
*/
/* XXX: repetition of stack pre-checks -> helper or macro or inline */
/* XXX: shared api error strings, and perhaps even throw code for rare cases? */
#include "duk_internal.h"
/*
* Forward declarations
*/
DUK_LOCAL_DECL duk_idx_t duk__push_c_function_raw(duk_context *ctx, duk_c_function func, duk_idx_t nargs, duk_uint_t flags);
/*
* Global state for working around missing variadic macros
*/
#ifndef DUK_USE_VARIADIC_MACROS
DUK_EXTERNAL const char *duk_api_global_filename = NULL;
DUK_EXTERNAL duk_int_t duk_api_global_line = 0;
#endif
/*
* Misc helpers
*/
/* Check that there's room to push one value. */
#if defined(DUK_USE_VALSTACK_UNSAFE)
/* Faster but value stack overruns are memory unsafe. */
#define DUK__CHECK_SPACE() do { \
DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \
} while (0)
#else
#define DUK__CHECK_SPACE() do { \
if (DUK_UNLIKELY(thr->valstack_top >= thr->valstack_end)) { \
DUK_ERROR_API(thr, DUK_STR_PUSH_BEYOND_ALLOC_STACK); \
} \
} while (0)
#endif
DUK_LOCAL_DECL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_context *ctx, duk_idx_t index, duk_uint_t tag);
DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_context *ctx, duk_idx_t index, duk_bool_t require) {
duk_hthread *thr;
duk_tval *tv;
duk_small_int_t c;
duk_double_t d;
thr = (duk_hthread *) ctx;
tv = duk_get_tval(ctx, index);
if (tv == NULL) {
goto error_notnumber;
}
/*
* Special cases like NaN and +/- Infinity are handled explicitly
* because a plain C coercion from double to int handles these cases
* in undesirable ways. For instance, NaN may coerce to INT_MIN
* (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX).
*
* This double-to-int coercion differs from ToInteger() because it
* has a finite range (ToInteger() allows e.g. +/- Infinity). It
* also differs from ToInt32() because the INT_MIN/INT_MAX clamping
* depends on the size of the int type on the platform. In particular,
* on platforms with a 64-bit int type, the full range is allowed.
*/
#if defined(DUK_USE_FASTINT)
if (DUK_TVAL_IS_FASTINT(tv)) {
duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
#if (DUK_INT_MAX <= 0x7fffffffL)
/* Clamping only necessary for 32-bit ints. */
if (t < DUK_INT_MIN) {
t = DUK_INT_MIN;
} else if (t > DUK_INT_MAX) {
t = DUK_INT_MAX;
}
#endif
return (duk_int_t) t;
}
#endif
if (DUK_TVAL_IS_NUMBER(tv)) {
d = DUK_TVAL_GET_NUMBER(tv);
c = (duk_small_int_t) DUK_FPCLASSIFY(d);
if (c == DUK_FP_NAN) {
return 0;
} else if (d < (duk_double_t) DUK_INT_MIN) {
/* covers -Infinity */
return DUK_INT_MIN;
} else if (d > (duk_double_t) DUK_INT_MAX) {
/* covers +Infinity */
return DUK_INT_MAX;
} else {
/* coerce towards zero */
return (duk_int_t) d;
}
}
error_notnumber:
if (require) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER);
/* not reachable */
}
return 0;
}
DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_context *ctx, duk_idx_t index, duk_bool_t require) {
duk_hthread *thr;
duk_tval *tv;
duk_small_int_t c;
duk_double_t d;
/* Same as above but for unsigned int range. */
thr = (duk_hthread *) ctx;
tv = duk_get_tval(ctx, index);
if (tv == NULL) {
goto error_notnumber;
}
#if defined(DUK_USE_FASTINT)
if (DUK_TVAL_IS_FASTINT(tv)) {
duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
if (t < 0) {
t = 0;
}
#if (DUK_UINT_MAX <= 0xffffffffUL)
/* Clamping only necessary for 32-bit ints. */
else if (t > DUK_UINT_MAX) {
t = DUK_UINT_MAX;
}
#endif
return (duk_uint_t) t;
}
#endif
if (DUK_TVAL_IS_NUMBER(tv)) {
d = DUK_TVAL_GET_NUMBER(tv);
c = (duk_small_int_t) DUK_FPCLASSIFY(d);
if (c == DUK_FP_NAN) {
return 0;
} else if (d < 0.0) {
/* covers -Infinity */
return (duk_uint_t) 0;
} else if (d > (duk_double_t) DUK_UINT_MAX) {
/* covers +Infinity */
return (duk_uint_t) DUK_UINT_MAX;
} else {
/* coerce towards zero */
return (duk_uint_t) d;
}
}
error_notnumber:
if (require) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, index, "number", DUK_STR_NOT_NUMBER);
/* not reachable */
}
return 0;
}
/*
* Stack index validation/normalization and getting a stack duk_tval ptr.
*
* These are called by many API entrypoints so the implementations must be
* fast and "inlined".
*
* There's some repetition because of this; keep the functions in sync.
*/
DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_uidx_t vs_size;
duk_uidx_t uindex;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
/* Care must be taken to avoid pointer wrapping in the index
* validation. For instance, on a 32-bit platform with 8-byte
* duk_tval the index 0x20000000UL would wrap the memory space
* once.
*/
/* Assume value stack sizes (in elements) fits into duk_idx_t. */
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
if (index < 0) {
uindex = vs_size + (duk_uidx_t) index;
} else {
/* since index non-negative */
DUK_ASSERT(index != DUK_INVALID_INDEX);
uindex = (duk_uidx_t) index;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uindex < vs_size)) {
return (duk_idx_t) uindex;
}
return DUK_INVALID_INDEX;
}
DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_uidx_t vs_size;
duk_uidx_t uindex;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
if (index < 0) {
uindex = vs_size + (duk_uidx_t) index;
} else {
DUK_ASSERT(index != DUK_INVALID_INDEX);
uindex = (duk_uidx_t) index;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uindex < vs_size)) {
return (duk_idx_t) uindex;
}
DUK_ERROR_API_INDEX(thr, index);
return 0; /* unreachable */
}
DUK_INTERNAL duk_tval *duk_get_tval(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_uidx_t vs_size;
duk_uidx_t uindex;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
if (index < 0) {
uindex = vs_size + (duk_uidx_t) index;
} else {
DUK_ASSERT(index != DUK_INVALID_INDEX);
uindex = (duk_uidx_t) index;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uindex < vs_size)) {
return thr->valstack_bottom + uindex;
}
return NULL;
}
DUK_INTERNAL duk_tval *duk_require_tval(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_uidx_t vs_size;
duk_uidx_t uindex;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
/* Use unsigned arithmetic to optimize comparison. */
if (index < 0) {
uindex = vs_size + (duk_uidx_t) index;
} else {
DUK_ASSERT(index != DUK_INVALID_INDEX);
uindex = (duk_uidx_t) index;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uindex < vs_size)) {
return thr->valstack_bottom + uindex;
}
DUK_ERROR_API_INDEX(thr, index);
return NULL;
}
/* Non-critical. */
DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_context *ctx, duk_idx_t index) {
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
return (duk_normalize_index(ctx, index) >= 0);
}
/* Non-critical. */
DUK_EXTERNAL void duk_require_valid_index(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
if (duk_normalize_index(ctx, index) < 0) {
DUK_ERROR_API_INDEX(thr, index);
return; /* unreachable */
}
}
/*
* Value stack top handling
*/
DUK_EXTERNAL duk_idx_t duk_get_top(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
DUK_ASSERT_CTX_VALID(ctx);
return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
}
/* Set stack top within currently allocated range, but don't reallocate.
* This is performance critical especially for call handling, so whenever
* changing, profile and look at generated code.
*/
DUK_EXTERNAL void duk_set_top(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_uidx_t vs_size;
duk_uidx_t vs_limit;
duk_uidx_t uindex;
duk_tval *tv;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
vs_limit = (duk_uidx_t) (thr->valstack_end - thr->valstack_bottom);
if (index < 0) {
/* Negative indices are always within allocated stack but
* must not go below zero index.
*/
uindex = vs_size + (duk_uidx_t) index;
} else {
/* Positive index can be higher than valstack top but must
* not go above allocated stack (equality is OK).
*/
uindex = (duk_uidx_t) index;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_limit);
#if defined(DUK_USE_VALSTACK_UNSAFE)
DUK_ASSERT(uindex <= vs_limit);
DUK_UNREF(vs_limit);
#else
if (DUK_UNLIKELY(uindex > vs_limit)) {
DUK_ERROR_API_INDEX(thr, index);
return; /* unreachable */
}
#endif
DUK_ASSERT(uindex <= vs_limit);
/* Handle change in value stack top. Respect value stack
* initialization policy: 'undefined' above top. Note that
* DECREF may cause a side effect that reallocates valstack,
* so must relookup after DECREF.
*/
if (uindex >= vs_size) {
/* Stack size increases or stays the same. */
#if defined(DUK_USE_ASSERTIONS)
duk_uidx_t count;
count = uindex - vs_size;
while (count != 0) {
count--;
tv = thr->valstack_top + count;
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
}
#endif
thr->valstack_top = thr->valstack_bottom + uindex;
} else {
/* Stack size decreases. */
#if defined(DUK_USE_REFERENCE_COUNTING)
duk_uidx_t count;
count = vs_size - uindex;
DUK_ASSERT(count > 0);
while (count > 0) {
count--;
tv = --thr->valstack_top; /* tv -> value just before prev top value; must relookup */
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
}
#else /* DUK_USE_REFERENCE_COUNTING */
duk_uidx_t count;
duk_tval *tv_end;
count = vs_size - uindex;
tv = thr->valstack_top;
tv_end = tv - count;
DUK_ASSERT(tv > tv_end);
do {
tv--;
DUK_TVAL_SET_UNDEFINED(tv);
} while (tv != tv_end);
thr->valstack_top = tv_end;
#endif /* DUK_USE_REFERENCE_COUNTING */
}
}
DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_idx_t ret;
DUK_ASSERT_CTX_VALID(ctx);
ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
if (DUK_UNLIKELY(ret < 0)) {
/* Return invalid index; if caller uses this without checking
* in another API call, the index won't map to a valid stack
* entry.
*/
return DUK_INVALID_INDEX;
}
return ret;
}
DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_idx_t ret;
DUK_ASSERT_CTX_VALID(ctx);
ret = ((duk_idx_t) (thr->valstack_top - thr->valstack_bottom)) - 1;
if (DUK_UNLIKELY(ret < 0)) {
DUK_ERROR_API_INDEX(thr, -1);
return 0; /* unreachable */
}
return ret;
}
/*
* Value stack resizing.
*
* This resizing happens above the current "top": the value stack can be
* grown or shrunk, but the "top" is not affected. The value stack cannot
* be resized to a size below the current "top".
*
* The low level reallocation primitive must carefully recompute all value
* stack pointers, and must also work if ALL pointers are NULL. The resize
* is quite tricky because the valstack realloc may cause a mark-and-sweep,
* which may run finalizers. Running finalizers may resize the valstack
* recursively (the same value stack we're working on). So, after realloc
* returns, we know that the valstack "top" should still be the same (there
* should not be live values above the "top"), but its underlying size and
* pointer may have changed.
*/
/* XXX: perhaps refactor this to allow caller to specify some parameters, or
* at least a 'compact' flag which skips any spare or round-up .. useful for
* emergency gc.
*/
DUK_LOCAL duk_bool_t duk__resize_valstack(duk_context *ctx, duk_size_t new_size) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_ptrdiff_t old_bottom_offset;
duk_ptrdiff_t old_top_offset;
duk_ptrdiff_t old_end_offset_post;
#ifdef DUK_USE_DEBUG
duk_ptrdiff_t old_end_offset_pre;
duk_tval *old_valstack_pre;
duk_tval *old_valstack_post;
#endif
duk_tval *new_valstack;
duk_size_t new_alloc_size;
duk_tval *p;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */
DUK_ASSERT(new_size <= thr->valstack_max); /* valstack limit caller has check, prevents wrapping */
DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */
/* get pointer offsets for tweaking below */
old_bottom_offset = (((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack));
old_top_offset = (((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack));
#ifdef DUK_USE_DEBUG
old_end_offset_pre = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* not very useful, used for debugging */
old_valstack_pre = thr->valstack;
#endif
/* Allocate a new valstack.
*
* Note: cannot use a plain DUK_REALLOC() because a mark-and-sweep may
* invalidate the original thr->valstack base pointer inside the realloc
* process. See doc/memory-management.rst.
*/
new_alloc_size = sizeof(duk_tval) * new_size;
new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size);
if (!new_valstack) {
/* Because new_size != 0, if condition doesn't need to be
* (new_valstack != NULL || new_size == 0).
*/
DUK_ASSERT(new_size != 0);
DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)",
(unsigned long) new_size, (unsigned long) new_alloc_size));
return 0;
}
/* Note: the realloc may have triggered a mark-and-sweep which may
* have resized our valstack internally. However, the mark-and-sweep
* MUST NOT leave the stack bottom/top in a different state. Particular
* assumptions and facts:
*
* - The thr->valstack pointer may be different after realloc,
* and the offset between thr->valstack_end <-> thr->valstack
* may have changed.
* - The offset between thr->valstack_bottom <-> thr->valstack
* and thr->valstack_top <-> thr->valstack MUST NOT have changed,
* because mark-and-sweep must adhere to a strict stack policy.
* In other words, logical bottom and top MUST NOT have changed.
* - All values above the top are unreachable but are initialized
* to UNDEFINED, up to the post-realloc valstack_end.
* - 'old_end_offset' must be computed after realloc to be correct.
*/
DUK_ASSERT((((duk_uint8_t *) thr->valstack_bottom) - ((duk_uint8_t *) thr->valstack)) == old_bottom_offset);
DUK_ASSERT((((duk_uint8_t *) thr->valstack_top) - ((duk_uint8_t *) thr->valstack)) == old_top_offset);
/* success, fixup pointers */
old_end_offset_post = (((duk_uint8_t *) thr->valstack_end) - ((duk_uint8_t *) thr->valstack)); /* must be computed after realloc */
#ifdef DUK_USE_DEBUG
old_valstack_post = thr->valstack;
#endif
thr->valstack = new_valstack;
thr->valstack_end = new_valstack + new_size;
#if !defined(DUK_USE_PREFER_SIZE)
thr->valstack_size = new_size;
#endif
thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_bottom_offset);
thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + old_top_offset);
DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
/* useful for debugging */
#ifdef DUK_USE_DEBUG
if (old_end_offset_pre != old_end_offset_post) {
DUK_D(DUK_DPRINT("valstack was resized during valstack_resize(), probably by mark-and-sweep; "
"end offset changed: %lu -> %lu",
(unsigned long) old_end_offset_pre,
(unsigned long) old_end_offset_post));
}
if (old_valstack_pre != old_valstack_post) {
DUK_D(DUK_DPRINT("valstack pointer changed during valstack_resize(), probably by mark-and-sweep: %p -> %p",
(void *) old_valstack_pre,
(void *) old_valstack_post));
}
#endif
DUK_DD(DUK_DDPRINT("resized valstack to %lu elements (%lu bytes), bottom=%ld, top=%ld, "
"new pointers: start=%p end=%p bottom=%p top=%p",
(unsigned long) new_size, (unsigned long) new_alloc_size,
(long) (thr->valstack_bottom - thr->valstack),
(long) (thr->valstack_top - thr->valstack),
(void *) thr->valstack, (void *) thr->valstack_end,
(void *) thr->valstack_bottom, (void *) thr->valstack_top));
/* Init newly allocated slots (only). */
p = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + old_end_offset_post);
while (p < thr->valstack_end) {
/* Never executed if new size is smaller. */
DUK_TVAL_SET_UNDEFINED(p);
p++;
}
/* Assert for value stack initialization policy. */
#if defined(DUK_USE_ASSERTIONS)
p = thr->valstack_top;
while (p < thr->valstack_end) {
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(p));
p++;
}
#endif
return 1;
}
DUK_INTERNAL
duk_bool_t duk_valstack_resize_raw(duk_context *ctx,
duk_size_t min_new_size,
duk_small_uint_t flags) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_size_t old_size;
duk_size_t new_size;
duk_bool_t is_shrink = 0;
duk_small_uint_t shrink_flag = (flags & DUK_VSRESIZE_FLAG_SHRINK);
duk_small_uint_t compact_flag = (flags & DUK_VSRESIZE_FLAG_COMPACT);
duk_small_uint_t throw_flag = (flags & DUK_VSRESIZE_FLAG_THROW);
DUK_DDD(DUK_DDDPRINT("check valstack resize: min_new_size=%lu, curr_size=%ld, curr_top=%ld, "
"curr_bottom=%ld, shrink=%d, compact=%d, throw=%d",
(unsigned long) min_new_size,
(long) (thr->valstack_end - thr->valstack),
(long) (thr->valstack_top - thr->valstack),
(long) (thr->valstack_bottom - thr->valstack),
(int) shrink_flag, (int) compact_flag, (int) throw_flag));
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
#if defined(DUK_USE_PREFER_SIZE)
old_size = (duk_size_t) (thr->valstack_end - thr->valstack);
#else
DUK_ASSERT((duk_size_t) (thr->valstack_end - thr->valstack) == thr->valstack_size);
old_size = thr->valstack_size;
#endif
if (min_new_size <= old_size) {
is_shrink = 1;
if (!shrink_flag ||
old_size - min_new_size < DUK_VALSTACK_SHRINK_THRESHOLD) {
DUK_DDD(DUK_DDDPRINT("no need to grow or shrink valstack"));
return 1;
}
}
new_size = min_new_size;
if (!compact_flag) {
if (is_shrink) {
/* shrink case; leave some spare */
new_size += DUK_VALSTACK_SHRINK_SPARE;
}
/* round up roughly to next 'grow step' */
new_size = (new_size / DUK_VALSTACK_GROW_STEP + 1) * DUK_VALSTACK_GROW_STEP;
}
DUK_DD(DUK_DDPRINT("want to %s valstack: %lu -> %lu elements (min_new_size %lu)",
(const char *) (new_size > old_size ? "grow" : "shrink"),
(unsigned long) old_size, (unsigned long) new_size,
(unsigned long) min_new_size));
if (new_size > thr->valstack_max) {
/* Note: may be triggered even if minimal new_size would not reach the limit,
* plan limit accordingly (taking DUK_VALSTACK_GROW_STEP into account).
*/
if (throw_flag) {
DUK_ERROR_RANGE(thr, DUK_STR_VALSTACK_LIMIT);
} else {
return 0;
}
}
/*
* When resizing the valstack, a mark-and-sweep may be triggered for
* the allocation of the new valstack. If the mark-and-sweep needs
* to use our thread for something, it may cause *the same valstack*
* to be resized recursively. This happens e.g. when mark-and-sweep
* finalizers are called. This is taken into account carefully in
* duk__resize_valstack().
*
* 'new_size' is known to be <= valstack_max, which ensures that
* size_t and pointer arithmetic won't wrap in duk__resize_valstack().
*/
if (!duk__resize_valstack(ctx, new_size)) {
if (is_shrink) {
DUK_DD(DUK_DDPRINT("valstack resize failed, but is a shrink, ignore"));
return 1;
}
DUK_DD(DUK_DDPRINT("valstack resize failed"));
if (throw_flag) {
DUK_ERROR_ALLOC_DEFMSG(thr);
} else {
return 0;
}
}
DUK_DDD(DUK_DDDPRINT("valstack resize successful"));
return 1;
}
DUK_EXTERNAL duk_bool_t duk_check_stack(duk_context *ctx, duk_idx_t extra) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_size_t min_new_size;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(thr != NULL);
if (DUK_UNLIKELY(extra < 0)) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
extra = 0;
}
min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
return duk_valstack_resize_raw(ctx,
min_new_size, /* min_new_size */
0 /* no shrink */ | /* flags */
0 /* no compact */ |
0 /* no throw */);
}
DUK_EXTERNAL void duk_require_stack(duk_context *ctx, duk_idx_t extra) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_size_t min_new_size;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(thr != NULL);
if (DUK_UNLIKELY(extra < 0)) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
extra = 0;
}
min_new_size = (thr->valstack_top - thr->valstack) + extra + DUK_VALSTACK_INTERNAL_EXTRA;
(void) duk_valstack_resize_raw(ctx,
min_new_size, /* min_new_size */
0 /* no shrink */ | /* flags */
0 /* no compact */ |
DUK_VSRESIZE_FLAG_THROW);
}
DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_context *ctx, duk_idx_t top) {
duk_size_t min_new_size;
DUK_ASSERT_CTX_VALID(ctx);
if (DUK_UNLIKELY(top < 0)) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
top = 0;
}
min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
return duk_valstack_resize_raw(ctx,
min_new_size, /* min_new_size */
0 /* no shrink */ | /* flags */
0 /* no compact */ |
0 /* no throw */);
}
DUK_EXTERNAL void duk_require_stack_top(duk_context *ctx, duk_idx_t top) {
duk_size_t min_new_size;
DUK_ASSERT_CTX_VALID(ctx);
if (DUK_UNLIKELY(top < 0)) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
top = 0;
}
min_new_size = top + DUK_VALSTACK_INTERNAL_EXTRA;
(void) duk_valstack_resize_raw(ctx,
min_new_size, /* min_new_size */
0 /* no shrink */ | /* flags */
0 /* no compact */ |
DUK_VSRESIZE_FLAG_THROW);
}
/*
* Basic stack manipulation: swap, dup, insert, replace, etc
*/
DUK_EXTERNAL void duk_swap(duk_context *ctx, duk_idx_t index1, duk_idx_t index2) {
duk_tval *tv1;
duk_tval *tv2;
duk_tval tv_tmp;
DUK_ASSERT_CTX_VALID(ctx);
tv1 = duk_require_tval(ctx, index1);
DUK_ASSERT(tv1 != NULL);
tv2 = duk_require_tval(ctx, index2);
DUK_ASSERT(tv2 != NULL);
/* If tv1==tv2 this is a NOP, no check is needed */
DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
DUK_TVAL_SET_TVAL(tv1, tv2);
DUK_TVAL_SET_TVAL(tv2, &tv_tmp);
}
DUK_EXTERNAL void duk_swap_top(duk_context *ctx, duk_idx_t index) {
DUK_ASSERT_CTX_VALID(ctx);
duk_swap(ctx, index, -1);
}
DUK_EXTERNAL void duk_dup(duk_context *ctx, duk_idx_t from_index) {
duk_hthread *thr;
duk_tval *tv_from;
duk_tval *tv_to;
DUK_ASSERT_CTX_VALID(ctx);
thr = (duk_hthread *) ctx;
DUK__CHECK_SPACE();
tv_from = duk_require_tval(ctx, from_index);
tv_to = thr->valstack_top++;
DUK_ASSERT(tv_from != NULL);
DUK_ASSERT(tv_to != NULL);
DUK_TVAL_SET_TVAL(tv_to, tv_from);
DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
}
DUK_EXTERNAL void duk_dup_top(duk_context *ctx) {
duk_hthread *thr;
duk_tval *tv_from;
duk_tval *tv_to;
DUK_ASSERT_CTX_VALID(ctx);
thr = (duk_hthread *) ctx;
DUK__CHECK_SPACE();
if (thr->valstack_top - thr->valstack_bottom <= 0) {
DUK_ERROR_API_INDEX(thr, -1);
return; /* unreachable */
}
tv_from = thr->valstack_top - 1;
tv_to = thr->valstack_top++;
DUK_ASSERT(tv_from != NULL);
DUK_ASSERT(tv_to != NULL);
DUK_TVAL_SET_TVAL(tv_to, tv_from);
DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
}
DUK_EXTERNAL void duk_insert(duk_context *ctx, duk_idx_t to_index) {
duk_tval *p;
duk_tval *q;
duk_tval tv_tmp;
duk_size_t nbytes;
DUK_ASSERT_CTX_VALID(ctx);
p = duk_require_tval(ctx, to_index);
DUK_ASSERT(p != NULL);
q = duk_require_tval(ctx, -1);
DUK_ASSERT(q != NULL);
DUK_ASSERT(q >= p);
/* nbytes
* <--------->
* [ ... | p | x | x | q ]
* => [ ... | q | p | x | x ]
*/
nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
DUK_DDD(DUK_DDDPRINT("duk_insert: to_index=%ld, p=%p, q=%p, nbytes=%lu",
(long) to_index, (void *) p, (void *) q, (unsigned long) nbytes));
/* No net refcount changes. */
if (nbytes > 0) {
DUK_TVAL_SET_TVAL(&tv_tmp, q);
DUK_ASSERT(nbytes > 0);
DUK_MEMMOVE((void *) (p + 1), (const void *) p, (size_t) nbytes);
DUK_TVAL_SET_TVAL(p, &tv_tmp);
} else {
/* nop: insert top to top */
DUK_ASSERT(nbytes == 0);
DUK_ASSERT(p == q);
}
}
DUK_EXTERNAL void duk_replace(duk_context *ctx, duk_idx_t to_index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_tval *tv1;
duk_tval *tv2;
duk_tval tv_tmp;
DUK_ASSERT_CTX_VALID(ctx);
tv1 = duk_require_tval(ctx, -1);
DUK_ASSERT(tv1 != NULL);
tv2 = duk_require_tval(ctx, to_index);
DUK_ASSERT(tv2 != NULL);
/* For tv1 == tv2, both pointing to stack top, the end result
* is same as duk_pop(ctx).
*/
DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
DUK_TVAL_SET_TVAL(tv2, tv1);
DUK_TVAL_SET_UNDEFINED(tv1);
thr->valstack_top--;
DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
}
DUK_EXTERNAL void duk_copy(duk_context *ctx, duk_idx_t from_index, duk_idx_t to_index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_tval *tv1;
duk_tval *tv2;
DUK_ASSERT_CTX_VALID(ctx);
DUK_UNREF(thr); /* w/o refcounting */
tv1 = duk_require_tval(ctx, from_index);
DUK_ASSERT(tv1 != NULL);
tv2 = duk_require_tval(ctx, to_index);
DUK_ASSERT(tv2 != NULL);
/* For tv1 == tv2, this is a no-op (no explicit check needed). */
DUK_TVAL_SET_TVAL_UPDREF(thr, tv2, tv1); /* side effects */
}
DUK_EXTERNAL void duk_remove(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_tval *p;
duk_tval *q;
#ifdef DUK_USE_REFERENCE_COUNTING
duk_tval tv_tmp;
#endif
duk_size_t nbytes;
DUK_ASSERT_CTX_VALID(ctx);
p = duk_require_tval(ctx, index);
DUK_ASSERT(p != NULL);
q = duk_require_tval(ctx, -1);
DUK_ASSERT(q != NULL);
DUK_ASSERT(q >= p);
/* nbytes zero size case
* <--------->
* [ ... | p | x | x | q ] [ ... | p==q ]
* => [ ... | x | x | q ] [ ... ]
*/
#ifdef DUK_USE_REFERENCE_COUNTING
/* use a temp: decref only when valstack reachable values are correct */
DUK_TVAL_SET_TVAL(&tv_tmp, p);
#endif
nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
DUK_MEMMOVE((void *) p, (const void *) (p + 1), (size_t) nbytes); /* zero size not an issue: pointers are valid */
DUK_TVAL_SET_UNDEFINED(q);
thr->valstack_top--;
#ifdef DUK_USE_REFERENCE_COUNTING
DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
#endif
}
/*
* Stack slice primitives
*/
DUK_EXTERNAL void duk_xcopymove_raw(duk_context *to_ctx, duk_context *from_ctx, duk_idx_t count, duk_bool_t is_copy) {
duk_hthread *to_thr = (duk_hthread *) to_ctx;
duk_hthread *from_thr = (duk_hthread *) from_ctx;
void *src;
duk_size_t nbytes;
duk_tval *p;
duk_tval *q;
/* XXX: several pointer comparison issues here */