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#include "ecmascript/js_native_pointer.h"
17#include "ecmascript/js_thread.h"
18
19namespace panda::ecmascript {
20void JSNativePointer::ResetExternalPointer(JSThread *thread, void *externalPointer)
21{
22    DeleteExternalPointer(thread);
23    SetExternalPointer(externalPointer);
24}
25
26void JSNativePointer::Destroy(JSThread *thread)
27{
28    DeleteExternalPointer(thread);
29    SetExternalPointer(nullptr);
30    SetDeleter(nullptr);
31    SetData(nullptr);
32    SetNativeFlag(NativeFlag::NO_DIV);
33}
34
35void JSNativePointer::Detach()
36{
37    // Keep other fields accessible after detached
38    SetDeleter(nullptr);
39}
40
41void JSNativePointer::DeleteExternalPointer(JSThread *thread)
42{
43    void *externalPointer = GetExternalPointer();
44    NativePointerCallback deleter = GetDeleter();
45    auto env = thread->GetEnv();
46    if (deleter != nullptr) {
47        deleter(env, externalPointer, GetData());
48    }
49}
50}  // namespace panda::ecmascript
51