• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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/base/atomic_helper.h"
17 #include "ecmascript/base/typed_array_helper-inl.h"
18 
19 namespace panda::ecmascript::base {
20 using BuiltinsArrayBuffer = builtins::BuiltinsArrayBuffer;
21 
ValidateIntegerTypedArray(JSThread * thread,JSHandle<JSTaggedValue> typedArray,bool waitable)22 JSTaggedValue AtomicHelper::ValidateIntegerTypedArray(JSThread *thread, JSHandle<JSTaggedValue> typedArray,
23                                                       bool waitable)
24 {
25     // 1. If waitable is not present, set waitable to false.
26     // 2. Let buffer be ? ValidateTypedArray(typedArray).
27     JSTaggedValue buffer = TypedArrayHelper::ValidateTypedArray(thread, typedArray);
28     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
29 
30     JSHandle<JSTaggedValue> bufferHandle(thread, buffer);
31 
32     // 3. Let typeName be typedArray.[[TypedArrayName]].
33     // 4. Let type be the Element Type value in Table 60 for typeName.
34     JSHandle<JSTaggedValue> typeName(thread, JSTypedArray::Cast(typedArray->GetTaggedObject())->GetTypedArrayName());
35     DataViewType type = JSTypedArray::GetTypeFromName(thread, typeName);
36 
37     // 5. If waitable is true, then
38     // a. If typeName is not "Int32Array" or "BigInt64Array", throw a TypeError exception.
39     // 6. Else,
40     // a. If ! IsUnclampedIntegerElementType(type) is false and ! IsBigIntElementType(type) is false,
41     // throw a  TypeError exception.
42     if (waitable) {
43         if (!(type == DataViewType::INT32 || type == DataViewType::BIGINT64)) {
44             THROW_TYPE_ERROR_AND_RETURN(thread, "The typeName is not Int32Array/BigInt64Array.",
45                                         JSTaggedValue::Exception());
46         }
47     } else {
48         if (!(BuiltinsArrayBuffer::IsUnclampedIntegerElementType(type) ||
49               BuiltinsArrayBuffer::IsBigIntElementType(type))) {
50             THROW_TYPE_ERROR_AND_RETURN(thread, "The typedArray type is not UnclampedInteger/BigInt.",
51                                         JSTaggedValue::Exception());
52         }
53     }
54     // 7. Return buffer.
55     return bufferHandle.GetTaggedValue();
56 }
57 
ValidateAtomicAccess(JSThread * thread,const JSHandle<JSTaggedValue> typedArray,JSHandle<JSTaggedValue> requestIndex)58 uint32_t AtomicHelper::ValidateAtomicAccess(JSThread *thread, const JSHandle<JSTaggedValue> typedArray,
59                                             JSHandle<JSTaggedValue> requestIndex)
60 {
61     // 1. Assert: typedArray is an Object that has a [[ViewedArrayBuffer]] internal slot.
62     ASSERT(typedArray->IsECMAObject() && typedArray->IsTypedArray());
63     // 2. Let length be typedArray.[[ArrayLength]].
64     JSHandle<JSObject> typedArrayObj(typedArray);
65     JSHandle<JSTypedArray> srcObj(typedArray);
66     uint32_t length = srcObj->GetArrayLength();
67 
68     // 3. Let accessIndex be ? ToIndex(requestIndex).
69     JSTaggedNumber accessIndex = JSTaggedValue::ToIndex(thread, requestIndex);
70     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, 0);
71     uint64_t index = base::NumberHelper::DoubleToUInt64(accessIndex.GetNumber());
72     // 4. If accessIndex ≥ length, throw a RangeError exception.
73     if (index >= length) {
74         THROW_RANGE_ERROR_AND_RETURN(thread, "Index is overflow.", 0);
75     }
76 
77     // 5. Let arrayTypeName be typedArray.[[TypedArrayName]].
78     // 6. Let elementSize be the Element Size value specified in Table 60 for arrayTypeName.
79     // 7. Let offset be typedArray.[[ByteOffset]].
80     JSHandle<JSTaggedValue> arrayTypeName(thread, JSTypedArray::Cast(*typedArrayObj)->GetTypedArrayName());
81     DataViewType elementType = JSTypedArray::GetTypeFromName(thread, arrayTypeName);
82     uint32_t elementSize = TypedArrayHelper::GetSizeFromType(elementType);
83     uint32_t offset = srcObj->GetByteOffset();
84     // 8. Return (accessIndex × elementSize) + offset.
85     ASSERT(index * elementSize + offset <= UINT32_MAX);
86     uint32_t allOffset = static_cast<uint32_t>(index * elementSize + offset);
87     return allOffset;
88 }
89 
AtomicStore(JSThread * thread,const JSHandle<JSTaggedValue> & typedArray,JSHandle<JSTaggedValue> index,JSHandle<JSTaggedValue> & value)90 JSTaggedValue AtomicHelper::AtomicStore(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray,
91                                         JSHandle<JSTaggedValue> index, JSHandle<JSTaggedValue> &value)
92 {
93     JSTaggedValue bufferValue = ValidateIntegerTypedArray(thread, typedArray);
94     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
95     JSHandle<JSTaggedValue> buffer(thread, bufferValue);
96     uint32_t indexedPosition = ValidateAtomicAccess(thread, typedArray, index);
97     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
98     JSHandle<JSTaggedValue> arrayTypeName(thread,
99                                           JSTypedArray::Cast(typedArray->GetTaggedObject())->GetTypedArrayName());
100     DataViewType type = JSTypedArray::GetTypeFromName(thread, arrayTypeName);
101     JSHandle<JSTaggedValue> bufferTag;
102     if (type == DataViewType::BIGUINT64 || type == DataViewType::BIGINT64) {
103         JSTaggedValue integerValue = JSTaggedValue::ToBigInt(thread, value);
104         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
105         bufferTag = JSHandle<JSTaggedValue>(thread, integerValue);
106     } else {
107         JSTaggedNumber integerValue = JSTaggedValue::ToInteger(thread, value);
108         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
109         bufferTag = JSHandle<JSTaggedValue>(thread, integerValue);
110     }
111     BuiltinsArrayBuffer::IsDetachedBuffer(thread, JSHandle<JSTypedArray>::Cast(typedArray));
112     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
113     BuiltinsArrayBuffer::SetValueInBuffer(thread, buffer.GetTaggedValue(), indexedPosition, type, bufferTag, true);
114     return bufferTag.GetTaggedValue();
115 }
116 
AtomicLoad(JSThread * thread,const JSHandle<JSTaggedValue> & typedArray,JSHandle<JSTaggedValue> index)117 JSTaggedValue AtomicHelper::AtomicLoad(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray,
118                                        JSHandle<JSTaggedValue> index)
119 {
120     JSTaggedValue bufferValue = ValidateIntegerTypedArray(thread, typedArray);
121     JSHandle<JSTaggedValue> buffer(thread, bufferValue);
122     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
123     uint32_t indexedPosition = ValidateAtomicAccess(thread, typedArray, index);
124     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
125     BuiltinsArrayBuffer::IsDetachedBuffer(thread, JSHandle<JSTypedArray>::Cast(typedArray));
126     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
127     JSHandle<JSTaggedValue> arrayTypeName(thread,
128                                           JSTypedArray::Cast(typedArray->GetTaggedObject())->GetTypedArrayName());
129     DataViewType elementType = JSTypedArray::GetTypeFromName(thread, arrayTypeName);
130     return BuiltinsArrayBuffer::GetValueFromBuffer(thread, buffer.GetTaggedValue(),
131                                                    indexedPosition, elementType, true);
132 }
133 }  // panda::ecmascript::base
134 
135