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_HANDLE_SCOPE_H
17#define ECMASCRIPT_HANDLE_SCOPE_H
18
19#include "ecmascript/js_tagged_value.h"
20#ifdef ECMASCRIPT_ENABLE_HANDLE_LEAK_CHECK
21#include "ecmascript/platform/backtrace.h"
22#include "ecmascript/log_wrapper.h"
23#include "ecmascript/mem/clock_scope.h"
24#endif
25
26namespace panda::ecmascript {
27class JSThread;
28/*
29 * Handles are only valid within a HandleScope. When a handle is created for an object a cell is allocated in the
30 * current HandleScope.
31 */
32
33class EcmaHandleScope {
34public:
35    explicit PUBLIC_API EcmaHandleScope(JSThread *thread);
36
37    PUBLIC_API ~EcmaHandleScope();
38    static uintptr_t PUBLIC_API NewHandle(JSThread *thread, JSTaggedType value);
39    static uintptr_t PUBLIC_API NewPrimitiveHandle(JSThread *thread, JSTaggedType value);
40    void OpenHandleScope(EcmaContext *context);
41    void OpenPrimitiveScope(EcmaContext *context);
42    void CloseHandleScope(EcmaContext *context);
43    void ClosePrimitiveScope(EcmaContext *context);
44
45    JSThread *GetThread() const
46    {
47        return thread_;
48    }
49
50private:
51    JSThread *thread_;
52    JSTaggedType *prevNext_;
53    JSTaggedType *prevEnd_;
54    int prevHandleStorageIndex_ {-1};
55    JSTaggedType *prevPrimitiveNext_;
56    JSTaggedType *prevPrimitiveEnd_;
57    int prevPrimitiveStorageIndex_ {-1};
58#ifdef ECMASCRIPT_ENABLE_HANDLE_LEAK_CHECK
59    ClockScope scope_;
60    EcmaHandleScope *prevHandleScope_ {nullptr};
61    EcmaHandleScope *prevPrimitiveScope_ {nullptr};
62#endif
63
64    NO_COPY_SEMANTIC(EcmaHandleScope);
65    NO_MOVE_SEMANTIC(EcmaHandleScope);
66};
67}  // namespace panda::ecmascript
68#endif  // ECMASCRIPT_HANDLE_SCOPE_H
69