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