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