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_HANDLE_SCOPE_INL_H
17 #define ECMASCRIPT_HANDLE_SCOPE_INL_H
18
19 #include "ecmascript/ecma_handle_scope.h"
20 #include "ecmascript/js_thread.h"
21
22 namespace panda::ecmascript {
EcmaHandleScope(JSThread * thread)23 inline EcmaHandleScope::EcmaHandleScope(JSThread *thread)
24 : thread_(thread), prevNext_(thread->handleScopeStorageNext_), prevEnd_(thread->handleScopeStorageEnd_),
25 prevHandleStorageIndex_(thread->currentHandleStorageIndex_)
26 {
27 thread->HandleScopeCountAdd();
28 }
29
~EcmaHandleScope()30 inline EcmaHandleScope::~EcmaHandleScope()
31 {
32 thread_->HandleScopeCountDec();
33 thread_->handleScopeStorageNext_ = prevNext_;
34 if (thread_->handleScopeStorageEnd_ != prevEnd_) {
35 thread_->handleScopeStorageEnd_ = prevEnd_;
36 thread_->ShrinkHandleStorage(prevHandleStorageIndex_);
37 }
38 }
39
NewHandle(JSThread * thread,JSTaggedType value)40 uintptr_t EcmaHandleScope::NewHandle(JSThread *thread, JSTaggedType value)
41 {
42 // Each Handle must be managed by HandleScope, otherwise it may cause Handle leakage.
43 ASSERT(thread->handleScopeCount_ > 0);
44 auto result = thread->handleScopeStorageNext_;
45 if (result == thread->handleScopeStorageEnd_) {
46 result = reinterpret_cast<JSTaggedType *>(thread->ExpandHandleStorage());
47 }
48 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
49 thread->handleScopeStorageNext_ = result + 1;
50 *result = value;
51 return reinterpret_cast<uintptr_t>(result);
52 }
53 } // namespace panda::ecmascript
54 #endif // ECMASCRIPT_HANDLE_SCOPE_INL_H
55