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