14514f5e3Sopenharmony_ci /*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/mem/assert_scope.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_cinamespace panda::ecmascript {
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_ciusing AssertGarbageCollectBit = panda::BitField<bool, 0, 1>;            // default enable
214514f5e3Sopenharmony_ciusing AssertHeapAllocBit = AssertGarbageCollectBit::NextFlag;           // default enable
224514f5e3Sopenharmony_ciusing AssertHandleAllocBit = AssertHeapAllocBit::NextFlag;              // default enable
234514f5e3Sopenharmony_ciusing AssertDeRefHandleBit = AssertHandleAllocBit::NextFlag;            // default enable
244514f5e3Sopenharmony_ciusing AssertLocalToShareWeakRefBit = AssertDeRefHandleBit::NextFlag;    // default disable
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ciconstexpr size_t INITIAL_ASSERT_DATA = AssertGarbageCollectBit::Mask() |
274514f5e3Sopenharmony_ci                                       AssertHeapAllocBit::Mask() |
284514f5e3Sopenharmony_ci                                       AssertHandleAllocBit::Mask() |
294514f5e3Sopenharmony_ci                                       AssertDeRefHandleBit::Mask();
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_cithread_local size_t currentAssertData = INITIAL_ASSERT_DATA;
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_citemplate<AssertType type, bool isAllow, bool IsDebug>
344514f5e3Sopenharmony_cibool AssertScopeT<type, isAllow, IsDebug>::IsAllowed()
354514f5e3Sopenharmony_ci{
364514f5e3Sopenharmony_ci    return true;
374514f5e3Sopenharmony_ci};
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_citemplate<AssertType type, bool isAllow>
404514f5e3Sopenharmony_ciAssertScopeT<type, isAllow, true>::AssertScopeT() : oldData_(currentAssertData)
414514f5e3Sopenharmony_ci{
424514f5e3Sopenharmony_ci    switch (type) {
434514f5e3Sopenharmony_ci        case AssertType::GARBAGE_COLLECTION_ASSERT:
444514f5e3Sopenharmony_ci            currentAssertData = AssertGarbageCollectBit::Update(oldData_.value(), isAllow);
454514f5e3Sopenharmony_ci            break;
464514f5e3Sopenharmony_ci        case AssertType::HEAP_ALLOC_ASSERT:
474514f5e3Sopenharmony_ci            currentAssertData = AssertHeapAllocBit::Update(oldData_.value(), isAllow);
484514f5e3Sopenharmony_ci            break;
494514f5e3Sopenharmony_ci        case AssertType::HANDLE_ALLOC_ASSERT:
504514f5e3Sopenharmony_ci            currentAssertData = AssertHandleAllocBit::Update(oldData_.value(), isAllow);
514514f5e3Sopenharmony_ci            break;
524514f5e3Sopenharmony_ci        case AssertType::DEREF_HANDLE_ASSERT:
534514f5e3Sopenharmony_ci            currentAssertData = AssertDeRefHandleBit::Update(oldData_.value(), isAllow);
544514f5e3Sopenharmony_ci            break;
554514f5e3Sopenharmony_ci        case AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT:
564514f5e3Sopenharmony_ci            currentAssertData = AssertLocalToShareWeakRefBit::Update(oldData_.value(), isAllow);
574514f5e3Sopenharmony_ci            break;
584514f5e3Sopenharmony_ci        default:
594514f5e3Sopenharmony_ci            break;
604514f5e3Sopenharmony_ci    }
614514f5e3Sopenharmony_ci}
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_citemplate<AssertType type, bool isAllow>
644514f5e3Sopenharmony_ciAssertScopeT<type, isAllow, true>::~AssertScopeT()
654514f5e3Sopenharmony_ci{
664514f5e3Sopenharmony_ci    if (!oldData_.has_value()) {
674514f5e3Sopenharmony_ci        return;
684514f5e3Sopenharmony_ci    }
694514f5e3Sopenharmony_ci    currentAssertData = oldData_.value();
704514f5e3Sopenharmony_ci    oldData_.reset();
714514f5e3Sopenharmony_ci}
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_citemplate<AssertType type, bool isAllow>
744514f5e3Sopenharmony_cibool AssertScopeT<type, isAllow, true>::IsAllowed()
754514f5e3Sopenharmony_ci{
764514f5e3Sopenharmony_ci    switch (type) {
774514f5e3Sopenharmony_ci        case AssertType::GARBAGE_COLLECTION_ASSERT:
784514f5e3Sopenharmony_ci            return AssertGarbageCollectBit::Decode(currentAssertData);
794514f5e3Sopenharmony_ci        case AssertType::HEAP_ALLOC_ASSERT:
804514f5e3Sopenharmony_ci            return AssertHeapAllocBit::Decode(currentAssertData);
814514f5e3Sopenharmony_ci        case AssertType::HANDLE_ALLOC_ASSERT:
824514f5e3Sopenharmony_ci            return AssertHandleAllocBit::Decode(currentAssertData);
834514f5e3Sopenharmony_ci        case AssertType::DEREF_HANDLE_ASSERT:
844514f5e3Sopenharmony_ci            return AssertDeRefHandleBit::Decode(currentAssertData);
854514f5e3Sopenharmony_ci        case AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT:
864514f5e3Sopenharmony_ci            return AssertLocalToShareWeakRefBit::Decode(currentAssertData);
874514f5e3Sopenharmony_ci        default:
884514f5e3Sopenharmony_ci            return true;
894514f5e3Sopenharmony_ci    }
904514f5e3Sopenharmony_ci}
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, false, IS_ALLOW_CHECK>;
934514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::GARBAGE_COLLECTION_ASSERT, true, IS_ALLOW_CHECK>;
944514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
954514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::HEAP_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
964514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::HANDLE_ALLOC_ASSERT, false, IS_ALLOW_CHECK>;
974514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::HANDLE_ALLOC_ASSERT, true, IS_ALLOW_CHECK>;
984514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::DEREF_HANDLE_ASSERT, false, IS_ALLOW_CHECK>;
994514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::DEREF_HANDLE_ASSERT, true, IS_ALLOW_CHECK>;
1004514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT, false, IS_ALLOW_CHECK>;
1014514f5e3Sopenharmony_citemplate class PUBLIC_API AssertScopeT<AssertType::LOCAL_TO_SHARE_WEAK_REF_ASSERT, true, IS_ALLOW_CHECK>;
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ci}  // namespace panda::ecmascript