1 /*
2  * Copyright (c) 2021-2024 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_ITERATOR_H
17 #define ECMASCRIPT_JS_ITERATOR_H
18 
19 #include "ecmascript/accessor_data.h"
20 #include "ecmascript/js_tagged_value.h"
21 
22 namespace panda::ecmascript {
23 enum class IterationKind : uint8_t {
24     KEY = 0,
25     VALUE,
26     KEY_AND_VALUE
27 };
28 
29 class AsyncIteratorRecord final : public Record {
30 public:
31     CAST_CHECK(AsyncIteratorRecord, IsAsyncIteratorRecord);
32 
33     static constexpr size_t ITERATOR_OFFSET = Record::SIZE;
34     ACCESSORS(Iterator, ITERATOR_OFFSET, NEXTMETHOD_OFFSET);
35     ACCESSORS(NextMethod, NEXTMETHOD_OFFSET, BIT_FIELD_OFFSET);
36     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
37     DEFINE_ALIGN_SIZE(LAST_OFFSET);
38 
39     static constexpr size_t DONE_BITS = 1;
40     FIRST_BIT_FIELD(BitField, Done, bool, DONE_BITS)
41 
42     DECL_VISIT_OBJECT(ITERATOR_OFFSET, BIT_FIELD_OFFSET)
43     DECL_DUMP()
44 };
45 
46 class JSIterator final {
47 public:
48     static constexpr int VALUE_INLINE_PROPERTY_INDEX = 0;
49     static constexpr int DONE_INLINE_PROPERTY_INDEX = 1;
50 
51     static JSTaggedValue IteratorCloseAndReturn(JSThread *thread, const JSHandle<JSTaggedValue> &iter);
52     // 7.4.1
53     static JSHandle<JSTaggedValue> GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj);
54 
55     static JSHandle<JSTaggedValue> GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
56                                                const JSHandle<JSTaggedValue> &method);
57 
58     static JSHandle<JSTaggedValue> GetAsyncIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj);
59     // 7.4.2
60     static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
61                                            const JSHandle<JSTaggedValue> &value);
62 
63     static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter,
64                                                  const JSHandle<JSTaggedValue> &value);
65 
66     static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter);
67 
68     static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter);
69     // 7.4.3
70     static bool IteratorComplete(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult);
71     // 7.4.4
72     static JSHandle<JSTaggedValue> IteratorValue(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult);
73     // 7.4.5
74     static JSHandle<JSTaggedValue> IteratorStep(JSThread *thread, const JSHandle<JSTaggedValue> &iter);
75     // 7.4.6
76     static JSHandle<JSTaggedValue> IteratorClose(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
77                                                  const JSHandle<JSTaggedValue> &completion);
78     // 7.4.7
79     static JSHandle<JSObject> CreateIterResultObject(JSThread *thread, const JSHandle<JSTaggedValue> &value, bool done);
80 };
81 }  // namespace panda::ecmascript
82 #endif  // ECMASCRIPT_JS_ITERATOR_H
83