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_JSNATIVEPOINTER_H
17#define ECMASCRIPT_JSNATIVEPOINTER_H
18
19#include "ecmascript/ecma_macros.h"
20#include "ecmascript/mem/barriers.h"
21#include "ecmascript/mem/tagged_object.h"
22#include "ecmascript/mem/visitor.h"
23#include "ecmascript/mem/native_area_allocator.h"
24
25namespace panda::ecmascript {
26// Used for the requirement of ACE that wants to associated a registered C++ resource with a JSObject.
27class JSNativePointer : public TaggedObject {
28public:
29    static JSNativePointer *Cast(TaggedObject *object)
30    {
31        ASSERT(JSTaggedValue(object).IsJSNativePointer());
32        return reinterpret_cast<JSNativePointer *>(object);
33    }
34
35    void ResetExternalPointer(JSThread *thread, void *externalPointer);
36
37    void Destroy(JSThread *thread);
38
39    void Detach();
40
41    static constexpr size_t POINTER_OFFSET = TaggedObjectSize();
42    ACCESSORS_NATIVE_FIELD(ExternalPointer, void, POINTER_OFFSET, DELETER_OFFSET);
43    ACCESSORS_PRIMITIVE_FIELD(Deleter, NativePointerCallback, DELETER_OFFSET, DATA_OFFSET)
44    ACCESSORS_NATIVE_FIELD(Data, void, DATA_OFFSET, DATA_SIZE_OFFSET);
45    ACCESSORS_PRIMITIVE_FIELD(BindingSize, uint32_t, DATA_SIZE_OFFSET, FLAG_OFFSET);
46    // native memory statistic flag
47    ACCESSORS_PRIMITIVE_FIELD(NativeFlag, NativeFlag, FLAG_OFFSET, LAST_OFFSET);
48    DEFINE_ALIGN_SIZE(LAST_OFFSET);
49
50    DECL_VISIT_NATIVE_FIELD(POINTER_OFFSET, DATA_SIZE_OFFSET)
51
52private:
53    void DeleteExternalPointer(JSThread *thread);
54};
55}  // namespace panda::ecmascript
56#endif  // ECMASCRIPT_JSNATIVEPOINTER_H
57