• 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 #ifndef ECMASCRIPT_JS_TYPED_ARRAY_H
17 #define ECMASCRIPT_JS_TYPED_ARRAY_H
18 
19 #include "ecmascript/js_dataview.h"
20 #include "ecmascript/js_object.h"
21 #include "ecmascript/tagged_array.h"
22 
23 namespace panda::ecmascript {
24 enum class ContentType : uint8_t { None = 1, Number, BigInt };
25 class JSTypedArray : public JSObject {
26 public:
27     static constexpr size_t MAX_ONHEAP_LENGTH = 512 * 8;
Cast(TaggedObject * object)28     static JSTypedArray *Cast(TaggedObject *object)
29     {
30     #if ECMASCRIPT_ENABLE_CAST_CHECK
31         if (!(JSTaggedValue(object).IsTypedArray() || JSTaggedValue(object).IsJSTypedArray())) {
32             LOG_ECMA(FATAL) << "this branch is unreachable";
33             UNREACHABLE();
34         }
35     #else
36         ASSERT(JSTaggedValue(object).IsTypedArray() || JSTaggedValue(object).IsJSTypedArray());
37     #endif
38         return static_cast<JSTypedArray *>(object);
39     }
40 
41     static JSHandle<JSTaggedValue> ToPropKey(JSThread *thread, const JSHandle<JSTaggedValue> &key);
42 
43     // 9.4.5 Integer-Indexed Exotic Objects
44     // 9.4.5.1 [[GetOwnProperty]] ( P )
45     static bool GetOwnProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
46                                const JSHandle<JSTaggedValue> &key, PropertyDescriptor &desc);
47     // 9.4.5.2 [[HasProperty]] ( P )
48     static bool HasProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
49                             const JSHandle<JSTaggedValue> &key);
50     // 9.4.5.3 [[DefineOwnProperty]] ( P, Desc )
51     static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
52                                   const JSHandle<JSTaggedValue> &key, const PropertyDescriptor &desc);
53     // 9.4.5.4 [[Get]] ( P, Receiver )
GetProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key)54     static inline OperationResult GetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
55                                               const JSHandle<JSTaggedValue> &key)
56     {
57         return GetProperty(thread, typedarray, key, typedarray);
58     }
GetProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,uint32_t index)59     static inline OperationResult GetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
60                                               uint32_t index)
61     {
62         return FastElementGet(thread, typedarray, index);
63     }
64     static OperationResult GetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
65                                        const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &receiver);
66     // 9.4.5.5 [[Set]] ( P, V, Receiver )
67     static inline bool SetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
68                                    const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value,
69                                    bool mayThrow = false)
70     {
71         return SetProperty(thread, typedarray, key, value, typedarray, mayThrow);
72     }
73     static bool SetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
74                             const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value,
75                             const JSHandle<JSTaggedValue> &receiver, bool mayThrow = false);
76     // s12 10.4.5.6 [[Delete]] ( P )
77     static bool DeleteProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
78                                const JSHandle<JSTaggedValue> &key);
79     // 9.4.5.6 [[OwnPropertyKeys]] ( )
80     static JSHandle<TaggedArray> OwnPropertyKeys(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray);
81     static JSHandle<TaggedArray> OwnEnumPropertyKeys(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray);
82     // 9.4.5.7 IntegerIndexedObjectCreate (prototype, internalSlotsList)
83     // 9.4.5.8 IntegerIndexedElementGet ( O, index )
84     static OperationResult IntegerIndexedElementGet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
85                                                     JSTaggedValue index);
86     static OperationResult FastElementGet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, uint32_t index);
87     static bool FastCopyElementToArray(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray,
88                                        JSHandle<TaggedArray> &array);
89     // 9.4.5.9 IntegerIndexedElementSet ( O, index, value )
90     static bool IntegerIndexedElementSet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray,
91                                          JSTaggedValue index, const JSHandle<JSTaggedValue> &value);
92     // s12 10.4.5.9 IsValidIntegerIndex ( O, index )
93     static bool IsValidIntegerIndex(const JSHandle<JSTaggedValue> &typedArray, JSTaggedValue index);
94     static JSTaggedValue FastGetPropertyByIndex(JSThread *thread, const JSTaggedValue typedarray, uint32_t index,
95                                                 JSType jsType);
96     static JSTaggedValue FastSetPropertyByIndex(JSThread *thread, const JSTaggedValue typedarray, uint32_t index,
97                                                 JSTaggedValue value, JSType jsType);
98     // only use in TypeArray fast set property
99     static JSTaggedNumber NonEcmaObjectToNumber(JSThread *thread, const JSTaggedValue tagged);
100     static JSTaggedValue GetOffHeapBuffer(JSThread *thread, JSHandle<JSTypedArray> &typedArray);
101     static constexpr size_t VIEWED_ARRAY_BUFFER_OFFSET = JSObject::SIZE;
102     static DataViewType GetTypeFromName(JSThread *thread, const JSHandle<JSTaggedValue> &typeName);
103     ACCESSORS(ViewedArrayBufferOrByteArray, VIEWED_ARRAY_BUFFER_OFFSET, TYPED_ARRAY_NAME_OFFSET)
104     ACCESSORS(TypedArrayName, TYPED_ARRAY_NAME_OFFSET, BYTE_LENGTH_OFFSET)
105     ACCESSORS_PRIMITIVE_FIELD(ByteLength, uint32_t, BYTE_LENGTH_OFFSET, BYTE_OFFSET_OFFSET)
106     ACCESSORS_PRIMITIVE_FIELD(ByteOffset, uint32_t, BYTE_OFFSET_OFFSET, ARRAY_LENGTH_OFFSET)
107     ACCESSORS_PRIMITIVE_FIELD(ArrayLength, uint32_t, ARRAY_LENGTH_OFFSET, CONTENT_TYPE_OFFSET)
108     ACCESSORS_PRIMITIVE_FIELD(ContentType, ContentType, CONTENT_TYPE_OFFSET, ON_HEAP_OFFSET)
109     ACCESSORS_PRIMITIVE_FIELD(IsOnHeap, bool, ON_HEAP_OFFSET, LAST_OFFSET)
110 
111     DEFINE_ALIGN_SIZE(LAST_OFFSET);
112     static const uint32_t MAX_TYPED_ARRAY_INDEX = MAX_ELEMENT_INDEX;
113     DECL_DUMP()
114     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, VIEWED_ARRAY_BUFFER_OFFSET, BYTE_LENGTH_OFFSET)
115 };
116 }  // namespace panda::ecmascript
117 #endif  // ECMASCRIPT_JS_TYPED_ARRAY_H
118