forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduk_bi_buffer.c
More file actions
2916 lines (2557 loc) · 93.4 KB
/
duk_bi_buffer.c
File metadata and controls
2916 lines (2557 loc) · 93.4 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
/*
* ES2015 TypedArray and Node.js Buffer built-ins
*/
#include "duk_internal.h"
/*
* Helpers for buffer handling, enabled with DUK_USE_BUFFEROBJECT_SUPPORT.
*/
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
/* Map class number (minus DUK_HOBJECT_CLASS_BUFOBJ_MIN) to a bidx for the
* default internal prototype.
*/
static const duk_uint8_t duk__buffer_proto_from_classnum[] = {
DUK_BIDX_ARRAYBUFFER_PROTOTYPE, DUK_BIDX_DATAVIEW_PROTOTYPE, DUK_BIDX_INT8ARRAY_PROTOTYPE,
DUK_BIDX_UINT8ARRAY_PROTOTYPE, DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, DUK_BIDX_INT16ARRAY_PROTOTYPE,
DUK_BIDX_UINT16ARRAY_PROTOTYPE, DUK_BIDX_INT32ARRAY_PROTOTYPE, DUK_BIDX_UINT32ARRAY_PROTOTYPE,
DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, DUK_BIDX_FLOAT64ARRAY_PROTOTYPE
};
/* Map DUK_HBUFOBJ_ELEM_xxx to duk_hobject class number.
* Sync with duk_hbufobj.h and duk_hobject.h.
*/
static const duk_uint8_t duk__buffer_class_from_elemtype[9] = { DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY,
DUK_HOBJECT_CLASS_INT8ARRAY, DUK_HOBJECT_CLASS_UINT16ARRAY,
DUK_HOBJECT_CLASS_INT16ARRAY, DUK_HOBJECT_CLASS_UINT32ARRAY,
DUK_HOBJECT_CLASS_INT32ARRAY, DUK_HOBJECT_CLASS_FLOAT32ARRAY,
DUK_HOBJECT_CLASS_FLOAT64ARRAY };
/* Map DUK_HBUFOBJ_ELEM_xxx to prototype object built-in index.
* Sync with duk_hbufobj.h.
*/
static const duk_uint8_t duk__buffer_proto_from_elemtype[9] = {
DUK_BIDX_UINT8ARRAY_PROTOTYPE, DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, DUK_BIDX_INT8ARRAY_PROTOTYPE,
DUK_BIDX_UINT16ARRAY_PROTOTYPE, DUK_BIDX_INT16ARRAY_PROTOTYPE, DUK_BIDX_UINT32ARRAY_PROTOTYPE,
DUK_BIDX_INT32ARRAY_PROTOTYPE, DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, DUK_BIDX_FLOAT64ARRAY_PROTOTYPE
};
/* Map DUK__FLD_xxx to byte size. */
static const duk_uint8_t duk__buffer_nbytes_from_fldtype[6] = {
1, /* DUK__FLD_8BIT */
2, /* DUK__FLD_16BIT */
4, /* DUK__FLD_32BIT */
4, /* DUK__FLD_FLOAT */
8, /* DUK__FLD_DOUBLE */
0 /* DUK__FLD_VARINT; not relevant here */
};
/* Bitfield for each DUK_HBUFOBJ_ELEM_xxx indicating which element types
* are compatible with a blind byte copy for the TypedArray set() method (also
* used for TypedArray constructor). Array index is target buffer elem type,
* bitfield indicates compatible source types. The types must have same byte
* size and they must be coercion compatible.
*/
#if !defined(DUK_USE_PREFER_SIZE)
static duk_uint16_t duk__buffer_elemtype_copy_compatible[9] = {
/* xxx -> DUK_HBUFOBJ_ELEM_UINT8 */
(1U << DUK_HBUFOBJ_ELEM_UINT8) | (1U << DUK_HBUFOBJ_ELEM_UINT8CLAMPED) | (1U << DUK_HBUFOBJ_ELEM_INT8),
/* xxx -> DUK_HBUFOBJ_ELEM_UINT8CLAMPED
* Note: INT8 is -not- copy compatible, e.g. -1 would coerce to 0x00.
*/
(1U << DUK_HBUFOBJ_ELEM_UINT8) | (1U << DUK_HBUFOBJ_ELEM_UINT8CLAMPED),
/* xxx -> DUK_HBUFOBJ_ELEM_INT8 */
(1U << DUK_HBUFOBJ_ELEM_UINT8) | (1U << DUK_HBUFOBJ_ELEM_UINT8CLAMPED) | (1U << DUK_HBUFOBJ_ELEM_INT8),
/* xxx -> DUK_HBUFOBJ_ELEM_UINT16 */
(1U << DUK_HBUFOBJ_ELEM_UINT16) | (1U << DUK_HBUFOBJ_ELEM_INT16),
/* xxx -> DUK_HBUFOBJ_ELEM_INT16 */
(1U << DUK_HBUFOBJ_ELEM_UINT16) | (1U << DUK_HBUFOBJ_ELEM_INT16),
/* xxx -> DUK_HBUFOBJ_ELEM_UINT32 */
(1U << DUK_HBUFOBJ_ELEM_UINT32) | (1U << DUK_HBUFOBJ_ELEM_INT32),
/* xxx -> DUK_HBUFOBJ_ELEM_INT32 */
(1U << DUK_HBUFOBJ_ELEM_UINT32) | (1U << DUK_HBUFOBJ_ELEM_INT32),
/* xxx -> DUK_HBUFOBJ_ELEM_FLOAT32 */
(1U << DUK_HBUFOBJ_ELEM_FLOAT32),
/* xxx -> DUK_HBUFOBJ_ELEM_FLOAT64 */
(1U << DUK_HBUFOBJ_ELEM_FLOAT64)
};
#endif /* !DUK_USE_PREFER_SIZE */
DUK_LOCAL duk_hbufobj *duk__hbufobj_promote_this(duk_hthread *thr) {
duk_tval *tv_dst;
duk_hbufobj *res;
duk_push_this(thr);
DUK_ASSERT(duk_is_buffer(thr, -1));
res = (duk_hbufobj *) duk_to_hobject(thr, -1);
DUK_HBUFOBJ_ASSERT_VALID(res);
DUK_DD(DUK_DDPRINT("promoted 'this' automatically to an ArrayBuffer: %!iT", duk_get_tval(thr, -1)));
tv_dst = duk_get_borrowed_this_tval(thr);
DUK_TVAL_SET_OBJECT_UPDREF(thr, tv_dst, (duk_hobject *) res);
duk_pop(thr);
return res;
}
#define DUK__BUFOBJ_FLAG_THROW (1 << 0)
#define DUK__BUFOBJ_FLAG_PROMOTE (1 << 1)
/* Shared helper. When DUK__BUFOBJ_FLAG_PROMOTE is given, the return value is
* always a duk_hbufobj *. Without the flag the return value can also be a
* plain buffer, and the caller must check for it using DUK_HEAPHDR_IS_BUFFER().
*/
DUK_LOCAL duk_heaphdr *duk__getrequire_bufobj_this(duk_hthread *thr, duk_small_uint_t flags) {
duk_tval *tv;
duk_hbufobj *h_this;
DUK_ASSERT(thr != NULL);
tv = duk_get_borrowed_this_tval(thr);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_OBJECT(tv)) {
h_this = (duk_hbufobj *) DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h_this != NULL);
if (DUK_HOBJECT_IS_BUFOBJ((duk_hobject *) h_this)) {
DUK_HBUFOBJ_ASSERT_VALID(h_this);
return (duk_heaphdr *) h_this;
}
} else if (DUK_TVAL_IS_BUFFER(tv)) {
if (flags & DUK__BUFOBJ_FLAG_PROMOTE) {
/* Promote a plain buffer to a Uint8Array. This is very
* inefficient but allows plain buffer to be used wherever an
* Uint8Array is used with very small cost; hot path functions
* like index read/write calls should provide direct buffer
* support to avoid promotion.
*/
/* XXX: make this conditional to a flag if call sites need it? */
h_this = duk__hbufobj_promote_this(thr);
DUK_ASSERT(h_this != NULL);
DUK_HBUFOBJ_ASSERT_VALID(h_this);
return (duk_heaphdr *) h_this;
} else {
/* XXX: ugly, share return pointer for duk_hbuffer. */
return (duk_heaphdr *) DUK_TVAL_GET_BUFFER(tv);
}
}
if (flags & DUK__BUFOBJ_FLAG_THROW) {
DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER);
DUK_WO_NORETURN(return NULL;);
}
return NULL;
}
/* Check that 'this' is a duk_hbufobj and return a pointer to it. */
DUK_LOCAL duk_hbufobj *duk__get_bufobj_this(duk_hthread *thr) {
return (duk_hbufobj *) duk__getrequire_bufobj_this(thr, DUK__BUFOBJ_FLAG_PROMOTE);
}
/* Check that 'this' is a duk_hbufobj and return a pointer to it
* (NULL if not).
*/
DUK_LOCAL duk_hbufobj *duk__require_bufobj_this(duk_hthread *thr) {
return (duk_hbufobj *) duk__getrequire_bufobj_this(thr, DUK__BUFOBJ_FLAG_THROW | DUK__BUFOBJ_FLAG_PROMOTE);
}
/* Check that value is a duk_hbufobj and return a pointer to it. */
DUK_LOCAL duk_hbufobj *duk__require_bufobj_value(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_hbufobj *h_obj;
/* Don't accept relative indices now. */
DUK_ASSERT(idx >= 0);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_OBJECT(tv)) {
h_obj = (duk_hbufobj *) DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h_obj != NULL);
if (DUK_HOBJECT_IS_BUFOBJ((duk_hobject *) h_obj)) {
DUK_HBUFOBJ_ASSERT_VALID(h_obj);
return h_obj;
}
} else if (DUK_TVAL_IS_BUFFER(tv)) {
h_obj = (duk_hbufobj *) duk_to_hobject(thr, idx);
DUK_ASSERT(h_obj != NULL);
DUK_HBUFOBJ_ASSERT_VALID(h_obj);
return h_obj;
}
DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER);
DUK_WO_NORETURN(return NULL;);
}
DUK_LOCAL void duk__set_bufobj_buffer(duk_hthread *thr, duk_hbufobj *h_bufobj, duk_hbuffer *h_val) {
DUK_ASSERT(thr != NULL);
DUK_ASSERT(h_bufobj != NULL);
DUK_ASSERT(h_bufobj->buf == NULL); /* no need to decref */
DUK_ASSERT(h_val != NULL);
DUK_HBUFOBJ_ASSERT_VALID(h_bufobj);
DUK_UNREF(thr);
h_bufobj->buf = h_val;
DUK_HBUFFER_INCREF(thr, h_val);
h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val);
DUK_ASSERT(h_bufobj->shift == 0);
DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFOBJ_ELEM_UINT8);
DUK_ASSERT(h_bufobj->is_typedarray == 0);
DUK_HBUFOBJ_ASSERT_VALID(h_bufobj);
}
/* Shared offset/length coercion helper. */
DUK_LOCAL void duk__resolve_offset_opt_length(duk_hthread *thr,
duk_hbufobj *h_bufarg,
duk_idx_t idx_offset,
duk_idx_t idx_length,
duk_uint_t *out_offset,
duk_uint_t *out_length,
duk_bool_t throw_flag) {
duk_int_t offset_signed;
duk_int_t length_signed;
duk_uint_t offset;
duk_uint_t length;
offset_signed = duk_to_int(thr, idx_offset);
if (offset_signed < 0) {
goto fail_range;
}
offset = (duk_uint_t) offset_signed;
if (offset > h_bufarg->length) {
goto fail_range;
}
DUK_ASSERT_DISABLE(offset >= 0); /* unsigned */
DUK_ASSERT(offset <= h_bufarg->length);
if (duk_is_undefined(thr, idx_length)) {
DUK_ASSERT(h_bufarg->length >= offset);
length = h_bufarg->length - offset; /* >= 0 */
} else {
length_signed = duk_to_int(thr, idx_length);
if (length_signed < 0) {
goto fail_range;
}
length = (duk_uint_t) length_signed;
DUK_ASSERT(h_bufarg->length >= offset);
if (length > h_bufarg->length - offset) {
/* Unlike for negative arguments, some call sites
* want length to be clamped if it's positive.
*/
if (throw_flag) {
goto fail_range;
} else {
length = h_bufarg->length - offset;
}
}
}
DUK_ASSERT_DISABLE(length >= 0); /* unsigned */
DUK_ASSERT(offset + length <= h_bufarg->length);
*out_offset = offset;
*out_length = length;
return;
fail_range:
DUK_ERROR_RANGE(thr, DUK_STR_INVALID_ARGS);
DUK_WO_NORETURN(return;);
}
/* Shared lenient buffer length clamping helper. No negative indices, no
* element/byte shifting.
*/
DUK_LOCAL void duk__clamp_startend_nonegidx_noshift(duk_hthread *thr,
duk_int_t buffer_length,
duk_idx_t idx_start,
duk_idx_t idx_end,
duk_int_t *out_start_offset,
duk_int_t *out_end_offset) {
duk_int_t start_offset;
duk_int_t end_offset;
DUK_ASSERT(out_start_offset != NULL);
DUK_ASSERT(out_end_offset != NULL);
/* undefined coerces to zero which is correct */
start_offset = duk_to_int_clamped(thr, idx_start, 0, buffer_length);
if (duk_is_undefined(thr, idx_end)) {
end_offset = buffer_length;
} else {
end_offset = duk_to_int_clamped(thr, idx_end, start_offset, buffer_length);
}
DUK_ASSERT(start_offset >= 0);
DUK_ASSERT(start_offset <= buffer_length);
DUK_ASSERT(end_offset >= 0);
DUK_ASSERT(end_offset <= buffer_length);
DUK_ASSERT(start_offset <= end_offset);
*out_start_offset = start_offset;
*out_end_offset = end_offset;
}
/* Shared lenient buffer length clamping helper. Indices are treated as
* element indices (though output values are byte offsets) which only
* really matters for TypedArray views as other buffer object have a zero
* shift. Negative indices are counted from end of input slice; crossed
* indices are clamped to zero length; and final indices are clamped
* against input slice. Used for e.g. ArrayBuffer slice().
*/
DUK_LOCAL void duk__clamp_startend_negidx_shifted(duk_hthread *thr,
duk_int_t buffer_length,
duk_uint8_t buffer_shift,
duk_idx_t idx_start,
duk_idx_t idx_end,
duk_int_t *out_start_offset,
duk_int_t *out_end_offset) {
duk_int_t start_offset;
duk_int_t end_offset;
DUK_ASSERT(out_start_offset != NULL);
DUK_ASSERT(out_end_offset != NULL);
buffer_length >>= buffer_shift; /* as (full) elements */
/* Resolve start/end offset as element indices first; arguments
* at idx_start/idx_end are element offsets. Working with element
* indices first also avoids potential for wrapping.
*/
start_offset = duk_to_int(thr, idx_start);
if (start_offset < 0) {
start_offset = buffer_length + start_offset;
}
if (duk_is_undefined(thr, idx_end)) {
end_offset = buffer_length;
} else {
end_offset = duk_to_int(thr, idx_end);
if (end_offset < 0) {
end_offset = buffer_length + end_offset;
}
}
/* Note: start_offset/end_offset can still be < 0 here. */
if (start_offset < 0) {
start_offset = 0;
} else if (start_offset > buffer_length) {
start_offset = buffer_length;
}
if (end_offset < start_offset) {
end_offset = start_offset;
} else if (end_offset > buffer_length) {
end_offset = buffer_length;
}
DUK_ASSERT(start_offset >= 0);
DUK_ASSERT(start_offset <= buffer_length);
DUK_ASSERT(end_offset >= 0);
DUK_ASSERT(end_offset <= buffer_length);
DUK_ASSERT(start_offset <= end_offset);
/* Convert indices to byte offsets. */
start_offset <<= buffer_shift;
end_offset <<= buffer_shift;
*out_start_offset = start_offset;
*out_end_offset = end_offset;
}
DUK_INTERNAL void duk_hbufobj_promote_plain(duk_hthread *thr, duk_idx_t idx) {
if (duk_is_buffer(thr, idx)) {
duk_to_object(thr, idx);
}
}
DUK_INTERNAL void duk_hbufobj_push_uint8array_from_plain(duk_hthread *thr, duk_hbuffer *h_buf) {
/* Push Uint8Array which will share the same underlying buffer as
* the plain buffer argument. Also create an ArrayBuffer with the
* same backing for the result .buffer property.
*/
duk_push_hbuffer(thr, h_buf);
duk_push_buffer_object(thr, -1, 0, (duk_size_t) DUK_HBUFFER_GET_SIZE(h_buf), DUK_BUFOBJ_UINT8ARRAY);
duk_remove_m2(thr);
#if 0
/* More verbose equivalent; maybe useful if e.g. .buffer is omitted. */
h_bufobj = duk_push_bufobj_raw(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_BUFOBJ |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_UINT8ARRAY),
DUK_BIDX_UINT8ARRAY_PROTOTYPE);
DUK_ASSERT(h_bufobj != NULL);
duk__set_bufobj_buffer(thr, h_bufobj, h_buf);
h_bufobj->is_typedarray = 1;
DUK_HBUFOBJ_ASSERT_VALID(h_bufobj);
h_arrbuf = duk_push_bufobj_raw(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_BUFOBJ |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
DUK_ASSERT(h_arrbuf != NULL);
duk__set_bufobj_buffer(thr, h_arrbuf, h_buf);
DUK_ASSERT(h_arrbuf->is_typedarray == 0);
DUK_HBUFOBJ_ASSERT_VALID(h_arrbuf);
DUK_ASSERT(h_bufobj->buf_prop == NULL);
h_bufobj->buf_prop = (duk_hobject *) h_arrbuf;
DUK_ASSERT(h_arrbuf != NULL);
DUK_HBUFOBJ_INCREF(thr, h_arrbuf);
duk_pop(thr);
#endif
}
/* Indexed read helper for buffer objects, also called from outside this file. */
DUK_INTERNAL void duk_hbufobj_push_validated_read(duk_hthread *thr,
duk_hbufobj *h_bufobj,
duk_uint8_t *p,
duk_small_uint_t elem_size) {
duk_double_union du;
DUK_ASSERT(elem_size > 0);
duk_memcpy((void *) du.uc, (const void *) p, (size_t) elem_size);
switch (h_bufobj->elem_type) {
case DUK_HBUFOBJ_ELEM_UINT8:
case DUK_HBUFOBJ_ELEM_UINT8CLAMPED:
duk_push_uint(thr, (duk_uint_t) du.uc[0]);
break;
case DUK_HBUFOBJ_ELEM_INT8:
duk_push_int(thr, (duk_int_t) (duk_int8_t) du.uc[0]);
break;
case DUK_HBUFOBJ_ELEM_UINT16:
duk_push_uint(thr, (duk_uint_t) du.us[0]);
break;
case DUK_HBUFOBJ_ELEM_INT16:
duk_push_int(thr, (duk_int_t) (duk_int16_t) du.us[0]);
break;
case DUK_HBUFOBJ_ELEM_UINT32:
duk_push_uint(thr, (duk_uint_t) du.ui[0]);
break;
case DUK_HBUFOBJ_ELEM_INT32:
duk_push_int(thr, (duk_int_t) (duk_int32_t) du.ui[0]);
break;
case DUK_HBUFOBJ_ELEM_FLOAT32:
duk_push_number(thr, (duk_double_t) du.f[0]);
break;
case DUK_HBUFOBJ_ELEM_FLOAT64:
duk_push_number(thr, (duk_double_t) du.d);
break;
default:
DUK_UNREACHABLE();
}
}
/* Indexed write helper for buffer objects, also called from outside this file. */
DUK_INTERNAL void duk_hbufobj_validated_write(duk_hthread *thr, duk_hbufobj *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) {
duk_double_union du;
/* NOTE! Caller must ensure that any side effects from the
* coercions below are safe. If that cannot be guaranteed
* (which is normally the case), caller must coerce the
* argument using duk_to_number() before any pointer
* validations; the result of duk_to_number() always coerces
* without side effects here.
*/
switch (h_bufobj->elem_type) {
case DUK_HBUFOBJ_ELEM_UINT8:
du.uc[0] = (duk_uint8_t) duk_to_uint32(thr, -1);
break;
case DUK_HBUFOBJ_ELEM_UINT8CLAMPED:
du.uc[0] = (duk_uint8_t) duk_to_uint8clamped(thr, -1);
break;
case DUK_HBUFOBJ_ELEM_INT8:
du.uc[0] = (duk_uint8_t) duk_to_int32(thr, -1);
break;
case DUK_HBUFOBJ_ELEM_UINT16:
du.us[0] = (duk_uint16_t) duk_to_uint32(thr, -1);
break;
case DUK_HBUFOBJ_ELEM_INT16:
du.us[0] = (duk_uint16_t) duk_to_int32(thr, -1);
break;
case DUK_HBUFOBJ_ELEM_UINT32:
du.ui[0] = (duk_uint32_t) duk_to_uint32(thr, -1);
break;
case DUK_HBUFOBJ_ELEM_INT32:
du.ui[0] = (duk_uint32_t) duk_to_int32(thr, -1);
break;
case DUK_HBUFOBJ_ELEM_FLOAT32:
/* A double-to-float cast is undefined behavior in C99 if
* the cast is out-of-range, so use a helper. Example:
* runtime error: value -1e+100 is outside the range of representable values of type 'float'
*/
du.f[0] = duk_double_to_float_t(duk_to_number_m1(thr));
break;
case DUK_HBUFOBJ_ELEM_FLOAT64:
du.d = (duk_double_t) duk_to_number_m1(thr);
break;
default:
DUK_UNREACHABLE();
}
DUK_ASSERT(elem_size > 0);
duk_memcpy((void *) p, (const void *) du.uc, (size_t) elem_size);
}
/* Helper to create a fixed buffer from argument value at index 0.
* Node.js and allocPlain() compatible.
*/
DUK_LOCAL duk_hbuffer *duk__hbufobj_fixed_from_argvalue(duk_hthread *thr) {
duk_int_t len;
duk_int_t i;
duk_size_t buf_size;
duk_uint8_t *buf;
switch (duk_get_type(thr, 0)) {
case DUK_TYPE_NUMBER: {
len = duk_to_int_clamped(thr, 0, 0, DUK_INT_MAX);
(void) duk_push_fixed_buffer_zero(thr, (duk_size_t) len);
break;
}
case DUK_TYPE_BUFFER: { /* Treat like Uint8Array. */
goto slow_copy;
}
case DUK_TYPE_OBJECT: {
duk_hobject *h;
duk_hbufobj *h_bufobj;
/* For Node.js Buffers "Passing an ArrayBuffer returns a Buffer
* that shares allocated memory with the given ArrayBuffer."
* https://nodejs.org/api/buffer.html#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe
*/
h = duk_known_hobject(thr, 0);
if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAYBUFFER) {
DUK_ASSERT(DUK_HOBJECT_IS_BUFOBJ(h));
h_bufobj = (duk_hbufobj *) h;
if (DUK_UNLIKELY(h_bufobj->buf == NULL)) {
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return NULL;);
}
if (DUK_UNLIKELY(h_bufobj->offset != 0 || h_bufobj->length != DUK_HBUFFER_GET_SIZE(h_bufobj->buf))) {
/* No support for ArrayBuffers with slice
* offset/length.
*/
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return NULL;);
}
duk_push_hbuffer(thr, h_bufobj->buf);
return h_bufobj->buf;
}
goto slow_copy;
}
case DUK_TYPE_STRING: {
/* ignore encoding for now */
duk_require_hstring_notsymbol(thr, 0);
duk_dup_0(thr);
(void) duk_to_buffer(thr, -1, &buf_size);
break;
}
default:
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return NULL;);
}
done:
DUK_ASSERT(duk_is_buffer(thr, -1));
return duk_known_hbuffer(thr, -1);
slow_copy:
/* XXX: fast path for typed arrays and other buffer objects? */
(void) duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_LENGTH);
len = duk_to_int_clamped(thr, -1, 0, DUK_INT_MAX);
duk_pop(thr);
buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, (duk_size_t) len); /* no zeroing, all indices get initialized */
for (i = 0; i < len; i++) {
/* XXX: fast path for array or buffer arguments? */
duk_get_prop_index(thr, 0, (duk_uarridx_t) i);
buf[i] = (duk_uint8_t) (duk_to_uint32(thr, -1) & 0xffU);
duk_pop(thr);
}
goto done;
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
/*
* Node.js Buffer constructor
*
* Node.js Buffers are just Uint8Arrays with internal prototype set to
* Buffer.prototype so they're handled otherwise the same as Uint8Array.
* However, the constructor arguments are very different so a separate
* constructor entry point is used.
*/
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_hthread *thr) {
duk_hbuffer *h_buf;
h_buf = duk__hbufobj_fixed_from_argvalue(thr);
DUK_ASSERT(h_buf != NULL);
duk_push_buffer_object(thr, -1, 0, DUK_HBUFFER_FIXED_GET_SIZE((duk_hbuffer_fixed *) (void *) h_buf), DUK_BUFOBJ_UINT8ARRAY);
duk_push_hobject_bidx(thr, DUK_BIDX_NODEJS_BUFFER_PROTOTYPE);
duk_set_prototype(thr, -2);
/* XXX: a more direct implementation */
return 1;
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
/*
* ArrayBuffer, DataView, and TypedArray constructors
*/
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_hthread *thr) {
duk_hbufobj *h_bufobj;
duk_hbuffer *h_val;
duk_int_t len;
DUK_CTX_ASSERT_VALID(thr);
duk_require_constructor_call(thr);
len = duk_to_int(thr, 0);
if (len < 0) {
goto fail_length;
}
(void) duk_push_fixed_buffer_zero(thr, (duk_size_t) len);
h_val = (duk_hbuffer *) duk_known_hbuffer(thr, -1);
h_bufobj = duk_push_bufobj_raw(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_FLAG_BUFOBJ |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
DUK_ASSERT(h_bufobj != NULL);
duk__set_bufobj_buffer(thr, h_bufobj, h_val);
DUK_HBUFOBJ_ASSERT_VALID(h_bufobj);
return 1;
fail_length:
DUK_DCERROR_RANGE_INVALID_LENGTH(thr);
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
/* Format of magic, bits:
* 0...1: elem size shift (0-3)
* 2...5: elem type (DUK_HBUFOBJ_ELEM_xxx)
*
* XXX: add prototype bidx explicitly to magic instead of using a mapping?
*/
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_hthread *thr) {
duk_tval *tv;
duk_hobject *h_obj;
duk_hbufobj *h_bufobj = NULL;
duk_hbufobj *h_bufarg = NULL;
duk_hbuffer *h_val;
duk_small_uint_t magic;
duk_small_uint_t shift;
duk_small_uint_t elem_type;
duk_small_uint_t elem_size;
duk_small_uint_t class_num;
duk_small_uint_t proto_bidx;
duk_uint_t align_mask;
duk_uint_t elem_length;
duk_int_t elem_length_signed;
duk_uint_t byte_length;
duk_small_uint_t copy_mode;
/* XXX: The same copy helpers could be shared with at least some
* buffer functions.
*/
duk_require_constructor_call(thr);
/* We could fit built-in index into magic but that'd make the magic
* number dependent on built-in numbering (genbuiltins.py doesn't
* handle that yet). So map both class and prototype from the
* element type.
*/
magic = (duk_small_uint_t) duk_get_current_magic(thr);
shift = magic & 0x03U; /* bits 0...1: shift */
elem_type = (magic >> 2) & 0x0fU; /* bits 2...5: type */
elem_size = 1U << shift;
align_mask = elem_size - 1;
DUK_ASSERT(elem_type < sizeof(duk__buffer_proto_from_elemtype) / sizeof(duk_uint8_t));
proto_bidx = duk__buffer_proto_from_elemtype[elem_type];
DUK_ASSERT(proto_bidx < DUK_NUM_BUILTINS);
DUK_ASSERT(elem_type < sizeof(duk__buffer_class_from_elemtype) / sizeof(duk_uint8_t));
class_num = duk__buffer_class_from_elemtype[elem_type];
DUK_DD(DUK_DDPRINT("typedarray constructor, magic=%d, shift=%d, elem_type=%d, "
"elem_size=%d, proto_bidx=%d, class_num=%d",
(int) magic,
(int) shift,
(int) elem_type,
(int) elem_size,
(int) proto_bidx,
(int) class_num));
/* Argument variants. When the argument is an ArrayBuffer a view to
* the same buffer is created; otherwise a new ArrayBuffer is always
* created.
*/
/* XXX: initial iteration to treat a plain buffer like an ArrayBuffer:
* coerce to an ArrayBuffer object and use that as .buffer. The underlying
* buffer will be the same but result .buffer !== inputPlainBuffer.
*/
duk_hbufobj_promote_plain(thr, 0);
tv = duk_get_tval(thr, 0);
DUK_ASSERT(tv != NULL); /* arg count */
if (DUK_TVAL_IS_OBJECT(tv)) {
h_obj = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h_obj != NULL);
if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_ARRAYBUFFER) {
/* ArrayBuffer: unlike any other argument variant, create
* a view into the existing buffer.
*/
duk_int_t byte_offset_signed;
duk_uint_t byte_offset;
h_bufarg = (duk_hbufobj *) h_obj;
byte_offset_signed = duk_to_int(thr, 1);
if (byte_offset_signed < 0) {
goto fail_arguments;
}
byte_offset = (duk_uint_t) byte_offset_signed;
if (byte_offset > h_bufarg->length || (byte_offset & align_mask) != 0) {
/* Must be >= 0 and multiple of element size. */
goto fail_arguments;
}
if (duk_is_undefined(thr, 2)) {
DUK_ASSERT(h_bufarg->length >= byte_offset);
byte_length = h_bufarg->length - byte_offset;
if ((byte_length & align_mask) != 0) {
/* Must be element size multiple from
* start offset to end of buffer.
*/
goto fail_arguments;
}
elem_length = (byte_length >> shift);
} else {
elem_length_signed = duk_to_int(thr, 2);
if (elem_length_signed < 0) {
goto fail_arguments;
}
elem_length = (duk_uint_t) elem_length_signed;
byte_length = elem_length << shift;
if ((byte_length >> shift) != elem_length) {
/* Byte length would overflow. */
/* XXX: easier check with less code? */
goto fail_arguments;
}
DUK_ASSERT(h_bufarg->length >= byte_offset);
if (byte_length > h_bufarg->length - byte_offset) {
/* Not enough data. */
goto fail_arguments;
}
}
DUK_UNREF(elem_length);
DUK_ASSERT_DISABLE(byte_offset >= 0);
DUK_ASSERT(byte_offset <= h_bufarg->length);
DUK_ASSERT_DISABLE(byte_length >= 0);
DUK_ASSERT(byte_offset + byte_length <= h_bufarg->length);
DUK_ASSERT((elem_length << shift) == byte_length);
h_bufobj = duk_push_bufobj_raw(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_FLAG_BUFOBJ |
DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
(duk_small_int_t) proto_bidx);
h_val = h_bufarg->buf;
if (h_val == NULL) {
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
}
h_bufobj->buf = h_val;
DUK_HBUFFER_INCREF(thr, h_val);
h_bufobj->offset = h_bufarg->offset + byte_offset;
h_bufobj->length = byte_length;
h_bufobj->shift = (duk_uint8_t) shift;
h_bufobj->elem_type = (duk_uint8_t) elem_type;
h_bufobj->is_typedarray = 1;
DUK_HBUFOBJ_ASSERT_VALID(h_bufobj);
/* Set .buffer to the argument ArrayBuffer. */
DUK_ASSERT(h_bufobj->buf_prop == NULL);
h_bufobj->buf_prop = (duk_hobject *) h_bufarg;
DUK_ASSERT(h_bufarg != NULL);
DUK_HBUFOBJ_INCREF(thr, h_bufarg);
return 1;
} else if (DUK_HOBJECT_IS_BUFOBJ(h_obj)) {
/* TypedArray (or other non-ArrayBuffer duk_hbufobj).
* Conceptually same behavior as for an Array-like argument,
* with a few fast paths.
*/
h_bufarg = (duk_hbufobj *) h_obj;
DUK_HBUFOBJ_ASSERT_VALID(h_bufarg);
elem_length_signed = (duk_int_t) (h_bufarg->length >> h_bufarg->shift);
if (h_bufarg->buf == NULL) {
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
}
/* Select copy mode. Must take into account element
* compatibility and validity of the underlying source
* buffer.
*/
DUK_DDD(DUK_DDDPRINT("selecting copy mode for bufobj arg, "
"src byte_length=%ld, src shift=%d, "
"src/dst elem_length=%ld; "
"dst shift=%d -> dst byte_length=%ld",
(long) h_bufarg->length,
(int) h_bufarg->shift,
(long) elem_length_signed,
(int) shift,
(long) (elem_length_signed << shift)));
copy_mode = 2; /* default is explicit index read/write copy */
#if !defined(DUK_USE_PREFER_SIZE)
/* With a size optimized build copy_mode 2 is enough.
* Modes 0 and 1 are faster but conceptually the same.
*/
DUK_ASSERT(elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t));
if (DUK_HBUFOBJ_VALID_SLICE(h_bufarg)) {
if ((duk__buffer_elemtype_copy_compatible[elem_type] & (1 << h_bufarg->elem_type)) != 0) {
DUK_DDD(DUK_DDDPRINT("source/target are copy compatible, memcpy"));
DUK_ASSERT(shift == h_bufarg->shift); /* byte sizes will match */
copy_mode = 0;
} else {
DUK_DDD(DUK_DDDPRINT("source/target not copy compatible but valid, fast copy"));
copy_mode = 1;
}
}
#endif /* !DUK_USE_PREFER_SIZE */
} else {
/* Array or Array-like */
elem_length_signed = (duk_int_t) duk_get_length(thr, 0);
copy_mode = 2;
}
} else {
/* Non-object argument is simply int coerced, matches
* V8 behavior (except for "null", which we coerce to
* 0 but V8 TypeErrors).
*/
elem_length_signed = duk_to_int(thr, 0);
copy_mode = 3;
}
if (elem_length_signed < 0) {
goto fail_arguments;
}
elem_length = (duk_uint_t) elem_length_signed;
byte_length = (duk_uint_t) (elem_length << shift);
if ((byte_length >> shift) != elem_length) {
/* Byte length would overflow. */
/* XXX: easier check with less code? */
goto fail_arguments;
}
DUK_DDD(DUK_DDDPRINT("elem_length=%ld, byte_length=%ld", (long) elem_length, (long) byte_length));
/* ArrayBuffer argument is handled specially above; the rest of the
* argument variants are handled by shared code below.
*
* ArrayBuffer in h_bufobj->buf_prop is intentionally left unset.
* It will be automatically created by the .buffer accessor on
* first access.
*/
/* Push the resulting view object on top of a plain fixed buffer. */
(void) duk_push_fixed_buffer(thr, byte_length);
h_val = duk_known_hbuffer(thr, -1);
DUK_ASSERT(h_val != NULL);
h_bufobj =
duk_push_bufobj_raw(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE | DUK_HOBJECT_FLAG_BUFOBJ | DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
(duk_small_int_t) proto_bidx);
h_bufobj->buf = h_val;
DUK_HBUFFER_INCREF(thr, h_val);
DUK_ASSERT(h_bufobj->offset == 0);
h_bufobj->length = byte_length;
h_bufobj->shift = (duk_uint8_t) shift;
h_bufobj->elem_type = (duk_uint8_t) elem_type;
h_bufobj->is_typedarray = 1;
DUK_HBUFOBJ_ASSERT_VALID(h_bufobj);
/* Copy values, the copy method depends on the arguments.
*
* Copy mode decision may depend on the validity of the underlying
* buffer of the source argument; there must be no harmful side effects
* from there to here for copy_mode to still be valid.
*/
DUK_DDD(DUK_DDDPRINT("copy mode: %d", (int) copy_mode));
switch (copy_mode) {
/* Copy modes 0 and 1 can be omitted in size optimized build,
* copy mode 2 handles them (but more slowly).
*/
#if !defined(DUK_USE_PREFER_SIZE)
case 0: {
/* Use byte copy. */
duk_uint8_t *p_src;
duk_uint8_t *p_dst;
DUK_ASSERT(h_bufobj != NULL);
DUK_ASSERT(h_bufobj->buf != NULL);
DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufobj));
DUK_ASSERT(h_bufarg != NULL);
DUK_ASSERT(h_bufarg->buf != NULL);
DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufarg));
p_dst = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufobj);
p_src = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufarg);
DUK_DDD(DUK_DDDPRINT("using memcpy: p_src=%p, p_dst=%p, byte_length=%ld",
(void *) p_src,
(void *) p_dst,
(long) byte_length));
duk_memcpy_unsafe((void *) p_dst, (const void *) p_src, (size_t) byte_length);
break;
}
case 1: {
/* Copy values through direct validated reads and writes. */
duk_small_uint_t src_elem_size;
duk_small_uint_t dst_elem_size;
duk_uint8_t *p_src;
duk_uint8_t *p_src_end;
duk_uint8_t *p_dst;
DUK_ASSERT(h_bufobj != NULL);
DUK_ASSERT(h_bufobj->buf != NULL);
DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufobj));
DUK_ASSERT(h_bufarg != NULL);
DUK_ASSERT(h_bufarg->buf != NULL);
DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufarg));
src_elem_size = (duk_small_uint_t) (1U << h_bufarg->shift);
dst_elem_size = elem_size;
p_src = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufarg);
p_dst = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufobj);
p_src_end = p_src + h_bufarg->length;
DUK_DDD(DUK_DDDPRINT("using fast copy: p_src=%p, p_src_end=%p, p_dst=%p, "
"src_elem_size=%d, dst_elem_size=%d",
(void *) p_src,
(void *) p_src_end,
(void *) p_dst,
(int) src_elem_size,
(int) dst_elem_size));
while (p_src != p_src_end) {
DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: "
"p_src=%p, p_src_end=%p, p_dst=%p",
(void *) p_src,
(void *) p_src_end,
(void *) p_dst));
/* A validated read() is always a number, so it's write coercion
* is always side effect free an won't invalidate pointers etc.
*/
duk_hbufobj_push_validated_read(thr, h_bufarg, p_src, src_elem_size);
duk_hbufobj_validated_write(thr, h_bufobj, p_dst, dst_elem_size);
duk_pop(thr);
p_src += src_elem_size;
p_dst += dst_elem_size;
}
break;
}
#endif /* !DUK_USE_PREFER_SIZE */
case 2: {
/* Copy values by index reads and writes. Let virtual
* property handling take care of coercion.
*/
duk_uint_t i;
DUK_DDD(DUK_DDDPRINT("using slow copy"));
for (i = 0; i < elem_length; i++) {
duk_get_prop_index(thr, 0, (duk_uarridx_t) i);
duk_put_prop_index(thr, -2, (duk_uarridx_t) i);
}
break;
}
default:
case 3: {
/* No copy, leave zero bytes in the buffer. There's no
* ambiguity with Float32/Float64 because zero bytes also