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