• 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 #include "ecmascript/js_for_in_iterator.h"
17 
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/global_dictionary-inl.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_object-inl.h"
22 #include "ecmascript/js_primitive_ref.h"
23 #include "ecmascript/object_factory.h"
24 #include "ecmascript/tagged_array-inl.h"
25 #include "ecmascript/tagged_dictionary.h"
26 #include "ecmascript/tagged_queue.h"
27 #include "ecmascript/tagged_queue.h"
28 
29 namespace panda::ecmascript {
30 using BuiltinsBase = base::BuiltinsBase;
31 
CheckObjProto(const JSThread * thread,const JSHandle<JSForInIterator> & it)32 bool JSForInIterator::CheckObjProto(const JSThread *thread, const JSHandle<JSForInIterator> &it)
33 {
34     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
35     JSHandle<JSTaggedValue> object(thread, it->GetObject());
36     if (!object->IsJSObject()) {
37         return false;
38     }
39     auto *hclass = object->GetTaggedObject()->GetClass();
40     JSType jsType = hclass->GetObjectType();
41     if (jsType != JSType::JS_OBJECT) {
42         return false;
43     }
44     JSTaggedValue proto = hclass->GetPrototype();
45     if (!proto.IsJSObject()) {
46         return false;
47     }
48     return hclass->GetPrototype().GetTaggedObject()->GetClass() ==
49            env->GetObjectFunctionPrototypeClass().GetTaggedValue().GetTaggedObject()->GetClass();
50 }
51 
GetAllEnumKeys(JSThread * thread,const JSHandle<JSForInIterator> & it,const JSHandle<JSTaggedValue> & object)52 void JSForInIterator::GetAllEnumKeys(JSThread *thread, const JSHandle<JSForInIterator> &it,
53                                      const JSHandle<JSTaggedValue> &object)
54 {
55     ASSERT(object->IsHeapObject());
56     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
57     JSMutableHandle<TaggedQueue> remaining(thread, thread->GlobalConstants()->GetEmptyTaggedQueue());
58     if (object->IsJSProxy()) {
59         JSHandle<TaggedArray> proxyArr = JSProxy::OwnPropertyKeys(thread, JSHandle<JSProxy>(object));
60         RETURN_IF_ABRUPT_COMPLETION(thread);
61         uint32_t length = proxyArr->GetLength();
62         for (uint32_t i = 0; i < length; i++) {
63             value.Update(proxyArr->Get(i));
64             PropertyDescriptor desc(thread);
65             JSProxy::GetOwnProperty(thread, JSHandle<JSProxy>(object), value, desc);
66             RETURN_IF_ABRUPT_COMPLETION(thread);
67             if (desc.IsEnumerable()) {
68                 TaggedQueue *newQueue = TaggedQueue::Push(thread, remaining, value);
69                 remaining.Update(JSTaggedValue(newQueue));
70             }
71         }
72     } else {
73         JSHandle<TaggedArray> arr = JSTaggedValue::GetOwnEnumPropertyKeys(thread, object);
74         uint32_t len = arr->GetLength();
75         for (uint32_t i = 0; i < len; i++) {
76             value.Update(arr->Get(i));
77             if (value->IsString()) {
78                 TaggedQueue *newQueue = TaggedQueue::Push(thread, remaining, value);
79                 remaining.Update(JSTaggedValue(newQueue));
80             }
81         }
82     }
83     if (it->GetHasVisitObjs()) {
84         JSMutableHandle<TaggedQueue> remained(thread, thread->GlobalConstants()->GetEmptyTaggedQueue());
85         JSMutableHandle<TaggedQueue> visited(thread, it->GetVisitedObjs());
86         uint32_t size = visited->Size();
87         while (!remaining->Empty()) {
88             JSHandle<JSTaggedValue> key(thread, remaining->Pop(thread));
89             bool has = false;
90             for (uint32_t i = 0; i < size; i++) {
91                 value.Update(visited->Get(i));
92                 PropertyDescriptor desc(thread);
93                 has = JSTaggedValue::GetOwnProperty(thread, value, key, desc);
94                 RETURN_IF_ABRUPT_COMPLETION(thread);
95                 if (has) {
96                     break;
97                 }
98             }
99             if (!has) {
100                 TaggedQueue *newQueue = TaggedQueue::Push(thread, remained, key);
101                 remained.Update(JSTaggedValue(newQueue));
102             }
103         }
104         it->SetRemainingKeys(thread, remained);
105     } else {
106         it->SetRemainingKeys(thread, remaining);
107     }
108     it->SetWasVisited(true);
109     object->GetTaggedObject()->GetClass()->SetHasDeleteProperty(false);
110 }
111 
NextInternal(JSThread * thread,const JSHandle<JSForInIterator> & it)112 std::pair<JSTaggedValue, bool> JSForInIterator::NextInternal(JSThread *thread, const JSHandle<JSForInIterator> &it)
113 {
114     while (true) {
115         JSHandle<JSTaggedValue> object(thread, it->GetObject());
116         if (object->IsNull() || object->IsUndefined()) {
117             return std::make_pair(JSTaggedValue::Undefined(), true);
118         }
119         if (!it->GetWasVisited()) {
120             GetAllEnumKeys(thread, it, object);
121             RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, std::make_pair(JSTaggedValue::Exception(), false));
122         }
123 
124         JSHandle<TaggedQueue> remaining(thread, it->GetRemainingKeys());
125         while (!remaining->Empty()) {
126             ASSERT(object->IsHeapObject());
127             JSTaggedValue r = remaining->Pop(thread);
128             bool hasDelete = object->GetTaggedObject()->GetClass()->HasDeleteProperty();
129             if (object->IsJSObject() && !hasDelete) {
130                 return std::make_pair(r, false);
131             }
132             JSHandle<JSTaggedValue> key(thread, r);
133             PropertyDescriptor desc(thread);
134             bool has = JSTaggedValue::GetOwnProperty(thread, object, key, desc);
135             if (has) {
136                 if (desc.IsEnumerable()) {
137                     return std::make_pair(key.GetTaggedValue(), false);
138                 }
139             }
140         }
141         JSMutableHandle<TaggedQueue> visited(thread, it->GetVisitedObjs());
142         TaggedQueue *newQueue = TaggedQueue::Push(thread, visited, object);
143         visited.Update(JSTaggedValue(newQueue));
144         it->SetVisitedObjs(thread, visited);
145         JSTaggedValue proto = JSTaggedValue::GetPrototype(thread, object);
146         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, std::make_pair(JSTaggedValue::Exception(), false));
147         it->SetObject(thread, proto);
148         it->SetWasVisited(false);
149         it->SetHasVisitObjs(true);
150     }
151 }
152 
153 // 13.7.5.16.2.1 %ForInIteratorPrototype%.next ( )
Next(EcmaRuntimeCallInfo * msg)154 JSTaggedValue JSForInIterator::Next(EcmaRuntimeCallInfo *msg)
155 {
156     ASSERT(msg);
157     JSThread *thread = msg->GetThread();
158     [[maybe_unused]] EcmaHandleScope handleScope(thread);
159     // 1. Let O be the this value.
160     JSHandle<JSForInIterator> it(BuiltinsBase::GetThis(msg));
161     ASSERT(it->IsForinIterator());
162     std::pair<JSTaggedValue, bool> res = NextInternal(thread, it);
163     return res.first;
164 }
165 }  // namespace panda::ecmascript
166