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 #include "ecmascript/ecma_vm.h" 19 #include "ecmascript/object_factory.h" 20 21 namespace panda::ecmascript { ResetExternalPointer(JSThread * thread,void * externalPointer)22void JSNativePointer::ResetExternalPointer(JSThread *thread, void *externalPointer) 23 { 24 DeleteExternalPointer(thread); 25 SetExternalPointer(externalPointer); 26 } 27 Destroy(JSThread * thread)28void JSNativePointer::Destroy(JSThread *thread) 29 { 30 DeleteExternalPointer(thread); 31 SetExternalPointer(nullptr); 32 SetDeleter(nullptr); 33 SetData(nullptr); 34 SetNativeFlag(NativeFlag::NO_DIV); 35 } 36 Detach()37void JSNativePointer::Detach() 38 { 39 // Keep other fields accessible after detached 40 SetDeleter(nullptr); 41 } 42 DeleteExternalPointer(JSThread * thread)43void JSNativePointer::DeleteExternalPointer(JSThread *thread) 44 { 45 void *externalPointer = GetExternalPointer(); 46 NativePointerCallback deleter = GetDeleter(); 47 auto env = thread->GetEnv(); 48 if (deleter != nullptr) { 49 deleter(env, externalPointer, GetData()); 50 } 51 } 52 ToString(JSThread * thread)53JSHandle<EcmaString> JSNativePointer::ToString(JSThread *thread) 54 { 55 std::stringstream stringstream; 56 stringstream << "[External: " << std::hex << GetExternalPointer() << "]"; 57 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 58 return factory->NewFromASCII(stringstream.str()); 59 } 60 } // namespace panda::ecmascript 61