/src/WasmEdge/lib/driver/instantiateTool.cpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | #include "common/filesystem.h" |
5 | | #include "common/spdlog.h" |
6 | | #include "driver/tool.h" |
7 | | #include "vm/vm.h" |
8 | | |
9 | | #include <cstdlib> |
10 | | #include <string> |
11 | | #include <string_view> |
12 | | |
13 | | using namespace std::literals; |
14 | | |
15 | | namespace WasmEdge { |
16 | | namespace Driver { |
17 | | |
18 | 0 | int InstantiateTool(struct DriverToolOptions &Opt) noexcept { |
19 | 0 | std::ios::sync_with_stdio(false); |
20 | |
|
21 | 0 | Configure Conf = createConfigure(Opt); |
22 | |
|
23 | 0 | if (Opt.MemLim.value().size() > 0) { |
24 | 0 | Conf.getRuntimeConfigure().setMaxMemoryPage( |
25 | 0 | static_cast<uint32_t>(Opt.MemLim.value().back())); |
26 | 0 | } |
27 | |
|
28 | 0 | Conf.addHostRegistration(HostRegistration::Wasi); |
29 | | |
30 | | // Reject an empty input path here: std::filesystem::absolute("") throws on |
31 | | // some standard library implementations (e.g. libstdc++), which would |
32 | | // std::terminate() this noexcept function instead of failing gracefully. |
33 | 0 | if (Opt.SoName.value().empty()) { |
34 | 0 | spdlog::error("No input wasm file provided."sv); |
35 | 0 | return EXIT_FAILURE; |
36 | 0 | } |
37 | 0 | const auto InputPath = |
38 | 0 | std::filesystem::absolute(std::filesystem::u8path(Opt.SoName.value())); |
39 | |
|
40 | 0 | VM::VM VM(Conf); |
41 | |
|
42 | 0 | for (const auto &ModEntry : Opt.LinkedModules.value()) { |
43 | 0 | auto Pos = ModEntry.find(':'); |
44 | 0 | if (Pos == std::string::npos) { |
45 | 0 | spdlog::error("Invalid --module format: \"{}\". Expected name:path."sv, |
46 | 0 | ModEntry); |
47 | 0 | return EXIT_FAILURE; |
48 | 0 | } |
49 | 0 | auto Name = ModEntry.substr(0, Pos); |
50 | 0 | auto Path = std::filesystem::absolute( |
51 | 0 | std::filesystem::u8path(ModEntry.substr(Pos + 1))); |
52 | 0 | if (auto Result = VM.registerModule(Name, Path); !Result) { |
53 | 0 | spdlog::error("Failed to register module \"{}\" from: {}"sv, Name, |
54 | 0 | Path.u8string()); |
55 | 0 | return EXIT_FAILURE; |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | 0 | if (auto Result = VM.loadWasm(InputPath.u8string()); !Result) { |
60 | 0 | return EXIT_FAILURE; |
61 | 0 | } |
62 | 0 | if (auto Result = VM.validate(); !Result) { |
63 | 0 | return EXIT_FAILURE; |
64 | 0 | } |
65 | 0 | if (auto Result = VM.instantiate(); !Result) { |
66 | 0 | return EXIT_FAILURE; |
67 | 0 | } |
68 | | |
69 | 0 | spdlog::info("Instantiation succeeded."sv); |
70 | | return EXIT_SUCCESS; |
71 | 0 | } |
72 | | |
73 | | } // namespace Driver |
74 | | } // namespace WasmEdge |