1 /* 2 * Copyright (c) 2021 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/tagged_array.h" 20 #include "js_object.h" 21 22 namespace panda::ecmascript { 23 class JSTypedArray : public JSObject { 24 public: Cast(ObjectHeader * object)25 static JSTypedArray *Cast(ObjectHeader *object) 26 { 27 #if ECMASCRIPT_ENABLE_CAST_CHECK 28 if (!(JSTaggedValue(object).IsTypedArray() || JSTaggedValue(object).IsJSTypedArray())) { 29 UNREACHABLE(); 30 } 31 #else 32 ASSERT(JSTaggedValue(object).IsTypedArray() || JSTaggedValue(object).IsJSTypedArray()); 33 #endif 34 return static_cast<JSTypedArray *>(object); 35 } 36 37 static JSHandle<JSTaggedValue> ToPropKey(JSThread *thread, const JSHandle<JSTaggedValue> &key); 38 39 // 9.4.5 Integer-Indexed Exotic Objects 40 // 9.4.5.1 [[GetOwnProperty]] ( P ) 41 static bool GetOwnProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 42 const JSHandle<JSTaggedValue> &key, PropertyDescriptor &desc); 43 // 9.4.5.2 [[HasProperty]] ( P ) 44 static bool HasProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 45 const JSHandle<JSTaggedValue> &key); 46 // 9.4.5.3 [[DefineOwnProperty]] ( P, Desc ) 47 static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 48 const JSHandle<JSTaggedValue> &key, const PropertyDescriptor &desc); 49 // 9.4.5.4 [[Get]] ( P, Receiver ) GetProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,const JSHandle<JSTaggedValue> & key)50 static inline OperationResult GetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 51 const JSHandle<JSTaggedValue> &key) 52 { 53 return GetProperty(thread, typedarray, key, typedarray); 54 } GetProperty(JSThread * thread,const JSHandle<JSTaggedValue> & typedarray,uint32_t index)55 static inline OperationResult GetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 56 uint32_t index) 57 { 58 return FastElementGet(thread, typedarray, index); 59 } 60 static OperationResult GetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 61 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &receiver); 62 // 9.4.5.5 [[Set]] ( P, V, Receiver ) 63 static inline bool SetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 64 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value, 65 bool mayThrow = false) 66 { 67 return SetProperty(thread, typedarray, key, value, typedarray, mayThrow); 68 } 69 static bool SetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 70 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value, 71 const JSHandle<JSTaggedValue> &receiver, bool mayThrow = false); 72 // 9.4.5.6 [[OwnPropertyKeys]] ( ) 73 static JSHandle<TaggedArray> OwnPropertyKeys(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray); 74 // 9.4.5.7 IntegerIndexedObjectCreate (prototype, internalSlotsList) 75 // 9.4.5.8 IntegerIndexedElementGet ( O, index ) 76 static OperationResult IntegerIndexedElementGet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 77 JSTaggedValue index); 78 static OperationResult FastElementGet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, uint32_t index); 79 static bool FastCopyElementToArray(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray, 80 JSHandle<TaggedArray> &array); 81 // 9.4.5.9 IntegerIndexedElementSet ( O, index, value ) 82 static bool IntegerIndexedElementSet(JSThread *thread, const JSHandle<JSTaggedValue> &typedarray, 83 JSTaggedValue index, const JSHandle<JSTaggedValue> &value); 84 85 static constexpr size_t VIEWED_ARRAY_BUFFER_OFFSET = JSObject::SIZE; 86 ACCESSORS(ViewedArrayBuffer, VIEWED_ARRAY_BUFFER_OFFSET, TYPED_ARRAY_NAME_OFFSET) 87 ACCESSORS(TypedArrayName, TYPED_ARRAY_NAME_OFFSET, BYTE_LENGTH_OFFSET) 88 ACCESSORS(ByteLength, BYTE_LENGTH_OFFSET, BYTE_OFFSET_OFFSET) 89 ACCESSORS(ByteOffset, BYTE_OFFSET_OFFSET, ARRAY_LENGTH_OFFSET) 90 ACCESSORS(ArrayLength, ARRAY_LENGTH_OFFSET, SIZE) 91 92 static const uint32_t MAX_TYPED_ARRAY_INDEX = MAX_ELEMENT_INDEX; 93 DECL_DUMP() 94 95 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, VIEWED_ARRAY_BUFFER_OFFSET, SIZE) 96 }; 97 } // namespace panda::ecmascript 98 #endif // ECMASCRIPT_JS_TYPED_ARRAY_H 99