/src/WasmEdge/lib/vm/plugin_modules.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "plugin_modules.h" |
5 | | |
6 | | #include "common/spdlog.h" |
7 | | #include "plugin/plugin.h" |
8 | | |
9 | | #include "host/mock/wasi_crypto_module.h" |
10 | | #include "host/mock/wasi_logging_module.h" |
11 | | #include "host/mock/wasi_nn_module.h" |
12 | | #include "host/mock/wasmedge_image_module.h" |
13 | | #include "host/mock/wasmedge_stablediffusion_module.h" |
14 | | #include "host/mock/wasmedge_tensorflow_module.h" |
15 | | #include "host/mock/wasmedge_tensorflowlite_module.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <array> |
19 | | |
20 | | namespace WasmEdge { |
21 | | namespace VM { |
22 | | |
23 | | namespace { |
24 | | |
25 | | using namespace std::literals::string_view_literals; |
26 | | |
27 | | using ModuleFactory = std::unique_ptr<Runtime::Instance::ModuleInstance> (*)(); |
28 | | |
29 | | template <typename T> |
30 | 0 | std::unique_ptr<Runtime::Instance::ModuleInstance> createMock() { |
31 | 0 | return std::make_unique<T>(); |
32 | 0 | } Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasiNNModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasiCryptoCommonModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasiCryptoAsymmetricCommonModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasiCryptoKxModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasiCryptoSignaturesModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasiCryptoSymmetricModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasiLoggingModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasmEdgeTensorflowModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasmEdgeTensorflowLiteModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasmEdgeImageModuleMock>() Unexecuted instantiation: plugin_modules.cpp:std::__1::unique_ptr<WasmEdge::Runtime::Instance::ModuleInstance, std::__1::default_delete<WasmEdge::Runtime::Instance::ModuleInstance> > WasmEdge::VM::(anonymous namespace)::createMock<WasmEdge::Host::WasmEdgeStableDiffusionModuleMock>() |
33 | | |
34 | | struct OfficialPluginEntry { |
35 | | std::string_view PluginName; |
36 | | std::string_view ModuleName; |
37 | | ModuleFactory MockFactory; |
38 | | }; |
39 | | |
40 | | /// One row per official plugin module. Both the load order of the mock-or-real |
41 | | /// instances and the official plugin name check derive from this table. |
42 | | constexpr std::array<OfficialPluginEntry, 11> OfficialPluginTable{{ |
43 | | {"wasi_nn"sv, "wasi_nn"sv, createMock<Host::WasiNNModuleMock>}, |
44 | | {"wasi_crypto"sv, "wasi_crypto_common"sv, |
45 | | createMock<Host::WasiCryptoCommonModuleMock>}, |
46 | | {"wasi_crypto"sv, "wasi_crypto_asymmetric_common"sv, |
47 | | createMock<Host::WasiCryptoAsymmetricCommonModuleMock>}, |
48 | | {"wasi_crypto"sv, "wasi_crypto_kx"sv, |
49 | | createMock<Host::WasiCryptoKxModuleMock>}, |
50 | | {"wasi_crypto"sv, "wasi_crypto_signatures"sv, |
51 | | createMock<Host::WasiCryptoSignaturesModuleMock>}, |
52 | | {"wasi_crypto"sv, "wasi_crypto_symmetric"sv, |
53 | | createMock<Host::WasiCryptoSymmetricModuleMock>}, |
54 | | {"wasi_logging"sv, "wasi:logging/logging"sv, |
55 | | createMock<Host::WasiLoggingModuleMock>}, |
56 | | {"wasmedge_tensorflow"sv, "wasmedge_tensorflow"sv, |
57 | | createMock<Host::WasmEdgeTensorflowModuleMock>}, |
58 | | {"wasmedge_tensorflowlite"sv, "wasmedge_tensorflowlite"sv, |
59 | | createMock<Host::WasmEdgeTensorflowLiteModuleMock>}, |
60 | | {"wasmedge_image"sv, "wasmedge_image"sv, |
61 | | createMock<Host::WasmEdgeImageModuleMock>}, |
62 | | {"wasmedge_stablediffusion"sv, "wasmedge_stablediffusion"sv, |
63 | | createMock<Host::WasmEdgeStableDiffusionModuleMock>}, |
64 | | }}; |
65 | | |
66 | | std::unique_ptr<Runtime::Instance::ModuleInstance> |
67 | 0 | createPluginModule(const OfficialPluginEntry &Entry) { |
68 | 0 | if (const auto *Plugin = Plugin::Plugin::find(Entry.PluginName)) { |
69 | 0 | if (const auto *Module = Plugin->findModule(Entry.ModuleName)) { |
70 | 0 | return Module->create(); |
71 | 0 | } |
72 | 0 | } |
73 | 0 | spdlog::debug("Plugin: {} , module name: {} not found. Mock instead."sv, |
74 | 0 | Entry.PluginName, Entry.ModuleName); |
75 | 0 | return Entry.MockFactory(); |
76 | 0 | } |
77 | | |
78 | | } // namespace |
79 | | |
80 | | std::vector<std::unique_ptr<Runtime::Instance::ModuleInstance>> |
81 | 0 | loadOfficialPluginModules() { |
82 | 0 | std::vector<std::unique_ptr<Runtime::Instance::ModuleInstance>> Modules; |
83 | 0 | Modules.reserve(OfficialPluginTable.size()); |
84 | 0 | for (const auto &Entry : OfficialPluginTable) { |
85 | 0 | Modules.push_back(createPluginModule(Entry)); |
86 | 0 | } |
87 | 0 | return Modules; |
88 | 0 | } |
89 | | |
90 | 0 | bool isOfficialPlugin(std::string_view PName) { |
91 | 0 | return std::any_of(OfficialPluginTable.begin(), OfficialPluginTable.end(), |
92 | 0 | [PName](const OfficialPluginEntry &Entry) { |
93 | 0 | return Entry.PluginName == PName; |
94 | 0 | }); |
95 | 0 | } |
96 | | |
97 | | } // namespace VM |
98 | | } // namespace WasmEdge |