1/*
2 * Copyright (c) 2022 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_COMPILER_BUILTINS_CONTAINERS_STACK_STUB_BUILDER_H
17#define ECMASCRIPT_COMPILER_BUILTINS_CONTAINERS_STACK_STUB_BUILDER_H
18#include "ecmascript/compiler/stub_builder-inl.h"
19#include "ecmascript/js_api/js_api_stack.h"
20
21namespace panda::ecmascript::kungfu {
22class ContainersStackStubBuilder : public StubBuilder {
23public:
24    explicit ContainersStackStubBuilder(StubBuilder *parent)
25        : StubBuilder(parent) {}
26    ~ContainersStackStubBuilder() override = default;
27    NO_MOVE_SEMANTIC(ContainersStackStubBuilder);
28    NO_COPY_SEMANTIC(ContainersStackStubBuilder);
29    void GenerateCircuit() override {}
30    GateRef GetSize(GateRef obj)
31    {
32        GateRef top = Load(VariableType::INT32(), obj, IntPtr(JSAPIStack::TOP_OFFSET));
33        return Int32Add(top, Int32(1));
34    }
35
36    GateRef Get(GateRef obj, GateRef index)
37    {
38        GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
39        GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
40        return GetValueFromTaggedArray(elements, index);
41    }
42    void Set(GateRef glue, GateRef obj, GateRef index, GateRef value)
43    {
44        GateRef elementsOffset = IntPtr(JSObject::ELEMENTS_OFFSET);
45        GateRef elements = Load(VariableType::JS_POINTER(), obj, elementsOffset);
46        SetValueToTaggedArray(VariableType::JS_ANY(), glue, elements, index, value);
47    }
48};
49}  // namespace panda::ecmascript::kungfu
50#endif  // ECMASCRIPT_COMPILER_BUILTINS_CONTAINERS_STACK_STUB_BUILDER_H