-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTypedArray.h
More file actions
699 lines (597 loc) · 35.5 KB
/
Copy pathTypedArray.h
File metadata and controls
699 lines (597 loc) · 35.5 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
// Implements typed array.
//----------------------------------------------------------------------------
#pragma once
namespace Js
{
typedef Var (*PFNCreateTypedArray)(Js::ArrayBufferBase* arrayBuffer, uint32 offSet, uint32 mappedLength, Js::JavascriptLibrary* javascriptLibrary);
template<typename T> bool TypedArrayCompareElementsHelper(JavascriptArray::CompareVarsInfo* cvInfo, const void* elem1, const void* elem2);
class TypedArrayBase : public ArrayBufferParent
{
friend ArrayBuffer;
friend ArrayBufferBase;
protected:
DEFINE_VTABLE_CTOR_ABSTRACT(TypedArrayBase, ArrayBufferParent);
public:
static JavascriptFunction* GetDefaultConstructor(Var object, ScriptContext* scriptContext);
class EntryInfo
{
public:
static FunctionInfo NewInstance;
static FunctionInfo Set;
static FunctionInfo Subarray;
static FunctionInfo From;
static FunctionInfo Of;
static FunctionInfo At;
static FunctionInfo CopyWithin;
static FunctionInfo Entries;
static FunctionInfo Every;
static FunctionInfo Fill;
static FunctionInfo Filter;
static FunctionInfo Find;
static FunctionInfo FindIndex;
static FunctionInfo FindLast;
static FunctionInfo FindLastIndex;
static FunctionInfo ForEach;
static FunctionInfo IndexOf;
static FunctionInfo Includes;
static FunctionInfo Join;
static FunctionInfo Keys;
static FunctionInfo LastIndexOf;
static FunctionInfo Map;
static FunctionInfo Reduce;
static FunctionInfo ReduceRight;
static FunctionInfo Reverse;
static FunctionInfo Slice;
static FunctionInfo Some;
static FunctionInfo Sort;
static FunctionInfo Values;
static FunctionInfo GetterBuffer;
static FunctionInfo GetterByteLength;
static FunctionInfo GetterByteOffset;
static FunctionInfo GetterLength;
static FunctionInfo GetterSymbolToStringTag;
static FunctionInfo GetterSymbolSpecies;
};
TypedArrayBase(ArrayBufferBase* arrayBuffer, uint byteOffset, uint mappedLength, uint elementSize, DynamicType* type);
static Var NewInstance(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySet(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySubarray(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryFrom(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryOf(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryAt(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryCopyWithin(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryEntries(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryEvery(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryFill(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryFilter(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryFind(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryFindIndex(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryFindLast(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryFindLastIndex(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryForEach(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryIndexOf(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryIncludes(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryJoin(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryKeys(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryLastIndexOf(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryMap(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryReduce(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryReduceRight(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryReverse(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySlice(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySome(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySort(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryValues(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterBuffer(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterByteLength(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterByteOffset(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterLength(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterSymbolToStringTag(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterSymbolSpecies(RecyclableObject* function, CallInfo callInfo, ...);
virtual DescriptorFlags GetSetter(PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext) override;
virtual DescriptorFlags GetSetter(JavascriptString* propertyNameString, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext) override;
virtual DescriptorFlags GetItemSetter(uint32 index, Var* setterValue, ScriptContext* requestContext) override;
virtual PropertyQueryFlags HasPropertyQuery(Js::PropertyId propertyId, _Inout_opt_ PropertyValueInfo* info) override;
virtual BOOL HasOwnProperty(Js::PropertyId propertyId) override;
virtual PropertyQueryFlags GetPropertyQuery(Js::Var originalInstance, Js::PropertyId propertyId, Js::Var* value, Js::PropertyValueInfo* info, Js::ScriptContext* requestContext) override;
virtual PropertyQueryFlags GetPropertyQuery(Js::Var originalInstance, Js::JavascriptString* propertyNameString, Js::Var* value, Js::PropertyValueInfo* info, Js::ScriptContext* requestContext) override;
virtual PropertyQueryFlags GetPropertyReferenceQuery(Js::Var originalInstance, Js::PropertyId propertyId, Js::Var* value, Js::PropertyValueInfo* info, Js::ScriptContext* requestContext) override;
virtual PropertyQueryFlags HasItemQuery(uint32 index) override;
virtual BOOL DeleteItem(uint32 index, Js::PropertyOperationFlags flags) override;
virtual PropertyQueryFlags GetItemQuery(Js::Var originalInstance, uint32 index, Js::Var* value, Js::ScriptContext * requestContext) override;
virtual BOOL SetItem(uint32 index, Js::Var value, Js::PropertyOperationFlags flags = PropertyOperation_None) override;
virtual BOOL SetProperty(Js::PropertyId propertyId, Js::Var value, Js::PropertyOperationFlags flags, Js::PropertyValueInfo* info) override;
virtual BOOL SetProperty(Js::JavascriptString* propertyNameString, Js::Var value, Js::PropertyOperationFlags flags, Js::PropertyValueInfo* info) override;
virtual BOOL DeleteProperty(Js::PropertyId propertyId, Js::PropertyOperationFlags flags) override;
virtual BOOL DeleteProperty(JavascriptString *propertyNameString, Js::PropertyOperationFlags flags) override;
virtual PropertyQueryFlags GetItemReferenceQuery(Js::Var originalInstance, uint32 index, Js::Var* value, Js::ScriptContext * requestContext) override;
virtual BOOL GetEnumerator(JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext* requestContext, EnumeratorCache * enumeratorCache = nullptr) override;
virtual JavascriptEnumerator * GetIndexEnumerator(EnumeratorFlags flags, ScriptContext * requestContext) override;
virtual BOOL IsEnumerable(PropertyId propertyId) override;
virtual BOOL IsConfigurable(PropertyId propertyId) override;
virtual BOOL IsWritable(PropertyId propertyId) override;
virtual BOOL SetEnumerable(PropertyId propertyId, BOOL value) override;
virtual BOOL SetWritable(PropertyId propertyId, BOOL value) override;
virtual BOOL SetConfigurable(PropertyId propertyId, BOOL value) override;
virtual BOOL SetAttributes(PropertyId propertyId, PropertyAttributes attributes) override;
virtual BOOL SetAccessors(PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags) override;
virtual BOOL InitProperty(Js::PropertyId propertyId, Js::Var value, PropertyOperationFlags flags = PropertyOperation_None, Js::PropertyValueInfo* info = NULL) override;
virtual BOOL SetPropertyWithAttributes(PropertyId propertyId, Var value, PropertyAttributes attributes, PropertyValueInfo* info, PropertyOperationFlags flags = PropertyOperation_None, SideEffects possibleSideEffects = SideEffects_Any) override;
static BOOL Is(TypeId typeId);
// Returns false if this is not a TypedArray or it's not detached
static BOOL IsDetachedTypedArray(Var aValue);
static HRESULT GetBuffer(Var aValue, ArrayBuffer** outBuffer, uint32* outOffset, uint32* outLength);
static TypedArrayBase * ValidateTypedArray(Var aValue, ScriptContext *scriptContext, LPCWSTR apiName);
static TypedArrayBase * ValidateTypedArray(Arguments &args, ScriptContext *scriptContext, LPCWSTR apiName);
static Var TypedArrayCreate(Var constructor, Arguments *args, uint32 length, ScriptContext *scriptContext);
virtual BOOL DirectSetItem(_In_ uint32 index, _In_ Js::Var value) = 0;
virtual BOOL DirectSetItemNoSet(_In_ uint32 index, _In_ Js::Var value) = 0;
virtual Var DirectGetItem(_In_ uint32 index) = 0;
virtual BOOL DirectSetItemNoDetachCheck(_In_ uint32 index, _In_ Js::Var value) = 0;
virtual Var DirectGetItemNoDetachCheck(_In_ uint32 index) = 0;
virtual Var TypedAdd(_In_ uint32 index, _In_ Var second) = 0;
virtual Var TypedAnd(_In_ uint32 index, _In_ Var second) = 0;
virtual Var TypedLoad(_In_ uint32 index) = 0;
virtual Var TypedOr(_In_ uint32 index, _In_ Var second) = 0;
virtual Var TypedStore(_In_ uint32 index, _In_ Var second) = 0;
virtual Var TypedSub(_In_ uint32 index, _In_ Var second) = 0;
virtual Var TypedXor(_In_ uint32 index, _In_ Var second) = 0;
virtual Var TypedExchange(_In_ uint32 index, _In_ Var second) = 0;
virtual Var TypedCompareExchange(_In_ uint32 index, _In_ Var comparand, _In_ Var replacementValue) = 0;
uint32 GetByteLength() const { return length * BYTES_PER_ELEMENT; }
uint32 GetByteOffset() const { return byteOffset; }
uint32 GetBytesPerElement() const { return BYTES_PER_ELEMENT; }
byte* GetByteBuffer() const { return buffer; };
bool IsDetachedBuffer() const { return this->GetArrayBuffer()->IsDetached(); }
void ClearLengthAndBufferOnDetach();
static Var CommonSet(Arguments& args);
static Var CommonSubarray(Arguments& args);
void SetObject(RecyclableObject* arraySource, uint32 targetLength, uint32 offset = 0);
void SetObjectNoDetachCheck(RecyclableObject* arraySource, uint32 targetLength, uint32 offset = 0);
void Set(TypedArrayBase* typedArraySource, uint32 offset = 0);
virtual BOOL GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
virtual BOOL GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
static bool TryGetLengthForOptimizedTypedArray(const Var var, uint32 *const lengthRef, TypeId *const typeIdRef);
BOOL ValidateIndexAndDirectSetItem(_In_ Js::Var index, _In_ Js::Var value, _Out_ bool * isNumericIndex);
uint32 ValidateAndReturnIndex(_In_ Js::Var index, _Out_ bool * skipOperation, _Out_ bool * isNumericIndex);
// objectArray support
virtual BOOL SetItemWithAttributes(uint32 index, Var value, PropertyAttributes attributes) override;
virtual BOOL IsObjectArrayFrozen() override;
Var FindMinOrMax(Js::ScriptContext * scriptContext, TypeId typeId, bool findMax);
template<typename T, bool checkNaNAndNegZero> Var FindMinOrMax(Js::ScriptContext * scriptContext, bool findMax);
static Var GetKeysEntriesValuesHelper(Arguments& args, ScriptContext *scriptContext, LPCWSTR apiName, JavascriptArrayIteratorKind kind);
static uint32 GetFromIndex(Var arg, uint32 length, ScriptContext *scriptContext);
private:
uint32 GetSourceLength(RecyclableObject* arraySource, uint32 targetLength, uint32 offset);
protected:
static Var CreateNewInstanceFromIterator(RecyclableObject *iterator, ScriptContext *scriptContext, uint32 elementSize, PFNCreateTypedArray pfnCreateTypedArray);
static Var CreateNewInstance(Arguments& args, ScriptContext* scriptContext, uint32 elementSize, PFNCreateTypedArray pfnCreateTypedArray );
static bool ArrayIteratorPrototypeHasUserDefinedNext(ScriptContext *scriptContext);
static BOOL CanonicalNumericIndexString(PropertyId propertyId, ScriptContext *scriptContext);
static BOOL CanonicalNumericIndexString(JavascriptString *propertyString, ScriptContext *scriptContext);
virtual void SortHelper(byte* listBuffer, uint32 length, RecyclableObject* compareFn, ScriptContext* scriptContext, ArenaAllocator* allocator) = 0;
virtual Var Subarray(uint32 begin, uint32 end) = 0;
Field(int32) BYTES_PER_ELEMENT;
Field(uint32) byteOffset;
FieldNoBarrier(BYTE*) buffer; // beginning of mapped array.
public:
static uint32 GetOffsetOfBuffer() { return offsetof(TypedArrayBase, buffer); }
static uint32 GetOffsetOfLength() { return offsetof(TypedArrayBase, length); }
#if ENABLE_TTD
public:
virtual TTD::NSSnapObjects::SnapObjectType GetSnapTag_TTD() const override;
virtual void ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc) override;
#endif
};
template <> inline bool VarIsImpl<TypedArrayBase>(RecyclableObject* obj)
{
return TypedArrayBase::Is(JavascriptOperators::GetTypeId(obj));
}
template <typename TypeName, bool clamped = false, bool virtualAllocated = false>
class TypedArray;
// These are referenced in the TypedArray implementation, so we need to forward-typedef these.
typedef TypedArray<int8> Int8Array;
typedef TypedArray<uint8,false> Uint8Array;
typedef TypedArray<uint8,true> Uint8ClampedArray;
typedef TypedArray<int16> Int16Array;
typedef TypedArray<uint16> Uint16Array;
typedef TypedArray<int32> Int32Array;
typedef TypedArray<uint32> Uint32Array;
typedef TypedArray<float> Float32Array;
typedef TypedArray<double> Float64Array;
typedef TypedArray<int64> Int64Array;
typedef TypedArray<uint64> Uint64Array;
typedef TypedArray<bool> BoolArray;
typedef TypedArray<int8, false, true> Int8VirtualArray;
typedef TypedArray<uint8, false, true> Uint8VirtualArray;
typedef TypedArray<uint8, true, true> Uint8ClampedVirtualArray;
typedef TypedArray<int16, false, true> Int16VirtualArray;
typedef TypedArray<uint16, false, true> Uint16VirtualArray;
typedef TypedArray<int32, false, true> Int32VirtualArray;
typedef TypedArray<uint32, false, true> Uint32VirtualArray;
typedef TypedArray<float, false, true> Float32VirtualArray;
typedef TypedArray<double, false, true> Float64VirtualArray;
template <typename TypeName, bool clamped /*= false*/, bool virtualAllocated /*= false*/>
class TypedArray : public TypedArrayBase
{
protected:
DEFINE_VTABLE_CTOR(TypedArray, TypedArrayBase);
DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(TypedArray);
TypedArray(DynamicType *type): TypedArrayBase(nullptr, 0, 0, sizeof(TypeName), type) { buffer = nullptr; }
public:
typedef TypeName TypedArrayType;
class EntryInfo
{
public:
static FunctionInfo NewInstance;
static FunctionInfo Set;
};
TypedArray(ArrayBufferBase* arrayBuffer, uint32 byteOffset, uint32 mappedLength, DynamicType* type);
static Var Create(ArrayBufferBase* arrayBuffer, uint32 byteOffSet, uint32 mappedLength, JavascriptLibrary* javascriptLibrary);
static Var NewInstance(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySet(RecyclableObject* function, CallInfo callInfo, ...);
Var Subarray(uint32 begin, uint32 end);
static BOOL HasVirtualTableInfo(Var aValue)
{
return VirtualTableInfo<TypedArray<TypeName, clamped, virtualAllocated>>::HasVirtualTable(aValue) || VirtualTableInfo<CrossSiteObject<TypedArray<TypeName, clamped, virtualAllocated>>>::HasVirtualTable(aValue);
}
inline Var BaseTypedDirectGetItem(_In_ uint32 index)
{
if (this->IsDetachedBuffer()) // 9.4.5.8 IntegerIndexedElementGet
{
JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_DetachedTypedArray);
}
if (index < GetLength())
{
Assert((index + 1)* sizeof(TypeName)+GetByteOffset() <= GetArrayBuffer()->GetByteLength());
TypeName* typedBuffer = (TypeName*)buffer;
return JavascriptNumber::ToVar(typedBuffer[index], GetScriptContext());
}
return GetLibrary()->GetUndefined();
}
inline Var TypedDirectGetItemWithCheck(_In_ uint32 index)
{
if (this->IsDetachedBuffer()) // 9.4.5.8 IntegerIndexedElementGet
{
JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_DetachedTypedArray);
}
if (index < GetLength())
{
Assert((index + 1)* sizeof(TypeName)+GetByteOffset() <= GetArrayBuffer()->GetByteLength());
TypeName* typedBuffer = (TypeName*)buffer;
return JavascriptNumber::ToVarWithCheck(typedBuffer[index], GetScriptContext());
}
return GetLibrary()->GetUndefined();
}
inline Var BaseTypedDirectGetItemNoDetachCheck(_In_ uint32 index)
{
Assert(!IsDetachedBuffer());
Assert(index < GetLength());
Assert((index + 1)* sizeof(TypeName) + GetByteOffset() <= GetArrayBuffer()->GetByteLength());
TypeName* typedBuffer = (TypeName*)buffer;
return JavascriptNumber::ToVar(typedBuffer[index], GetScriptContext());
}
inline Var DirectGetItemVarCheckNoDetachCheck(_In_ uint32 index)
{
Assert(!IsDetachedBuffer());
Assert(index < GetLength());
Assert((index + 1)* sizeof(TypeName) + GetByteOffset() <= GetArrayBuffer()->GetByteLength());
TypeName* typedBuffer = (TypeName*)buffer;
return JavascriptNumber::ToVarWithCheck(typedBuffer[index], GetScriptContext());
}
inline BOOL DirectSetItemAtRange(TypedArray *fromArray, _In_ int32 iSrcStart, _In_ int32 iDstStart, _In_ uint32 length, TypeName(*convFunc)(Var value, ScriptContext* scriptContext))
{
TypeName* dstBuffer = (TypeName*)buffer;
TypeName* srcBuffer = (TypeName*)fromArray->buffer;
Assert(srcBuffer && dstBuffer);
Assert(length <= ArrayBuffer::MaxArrayBufferLength / sizeof(TypeName));
// caller checks that src and dst index are the same
Assert(iSrcStart == iDstStart);
if (this->IsDetachedBuffer() || fromArray->IsDetachedBuffer())
{
JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_DetachedTypedArray);
}
// Fixup destination start in case it's negative
uint32 start = iDstStart;
if (iDstStart < 0)
{
if ((int64)(length) + iDstStart < 0)
{
// nothing to do, all index are no-op
return true;
}
length += iDstStart;
start = 0;
}
uint32 dstLength = UInt32Math::Add(start, length) < GetLength() ? length : GetLength() > start ? GetLength() - start : 0;
uint32 srcLength = start + length < fromArray->GetLength() ? length : (fromArray->GetLength() > start ? fromArray->GetLength() - start : 0);
// length is the minimum of length, srcLength and dstLength
length = length < srcLength ? (length < dstLength ? length : dstLength) : (srcLength < dstLength ? srcLength : dstLength);
const size_t byteSize = sizeof(TypeName) * length;
Assert(byteSize >= length); // check for overflow
js_memcpy_s(dstBuffer + start, dstLength * sizeof(TypeName), srcBuffer + start, byteSize);
if (dstLength > length)
{
TypeName undefinedValue = convFunc(GetLibrary()->GetUndefined(), GetScriptContext());
for (uint32 i = length; i < dstLength; i++)
{
dstBuffer[i] = undefinedValue;
}
}
return true;
}
inline BOOL DirectSetItemAtRange(_In_ int32 start, _In_ uint32 length, _In_ TypeName typedValue)
{
if (CrossSite::IsCrossSiteObjectTyped(this))
{
return false;
}
if (this->IsDetachedBuffer()) // 9.4.5.9 IntegerIndexedElementSet
{
JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_DetachedTypedArray);
}
uint32 newStart = start, newLength = length;
if (start < 0)
{
if ((int64)(length) + start < 0)
{
// nothing to do, all index are no-op
return true;
}
newStart = 0;
// fixup the length with the change
newLength += start;
}
if (newStart >= GetLength())
{
// If we want to start copying past the length of the array, all index are no-op
return true;
}
if (UInt32Math::Add(newStart, newLength) > GetLength())
{
newLength = GetLength() - newStart;
}
TypeName* typedBuffer = (TypeName*)buffer;
if (typedValue == 0 || sizeof(TypeName) == 1)
{
const size_t byteSize = sizeof(TypeName) * newLength;
Assert(byteSize >= newLength); // check for overflow
memset(typedBuffer + newStart, (int)typedValue, byteSize);
}
else
{
for (uint32 i = 0; i < newLength; i++)
{
typedBuffer[newStart + i] = typedValue;
}
}
return TRUE;
}
inline BOOL BaseTypedDirectSetItem(_In_ uint32 index, _In_ Js::Var value, TypeName (*convFunc)(Var value, ScriptContext* scriptContext))
{
// This call can potentially invoke user code, and may end up detaching the underlying array (this).
// Therefore it was brought out and above the IsDetached check
TypeName typedValue = convFunc(value, GetScriptContext());
if (this->IsDetachedBuffer()) // 9.4.5.9 IntegerIndexedElementSet
{
JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_DetachedTypedArray);
}
if (index >= GetLength())
{
return FALSE;
}
AssertMsg(index < GetLength(), "Trying to set out of bound index for typed array.");
Assert((index + 1)* sizeof(TypeName) + GetByteOffset() <= GetArrayBuffer()->GetByteLength());
TypeName* typedBuffer = (TypeName*)buffer;
typedBuffer[index] = typedValue;
return TRUE;
}
inline BOOL BaseTypedDirectSetItemNoSet(_In_ uint32 index, _In_ Js::Var value, TypeName (*convFunc)(Var value, ScriptContext* scriptContext))
{
// This call can potentially invoke user code, and may end up detaching the underlying array (this).
// Therefore it was brought out and above the IsDetached check
convFunc(value, GetScriptContext());
if (this->IsDetachedBuffer()) // 9.4.5.9 IntegerIndexedElementSet
{
JavascriptError::ThrowTypeError(GetScriptContext(), JSERR_DetachedTypedArray);
}
return FALSE;
}
inline BOOL BaseTypedDirectSetItemNoDetachCheck(_In_ uint32 index, _In_ Js::Var value, TypeName(*convFunc)(Var value, ScriptContext* scriptContext))
{
TypeName typedValue = convFunc(value, GetScriptContext());
// The caller of the function made sure that no IsDetached check required.
// The caller of the function also made sure that no length check required.
Assert(!IsDetachedBuffer());
AssertMsg(index < GetLength(), "Trying to set out of bound index for typed array.");
Assert((index + 1)* sizeof(TypeName) + GetByteOffset() <= GetArrayBuffer()->GetByteLength());
TypeName* typedBuffer = (TypeName*)buffer;
typedBuffer[index] = typedValue;
return TRUE;
}
virtual BOOL DirectSetItem(_In_ uint32 index, _In_ Js::Var value) override sealed;
virtual BOOL DirectSetItemNoSet(_In_ uint32 index, _In_ Js::Var value) override sealed;
virtual Var DirectGetItem(_In_ uint32 index) override sealed;
virtual BOOL DirectSetItemNoDetachCheck(_In_ uint32 index, _In_ Js::Var value) override sealed;
virtual Var DirectGetItemNoDetachCheck(_In_ uint32 index) override sealed;
virtual Var TypedAdd(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedAnd(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedLoad(_In_ uint32 index) override;
virtual Var TypedOr(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedStore(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedSub(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedXor(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedExchange(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedCompareExchange(_In_ uint32 index, _In_ Var comparand, _In_ Var replacementValue) override;
static BOOL DirectSetItem(_In_ TypedArray* arr, _In_ uint32 index, _In_ Js::Var value)
{
AssertMsg(arr != nullptr, "Array shouldn't be nullptr.");
return arr->DirectSetItem(index, value);
}
protected:
void SortHelper(byte* listBuffer, uint32 length, RecyclableObject* compareFn, ScriptContext* scriptContext, ArenaAllocator* allocator)
{
TypeName* list = reinterpret_cast<TypeName*>(listBuffer);
JavascriptArray::CompareVarsInfo cvInfo;
cvInfo.scriptContext = scriptContext;
cvInfo.compFn = compareFn;
cvInfo.compareType = &TypedArrayCompareElementsHelper<TypeName>;
JavascriptArray::TypedArraySort<TypeName>(list, length, &cvInfo, allocator);
}
public:
virtual VTableValue DummyVirtualFunctionToHinderLinkerICF();
};
// in windows build environment, char16 is not an intrinsic type, and we cannot do the type
// specialization
class CharArray : public TypedArrayBase
{
protected:
DEFINE_VTABLE_CTOR(CharArray, TypedArrayBase);
DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(CharArray);
public:
class EntryInfo
{
public:
static FunctionInfo NewInstance;
static FunctionInfo Set;
static FunctionInfo Subarray;
};
static Var EntrySet(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySubarray(RecyclableObject* function, CallInfo callInfo, ...);
CharArray(ArrayBufferBase* arrayBuffer, uint32 byteOffset, uint32 mappedLength, DynamicType* type) :
TypedArrayBase(arrayBuffer, byteOffset, mappedLength, sizeof(char16), type)
{
AssertMsg(arrayBuffer->GetByteLength() >= byteOffset, "invalid offset");
AssertMsg(mappedLength*sizeof(char16)+byteOffset <= GetArrayBuffer()->GetByteLength(), "invalid length");
buffer = arrayBuffer->GetBuffer() + byteOffset;
}
static Var Create(ArrayBufferBase* arrayBuffer, uint32 byteOffSet, uint32 mappedLength, JavascriptLibrary* javascriptLibrary);
static Var NewInstance(RecyclableObject* function, CallInfo callInfo, ...);
Var Subarray(uint32 begin, uint32 end);
virtual BOOL DirectSetItem(_In_ uint32 index, _In_ Js::Var value) override;
virtual BOOL DirectSetItemNoSet(_In_ uint32 index, _In_ Js::Var value) override;
virtual Var DirectGetItem(_In_ uint32 index) override;
virtual BOOL DirectSetItemNoDetachCheck(_In_ uint32 index, _In_ Js::Var value) override;
virtual Var DirectGetItemNoDetachCheck(_In_ uint32 index) override;
virtual Var TypedAdd(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedAnd(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedLoad(_In_ uint32 index) override;
virtual Var TypedOr(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedStore(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedSub(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedXor(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedExchange(_In_ uint32 index, _In_ Var second) override;
virtual Var TypedCompareExchange(_In_ uint32 index, _In_ Var comparand, _In_ Var replacementValue) override;
protected:
void SortHelper(byte* listBuffer, uint32 length, RecyclableObject* compareFn, ScriptContext* scriptContext, ArenaAllocator* allocator)
{
char16* list = reinterpret_cast<char16*>(listBuffer);
JavascriptArray::CompareVarsInfo cvInfo;
cvInfo.scriptContext = scriptContext;
cvInfo.compFn = compareFn;
cvInfo.compareType = &TypedArrayCompareElementsHelper<char16>;
JavascriptArray::TypedArraySort<char16>(list, length, &cvInfo, allocator);
}
public:
virtual VTableValue DummyVirtualFunctionToHinderLinkerICF()
{
return VTableValue::VtableCharArray;
}
};
template <> bool VarIsImpl<CharArray>(RecyclableObject* obj);
template <typename TypeName, bool clamped, bool virtualAllocated>
TypedArray<TypeName, clamped, virtualAllocated>::TypedArray(ArrayBufferBase* arrayBuffer, uint32 byteOffset, uint32 mappedLength, DynamicType* type) :
TypedArrayBase(arrayBuffer, byteOffset, mappedLength, sizeof(TypeName), type)
{
AssertMsg(arrayBuffer->GetByteLength() >= byteOffset, "invalid offset");
AssertMsg(mappedLength*sizeof(TypeName)+byteOffset <= arrayBuffer->GetByteLength(), "invalid length");
buffer = arrayBuffer->GetBuffer() + byteOffset;
if (arrayBuffer->IsValidVirtualBufferLength(arrayBuffer->GetByteLength()) &&
(byteOffset == 0) &&
(mappedLength == (arrayBuffer->GetByteLength() / sizeof(TypeName)))
)
{
// update the vtable
switch (type->GetTypeId())
{
case TypeIds_Int8Array:
VirtualTableInfo<Int8VirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Uint8Array:
VirtualTableInfo<Uint8VirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Uint8ClampedArray:
VirtualTableInfo<Uint8ClampedVirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Int16Array:
VirtualTableInfo<Int16VirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Uint16Array:
VirtualTableInfo<Uint16VirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Int32Array:
VirtualTableInfo<Int32VirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Uint32Array:
VirtualTableInfo<Uint32VirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Float32Array:
VirtualTableInfo<Float32VirtualArray>::SetVirtualTable(this);
break;
case TypeIds_Float64Array:
VirtualTableInfo<Float64VirtualArray>::SetVirtualTable(this);
break;
default:
break;
}
}
}
template <typename TypeName, bool clamped, bool virtualAllocated>
Var TypedArray<TypeName, clamped, virtualAllocated>::Create(ArrayBufferBase* arrayBuffer, uint32 byteOffSet, uint32 mappedLength, JavascriptLibrary* javascriptLibrary)
{
uint32 totalLength, mappedByteLength;
if (UInt32Math::Mul(mappedLength, sizeof(TypeName), &mappedByteLength) ||
UInt32Math::Add(byteOffSet, mappedByteLength, &totalLength) ||
(totalLength > arrayBuffer->GetByteLength()))
{
JavascriptError::ThrowRangeError(arrayBuffer->GetScriptContext(), JSERR_InvalidTypedArrayLength);
}
DynamicType *type = javascriptLibrary->GetTypedArrayType<TypeName, clamped>(0);
return RecyclerNew(javascriptLibrary->GetRecycler(), TypedArray, arrayBuffer, byteOffSet, mappedLength, type);
}
#if defined(__clang__)
// hack for clang message: "...add an explicit instantiation declaration to .."
#define __EXPLICIT_INSTANTINATE_TA(x) \
template<> FunctionInfo Js::x::EntryInfo::NewInstance;\
template<> FunctionInfo Js::x::EntryInfo::Set
#else // MSVC
#define __EXPLICIT_INSTANTINATE_TA(x)
#endif
__EXPLICIT_INSTANTINATE_TA(Int8Array);
__EXPLICIT_INSTANTINATE_TA(Uint8Array);
__EXPLICIT_INSTANTINATE_TA(Uint8ClampedArray);
__EXPLICIT_INSTANTINATE_TA(Int16Array);
__EXPLICIT_INSTANTINATE_TA(Uint16Array);
__EXPLICIT_INSTANTINATE_TA(Int32Array);
__EXPLICIT_INSTANTINATE_TA(Uint32Array);
__EXPLICIT_INSTANTINATE_TA(Float32Array);
__EXPLICIT_INSTANTINATE_TA(Float64Array);
__EXPLICIT_INSTANTINATE_TA(Int64Array);
__EXPLICIT_INSTANTINATE_TA(Uint64Array);
__EXPLICIT_INSTANTINATE_TA(BoolArray);
__EXPLICIT_INSTANTINATE_TA(Int8VirtualArray);
__EXPLICIT_INSTANTINATE_TA(Uint8VirtualArray);
__EXPLICIT_INSTANTINATE_TA(Uint8ClampedVirtualArray);
__EXPLICIT_INSTANTINATE_TA(Int16VirtualArray);
__EXPLICIT_INSTANTINATE_TA(Uint16VirtualArray);
__EXPLICIT_INSTANTINATE_TA(Int32VirtualArray);
__EXPLICIT_INSTANTINATE_TA(Uint32VirtualArray);
__EXPLICIT_INSTANTINATE_TA(Float32VirtualArray);
__EXPLICIT_INSTANTINATE_TA(Float64VirtualArray);
#undef __EXPLICIT_INSTANTINATE_TA
}