• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/js_typed_array.h"
17 
18 #include "ecmascript/accessor_data.h"
19 #include "ecmascript/base/typed_array_helper-inl.h"
20 #include "ecmascript/builtins/builtins_arraybuffer.h"
21 
22 namespace panda::ecmascript {
23 using TypedArrayHelper = base::TypedArrayHelper;
24 using BuiltinsArrayBuffer = builtins::BuiltinsArrayBuffer;
25 
ToPropKey(JSThread * thread,const JSHandle<JSTaggedValue> & key)26 JSHandle<JSTaggedValue> JSTypedArray::ToPropKey(JSThread *thread, const JSHandle<JSTaggedValue> &key)
27 {
28     if (key->IsSymbol()) {
29         return key;
30     }
31     return JSHandle<JSTaggedValue>(JSTaggedValue::ToString(thread, key));
32 }
33 // 9.4.5.1 [[GetOwnProperty]] ( P )
GetOwnProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key,PropertyDescriptor & desc)34 bool JSTypedArray::GetOwnProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
35                                   const JSHandle<JSTaggedValue> &key, PropertyDescriptor &desc)
36 {
37     // 1. Assert : IsPropertyKey(P) is true.
38     ASSERT(JSTaggedValue::IsPropertyKey(key));
39     // 2. Assert: O is an Object that has a [[ViewedArrayBuffer]] internal slot.
40     // 3. If Type(P) is String, then
41     //   a. Let numericIndex be CanonicalNumericIndexString(P).
42     //   b. Assert: numericIndex is not an abrupt completion.
43     //   c. If numericIndex is not undefined, then
44     //     i. Let value be IntegerIndexedElementGet (O, numericIndex).
45     //     ii. ReturnIfAbrupt(value).
46     //     iii. If value is undefined, return undefined.
47     //     iv. Return a PropertyDescriptor{ [[Value]]: value, [[Enumerable]]: true, [[Writable]]: true,
48     //         [[Configurable]]: false }.
49     if (key->IsString() || key->IsNumber()) {
50         JSTaggedValue numericIndex = JSTaggedValue::CanonicalNumericIndexString(thread, key);
51         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
52         if (!numericIndex.IsUndefined()) {
53             JSHandle<JSTaggedValue> value =
54                 JSTypedArray::IntegerIndexedElementGet(thread, typedarray, numericIndex).GetValue();
55             RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
56             if (value->IsUndefined()) {
57                 return false;
58             }
59             desc.SetValue(value);
60             desc.SetEnumerable(true);
61             desc.SetWritable(true);
62             desc.SetConfigurable(true);
63             return true;
64         }
65     }
66     // 4. Return OrdinaryGetOwnProperty(O, P).
67     return JSObject::OrdinaryGetOwnProperty(thread, JSHandle<JSObject>(typedarray), key, desc);
68 }
69 
70 // 9.4.5.2 [[HasProperty]] ( P )
HasProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key)71 bool JSTypedArray::HasProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
72                                const JSHandle<JSTaggedValue> &key)
73 {
74     // 1. Assert: IsPropertyKey(P) is true.
75     ASSERT(JSTaggedValue::IsPropertyKey(key));
76     // 2. Assert: O is an Object that has a [[ViewedArrayBuffer]] internal slot.
77     // 3. If Type(P) is String, then
78     //   a. Let numericIndex be CanonicalNumericIndexString(P).
79     //   b. Assert: numericIndex is not an abrupt completion.
80     //   c. If numericIndex is not undefined, then
81     //     i. Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
82     //     ii. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
83     //     iii. If IsInteger(numericIndex) is false, return false
84     //     iv. If numericIndex = −0, return false.
85     //     v. If numericIndex < 0, return false.
86     //     vi. If numericIndex ≥ the value of O’s [[ArrayLength]] internal slot, return false.
87     //     vii. Return true.
88     JSHandle<JSTypedArray> typedarrayObj(typedarray);
89     if (key->IsString() || key->IsNumber()) {
90         JSTaggedValue numericIndex = JSTaggedValue::CanonicalNumericIndexString(thread, key);
91         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
92         if (!numericIndex.IsUndefined()) {
93             JSTaggedValue buffer = typedarrayObj->GetViewedArrayBufferOrByteArray();
94             if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
95                 THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer", false);
96             }
97             if (!numericIndex.IsInteger()) {
98                 return false;
99             }
100             JSHandle<JSTaggedValue> numericIndexHandle(thread, numericIndex);
101             JSTaggedNumber numericIndexNumber = JSTaggedValue::ToNumber(thread, numericIndexHandle);
102             RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
103             double tNegZero = -0.0;
104             auto eZero = JSTaggedNumber(tNegZero);
105             JSHandle<JSTaggedValue> zero(thread, JSTaggedValue(0));
106             if (JSTaggedNumber::SameValue(numericIndexNumber, eZero)) {
107                 return false;
108             }
109 
110             if (JSTaggedValue::Less(thread, numericIndexHandle, zero)) {
111                 return false;
112             }
113             uint32_t arrLen = typedarrayObj->GetArrayLength();
114             JSHandle<JSTaggedValue> arrLenHandle(thread, JSTaggedValue(arrLen));
115             return JSTaggedValue::Less(thread, numericIndexHandle, arrLenHandle);
116         }
117     }
118     // 4. Return OrdinaryHasProperty(O, P).
119     PropertyDescriptor desc(thread);
120     if (JSObject::OrdinaryGetOwnProperty(thread, JSHandle<JSObject>::Cast(typedarrayObj), key, desc)) {
121         return true;
122     }
123     JSTaggedValue parent = JSTaggedValue::GetPrototype(thread, JSHandle<JSTaggedValue>::Cast(typedarrayObj));
124     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
125     if (!parent.IsNull()) {
126         return JSTaggedValue::HasProperty(thread, JSHandle<JSTaggedValue>(thread, parent), key);
127     }
128     return false;
129 }
130 
131 // 9.4.5.3 [[DefineOwnProperty]] ( P, Desc )
DefineOwnProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key,const PropertyDescriptor & desc)132 bool JSTypedArray::DefineOwnProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
133                                      const JSHandle<JSTaggedValue> &key, const PropertyDescriptor &desc)
134 {
135     // 1. Assert: IsPropertyKey(P) is true.
136     ASSERT(JSTaggedValue::IsPropertyKey(key));
137     // 2. Assert: O is an Object that has a [[ViewedArrayBuffer]] internal slot.
138     // 3. If Type(P) is String, then
139     //   a. Let numericIndex be CanonicalNumericIndexString (P).
140     //   b. Assert: numericIndex is not an abrupt completion.
141     //   c. If numericIndex is not undefined, then
142     JSHandle<JSTypedArray> typedarrayObj(typedarray);
143     if (key->IsString() || key->IsNumber()) {
144         JSTaggedValue numericIndex = JSTaggedValue::CanonicalNumericIndexString(thread, key);
145         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
146         if (!numericIndex.IsUndefined()) {
147             // i. If IsInteger(numericIndex) is false, return false
148             // ii. Let intIndex be numericIndex.
149             // iii. If intIndex = −0, return false.
150             // iv. If intIndex < 0, return false.
151             // v. Let length be the value of O’s [[ArrayLength]] internal slot.
152             // vi. If intIndex ≥ length, return false.
153             // vii. If IsAccessorDescriptor(Desc) is true, return false.
154             // viii. If Desc has a [[Configurable]] field and if Desc.[[Configurable]] is true, return false.
155             // ix. If Desc has an [[Enumerable]] field and if Desc.[[Enumerable]] is false, return false.
156             // x. If Desc has a [[Writable]] field and if Desc.[[Writable]] is false, return false.
157             // xi. If Desc has a [[Value]] field, then
158             //   1. Let value be Desc.[[Value]].
159             //   2. Return IntegerIndexedElementSet (O, intIndex, value).
160             // xii. Return true.
161             if (!numericIndex.IsInteger()) {
162                 return false;
163             }
164             JSHandle<JSTaggedValue> numericIndexHandle(thread, numericIndex);
165             JSTaggedNumber numericIndexNumber = JSTaggedValue::ToNumber(thread, numericIndexHandle);
166             RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
167             double tNegZero = -0.0;
168             auto eZero = JSTaggedNumber(tNegZero);
169             JSHandle<JSTaggedValue> zero(thread, JSTaggedValue(0));
170             if (JSTaggedNumber::SameValue(numericIndexNumber, eZero)) {
171                 return false;
172             }
173             if (JSTaggedValue::Less(thread, numericIndexHandle, zero)) {
174                 return false;
175             }
176             uint32_t arrLen = typedarrayObj->GetArrayLength();
177             JSHandle<JSTaggedValue> arrLenHandle(thread, JSTaggedValue(arrLen));
178             if (!JSTaggedValue::Less(thread, numericIndexHandle, arrLenHandle)) {
179                 return false;
180             }
181             if (desc.IsAccessorDescriptor()) {
182                 return false;
183             }
184             if (desc.HasConfigurable() && !desc.IsConfigurable()) {
185                 return false;
186             }
187             if (desc.HasEnumerable() && !desc.IsEnumerable()) {
188                 return false;
189             }
190             if (desc.HasWritable() && !desc.IsWritable()) {
191                 return false;
192             }
193             if (desc.HasValue()) {
194                 JSHandle<JSTaggedValue> value = desc.GetValue();
195                 return (JSTypedArray::IntegerIndexedElementSet(thread, typedarray, numericIndex, value));
196             }
197             return true;
198         }
199     }
200     // 4. Return OrdinaryDefineOwnProperty(O, P, Desc).
201     return JSObject::OrdinaryDefineOwnProperty(thread, JSHandle<JSObject>::Cast(typedarrayObj), key, desc);
202 }
203 
204 // 9.4.5.4 [[Get]] ( P, Receiver )
GetProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & receiver)205 OperationResult JSTypedArray::GetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
206                                           const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &receiver)
207 {
208     // 1. Assert : IsPropertyKey(P) is true.
209     ASSERT(JSTaggedValue::IsPropertyKey(key));
210     // 2. If Type(P) is String and if SameValue(O, Receiver) is true, then
211     if ((key->IsString() || key->IsNumber()) && JSTaggedValue::SameValue(typedarray, receiver)) {
212         //   a. Let numericIndex be CanonicalNumericIndexString (P).
213         //   b. Assert: numericIndex is not an abrupt completion.
214         //   c. If numericIndex is not undefined, then
215         //     i. Return IntegerIndexedElementGet (O, numericIndex).
216         JSTaggedValue numericIndex = JSTaggedValue::CanonicalNumericIndexString(thread, key);
217         RETURN_VALUE_IF_ABRUPT_COMPLETION(
218             thread, OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
219         if (!numericIndex.IsUndefined()) {
220             return JSTypedArray::IntegerIndexedElementGet(thread, typedarray, numericIndex);
221         }
222     }
223 
224     // 3. Return the result of calling the default ordinary object [[Get]] internal method (9.1.8) on O
225     //   passing P and Receiver as arguments.
226     return JSObject::GetProperty(thread, typedarray, key, receiver);
227 }
228 
229 // 9.4.5.5 [[Set]] ( P, V, Receiver )
SetProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value,const JSHandle<JSTaggedValue> & receiver,bool mayThrow)230 bool JSTypedArray::SetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
231                                const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value,
232                                const JSHandle<JSTaggedValue> &receiver, bool mayThrow)
233 {
234     // 1. Assert : IsPropertyKey(P) is true.
235     ASSERT(JSTaggedValue::IsPropertyKey(key));
236     // 2. If Type(P) is String and if SameValue(O, Receiver) is true, then
237     if ((key->IsString() || key->IsNumber()) && JSTaggedValue::SameValue(typedarray, receiver)) {
238         //   a. Let numericIndex be CanonicalNumericIndexString (P).
239         //   b. Assert: numericIndex is not an abrupt completion.
240         //   c. If numericIndex is not undefined, then
241         //     i. Return IntegerIndexedElementSet (O, numericIndex, V).
242         JSTaggedValue numericIndex = JSTaggedValue::CanonicalNumericIndexString(thread, key);
243         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
244         if (!numericIndex.IsUndefined()) {
245             return JSTypedArray::IntegerIndexedElementSet(thread, typedarray, numericIndex, value);
246         }
247     }
248     // 3. Return the result of calling the default ordinary object [[Set]] internal method (9.1.8) on O passing
249     // P, V, and Receiver as arguments.
250     return JSObject::SetProperty(thread, typedarray, key, value, receiver, mayThrow);
251 }
252 
253 // s12 10.4.5.6 [[Delete]] ( P )
DeleteProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key)254 bool JSTypedArray::DeleteProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
255                                   const JSHandle<JSTaggedValue> &key)
256 {
257     // 1. Assert: IsPropertyKey(P) is true.
258     // 2. Assert: O is an Integer-Indexed exotic object.
259     ASSERT(JSTaggedValue::IsPropertyKey(key));
260     // 3. If Type(P) is String, then
261         // a. Let numericIndex be CanonicalNumericIndexString(P).
262         // b. If numericIndex is not undefined, then
263            // i. If IsValidIntegerIndex(O, numericIndex) is false, return true; else return false.
264     if (key->IsString() || key->IsNumber()) {
265         JSTaggedValue numericIndex = JSTaggedValue::CanonicalNumericIndexString(thread, key);
266         if (!numericIndex.IsUndefined()) {
267             if (!IsValidIntegerIndex(typedarray, numericIndex)) {
268                 return true;
269             }
270             return false;
271         }
272     }
273     // 4. Return ? OrdinaryDelete(O, P).
274     return JSObject::DeleteProperty(thread, JSHandle<JSObject>(typedarray), key);
275 }
276 
277 // 9.4.5.6 [[OwnPropertyKeys]] ( )
OwnPropertyKeys(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray)278 JSHandle<TaggedArray> JSTypedArray::OwnPropertyKeys(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray)
279 {
280     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
281     // 1. Let keys be a new empty List.
282     // 2. Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
283     // [[TypedArrayName]] internal slots.
284     // 3. Let len be the value of O’s [[ArrayLength]] internal slot.
285     JSHandle<JSTypedArray> arrayObj(typedarray);
286     JSHandle<TaggedArray> objKeys = JSObject::GetOwnPropertyKeys(thread, JSHandle<JSObject>::Cast(arrayObj));
287     uint32_t objKeysLen = objKeys->GetLength();
288     uint32_t bufferKeysLen = arrayObj->GetArrayLength();
289     uint32_t length = objKeysLen + bufferKeysLen;
290     JSHandle<TaggedArray> nameList = factory->NewTaggedArray(length);
291 
292     // 4. For each integer i starting with 0 such that i < len, in ascending order,
293     //   a. Add ToString(i) as the last element of keys.
294     uint32_t copyLength = 0;
295     JSMutableHandle<JSTaggedValue> tKey(thread, JSTaggedValue::Undefined());
296     for (uint32_t k = 0; k < bufferKeysLen; k++) {
297         tKey.Update(JSTaggedValue(k));
298         JSHandle<JSTaggedValue> sKey(JSTaggedValue::ToString(thread, tKey));
299         RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
300         nameList->Set(thread, copyLength, sKey.GetTaggedValue());
301         copyLength++;
302     }
303 
304     // 5. For each own property key P of O such that Type(P) is String and P is not an integer index, in
305     // property creation order
306     //   a. Add P as the last element of keys.
307     for (uint32_t i = 0; i < objKeysLen; i++) {
308         JSTaggedValue key = objKeys->Get(i);
309         if (JSTaggedValue(key).IsString()) {
310             nameList->Set(thread, copyLength, key);
311             copyLength++;
312         }
313     }
314 
315     // 6. For each own property key P of O such that Type(P) is Symbol, in property creation order
316     //   a. Add P as the last element of keys.
317     for (uint32_t i = 0; i < objKeysLen; i++) {
318         JSTaggedValue key = objKeys->Get(i);
319         if (JSTaggedValue(key).IsSymbol()) {
320             nameList->Set(thread, copyLength, key);
321             copyLength++;
322         }
323     }
324 
325     // 7. Return keys.
326     return factory->CopyArray(nameList, length, copyLength);
327 }
328 
OwnEnumPropertyKeys(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray)329 JSHandle<TaggedArray> JSTypedArray::OwnEnumPropertyKeys(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray)
330 {
331     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
332     // 1. Let keys be a new empty List.
333     // 2. Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
334     // [[TypedArrayName]] internal slots.
335     // 3. Let len be the value of O’s [[ArrayLength]] internal slot.
336     JSHandle<JSTypedArray> arrayObj(typedarray);
337     JSHandle<TaggedArray> objKeys = JSObject::GetOwnEnumPropertyKeys(thread, JSHandle<JSObject>::Cast(arrayObj));
338     uint32_t objKeysLen = objKeys->GetLength();
339     uint32_t bufferKeysLen = arrayObj->GetArrayLength();
340     uint32_t length = objKeysLen + bufferKeysLen;
341     JSHandle<TaggedArray> nameList = factory->NewTaggedArray(length);
342 
343     // 4. For each integer i starting with 0 such that i < len, in ascending order,
344     //   a. Add ToString(i) as the last element of keys.
345     uint32_t copyLength = 0;
346     for (uint32_t k = 0; k < bufferKeysLen; k++) {
347         auto key = base::NumberHelper::IntToEcmaString(thread, k);
348         nameList->Set(thread, copyLength, key);
349         copyLength++;
350     }
351 
352     // 5. For each own property key P of O such that Type(P) is String and P is not an integer index, in
353     // property creation order
354     //   a. Add P as the last element of keys.
355     for (uint32_t i = 0; i < objKeysLen; i++) {
356         JSTaggedValue key = objKeys->Get(i);
357         nameList->Set(thread, copyLength, key);
358         copyLength++;
359     }
360 
361     // 7. Return keys.
362     return factory->CopyArray(nameList, length, copyLength);
363 }
364 
365 // 9.4.5.7 IntegerIndexedObjectCreate (prototype, internalSlotsList)
366 
367 // 9.4.5.8 IntegerIndexedElementGet ( O, index )
IntegerIndexedElementGet(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,JSTaggedValue index)368 OperationResult JSTypedArray::IntegerIndexedElementGet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
369                                                        JSTaggedValue index)
370 {
371     // 1. Assert: Type(index) is Number.
372     ASSERT(index.IsNumber());
373     // 2. Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
374     // [[TypedArrayName]] internal slots.
375     ASSERT(typedarray->IsTypedArray());
376     // 3. Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
377     JSHandle<JSTypedArray> typedarrayObj(typedarray);
378     JSTaggedValue buffer = typedarrayObj->GetViewedArrayBufferOrByteArray();
379     // 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
380     if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
381         THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer",
382                                     OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
383     }
384     // 5. If IsInteger(index) is false, return undefined
385     if (!index.IsInteger()) {
386         return OperationResult(thread, JSTaggedValue::Undefined(), PropertyMetaData(true));
387     }
388 
389     // 6. If index = −0, return undefined.
390     // 7. Let length be the value of O’s [[ArrayLength]] internal slot.
391     // 8. If index < 0 or index ≥ length, return undefined.
392     JSHandle<JSTaggedValue> indexHandle(thread, index);
393     JSTaggedNumber indexNumber = JSTaggedValue::ToNumber(thread, indexHandle);
394     double tNegZero = -0.0;
395     auto eZero = JSTaggedNumber(tNegZero);
396     JSHandle<JSTaggedValue> zero(thread, JSTaggedValue(0));
397     if (JSTaggedNumber::SameValue(indexNumber, eZero)) {
398         return OperationResult(thread, JSTaggedValue::Undefined(), PropertyMetaData(true));
399     }
400     uint32_t arrLen = typedarrayObj->GetArrayLength();
401     JSHandle<JSTaggedValue> arrLenHandle(thread, JSTaggedValue(arrLen));
402     if (JSTaggedValue::Less(thread, indexHandle, zero) || !JSTaggedValue::Less(thread, indexHandle, arrLenHandle)) {
403         return OperationResult(thread, JSTaggedValue::Undefined(), PropertyMetaData(true));
404     }
405     // 9. Let offset be the value of O’s [[ByteOffset]] internal slot.
406     uint32_t offset = typedarrayObj->GetByteOffset();
407     // 10. Let arrayTypeName be the String value of O’s [[TypedArrayName]] internal slot.
408     // 11. Let elementSize be the Number value of the Element Size value specified in Table 49 for
409     // arrayTypeName.
410     uint32_t elementSize = TypedArrayHelper::GetElementSize(typedarrayObj);
411     // 12. Let indexedPosition = (index × elementSize) + offset.
412     uint32_t k = static_cast<uint32_t>(JSTaggedValue::ToInteger(thread, indexHandle).ToInt32());
413     uint32_t byteIndex = k * elementSize + offset;
414     // 13. Let elementType be the String value of the Element Type value in Table 49 for arrayTypeName.
415     DataViewType elementType = TypedArrayHelper::GetType(typedarrayObj);
416     // 14. Return GetValueFromBuffer(buffer, indexedPosition, elementType).
417     JSTaggedValue result = BuiltinsArrayBuffer::GetValueFromBuffer(thread, buffer, byteIndex, elementType, true);
418     return OperationResult(thread, result, PropertyMetaData(true));
419 }
420 
421 // s12 10.4.5.9 IsValidIntegerIndex ( O, index )
IsValidIntegerIndex(const JSHandle<JSTaggedValue> & typedArray,JSTaggedValue index)422 bool JSTypedArray::IsValidIntegerIndex(const JSHandle<JSTaggedValue> &typedArray, JSTaggedValue index)
423 {
424     // 1. Assert: O is an Integer-Indexed exotic object.
425     // 2. If IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is true, return false.
426     JSHandle<JSTypedArray> typedarrayObj(typedArray);
427     JSTaggedValue buffer = typedarrayObj->GetViewedArrayBufferOrByteArray();
428     if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
429         return false;
430     }
431     // 3. If ! IsIntegralNumber(index) is false, return false.
432     if (!index.IsInteger()) {
433         return false;
434     }
435     // 4. If index is -0��, return false.
436     double val = index.GetNumber();
437     if (val == 0 && std::signbit(val)) {
438         return false;
439     }
440 
441     uint32_t arrLen = typedarrayObj->GetArrayLength();
442     // 5. If ℝ(index) < 0 or ℝ(index) ≥ O.[[ArrayLength]], return false.
443     if (val < 0 || val >= arrLen) {
444         return false;
445     }
446     // 6. Return true.
447     return true;
448 }
449 
GetTypeFromName(JSThread * thread,const JSHandle<JSTaggedValue> & typeName)450 DataViewType JSTypedArray::GetTypeFromName(JSThread *thread, const JSHandle<JSTaggedValue> &typeName)
451 {
452     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
453     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledFloat32ArrayString())) {
454         return DataViewType::FLOAT32;
455     }
456     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledInt8ArrayString())) {
457         return DataViewType::INT8;
458     }
459     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledUint8ArrayString())) {
460         return DataViewType::UINT8;
461     }
462     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledUint8ClampedArrayString())) {
463         return DataViewType::UINT8_CLAMPED;
464     }
465     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledInt16ArrayString())) {
466         return DataViewType::INT16;
467     }
468     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledUint16ArrayString())) {
469         return DataViewType::UINT16;
470     }
471     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledInt32ArrayString())) {
472         return DataViewType::INT32;
473     }
474     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledUint32ArrayString())) {
475         return DataViewType::UINT32;
476     }
477     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledFloat64ArrayString())) {
478         return DataViewType::FLOAT64;
479     }
480     if (JSTaggedValue::SameValue(typeName, globalConst->GetHandledBigInt64ArrayString())) {
481         return DataViewType::BIGINT64;
482     }
483     return DataViewType::BIGUINT64;
484 }
485 
486 // static
FastCopyElementToArray(JSThread * thread,const JSHandle<JSTaggedValue> & typedArray,JSHandle<TaggedArray> & array)487 bool JSTypedArray::FastCopyElementToArray(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray,
488                                           JSHandle<TaggedArray> &array)
489 {
490     // 2. Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
491     // [[TypedArrayName]] internal slots.
492     ASSERT(typedArray->IsTypedArray());
493     // 3. Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
494     JSHandle<JSTypedArray> typedarrayObj(typedArray);
495     JSHandle<JSTaggedValue> bufferHandle = JSHandle<JSTaggedValue>(thread,
496                                                                    typedarrayObj->GetViewedArrayBufferOrByteArray());
497     // 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
498     if (BuiltinsArrayBuffer::IsDetachedBuffer(bufferHandle.GetTaggedValue())) {
499         THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer", false);
500     }
501 
502     // 7. Let length be the value of O’s [[ArrayLength]] internal slot.
503     // 8. If index < 0 or index ≥ length, return undefined.
504     uint32_t arrLen = typedarrayObj->GetArrayLength();
505 
506     // 9. Let offset be the value of O’s [[ByteOffset]] internal slot.
507     uint32_t offset = typedarrayObj->GetByteOffset();
508     // 11. Let elementSize be the Number value of the Element Size value specified in Table 49 for arrayTypeName.
509     uint32_t elementSize = TypedArrayHelper::GetElementSize(typedarrayObj);
510     // 13. Let elementType be the String value of the Element Type value in Table 49 for arrayTypeName.
511     DataViewType elementType = TypedArrayHelper::GetType(typedarrayObj);
512     for (uint32_t index = 0; index < arrLen; index++) {
513         // 12. Let indexedPosition = (index × elementSize) + offset.
514         uint32_t byteIndex = index * elementSize + offset;
515         // 14. Return GetValueFromBuffer(buffer, indexedPosition, elementType).
516         JSTaggedValue result = BuiltinsArrayBuffer::GetValueFromBuffer(thread, bufferHandle.GetTaggedValue(),
517                                                                        byteIndex, elementType, true);
518         array->Set(thread, index, result);
519     }
520     return true;
521 }
522 
523 // static
FastElementGet(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,uint32_t index)524 OperationResult JSTypedArray::FastElementGet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
525                                              uint32_t index)
526 {
527     // 2. Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
528     // [[TypedArrayName]] internal slots.
529     ASSERT(typedarray->IsTypedArray());
530     // 3. Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
531     JSHandle<JSTypedArray> typedarrayObj(typedarray);
532     JSTaggedValue buffer = typedarrayObj->GetViewedArrayBufferOrByteArray();
533     // 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
534     if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
535         THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer",
536                                     OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
537     }
538 
539     // 7. Let length be the value of O’s [[ArrayLength]] internal slot.
540     // 8. If index < 0 or index ≥ length, return undefined.
541     uint32_t arrLen = typedarrayObj->GetArrayLength();
542     if (index >= arrLen) {
543         return OperationResult(thread, JSTaggedValue::Undefined(), PropertyMetaData(true));
544     }
545     // 9. Let offset be the value of O’s [[ByteOffset]] internal slot.
546     uint32_t offset = typedarrayObj->GetByteOffset();
547     // 11. Let elementSize be the Number value of the Element Size value specified in Table 49 for arrayTypeName.
548     uint32_t elementSize = TypedArrayHelper::GetElementSize(typedarrayObj);
549     // 12. Let indexedPosition = (index × elementSize) + offset.
550     uint32_t byteIndex = index * elementSize + offset;
551     // 13. Let elementType be the String value of the Element Type value in Table 49 for arrayTypeName.
552     DataViewType elementType = TypedArrayHelper::GetType(typedarrayObj);
553     // 14. Return GetValueFromBuffer(buffer, indexedPosition, elementType).
554     JSTaggedValue result = BuiltinsArrayBuffer::GetValueFromBuffer(thread, buffer, byteIndex, elementType, true);
555     return OperationResult(thread, result, PropertyMetaData(true));
556 }
557 
558 // 9.4.5.9 IntegerIndexedElementSet ( O, index, value )
IntegerIndexedElementSet(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,JSTaggedValue index,const JSHandle<JSTaggedValue> & value)559 bool JSTypedArray::IntegerIndexedElementSet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
560                                             JSTaggedValue index, const JSHandle<JSTaggedValue> &value)
561 {
562     // 1. Assert: Type(index) is Number.
563     ASSERT(index.IsNumber());
564     // 2. Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
565     // [[TypedArrayName]] internal slots.
566     ASSERT(typedarray->IsTypedArray());
567     // 3. If O.[[ContentType]] is BigInt, let numValue be ? ToBigInt(value).
568     JSHandle<JSTaggedValue> numValueHandle;
569     ContentType contentType = JSHandle<JSTypedArray>::Cast(typedarray)->GetContentType();
570     if (UNLIKELY(contentType == ContentType::BigInt)) {
571         numValueHandle = JSHandle<JSTaggedValue>(thread, JSTaggedValue::ToBigInt(thread, value));
572     } else {
573         numValueHandle = JSHandle<JSTaggedValue>(thread, JSTaggedValue::ToNumber(thread, value));
574     }
575     // 4. ReturnIfAbrupt(numValue).
576     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
577 
578     JSHandle<JSTypedArray> typedarrayObj(typedarray);
579     JSTaggedValue buffer = typedarrayObj->GetViewedArrayBufferOrByteArray();
580     JSHandle<JSTaggedValue> indexHandle(thread, index);
581     // 5. If ! IsValidIntegerIndex(O, index) is true, then
582     if (IsValidIntegerIndex(typedarray, index)) {
583         // 6. Let offset be the value of O’s [[ByteOffset]] internal slot.
584         uint32_t offset = typedarrayObj->GetByteOffset();
585         // 7. Let arrayTypeName be the String value of O’s [[TypedArrayName]] internal slot.
586         // 8. Let elementSize be the Number value of the Element Size value specified in Table 49 for
587         // arrayTypeName.
588         uint32_t elementSize = TypedArrayHelper::GetElementSize(typedarrayObj);
589         // 9. Let indexedPosition = (index × elementSize) + offset.
590         uint32_t k = JSTaggedValue::ToInteger(thread, indexHandle).ToUint32();
591         uint32_t byteIndex = k * elementSize + offset;
592         // 10. Let elementType be the String value of the Element Type value in Table 49 for arrayTypeName.
593         DataViewType elementType = TypedArrayHelper::GetType(typedarrayObj);
594         // 11. Perform SetValueInBuffer(buffer, indexedPosition, elementType, numValue).
595         BuiltinsArrayBuffer::SetValueInBuffer(thread, buffer, byteIndex, elementType, numValueHandle, true);
596         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
597     }
598     return true;
599 }
600 
601 // only use in TypeArray fast set property
NonEcmaObjectToNumber(JSThread * thread,const JSTaggedValue tagged)602 JSTaggedNumber JSTypedArray::NonEcmaObjectToNumber(JSThread *thread, const JSTaggedValue tagged)
603 {
604     ASSERT_PRINT(!tagged.IsECMAObject(), "tagged must not be EcmaObject");
605     if (tagged.IsInt() || tagged.IsDouble()) {
606         return JSTaggedNumber(tagged);
607     }
608     if (tagged.IsString()) {
609         return JSTaggedValue::StringToDouble(tagged);
610     }
611     switch (tagged.GetRawData()) {
612         case JSTaggedValue::VALUE_UNDEFINED:
613         case JSTaggedValue::VALUE_HOLE: {
614             return JSTaggedNumber(base::NAN_VALUE);
615         }
616         case JSTaggedValue::VALUE_TRUE: {
617             return JSTaggedNumber(1);
618         }
619         case JSTaggedValue::VALUE_FALSE:
620         case JSTaggedValue::VALUE_NULL: {
621             return JSTaggedNumber(0);
622         }
623         default: {
624             break;
625         }
626     }
627     if (tagged.IsSymbol()) {
628         THROW_TYPE_ERROR_AND_RETURN(thread, "Cannot convert a Symbol value to a number", JSTaggedNumber::Exception());
629     }
630     if (tagged.IsBigInt()) {
631         THROW_TYPE_ERROR_AND_RETURN(thread, "Cannot convert a BigInt value to a number", JSTaggedNumber::Exception());
632     }
633     THROW_TYPE_ERROR_AND_RETURN(thread, "Cannot convert a Unknown value to a number", JSTaggedNumber::Exception());
634 }
635 
FastGetPropertyByIndex(JSThread * thread,const JSTaggedValue typedarray,uint32_t index,JSType jsType)636 JSTaggedValue JSTypedArray::FastGetPropertyByIndex(JSThread *thread, const JSTaggedValue typedarray, uint32_t index,
637                                                    JSType jsType)
638 {
639     // Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
640     // [[TypedArrayName]] internal slots.
641     ASSERT(typedarray.IsTypedArray());
642     // Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
643     JSTypedArray *typedarrayObj = JSTypedArray::Cast(typedarray.GetTaggedObject());
644     JSTaggedValue buffer = typedarrayObj->GetViewedArrayBufferOrByteArray();
645     // If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
646     if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
647         THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer", JSTaggedValue::Exception());
648     }
649 
650     DISALLOW_GARBAGE_COLLECTION;
651     // Let length be the value of O’s [[ArrayLength]] internal slot.
652     // If arrLen < 0 or index ≥ length, return undefined.
653     uint32_t arrLen = typedarrayObj->GetArrayLength();
654     if (index >= arrLen) {
655         return JSTaggedValue::Undefined();
656     }
657     // Let offset be the value of O’s [[ByteOffset]] internal slot.
658     uint32_t offset = typedarrayObj->GetByteOffset();
659     // Let elementSize be the Number value of the Element Size value specified in Table 49 for arrayTypeName.
660     uint32_t elementSize = TypedArrayHelper::GetElementSize(jsType);
661     // Let indexedPosition = (index × elementSize) + offset.
662     uint32_t byteIndex = index * elementSize + offset;
663     // Let elementType be the String value of the Element Type value in Table 49 for arrayTypeName.
664     DataViewType elementType = TypedArrayHelper::GetType(jsType);
665     // Return GetValueFromBuffer(buffer, indexedPosition, elementType).
666     return BuiltinsArrayBuffer::GetValueFromBuffer(thread, buffer, byteIndex, elementType, true);
667 }
668 
FastSetPropertyByIndex(JSThread * thread,const JSTaggedValue typedarray,uint32_t index,JSTaggedValue value,JSType jsType)669 JSTaggedValue JSTypedArray::FastSetPropertyByIndex(JSThread *thread, const JSTaggedValue typedarray, uint32_t index,
670                                                    JSTaggedValue value, JSType jsType)
671 {
672     // Assert: O is an Object that has [[ViewedArrayBuffer]], [[ArrayLength]], [[ByteOffset]], and
673     // [[TypedArrayName]] internal slots.
674     ASSERT(typedarray.IsTypedArray());
675     // If O.[[ContentType]] is BigInt, let numValue be ? ToBigInt(value).
676     JSTypedArray *typedarrayObj = JSTypedArray::Cast(typedarray.GetTaggedObject());
677     if (UNLIKELY(typedarrayObj->GetContentType() == ContentType::BigInt || value.IsECMAObject())) {
678         return JSTaggedValue::Hole();
679     }
680     JSTaggedNumber numValue = JSTypedArray::NonEcmaObjectToNumber(thread, value);
681     // ReturnIfAbrupt(numValue).
682     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception());
683 
684     DISALLOW_GARBAGE_COLLECTION;
685     JSTaggedValue buffer = typedarrayObj->GetViewedArrayBufferOrByteArray();
686 
687     // If ℝ(index) < 0 or ℝ(index) ≥ O.[[ArrayLength]], return false.
688     uint32_t arrLen = typedarrayObj->GetArrayLength();
689     if (index >= arrLen) {
690         return JSTaggedValue::Undefined();
691     }
692     // Let offset be the value of O’s [[ByteOffset]] internal slot.
693     uint32_t offset = typedarrayObj->GetByteOffset();
694     // Let arrayTypeName be the String value of O’s [[TypedArrayName]]
695     // Let elementSize be the Number value of the Element Size value specified in Table 49 for
696     // arrayTypeName.
697     uint32_t elementSize = TypedArrayHelper::GetElementSize(jsType);
698     // Let indexedPosition = (index × elementSize) + offset.
699     uint32_t byteIndex = index * elementSize + offset;
700     // Let elementType be the String value of the Element Type value in Table 49 for arrayTypeName.
701     DataViewType elementType = TypedArrayHelper::GetType(jsType);
702     // Perform SetValueInBuffer(buffer, indexedPosition, elementType, numValue).
703     return BuiltinsArrayBuffer::FastSetValueInBuffer(thread,
704         buffer, byteIndex, elementType, numValue.GetNumber(), true);
705 }
706 
GetOffHeapBuffer(JSThread * thread,JSHandle<JSTypedArray> & typedArray)707 JSTaggedValue JSTypedArray::GetOffHeapBuffer(JSThread *thread, JSHandle<JSTypedArray> &typedArray)
708 {
709     JSTaggedValue arrBuf = typedArray->GetViewedArrayBufferOrByteArray();
710     if (arrBuf.IsArrayBuffer() || arrBuf.IsSharedArrayBuffer()) {
711         return arrBuf;
712     }
713 
714     ByteArray *byteArray = ByteArray::Cast(arrBuf.GetTaggedObject());
715     int32_t length = static_cast<int32_t>(byteArray->GetArrayLength() * byteArray->GetByteLength());
716     JSHandle<JSArrayBuffer> arrayBuffer = thread->GetEcmaVM()->GetFactory()->NewJSArrayBuffer(length);
717 
718     if (length > 0) {
719         void *fromBuf = reinterpret_cast<void *>(ToUintPtr(
720             ByteArray::Cast(typedArray->GetViewedArrayBufferOrByteArray().GetTaggedObject())->GetData()));
721         JSTaggedValue data = arrayBuffer->GetArrayBufferData();
722         void *toBuf = reinterpret_cast<void *>(
723             ToUintPtr(JSNativePointer::Cast(data.GetTaggedObject())->GetExternalPointer()));
724         JSArrayBuffer::CopyDataPointBytes(toBuf, fromBuf, 0, length);
725     }
726     typedArray->SetViewedArrayBufferOrByteArray(thread, arrayBuffer.GetTaggedValue());
727     typedArray->SetIsOnHeap(false);
728 
729     return arrayBuffer.GetTaggedValue();
730 }
731 }  // namespace panda::ecmascript
732