1 /** 2 * Copyright (c) 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 PANDA_PLUGINS_ETS_RUNTIME_EXTERNAL_IFACE_TABLE_H_ 17 #define PANDA_PLUGINS_ETS_RUNTIME_EXTERNAL_IFACE_TABLE_H_ 18 19 #include <functional> 20 21 #include "runtime/interpreter/frame.h" 22 #include "plugins/ets/runtime/job_queue.h" 23 24 namespace ark { 25 class Coroutine; 26 } // namespace ark 27 28 namespace ark::ets { 29 30 class ExternalIfaceTable { 31 public: 32 using JSEnv = void *; 33 using ClearInteropHandleScopesFunction = std::function<void(Frame *)>; 34 using CreateJSRuntimeFunction = std::function<JSEnv()>; 35 using CreateInteropCtxFunction = std::function<void(Coroutine *, JSEnv)>; 36 37 NO_COPY_SEMANTIC(ExternalIfaceTable); 38 NO_MOVE_SEMANTIC(ExternalIfaceTable); 39 40 ExternalIfaceTable() = default; 41 virtual ~ExternalIfaceTable() = default; 42 GetJobQueue()43 JobQueue *GetJobQueue() const 44 { 45 return jobQueue_.get(); 46 } 47 SetJobQueue(PandaUniquePtr<JobQueue> jobQueue)48 void SetJobQueue(PandaUniquePtr<JobQueue> jobQueue) 49 { 50 jobQueue_ = std::move(jobQueue); 51 } 52 GetClearInteropHandleScopesFunction()53 const ClearInteropHandleScopesFunction &GetClearInteropHandleScopesFunction() const 54 { 55 return clearInteropHandleScopes_; 56 } 57 SetClearInteropHandleScopesFunction(const ClearInteropHandleScopesFunction & func)58 void SetClearInteropHandleScopesFunction(const ClearInteropHandleScopesFunction &func) 59 { 60 clearInteropHandleScopes_ = func; 61 } 62 SetCreateJSRuntimeFunction(CreateJSRuntimeFunction && cb)63 void SetCreateJSRuntimeFunction(CreateJSRuntimeFunction &&cb) 64 { 65 ASSERT(!createJSRuntime_); 66 createJSRuntime_ = std::move(cb); 67 } 68 CreateJSRuntime()69 void *CreateJSRuntime() 70 { 71 void *jsEnv = nullptr; 72 if (createJSRuntime_) { 73 jsEnv = createJSRuntime_(); 74 } 75 return jsEnv; 76 } 77 SetCreateInteropCtxFunction(CreateInteropCtxFunction && cb)78 void SetCreateInteropCtxFunction(CreateInteropCtxFunction &&cb) 79 { 80 ASSERT(!createInteropCtx_); 81 createInteropCtx_ = std::move(cb); 82 } 83 CreateInteropCtx(Coroutine * coro,JSEnv jsEnv)84 void CreateInteropCtx(Coroutine *coro, JSEnv jsEnv) 85 { 86 if (createInteropCtx_) { 87 createInteropCtx_(coro, jsEnv); 88 } 89 } 90 91 private: 92 PandaUniquePtr<JobQueue> jobQueue_ = nullptr; 93 ClearInteropHandleScopesFunction clearInteropHandleScopes_ = nullptr; 94 CreateJSRuntimeFunction createJSRuntime_ = nullptr; 95 CreateInteropCtxFunction createInteropCtx_ = nullptr; 96 }; 97 98 } // namespace ark::ets 99 #endif // #ifndef PANDA_PLUGINS_ETS_RUNTIME_EXTERNAL_IFACE_TABLE_H_ 100