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