• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JSARRAY_H
17 #define ECMASCRIPT_JSARRAY_H
18 
19 #include <limits>
20 #include "ecmascript/js_object.h"
21 #include "ecmascript/js_tagged_value-inl.h"
22 #include "ecmascript/js_tagged_value.h"
23 #include "ecmascript/tagged_array.h"
24 
25 namespace panda::ecmascript {
26 // ecma6 9.4.2 Array Exotic Object
27 class JSArray : public JSObject {
28 public:
29     static constexpr int LENGTH_INLINE_PROPERTY_INDEX = 0;
30 
31     CAST_CHECK(JSArray, IsJSArray);
32 
33     static JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length);
34     static JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length,
35                                                const JSHandle<JSTaggedValue> &newTarget);
36     static JSTaggedValue ArraySpeciesCreate(JSThread *thread, const JSHandle<JSObject> &originalArray,
37                                             JSTaggedNumber length);
38     static bool ArraySetLength(JSThread *thread, const JSHandle<JSObject> &array, const PropertyDescriptor &desc);
39     static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, const JSHandle<JSTaggedValue> &key,
40                                   const PropertyDescriptor &desc);
41     static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, uint32_t index,
42                                   const PropertyDescriptor &desc);
43 
44     static bool IsLengthString(JSThread *thread, const JSHandle<JSTaggedValue> &key);
45     // ecma6 7.3 Operations on Objects
46     static JSHandle<JSArray> CreateArrayFromList(JSThread *thread, const JSHandle<TaggedArray> &elements);
47     // use first inlined property slot for array length
GetArrayLength()48     inline uint32_t GetArrayLength() const
49     {
50         return GetLength().GetArrayLength();
51     }
52 
SetArrayLength(const JSThread * thread,uint32_t length)53     inline void SetArrayLength(const JSThread *thread, uint32_t length)
54     {
55         SetLength(thread, JSTaggedValue(length));
56     }
57 
58     static constexpr size_t LENGTH_OFFSET = JSObject::SIZE;
59     ACCESSORS(Length, LENGTH_OFFSET, SIZE);
60 
61     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LENGTH_OFFSET, SIZE)
62 
63     static const uint32_t MAX_ARRAY_INDEX = MAX_ELEMENT_INDEX;
DECL_DUMP()64     DECL_DUMP()
65 
66     static int32_t GetArrayLengthOffset()
67     {
68         return LENGTH_OFFSET;
69     }
70 
71     static bool PropertyKeyToArrayIndex(JSThread *thread, const JSHandle<JSTaggedValue> &key, uint32_t *output);
72 
73     static JSTaggedValue LengthGetter(JSThread *thread, const JSHandle<JSObject> &self);
74 
75     static bool LengthSetter(JSThread *thread, const JSHandle<JSObject> &self, const JSHandle<JSTaggedValue> &value,
76                              bool mayThrow = false);
77 
78     static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
79                                                           uint32_t index);
80 
81     static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
82                                                           const JSHandle<JSTaggedValue> &key);
83 
84     static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, uint32_t index,
85                                        const JSHandle<JSTaggedValue> &value);
86 
87     static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
88                                        const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value);
89 
90 private:
91     static void SetCapacity(JSThread *thread, const JSHandle<JSObject> &array, uint32_t oldLen, uint32_t newLen);
92 };
93 }  // namespace panda::ecmascript
94 
95 #endif  // ECMASCRIPT_JSARRAY_H
96