1 /* 2 * Copyright (c) 2025 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 #ifndef ECMASCRIPT_CROSS_VM_JS_NAPI_EXPO_HYBRID_H 16 #define ECMASCRIPT_CROSS_VM_JS_NAPI_EXPO_HYBRID_H 17 18 #define GLOBAL_PUBLIC_HYBRID_EXTENSION() \ 19 template<typename S> \ 20 void CreateXRefGloablReference(const EcmaVM *vm, const Local<S> ¤t); \ 21 /* This method must be called before Global is released.*/ \ 22 void FreeGlobalHandleAddr(); \ 23 void FreeXRefGlobalHandleAddr(); \ 24 void MarkFromObject(); \ 25 bool IsObjectAlive() const; \ 26 bool IsValidHeapObject() const 27 28 #define OBJECTREF_PUBLIC_HYBRID_EXTENSION() \ 29 static Local<ObjectRef> NewJSXRefObject(const EcmaVM *vm) 30 31 #define STRINGREF_PUBLIC_HYBRID_EXTENSION() \ 32 static Local<StringRef> GetProxyNapiWrapperString(const EcmaVM *vm) 33 34 #define JSNAPI_PUBLIC_HYBRID_EXTENSION() \ 35 static void SetStackInfo(const EcmaVM *vm, const panda::StackInfo &info); \ 36 static panda::StackInfo GetStackInfo(const EcmaVM *vm); \ 37 static Local<ObjectRef> GetModuleNameSpaceWithPath(const EcmaVM *vm, const char *path); \ 38 static std::pair<std::string, std::string> ResolveOhmUrl(std::string ohmUrl); \ 39 static void SetHostResolveBufferTracker(EcmaVM *vm, std::function<bool(std::string dirPath, \ 40 uint8_t **buff, size_t *buffSize, std::string &errorMsg)> cb); \ 41 static bool IsObjectAlive(const EcmaVM *vm, uintptr_t addr); \ 42 static bool IsValidHeapObject(const EcmaVM *vm, uintptr_t addr); \ 43 static void InitHybridVMEnv(EcmaVM *vm) 44 45 #define JSNAPI_PRIVATE_HYBRID_EXTENSION() \ 46 static uintptr_t GetXRefGlobalHandleAddr(const EcmaVM *vm, uintptr_t localAddress); \ 47 static void DisposeXRefGlobalHandleAddr(const EcmaVM *vm, uintptr_t addr) 48 49 #define JSNAPI_PRIVATE_HYBRID_MODE_EXTENSION() \ 50 static void MarkFromObject(const EcmaVM *vm, uintptr_t addr) 51 52 #define GLOBAL_PUBLIC_DEF_HYBRID_EXTENSION() \ 53 template<typename T> \ 54 template<typename S> \ 55 void Global<T>::CreateXRefGloablReference(const EcmaVM *vm, const Local<S> ¤t) \ 56 { \ 57 vm_ = vm; \ 58 if (!current.IsEmpty()) { \ 59 address_ = JSNApi::GetXRefGlobalHandleAddr(vm_, reinterpret_cast<uintptr_t>(*current)); \ 60 } \ 61 } \ 62 template<typename T> \ 63 void Global<T>::FreeXRefGlobalHandleAddr() \ 64 { \ 65 if (address_ == 0) { \ 66 return; \ 67 } \ 68 JSNApi::DisposeXRefGlobalHandleAddr(vm_, address_); \ 69 address_ = 0; \ 70 } 71 72 #define GLOBAL_PUBLIC_DEF_HYBRID_MODE_EXTENSION() \ 73 template<typename T> \ 74 void Global<T>::MarkFromObject() \ 75 { \ 76 if (address_ == 0) { \ 77 return; \ 78 } \ 79 JSNApi::MarkFromObject(vm_, address_); \ 80 } \ 81 template<typename T> \ 82 bool Global<T>::IsObjectAlive() const \ 83 { \ 84 if (address_ == 0) { \ 85 return false ; \ 86 } \ 87 return JSNApi::IsObjectAlive(vm_, address_); \ 88 } \ 89 template<typename T> \ 90 bool Global<T>::IsValidHeapObject() const \ 91 { \ 92 if (address_ == 0) { \ 93 return false; \ 94 } \ 95 return JSNApi::IsValidHeapObject(vm_, address_); \ 96 } 97 98 namespace panda { 99 namespace ecmascript { 100 class EcmaVM; 101 } 102 using EcmaVM = ecmascript::EcmaVM; 103 104 #ifdef PANDA_JS_ETS_HYBRID_MODE 105 class PUBLIC_API HandshakeHelper final { 106 public: 107 static void DoHandshake(EcmaVM *vm, void *stsiface, void **ecmaiface); 108 }; 109 #endif 110 } 111 #endif // ECMASCRIPT_CROSS_VM_JS_NAPI_EXPO_HYBRID_H 112