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 constexpr size_t SIZE = JSObject::SIZE; 52 GetInlinedPropertyOffset(uint32_t index)53 static uint32_t GetInlinedPropertyOffset(uint32_t index) 54 { 55 return JSIterator::SIZE + index * JSTaggedValue::TaggedTypeSize(); 56 } 57 58 static JSTaggedValue IteratorCloseAndReturn(JSThread *thread, const JSHandle<JSTaggedValue> &iter); 59 // 7.4.1 60 static JSHandle<JSTaggedValue> GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 61 62 static JSHandle<JSTaggedValue> GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj, 63 const JSHandle<JSTaggedValue> &method); 64 65 static JSHandle<JSTaggedValue> GetAsyncIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj); 66 // 7.4.2 67 static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter, 68 const JSHandle<JSTaggedValue> &value); 69 70 static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter, 71 const JSHandle<JSTaggedValue> &value); 72 73 static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter); 74 75 static JSHandle<JSTaggedValue> IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter); 76 // 7.4.3 77 static bool IteratorComplete(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult); 78 // 7.4.4 79 static JSHandle<JSTaggedValue> IteratorValue(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult); 80 // 7.4.5 81 static JSHandle<JSTaggedValue> IteratorStep(JSThread *thread, const JSHandle<JSTaggedValue> &iter); 82 // 7.4.6 83 static JSHandle<JSTaggedValue> IteratorClose(JSThread *thread, const JSHandle<JSTaggedValue> &iter, 84 const JSHandle<JSTaggedValue> &completion); 85 // 7.4.7 86 static JSHandle<JSObject> CreateIterResultObject(JSThread *thread, const JSHandle<JSTaggedValue> &value, bool done); 87 }; 88 } // namespace panda::ecmascript 89 #endif // ECMASCRIPT_JS_ITERATOR_H 90