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_TAGGED_QUEUE_INL_H
17 #define ECMASCRIPT_TAGGED_QUEUE_INL_H
18
19 #include "ecmascript/tagged_queue.h"
20
21 namespace panda::ecmascript {
Create(JSThread * thread,uint32_t capacity,JSTaggedValue initVal)22 inline TaggedQueue *TaggedQueue::Create(JSThread *thread, uint32_t capacity, JSTaggedValue initVal)
23 {
24 uint32_t length = QueueToArrayIndex(capacity);
25
26 auto queue = TaggedQueue::Cast(*thread->GetEcmaVM()->GetFactory()->NewTaggedArray(length, initVal));
27 queue->SetStart(thread, JSTaggedValue(0)); // equal to 0 when add 1.
28 queue->SetEnd(thread, JSTaggedValue(0));
29 queue->SetCapacity(thread, JSTaggedValue(capacity));
30 return queue;
31 }
32
Pop(JSThread * thread)33 inline JSTaggedValue TaggedQueue::Pop(JSThread *thread)
34 {
35 if (Empty()) {
36 return JSTaggedValue::Hole();
37 }
38
39 uint32_t start = GetStart().GetArrayLength();
40 JSTaggedValue value = Get(start);
41
42 uint32_t capacity = GetCapacity().GetArrayLength();
43 ASSERT(capacity != 0);
44 SetStart(thread, JSTaggedValue((start + 1) % capacity));
45 return value;
46 }
47 } // namespace panda::ecmascript
48 #endif // ECMASCRIPT_TAGGED_QUEUE_INL_H