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