• 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 "ecmascript/js_object.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 #include "ecmascript/js_tagged_value.h"
22 #include "ecmascript/tagged_array.h"
23 
24 namespace panda::ecmascript {
25 enum class ArrayMode : uint8_t { UNDEFINED = 0, DICTIONARY, LITERAL };
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                                                ArrayMode mode = ArrayMode::UNDEFINED);
35     static JSHandle<JSTaggedValue> ArrayCreate(JSThread *thread, JSTaggedNumber length,
36                                                const JSHandle<JSTaggedValue> &newTarget,
37                                                ArrayMode mode = ArrayMode::UNDEFINED);
38     static JSTaggedValue ArraySpeciesCreate(JSThread *thread, const JSHandle<JSObject> &originalArray,
39                                             JSTaggedNumber length);
40     static bool ArraySetLength(JSThread *thread, const JSHandle<JSObject> &array, const PropertyDescriptor &desc);
41     static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, const JSHandle<JSTaggedValue> &key,
42                                   const PropertyDescriptor &desc);
43     static bool DefineOwnProperty(JSThread *thread, const JSHandle<JSObject> &array, uint32_t index,
44                                   const PropertyDescriptor &desc);
45 
46     static bool IsLengthString(JSThread *thread, const JSHandle<JSTaggedValue> &key);
47     // ecma6 7.3 Operations on Objects
48     static JSHandle<JSArray> CreateArrayFromList(JSThread *thread, const JSHandle<TaggedArray> &elements);
49     // use first inlined property slot for array length
GetArrayLength()50     inline uint32_t GetArrayLength() const
51     {
52         return GetLength();
53     }
54 
SetArrayLength(const JSThread * thread,uint32_t length)55     inline void SetArrayLength([[maybe_unused]]const JSThread *thread, uint32_t length)
56     {
57         SetLength(length);
58     }
59 
GetHintLength()60     inline uint32_t GetHintLength() const
61     {
62         auto trackInfo = GetTrackInfo();
63         if (trackInfo.IsInt()) {
64             int hint = trackInfo.GetInt();
65             return hint > 0 ? hint : 0;
66         }
67         return 0;
68     }
69 
70     static constexpr size_t LENGTH_OFFSET = JSObject::SIZE;
71     ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, LENGTH_OFFSET, TRACE_INDEX_OFFSET)
72     ACCESSORS_PRIMITIVE_FIELD(TraceIndex, uint32_t, TRACE_INDEX_OFFSET, TRACK_INFO_OFFSET)
73     ACCESSORS(TrackInfo, TRACK_INFO_OFFSET, SIZE)
74 
75     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, TRACK_INFO_OFFSET, SIZE)
76 
77     static const uint32_t MAX_ARRAY_INDEX = MAX_ELEMENT_INDEX;
DECL_DUMP()78     DECL_DUMP()
79 
80     static int32_t GetArrayLengthOffset()
81     {
82         return LENGTH_OFFSET;
83     }
84 
85     static bool PropertyKeyToArrayIndex(JSThread *thread, const JSHandle<JSTaggedValue> &key, uint32_t *output);
86 
87     static JSTaggedValue LengthGetter(JSThread *thread, const JSHandle<JSObject> &self);
88 
89     static bool LengthSetter(JSThread *thread, const JSHandle<JSObject> &self, const JSHandle<JSTaggedValue> &value,
90                              bool mayThrow = false);
91 
92     static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
93                                                           uint32_t index);
94 
95     static JSHandle<JSTaggedValue> FastGetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
96                                                           const JSHandle<JSTaggedValue> &key);
97 
98     static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj, uint32_t index,
99                                        const JSHandle<JSTaggedValue> &value);
100 
101     static bool FastSetPropertyByValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
102                                        const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value);
103 
104     static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSTaggedValue> &obj, const JSHandle<JSTaggedValue> &fn);
105     static bool IncludeInSortedValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
106                                      const JSHandle<JSTaggedValue> &value);
107     static JSHandle<TaggedArray> ToTaggedArray(JSThread *thread, const JSHandle<JSTaggedValue> &obj);
108     static void CheckAndCopyArray(const JSThread *thread, JSHandle<JSArray> obj);
109     static void SetCapacity(JSThread *thread, const JSHandle<JSObject> &array, uint32_t oldLen, uint32_t newLen,
110                             bool isNew = false);
111     static void SortElements(JSThread *thread, const JSHandle<TaggedArray> &elements,
112                              const JSHandle<JSTaggedValue> &fn);
113     static void SortElementsByObject(JSThread *thread, const JSHandle<JSObject> &thisObjHandle,
114                                      const JSHandle<JSTaggedValue> &fn);
115 
116     template <class Callback>
ArrayCreateWithInit(JSThread * thread,uint32_t length,const Callback & cb)117     static JSTaggedValue ArrayCreateWithInit(JSThread *thread, uint32_t length, const Callback &cb)
118     {
119         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
120         JSHandle<TaggedArray> newElements(factory->NewTaggedArray(length));
121         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
122         JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, newElements));
123         cb(newElements, length);
124         return array.GetTaggedValue();
125     }
126 };
127 
128 class TrackInfo : public TaggedObject {
129 public:
Cast(TaggedObject * object)130     static TrackInfo *Cast(TaggedObject *object)
131     {
132         ASSERT(JSTaggedValue(object).IsTrackInfoObject());
133         return static_cast<TrackInfo *>(object);
134     }
135 
136     static constexpr size_t CACHED_HCLASS_OFFSET = TaggedObjectSize();
137     ACCESSORS(CachedHClass, CACHED_HCLASS_OFFSET, CACHED_FUNC_OFFSET);
138     ACCESSORS(CachedFunc, CACHED_FUNC_OFFSET, BIT_FIELD_OFFSET);
139     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, ARRAY_LENGTH_OFFSET);
140     ACCESSORS_PRIMITIVE_FIELD(ArrayLength, uint32_t, ARRAY_LENGTH_OFFSET, LAST_OFFSET)
141     DEFINE_ALIGN_SIZE(LAST_OFFSET);
142 
143     // define BitField
144     static constexpr size_t ELEMENTS_KIND_BITS = 8;
145     static constexpr size_t SPACE_FALG_BITS = 8;
146     FIRST_BIT_FIELD(BitField, ElementsKind, ElementsKind, ELEMENTS_KIND_BITS);
147     NEXT_BIT_FIELD(BitField, SpaceFlag, RegionSpaceFlag, SPACE_FALG_BITS, ElementsKind);
148 
149     DECL_DUMP()
150 
151     DECL_VISIT_OBJECT(CACHED_HCLASS_OFFSET, BIT_FIELD_OFFSET);
152 };
153 }  // namespace panda::ecmascript
154 
155 #endif  // ECMASCRIPT_JSARRAY_H
156