Coverage Report

Created: 2026-09-28 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/api/wasmedge.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
#include "wasmedge/wasmedge.h"
5
6
#include "common/defines.h"
7
#include "driver/compiler.h"
8
#include "driver/tool.h"
9
#include "driver/unitool.h"
10
#include "host/wasi/wasimodule.h"
11
#include "plugin/plugin.h"
12
#include "system/winapi.h"
13
#include "vm/vm.h"
14
#include "llvm/codegen.h"
15
#include "llvm/compiler.h"
16
17
#ifdef WASMEDGE_BUILD_FUZZING
18
#include "driver/fuzzPO.h"
19
#include "driver/fuzzTool.h"
20
#endif
21
22
#ifdef WASMEDGE_BUILD_WASI_NN_RPC
23
#include "driver/wasiNNRPCServerTool.h"
24
#endif
25
26
#include <algorithm>
27
#include <chrono>
28
#include <cstdint>
29
#include <cstdlib>
30
#include <cstring>
31
#include <functional>
32
#include <map>
33
#include <memory>
34
#include <new>
35
#include <string>
36
#include <string_view>
37
#include <utility>
38
#include <vector>
39
40
// WasmEdge_ConfigureContext implementation.
41
struct WasmEdge_ConfigureContext {
42
  WasmEdge::Configure Conf;
43
};
44
45
// WasmEdge_StatisticsContext implementation.
46
struct WasmEdge_StatisticsContext {};
47
48
// WasmEdge_ASTModuleContext implementation.
49
struct WasmEdge_ASTModuleContext {};
50
51
// WasmEdge_FunctionTypeContext implementation.
52
struct WasmEdge_FunctionTypeContext {};
53
54
// WasmEdge_TableTypeContext implementation.
55
struct WasmEdge_TableTypeContext {};
56
57
// WasmEdge_MemoryTypeContext implementation.
58
struct WasmEdge_MemoryTypeContext {};
59
60
// WasmEdge_TagTypeContext implementation.
61
struct WasmEdge_TagTypeContext {};
62
63
// WasmEdge_GlobalTypeContext implementation.
64
struct WasmEdge_GlobalTypeContext {};
65
66
// WasmEdge_ImportTypeContext implementation.
67
struct WasmEdge_ImportTypeContext {};
68
69
// WasmEdge_ExportTypeContext implementation.
70
struct WasmEdge_ExportTypeContext {};
71
72
// WasmEdge_CompilerContext implementation.
73
struct WasmEdge_CompilerContext {
74
#ifdef WASMEDGE_USE_LLVM
75
  WasmEdge_CompilerContext(const WasmEdge::Configure &Conf) noexcept
76
0
      : Compiler(Conf), CodeGen(Conf), Load(Conf), Valid(Conf) {}
77
  WasmEdge::LLVM::Compiler Compiler;
78
  WasmEdge::LLVM::CodeGen CodeGen;
79
  WasmEdge::Loader::Loader Load;
80
  WasmEdge::Validator::Validator Valid;
81
#endif
82
};
83
84
// WasmEdge_LoaderContext implementation.
85
struct WasmEdge_LoaderContext {};
86
87
// WasmEdge_ValidatorContext implementation.
88
struct WasmEdge_ValidatorContext {};
89
90
// WasmEdge_ExecutorContext implementation.
91
struct WasmEdge_ExecutorContext {};
92
93
// WasmEdge_StoreContext implementation.
94
struct WasmEdge_StoreContext {};
95
96
// WasmEdge_ModuleInstanceContext implementation.
97
struct WasmEdge_ModuleInstanceContext {};
98
99
// WasmEdge_FunctionInstanceContext implementation.
100
struct WasmEdge_FunctionInstanceContext {};
101
102
// WasmEdge_TableInstanceContext implementation.
103
struct WasmEdge_TableInstanceContext {};
104
105
// WasmEdge_MemoryInstanceContext implementation.
106
struct WasmEdge_MemoryInstanceContext {};
107
108
// WasmEdge_TagInstanceContext implementation.
109
struct WasmEdge_TagInstanceContext {};
110
111
// WasmEdge_GlobalInstanceContext implementation.
112
struct WasmEdge_GlobalInstanceContext {};
113
114
// WasmEdge_CallingFrameContext implementation.
115
struct WasmEdge_CallingFrameContext {};
116
117
// WasmEdge_Async implementation.
118
struct WasmEdge_Async {
119
  template <typename... Args>
120
  WasmEdge_Async(Args &&...Vals) noexcept
121
0
      : Async(std::forward<Args>(Vals)...) {}
122
  WasmEdge::Async<WasmEdge::Expect<
123
      std::vector<std::pair<WasmEdge::ValVariant, WasmEdge::ValType>>>>
124
      Async;
125
};
126
127
// WasmEdge_VMContext implementation.
128
struct WasmEdge_VMContext {
129
  template <typename... Args>
130
  WasmEdge_VMContext(Args &&...Vals) noexcept
131
0
      : VM(std::forward<Args>(Vals)...) {}
Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure const&, WasmEdge::Runtime::StoreManager&>(WasmEdge::Configure const&, WasmEdge::Runtime::StoreManager&)
Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure const&>(WasmEdge::Configure const&)
Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure, WasmEdge::Runtime::StoreManager&>(WasmEdge::Configure&&, WasmEdge::Runtime::StoreManager&)
Unexecuted instantiation: WasmEdge_VMContext::WasmEdge_VMContext<WasmEdge::Configure>(WasmEdge::Configure&&)
132
  WasmEdge::VM::VM VM;
133
};
134
135
// WasmEdge_PluginContext implementation.
136
struct WasmEdge_PluginContext {};
137
138
namespace {
139
140
using namespace WasmEdge;
141
142
// Helper function for returning a WasmEdge_Result by error code.
143
inline constexpr WasmEdge_Result
144
0
genWasmEdge_Result(const ErrCode::Value &Code) noexcept {
145
0
  return WasmEdge_Result{/* Code */ static_cast<uint32_t>(Code) & 0x00FFFFFFU};
146
0
}
147
inline constexpr WasmEdge_Result
148
0
genWasmEdge_Result(const ErrCode &Code) noexcept {
149
0
  return WasmEdge_Result{/* Code */ Code.operator uint32_t()};
150
0
}
151
152
// Helper function for returning a struct uint128_t / int128_t
153
// from class WasmEdge::uint128_t / WasmEdge::int128_t.
154
template <typename C>
155
0
inline constexpr ::uint128_t to_uint128_t(C Val) noexcept {
156
0
#if defined(__x86_64__) || defined(__aarch64__) ||                             \
157
0
    (defined(__riscv) && __riscv_xlen == 64) || defined(__s390x__)
158
0
  return Val;
159
#else
160
  return {/* Low */ Val.low(), /* High */ static_cast<uint64_t>(Val.high())};
161
#endif
162
0
}
163
0
template <typename C> inline constexpr ::int128_t to_int128_t(C Val) noexcept {
164
0
#if defined(__x86_64__) || defined(__aarch64__) ||                             \
165
0
    (defined(__riscv) && __riscv_xlen == 64) || defined(__s390x__)
166
0
  return Val;
167
#else
168
  return {/* Low */ Val.low(), /* High */ Val.high()};
169
#endif
170
0
}
171
172
// Helper function for returning a class WasmEdge::uint128_t /
173
// WasmEdge::int128_t from struct uint128_t / int128_t.
174
template <typename C, typename T>
175
0
inline constexpr C to_WasmEdge_128_t(T Val) noexcept {
176
0
#if defined(__x86_64__) || defined(__aarch64__) ||                             \
177
0
    (defined(__riscv) && __riscv_xlen == 64) || defined(__s390x__)
178
0
  return Val;
179
#else
180
  return C(Val.High, Val.Low);
181
#endif
182
0
}
Unexecuted instantiation: wasmedge.cpp:__int128 (anonymous namespace)::to_WasmEdge_128_t<__int128, __int128>(__int128)
Unexecuted instantiation: wasmedge.cpp:unsigned __int128 (anonymous namespace)::to_WasmEdge_128_t<unsigned __int128, unsigned __int128>(unsigned __int128)
183
184
// Helper functions for returning a WasmEdge::ValType by WasmEdge_ValType.
185
0
inline ValType genValType(const WasmEdge_ValType &T) noexcept {
186
0
  std::array<uint8_t, 8> R;
187
0
  std::copy_n(T.Data, 8, R.begin());
188
0
  return ValType(R);
189
0
}
190
191
0
inline std::filesystem::path genPath(const char *Path) {
192
0
  return (Path && *Path) ? std::filesystem::absolute(Path)
193
0
                         : std::filesystem::path();
194
0
}
195
196
// Helper functions for returning a WasmEdge_ValType by WasmEdge::ValType.
197
0
inline WasmEdge_ValType genWasmEdge_ValType(const ValType &T) noexcept {
198
0
  WasmEdge_ValType VT;
199
0
  std::copy_n(T.getRawData().cbegin(), 8, VT.Data);
200
0
  return VT;
201
0
}
202
203
// Helper functions for returning a WasmEdge_Value by various values.
204
template <typename T>
205
0
inline WasmEdge_Value genWasmEdge_Value(const T &Val) noexcept {
206
0
  return WasmEdge_Value{
207
0
      /* Value */ to_uint128_t(ValVariant(Val).unwrap()),
208
0
      /* Type */ genWasmEdge_ValType(WasmEdge::ValTypeFromType<T>())};
209
0
}
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<int>(int const&)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<long>(long const&)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<float>(float const&)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<double>(double const&)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Value (anonymous namespace)::genWasmEdge_Value<__int128>(__int128 const&)
210
inline WasmEdge_Value genWasmEdge_Value(const ValVariant &Val,
211
0
                                        const ValType &T) noexcept {
212
0
  return WasmEdge_Value{/* Value */ to_uint128_t(Val.unwrap()),
213
0
                        /* Type */ genWasmEdge_ValType(T)};
214
0
}
215
216
// Helper function for converting a WasmEdge_Value array to a ValVariant
217
// vector.
218
inline std::pair<std::vector<ValVariant>, std::vector<ValType>>
219
0
genParamPair(const WasmEdge_Value *Val, const uint32_t Len) {
220
  // Nullable-value handling for reference-type checking is delegated to the
221
  // executor.
222
0
  std::vector<ValVariant> VVec;
223
0
  std::vector<ValType> TVec;
224
0
  if (Val == nullptr) {
225
0
    return {VVec, TVec};
226
0
  }
227
0
  VVec.resize(Len);
228
0
  TVec.resize(Len);
229
0
  for (uint32_t I = 0; I < Len; I++) {
230
0
    TVec[I] = genValType(Val[I].Type);
231
0
    switch (TVec[I].getCode()) {
232
0
    case TypeCode::I32:
233
0
      VVec[I] = ValVariant::wrap<uint32_t>(
234
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value));
235
0
      break;
236
0
    case TypeCode::I64:
237
0
      VVec[I] = ValVariant::wrap<uint64_t>(
238
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value));
239
0
      break;
240
0
    case TypeCode::F32:
241
0
      VVec[I] = ValVariant::wrap<float>(
242
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value));
243
0
      break;
244
0
    case TypeCode::F64:
245
0
      VVec[I] = ValVariant::wrap<double>(
246
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value));
247
0
      break;
248
0
    case TypeCode::V128:
249
0
      VVec[I] = ValVariant::wrap<WasmEdge::uint128_t>(
250
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value));
251
0
      break;
252
0
    case TypeCode::Ref:
253
0
    case TypeCode::RefNull: {
254
0
      VVec[I] = ValVariant::wrap<RefVariant>(
255
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val[I].Value));
256
0
      break;
257
0
    }
258
0
    default:
259
0
      assumingUnreachable();
260
0
    }
261
0
  }
262
0
  return {VVec, TVec};
263
0
}
264
265
// Helper function for making a Span to a uint8_t array.
266
template <typename T>
267
inline constexpr Span<const T> genSpan(const T *Buf,
268
0
                                       const uint64_t Len) noexcept {
269
0
  if (Buf && Len > 0) {
270
0
    return Span<const T>(Buf, Len);
271
0
  }
272
0
  return Span<const T>();
273
0
}
Unexecuted instantiation: wasmedge.cpp:cxx20::span<unsigned long const, 18446744073709551615ul> (anonymous namespace)::genSpan<unsigned long>(unsigned long const*, unsigned long)
Unexecuted instantiation: wasmedge.cpp:cxx20::span<unsigned char const, 18446744073709551615ul> (anonymous namespace)::genSpan<unsigned char>(unsigned char const*, unsigned long)
274
275
// Helper functions for converting WasmEdge_String to std::String.
276
0
inline std::string_view genStrView(const WasmEdge_String S) noexcept {
277
0
  return std::string_view(S.Buf, S.Length);
278
0
}
279
280
// Helper functions for converting a ValVariant vector to a WasmEdge_Value
281
// array.
282
inline constexpr void
283
fillWasmEdge_ValueArr(Span<const std::pair<ValVariant, ValType>> Vec,
284
0
                      WasmEdge_Value *Val, const uint32_t Len) noexcept {
285
0
  if (Val == nullptr) {
286
0
    return;
287
0
  }
288
0
  for (uint32_t I = 0; I < Len && I < Vec.size(); I++) {
289
0
    Val[I] = genWasmEdge_Value(Vec[I].first, Vec[I].second);
290
0
  }
291
0
}
292
293
// Helper template to run and return result.
294
0
auto EmptyThen = [](auto &&) noexcept {};
295
0
template <typename T> inline bool isContext(T *Cxt) noexcept {
296
0
  return (Cxt != nullptr);
297
0
}
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_CompilerContext>(WasmEdge_CompilerContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_LoaderContext>(WasmEdge_LoaderContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ASTModuleContext*>(WasmEdge_ASTModuleContext**)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ASTModuleContext const>(WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_Bytes>(WasmEdge_Bytes*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ValidatorContext>(WasmEdge_ValidatorContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext>(WasmEdge_ExecutorContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext*>(WasmEdge_ModuleInstanceContext**)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_StoreContext>(WasmEdge_StoreContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext const>(WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_FunctionInstanceContext const>(WasmEdge_FunctionInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext>(WasmEdge_ModuleInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_FunctionInstanceContext>(WasmEdge_FunctionInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_TableInstanceContext>(WasmEdge_TableInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext>(WasmEdge_MemoryInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_GlobalInstanceContext>(WasmEdge_GlobalInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_TableInstanceContext const>(WasmEdge_TableInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_Value>(WasmEdge_Value*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext const>(WasmEdge_MemoryInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<unsigned char>(unsigned char*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<unsigned char const>(unsigned char const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_Async const>(WasmEdge_Async const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_VMContext>(WasmEdge_VMContext*)
298
template <typename T, typename... Args>
299
0
inline bool isContext(T *Cxt, Args *...Cxts) noexcept {
300
0
  return isContext(Cxt) && isContext(Cxts...);
301
0
}
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_LoaderContext, WasmEdge_ASTModuleContext*>(WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext**)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_LoaderContext, WasmEdge_ASTModuleContext const, WasmEdge_Bytes>(WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext const*, WasmEdge_Bytes*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ASTModuleContext const, WasmEdge_Bytes>(WasmEdge_ASTModuleContext const*, WasmEdge_Bytes*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ValidatorContext, WasmEdge_ASTModuleContext const>(WasmEdge_ValidatorContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext, WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ExecutorContext*, WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext, WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_ExecutorContext*, WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ExecutorContext, WasmEdge_FunctionInstanceContext const>(WasmEdge_ExecutorContext*, WasmEdge_FunctionInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_FunctionInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_FunctionInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_TableInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_TableInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_MemoryInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_MemoryInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_ModuleInstanceContext, WasmEdge_GlobalInstanceContext>(WasmEdge_ModuleInstanceContext*, WasmEdge_GlobalInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_TableInstanceContext const, WasmEdge_Value>(WasmEdge_TableInstanceContext const*, WasmEdge_Value*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext const, unsigned char>(WasmEdge_MemoryInstanceContext const*, unsigned char*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_MemoryInstanceContext, unsigned char const>(WasmEdge_MemoryInstanceContext*, unsigned char const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:bool (anonymous namespace)::isContext<WasmEdge_VMContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_VMContext*, WasmEdge_ModuleInstanceContext const*)
302
// Helper to log and convert exceptions at the C API boundary.
303
0
inline WasmEdge_Result handleCAPIError() noexcept {
304
0
  try {
305
0
    throw;
306
0
  } catch (const std::bad_alloc &) {
307
0
    spdlog::error("C API failed: memory allocation failure"sv);
308
0
  } catch (const std::exception &E) {
309
0
    spdlog::error("C API failed: {}"sv, E.what());
310
0
  } catch (...) {
311
0
    spdlog::error("C API failed: unknown exception"sv);
312
0
  }
313
0
  return genWasmEdge_Result(ErrCode::Value::RuntimeError);
314
0
}
315
316
template <typename T, typename U, typename... CxtT>
317
0
inline WasmEdge_Result wrap(T &&Proc, U &&Then, CxtT *...Cxts) noexcept {
318
0
  if (isContext(Cxts...)) {
319
0
    try {
320
0
      if (auto Res = Proc()) {
321
0
        Then(Res);
322
0
        return genWasmEdge_Result(ErrCode::Value::Success);
323
0
      } else {
324
0
        return genWasmEdge_Result(Res.error());
325
0
      }
326
0
    } catch (...) {
327
0
      return handleCAPIError();
328
0
    }
329
0
  } else {
330
0
    return genWasmEdge_Result(ErrCode::Value::WrongVMWorkflow);
331
0
  }
332
0
}
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_CompilerCompile::$_0, (anonymous namespace)::$_12&, WasmEdge_CompilerContext>(WasmEdge_CompilerCompile::$_0&&, (anonymous namespace)::$_12&, WasmEdge_CompilerContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_CompilerCompileFromBytes::$_0, (anonymous namespace)::$_12&, WasmEdge_CompilerContext>(WasmEdge_CompilerCompileFromBytes::$_0&&, (anonymous namespace)::$_12&, WasmEdge_CompilerContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_LoaderParseFromFile::$_0, WasmEdge_LoaderParseFromFile::$_1, WasmEdge_LoaderContext, WasmEdge_ASTModuleContext*>(WasmEdge_LoaderParseFromFile::$_0&&, WasmEdge_LoaderParseFromFile::$_1&&, WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext**)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_LoaderParseFromBytes::$_0, WasmEdge_LoaderParseFromBytes::$_1, WasmEdge_LoaderContext, WasmEdge_ASTModuleContext*>(WasmEdge_LoaderParseFromBytes::$_0&&, WasmEdge_LoaderParseFromBytes::$_1&&, WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext**)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_LoaderSerializeASTModule::$_0, WasmEdge_LoaderSerializeASTModule::$_1, WasmEdge_LoaderContext, WasmEdge_ASTModuleContext const, WasmEdge_Bytes>(WasmEdge_LoaderSerializeASTModule::$_0&&, WasmEdge_LoaderSerializeASTModule::$_1&&, WasmEdge_LoaderContext*, WasmEdge_ASTModuleContext const*, WasmEdge_Bytes*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ValidatorValidate::$_0, (anonymous namespace)::$_12&, WasmEdge_ValidatorContext, WasmEdge_ASTModuleContext const>(WasmEdge_ValidatorValidate::$_0&&, (anonymous namespace)::$_12&, WasmEdge_ValidatorContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorInstantiate::$_0, WasmEdge_ExecutorInstantiate::$_1, WasmEdge_ExecutorContext, WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ExecutorInstantiate::$_0&&, WasmEdge_ExecutorInstantiate::$_1&&, WasmEdge_ExecutorContext*, WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorRegister::$_0, WasmEdge_ExecutorRegister::$_1, WasmEdge_ExecutorContext, WasmEdge_ModuleInstanceContext*, WasmEdge_StoreContext, WasmEdge_ASTModuleContext const>(WasmEdge_ExecutorRegister::$_0&&, WasmEdge_ExecutorRegister::$_1&&, WasmEdge_ExecutorContext*, WasmEdge_ModuleInstanceContext**, WasmEdge_StoreContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorRegisterImport::$_0, (anonymous namespace)::$_12&, WasmEdge_ExecutorContext, WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_ExecutorRegisterImport::$_0&&, (anonymous namespace)::$_12&, WasmEdge_ExecutorContext*, WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorRegisterImportWithAlias::$_0, (anonymous namespace)::$_12&, WasmEdge_ExecutorContext, WasmEdge_StoreContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_ExecutorRegisterImportWithAlias::$_0&&, (anonymous namespace)::$_12&, WasmEdge_ExecutorContext*, WasmEdge_StoreContext*, WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ExecutorInvoke::$_0, WasmEdge_ExecutorInvoke::$_1, WasmEdge_ExecutorContext, WasmEdge_FunctionInstanceContext const>(WasmEdge_ExecutorInvoke::$_0&&, WasmEdge_ExecutorInvoke::$_1&&, WasmEdge_ExecutorContext*, WasmEdge_FunctionInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddFunction::$_0, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext, WasmEdge_FunctionInstanceContext>(WasmEdge_ModuleInstanceAddFunction::$_0&&, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext*, WasmEdge_FunctionInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddTable::$_0, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext, WasmEdge_TableInstanceContext>(WasmEdge_ModuleInstanceAddTable::$_0&&, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext*, WasmEdge_TableInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddMemory::$_0, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext, WasmEdge_MemoryInstanceContext>(WasmEdge_ModuleInstanceAddMemory::$_0&&, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext*, WasmEdge_MemoryInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_ModuleInstanceAddGlobal::$_0, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext, WasmEdge_GlobalInstanceContext>(WasmEdge_ModuleInstanceAddGlobal::$_0&&, (anonymous namespace)::$_12&, WasmEdge_ModuleInstanceContext*, WasmEdge_GlobalInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_TableInstanceGetData::$_0, WasmEdge_TableInstanceGetData::$_1, WasmEdge_TableInstanceContext const, WasmEdge_Value>(WasmEdge_TableInstanceGetData::$_0&&, WasmEdge_TableInstanceGetData::$_1&&, WasmEdge_TableInstanceContext const*, WasmEdge_Value*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_TableInstanceSetData::$_0, (anonymous namespace)::$_12&, WasmEdge_TableInstanceContext>(WasmEdge_TableInstanceSetData::$_0&&, (anonymous namespace)::$_12&, WasmEdge_TableInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_TableInstanceGrow::$_0, (anonymous namespace)::$_12&, WasmEdge_TableInstanceContext>(WasmEdge_TableInstanceGrow::$_0&&, (anonymous namespace)::$_12&, WasmEdge_TableInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_MemoryInstanceGetData::$_0, WasmEdge_MemoryInstanceGetData::$_1, WasmEdge_MemoryInstanceContext const, unsigned char>(WasmEdge_MemoryInstanceGetData::$_0&&, WasmEdge_MemoryInstanceGetData::$_1&&, WasmEdge_MemoryInstanceContext const*, unsigned char*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_MemoryInstanceSetData::$_0, (anonymous namespace)::$_12&, WasmEdge_MemoryInstanceContext, unsigned char const>(WasmEdge_MemoryInstanceSetData::$_0&&, (anonymous namespace)::$_12&, WasmEdge_MemoryInstanceContext*, unsigned char const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_MemoryInstanceGrowPage::$_0, (anonymous namespace)::$_12&, WasmEdge_MemoryInstanceContext>(WasmEdge_MemoryInstanceGrowPage::$_0&&, (anonymous namespace)::$_12&, WasmEdge_MemoryInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_GlobalInstanceSetValue::$_0, (anonymous namespace)::$_12&, WasmEdge_GlobalInstanceContext>(WasmEdge_GlobalInstanceSetValue::$_0&&, (anonymous namespace)::$_12&, WasmEdge_GlobalInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_AsyncGet::$_0, WasmEdge_AsyncGet::$_1, WasmEdge_Async const>(WasmEdge_AsyncGet::$_0&&, WasmEdge_AsyncGet::$_1&&, WasmEdge_Async const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromFile::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext>(WasmEdge_VMRegisterModuleFromFile::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromBytes::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext>(WasmEdge_VMRegisterModuleFromBytes::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromASTModule::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMRegisterModuleFromASTModule::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromImport::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_VMRegisterModuleFromImport::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*, WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRegisterModuleFromImportWithAlias::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext, WasmEdge_ModuleInstanceContext const>(WasmEdge_VMRegisterModuleFromImportWithAlias::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*, WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRunWasmFromFile::$_0, WasmEdge_VMRunWasmFromFile::$_1, WasmEdge_VMContext>(WasmEdge_VMRunWasmFromFile::$_0&&, WasmEdge_VMRunWasmFromFile::$_1&&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRunWasmFromBytes::$_0, WasmEdge_VMRunWasmFromBytes::$_1, WasmEdge_VMContext>(WasmEdge_VMRunWasmFromBytes::$_0&&, WasmEdge_VMRunWasmFromBytes::$_1&&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMRunWasmFromASTModule::$_0, WasmEdge_VMRunWasmFromASTModule::$_1, WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMRunWasmFromASTModule::$_0&&, WasmEdge_VMRunWasmFromASTModule::$_1&&, WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMLoadWasmFromFile::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext>(WasmEdge_VMLoadWasmFromFile::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMLoadWasmFromBytes::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext>(WasmEdge_VMLoadWasmFromBytes::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMLoadWasmFromASTModule::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext, WasmEdge_ASTModuleContext const>(WasmEdge_VMLoadWasmFromASTModule::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*, WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMValidate::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext>(WasmEdge_VMValidate::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMInstantiate::$_0, (anonymous namespace)::$_12&, WasmEdge_VMContext>(WasmEdge_VMInstantiate::$_0&&, (anonymous namespace)::$_12&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMExecute::$_0, WasmEdge_VMExecute::$_1, WasmEdge_VMContext>(WasmEdge_VMExecute::$_0&&, WasmEdge_VMExecute::$_1&&, WasmEdge_VMContext*)
Unexecuted instantiation: wasmedge.cpp:WasmEdge_Result (anonymous namespace)::wrap<WasmEdge_VMExecuteRegistered::$_0, WasmEdge_VMExecuteRegistered::$_1, WasmEdge_VMContext>(WasmEdge_VMExecuteRegistered::$_0&&, WasmEdge_VMExecuteRegistered::$_1&&, WasmEdge_VMContext*)
333
334
// Helper function for retrieving exported maps.
335
template <typename T>
336
inline uint32_t fillMap(const std::map<std::string, T *, std::less<>> &Map,
337
0
                        WasmEdge_String *Names, const uint32_t Len) noexcept {
338
0
  uint32_t I = 0;
339
0
  for (auto &&Pair : Map) {
340
0
    if (I >= Len) {
341
0
      break;
342
0
    }
343
0
    if (Names) {
344
0
      Names[I] = WasmEdge_String{
345
0
          /* Length */ static_cast<uint32_t>(Pair.first.length()),
346
0
          /* Buf */ Pair.first.data()};
347
0
    }
348
0
    I++;
349
0
  }
350
0
  return static_cast<uint32_t>(Map.size());
351
0
}
Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::ModuleInstance const>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::ModuleInstance const*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::ModuleInstance const*> > > const&, WasmEdge_String*, unsigned int)
Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::FunctionInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::FunctionInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::FunctionInstance*> > > const&, WasmEdge_String*, unsigned int)
Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::TableInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::TableInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::TableInstance*> > > const&, WasmEdge_String*, unsigned int)
Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::MemoryInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::MemoryInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::MemoryInstance*> > > const&, WasmEdge_String*, unsigned int)
Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::TagInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::TagInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::TagInstance*> > > const&, WasmEdge_String*, unsigned int)
Unexecuted instantiation: wasmedge.cpp:unsigned int (anonymous namespace)::fillMap<WasmEdge::Runtime::Instance::GlobalInstance>(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, WasmEdge::Runtime::Instance::GlobalInstance*, std::__1::less<void>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, WasmEdge::Runtime::Instance::GlobalInstance*> > > const&, WasmEdge_String*, unsigned int)
352
353
// Helper functions of context conversions.
354
#define CONVTO(SIMP, INST, NAME, QUANT)                                        \
355
0
  inline QUANT auto *to##SIMP##Cxt(QUANT INST *Cxt) noexcept {                 \
356
0
    return reinterpret_cast<QUANT WasmEdge_##NAME##Context *>(Cxt);            \
357
0
  }
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncCxt(WasmEdge::Runtime::Instance::FunctionInstance const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toStatCxt(WasmEdge::Statistics::Statistics*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toImpTypeCxt(WasmEdge::AST::ImportDesc const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toExpTypeCxt(WasmEdge::AST::ExportDesc const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toLimitCxt(WasmEdge::AST::Limit*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncTypeCxt(WasmEdge::AST::FunctionType*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTabTypeCxt(WasmEdge::AST::TableType*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toLimitCxt(WasmEdge::AST::Limit const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toMemTypeCxt(WasmEdge::AST::MemoryType*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncTypeCxt(WasmEdge::AST::FunctionType const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toGlobTypeCxt(WasmEdge::AST::GlobalType*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTabTypeCxt(WasmEdge::AST::TableType const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toMemTypeCxt(WasmEdge::AST::MemoryType const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTagTypeCxt(WasmEdge::AST::TagType const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toGlobTypeCxt(WasmEdge::AST::GlobalType const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toLoaderCxt(WasmEdge::Loader::Loader*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toValidatorCxt(WasmEdge::Validator::Validator*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toExecutorCxt(WasmEdge::Executor::Executor*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toStoreCxt(WasmEdge::Runtime::StoreManager*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toModCxt(WasmEdge::Runtime::Instance::ModuleInstance const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toModCxt(WasmEdge::Runtime::Instance::ModuleInstance*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toFuncCxt(WasmEdge::Runtime::Instance::FunctionInstance*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTabCxt(WasmEdge::Runtime::Instance::TableInstance*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toMemCxt(WasmEdge::Runtime::Instance::MemoryInstance*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toTagCxt(WasmEdge::Runtime::Instance::TagInstance*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toGlobCxt(WasmEdge::Runtime::Instance::GlobalInstance*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toPluginCxt(WasmEdge::Plugin::Plugin const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toASTModCxt(WasmEdge::AST::Module*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::toCallFrameCxt(WasmEdge::Runtime::CallingFrame const*)
358
CONVTO(Stat, Statistics::Statistics, Statistics, )
359
CONVTO(ASTMod, AST::Module, ASTModule, )
360
CONVTO(Limit, AST::Limit, Limit, )
361
CONVTO(Limit, AST::Limit, Limit, const)
362
CONVTO(FuncType, AST::FunctionType, FunctionType, )
363
CONVTO(FuncType, AST::FunctionType, FunctionType, const)
364
CONVTO(TabType, AST::TableType, TableType, )
365
CONVTO(TabType, AST::TableType, TableType, const)
366
CONVTO(MemType, AST::MemoryType, MemoryType, )
367
CONVTO(MemType, AST::MemoryType, MemoryType, const)
368
CONVTO(TagType, AST::TagType, TagType, const)
369
CONVTO(GlobType, AST::GlobalType, GlobalType, )
370
CONVTO(GlobType, AST::GlobalType, GlobalType, const)
371
CONVTO(ImpType, AST::ImportDesc, ImportType, const)
372
CONVTO(ExpType, AST::ExportDesc, ExportType, const)
373
CONVTO(Store, Runtime::StoreManager, Store, )
374
CONVTO(Loader, Loader::Loader, Loader, )
375
CONVTO(Validator, Validator::Validator, Validator, )
376
CONVTO(Executor, Executor::Executor, Executor, )
377
CONVTO(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, )
378
CONVTO(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, const)
379
CONVTO(Func, Runtime::Instance::FunctionInstance, FunctionInstance, )
380
CONVTO(Func, Runtime::Instance::FunctionInstance, FunctionInstance, const)
381
CONVTO(Tab, Runtime::Instance::TableInstance, TableInstance, )
382
CONVTO(Mem, Runtime::Instance::MemoryInstance, MemoryInstance, )
383
CONVTO(Tag, Runtime::Instance::TagInstance, TagInstance, )
384
CONVTO(Glob, Runtime::Instance::GlobalInstance, GlobalInstance, )
385
CONVTO(CallFrame, Runtime::CallingFrame, CallingFrame, const)
386
CONVTO(Plugin, Plugin::Plugin, Plugin, const)
387
#undef CONVTO
388
389
#define CONVFROM(SIMP, INST, NAME, QUANT)                                      \
390
  inline QUANT auto *from##SIMP##Cxt(                                          \
391
0
      QUANT WasmEdge_##NAME##Context *Cxt) noexcept {                          \
392
0
    return reinterpret_cast<QUANT INST *>(Cxt);                                \
393
0
  }
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncCxt(WasmEdge_FunctionInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStatCxt(WasmEdge_StatisticsContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStatCxt(WasmEdge_StatisticsContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromASTModCxt(WasmEdge_ASTModuleContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromASTModCxt(WasmEdge_ASTModuleContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromLimitCxt(WasmEdge_LimitContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromLimitCxt(WasmEdge_LimitContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncTypeCxt(WasmEdge_FunctionTypeContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncTypeCxt(WasmEdge_FunctionTypeContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabTypeCxt(WasmEdge_TableTypeContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabTypeCxt(WasmEdge_TableTypeContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemTypeCxt(WasmEdge_MemoryTypeContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemTypeCxt(WasmEdge_MemoryTypeContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTagTypeCxt(WasmEdge_TagTypeContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobTypeCxt(WasmEdge_GlobalTypeContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobTypeCxt(WasmEdge_GlobalTypeContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromImpTypeCxt(WasmEdge_ImportTypeContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromExpTypeCxt(WasmEdge_ExportTypeContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromLoaderCxt(WasmEdge_LoaderContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromValidatorCxt(WasmEdge_ValidatorContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromExecutorCxt(WasmEdge_ExecutorContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStoreCxt(WasmEdge_StoreContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromStoreCxt(WasmEdge_StoreContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromModCxt(WasmEdge_ModuleInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromModCxt(WasmEdge_ModuleInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromFuncCxt(WasmEdge_FunctionInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabCxt(WasmEdge_TableInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTabCxt(WasmEdge_TableInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemCxt(WasmEdge_MemoryInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromMemCxt(WasmEdge_MemoryInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromTagCxt(WasmEdge_TagInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobCxt(WasmEdge_GlobalInstanceContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromGlobCxt(WasmEdge_GlobalInstanceContext*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromCallFrameCxt(WasmEdge_CallingFrameContext const*)
Unexecuted instantiation: wasmedge.cpp:(anonymous namespace)::fromPluginCxt(WasmEdge_PluginContext const*)
394
CONVFROM(Stat, Statistics::Statistics, Statistics, )
395
CONVFROM(Stat, Statistics::Statistics, Statistics, const)
396
CONVFROM(ASTMod, AST::Module, ASTModule, )
397
CONVFROM(ASTMod, AST::Module, ASTModule, const)
398
CONVFROM(Limit, AST::Limit, Limit, )
399
CONVFROM(Limit, AST::Limit, Limit, const)
400
CONVFROM(FuncType, AST::FunctionType, FunctionType, )
401
CONVFROM(FuncType, AST::FunctionType, FunctionType, const)
402
CONVFROM(TabType, AST::TableType, TableType, )
403
CONVFROM(TabType, AST::TableType, TableType, const)
404
CONVFROM(MemType, AST::MemoryType, MemoryType, )
405
CONVFROM(MemType, AST::MemoryType, MemoryType, const)
406
CONVFROM(TagType, AST::TagType, TagType, const)
407
CONVFROM(GlobType, AST::GlobalType, GlobalType, )
408
CONVFROM(GlobType, AST::GlobalType, GlobalType, const)
409
CONVFROM(ImpType, AST::ImportDesc, ImportType, const)
410
CONVFROM(ExpType, AST::ExportDesc, ExportType, const)
411
CONVFROM(Store, Runtime::StoreManager, Store, )
412
CONVFROM(Store, Runtime::StoreManager, Store, const)
413
CONVFROM(Loader, Loader::Loader, Loader, )
414
CONVFROM(Validator, Validator::Validator, Validator, )
415
CONVFROM(Executor, Executor::Executor, Executor, )
416
CONVFROM(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, )
417
CONVFROM(Mod, Runtime::Instance::ModuleInstance, ModuleInstance, const)
418
CONVFROM(Func, Runtime::Instance::FunctionInstance, FunctionInstance, )
419
CONVFROM(Func, Runtime::Instance::FunctionInstance, FunctionInstance, const)
420
CONVFROM(Tab, Runtime::Instance::TableInstance, TableInstance, )
421
CONVFROM(Tab, Runtime::Instance::TableInstance, TableInstance, const)
422
CONVFROM(Mem, Runtime::Instance::MemoryInstance, MemoryInstance, )
423
CONVFROM(Mem, Runtime::Instance::MemoryInstance, MemoryInstance, const)
424
CONVFROM(Tag, Runtime::Instance::TagInstance, TagInstance, const)
425
CONVFROM(Glob, Runtime::Instance::GlobalInstance, GlobalInstance, )
426
CONVFROM(Glob, Runtime::Instance::GlobalInstance, GlobalInstance, const)
427
CONVFROM(CallFrame, Runtime::CallingFrame, CallingFrame, const)
428
CONVFROM(Plugin, Plugin::Plugin, Plugin, const)
429
#undef CONVFROM
430
431
// C API Host function class
432
class CAPIHostFunc : public Runtime::HostFunctionBase {
433
public:
434
  CAPIHostFunc(const AST::FunctionType *Type, WasmEdge_HostFunc_t FuncPtr,
435
               void *ExtData, const uint64_t FuncCost = 0) noexcept
436
0
      : Runtime::HostFunctionBase(FuncCost), Func(FuncPtr), Wrap(nullptr),
437
0
        Binding(nullptr), Data(ExtData) {
438
0
    DefType.getCompositeType().getFuncType() = *Type;
439
0
  }
440
  CAPIHostFunc(const AST::FunctionType *Type, WasmEdge_WrapFunc_t WrapPtr,
441
               void *BindingPtr, void *ExtData,
442
               const uint64_t FuncCost = 0) noexcept
443
0
      : Runtime::HostFunctionBase(FuncCost), Func(nullptr), Wrap(WrapPtr),
444
0
        Binding(BindingPtr), Data(ExtData) {
445
0
    DefType.getCompositeType().getFuncType() = *Type;
446
0
  }
447
  ~CAPIHostFunc() noexcept override = default;
448
449
  Expect<void> run(const Runtime::CallingFrame &CallFrame,
450
                   Span<const ValVariant> Args,
451
0
                   Span<ValVariant> Rets) override {
452
0
    auto &FuncType = DefType.getCompositeType().getFuncType();
453
0
    std::vector<WasmEdge_Value> Params(FuncType.getParamTypes().size()),
454
0
        Returns(FuncType.getReturnTypes().size());
455
0
    for (uint32_t I = 0; I < Args.size(); I++) {
456
0
      Params[I] = genWasmEdge_Value(Args[I], FuncType.getParamTypes()[I]);
457
0
    }
458
0
    WasmEdge_Value *PPtr = Params.size() ? (&Params[0]) : nullptr;
459
0
    WasmEdge_Value *RPtr = Returns.size() ? (&Returns[0]) : nullptr;
460
0
    auto *CallFrameCxt = toCallFrameCxt(&CallFrame);
461
0
    WasmEdge_Result Stat;
462
0
    if (Func) {
463
0
      Stat = Func(Data, CallFrameCxt, PPtr, RPtr);
464
0
    } else {
465
0
      Stat = Wrap(Binding, Data, CallFrameCxt, PPtr,
466
0
                  static_cast<uint32_t>(Params.size()), RPtr,
467
0
                  static_cast<uint32_t>(Returns.size()));
468
0
    }
469
0
    for (uint32_t I = 0; I < Rets.size(); I++) {
470
0
      Rets[I] = to_WasmEdge_128_t<WasmEdge::uint128_t>(Returns[I].Value);
471
0
    }
472
0
    if (WasmEdge_ResultOK(Stat)) {
473
0
      if (WasmEdge_ResultGetCode(Stat) == 0x01U) {
474
0
        return Unexpect(ErrCode::Value::Terminated);
475
0
      }
476
0
    } else {
477
0
      return Unexpect(
478
0
          static_cast<ErrCategory>(WasmEdge_ResultGetCategory(Stat)),
479
0
          WasmEdge_ResultGetCode(Stat));
480
0
    }
481
0
    return {};
482
0
  }
483
0
  void *getData() const noexcept { return Data; }
484
485
private:
486
  WasmEdge_HostFunc_t Func;
487
  WasmEdge_WrapFunc_t Wrap;
488
  void *Binding;
489
  void *Data;
490
};
491
492
} // namespace
493
494
#ifdef __cplusplus
495
extern "C" {
496
#endif
497
498
// >>>>>>>> WasmEdge version functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
499
500
0
WASMEDGE_CAPI_EXPORT const char *WasmEdge_VersionGet(void) noexcept {
501
0
  return WASMEDGE_VERSION;
502
0
}
503
504
0
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VersionGetMajor(void) noexcept {
505
0
  return WASMEDGE_VERSION_MAJOR;
506
0
}
507
508
0
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VersionGetMinor(void) noexcept {
509
0
  return WASMEDGE_VERSION_MINOR;
510
0
}
511
512
0
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VersionGetPatch(void) noexcept {
513
0
  return WASMEDGE_VERSION_PATCH;
514
0
}
515
516
// <<<<<<<< WasmEdge version functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
517
518
// >>>>>>>> WasmEdge logging functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
519
520
0
WASMEDGE_CAPI_EXPORT void WasmEdge_LogSetErrorLevel(void) noexcept {
521
0
  WasmEdge::Log::setErrorLoggingLevel();
522
0
}
523
524
0
WASMEDGE_CAPI_EXPORT void WasmEdge_LogSetDebugLevel(void) noexcept {
525
0
  WasmEdge::Log::setDebugLoggingLevel();
526
0
}
527
528
0
WASMEDGE_CAPI_EXPORT void WasmEdge_LogOff(void) noexcept {
529
0
  WasmEdge::Log::setLogOff();
530
0
}
531
532
WASMEDGE_CAPI_EXPORT void
533
0
WasmEdge_LogSetLevel(WasmEdge_LogLevel Level) noexcept {
534
0
  switch (Level) {
535
0
  case WasmEdge_LogLevel_Trace:
536
0
    WasmEdge::Log::setTraceLoggingLevel();
537
0
    break;
538
0
  case WasmEdge_LogLevel_Debug:
539
0
    WasmEdge::Log::setDebugLoggingLevel();
540
0
    break;
541
0
  case WasmEdge_LogLevel_Info:
542
0
    WasmEdge::Log::setInfoLoggingLevel();
543
0
    break;
544
0
  case WasmEdge_LogLevel_Warn:
545
0
    WasmEdge::Log::setWarnLoggingLevel();
546
0
    break;
547
0
  case WasmEdge_LogLevel_Error:
548
0
    WasmEdge::Log::setErrorLoggingLevel();
549
0
    break;
550
0
  case WasmEdge_LogLevel_Critical:
551
0
    WasmEdge::Log::setCriticalLoggingLevel();
552
0
    break;
553
0
  }
554
0
}
555
556
WASMEDGE_CAPI_EXPORT void
557
0
WasmEdge_LogSetCallback(WasmEdge_LogCallback_t Callback) noexcept {
558
0
  if (Callback) {
559
0
    WasmEdge::Log::setLoggingCallback(
560
0
        [Callback](const spdlog::details::log_msg &Msg) {
561
0
          WasmEdge_LogMessage Message;
562
563
0
          Message.Message = WasmEdge_String{
564
0
              /* Length */ static_cast<uint32_t>(Msg.payload.size()),
565
0
              /* Buf */ Msg.payload.data()};
566
0
          Message.LoggerName = WasmEdge_String{
567
0
              /* Length */ static_cast<uint32_t>(Msg.logger_name.size()),
568
0
              /* Buf */ Msg.logger_name.data()};
569
0
          Message.Level = static_cast<WasmEdge_LogLevel>(Msg.level);
570
0
          Message.Time = std::chrono::system_clock::to_time_t(Msg.time);
571
0
          Message.ThreadId = static_cast<uint64_t>(Msg.thread_id);
572
573
0
          Callback(&Message);
574
0
        });
575
0
  } else {
576
0
    WasmEdge::Log::setLoggingCallback({});
577
0
  }
578
0
}
579
580
// <<<<<<<< WasmEdge logging functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
581
582
// >>>>>>>> WasmEdge valtype functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
583
584
0
WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenI32(void) noexcept {
585
0
  return genWasmEdge_ValType(ValType(TypeCode::I32));
586
0
}
587
588
0
WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenI64(void) noexcept {
589
0
  return genWasmEdge_ValType(ValType(TypeCode::I64));
590
0
}
591
592
0
WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenF32(void) noexcept {
593
0
  return genWasmEdge_ValType(ValType(TypeCode::F32));
594
0
}
595
596
0
WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenF64(void) noexcept {
597
0
  return genWasmEdge_ValType(ValType(TypeCode::F64));
598
0
}
599
600
0
WASMEDGE_CAPI_EXPORT WasmEdge_ValType WasmEdge_ValTypeGenV128(void) noexcept {
601
0
  return genWasmEdge_ValType(ValType(TypeCode::V128));
602
0
}
603
604
WASMEDGE_CAPI_EXPORT WasmEdge_ValType
605
0
WasmEdge_ValTypeGenFuncRef(void) noexcept {
606
0
  return genWasmEdge_ValType(ValType(TypeCode::FuncRef));
607
0
}
608
609
WASMEDGE_CAPI_EXPORT WasmEdge_ValType
610
0
WasmEdge_ValTypeGenExternRef(void) noexcept {
611
0
  return genWasmEdge_ValType(ValType(TypeCode::ExternRef));
612
0
}
613
614
WASMEDGE_CAPI_EXPORT bool
615
WasmEdge_ValTypeIsEqual(const WasmEdge_ValType ValType1,
616
0
                        const WasmEdge_ValType ValType2) noexcept {
617
0
  return genValType(ValType1) == genValType(ValType2);
618
0
}
619
620
WASMEDGE_CAPI_EXPORT bool
621
0
WasmEdge_ValTypeIsI32(const WasmEdge_ValType ValType) noexcept {
622
0
  return genValType(ValType).getCode() == WasmEdge::TypeCode::I32;
623
0
}
624
625
WASMEDGE_CAPI_EXPORT bool
626
0
WasmEdge_ValTypeIsI64(const WasmEdge_ValType ValType) noexcept {
627
0
  return genValType(ValType).getCode() == WasmEdge::TypeCode::I64;
628
0
}
629
630
WASMEDGE_CAPI_EXPORT bool
631
0
WasmEdge_ValTypeIsF32(const WasmEdge_ValType ValType) noexcept {
632
0
  return genValType(ValType).getCode() == WasmEdge::TypeCode::F32;
633
0
}
634
635
WASMEDGE_CAPI_EXPORT bool
636
0
WasmEdge_ValTypeIsF64(const WasmEdge_ValType ValType) noexcept {
637
0
  return genValType(ValType).getCode() == WasmEdge::TypeCode::F64;
638
0
}
639
640
WASMEDGE_CAPI_EXPORT bool
641
0
WasmEdge_ValTypeIsV128(const WasmEdge_ValType ValType) noexcept {
642
0
  return genValType(ValType).getCode() == WasmEdge::TypeCode::V128;
643
0
}
644
645
WASMEDGE_CAPI_EXPORT bool
646
0
WasmEdge_ValTypeIsFuncRef(const WasmEdge_ValType ValType) noexcept {
647
0
  return genValType(ValType).isFuncRefType();
648
0
}
649
650
WASMEDGE_CAPI_EXPORT bool
651
0
WasmEdge_ValTypeIsExternRef(const WasmEdge_ValType ValType) noexcept {
652
0
  return genValType(ValType).isExternRefType();
653
0
}
654
655
WASMEDGE_CAPI_EXPORT bool
656
0
WasmEdge_ValTypeIsRef(const WasmEdge_ValType ValType) noexcept {
657
0
  return genValType(ValType).isRefType();
658
0
}
659
660
WASMEDGE_CAPI_EXPORT bool
661
0
WasmEdge_ValTypeIsRefNull(const WasmEdge_ValType ValType) noexcept {
662
0
  return genValType(ValType).isNullableRefType();
663
0
}
664
665
// <<<<<<<< WasmEdge valtype functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
666
667
// >>>>>>>> WasmEdge value functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
668
669
WASMEDGE_CAPI_EXPORT WasmEdge_Value
670
0
WasmEdge_ValueGenI32(const int32_t Val) noexcept {
671
0
  return genWasmEdge_Value(Val);
672
0
}
673
674
WASMEDGE_CAPI_EXPORT WasmEdge_Value
675
0
WasmEdge_ValueGenI64(const int64_t Val) noexcept {
676
0
  return genWasmEdge_Value(Val);
677
0
}
678
679
WASMEDGE_CAPI_EXPORT WasmEdge_Value
680
0
WasmEdge_ValueGenF32(const float Val) noexcept {
681
0
  return genWasmEdge_Value(Val);
682
0
}
683
684
WASMEDGE_CAPI_EXPORT WasmEdge_Value
685
0
WasmEdge_ValueGenF64(const double Val) noexcept {
686
0
  return genWasmEdge_Value(Val);
687
0
}
688
689
WASMEDGE_CAPI_EXPORT WasmEdge_Value
690
0
WasmEdge_ValueGenV128(const ::int128_t Val) noexcept {
691
0
  return genWasmEdge_Value(to_WasmEdge_128_t<WasmEdge::int128_t>(Val));
692
0
}
693
694
WASMEDGE_CAPI_EXPORT WasmEdge_Value
695
0
WasmEdge_ValueGenFuncRef(const WasmEdge_FunctionInstanceContext *Cxt) noexcept {
696
0
  return genWasmEdge_Value(WasmEdge::RefVariant(fromFuncCxt(Cxt)),
697
0
                           TypeCode::FuncRef);
698
0
}
699
700
WASMEDGE_CAPI_EXPORT WasmEdge_Value
701
0
WasmEdge_ValueGenExternRef(void *Ref) noexcept {
702
0
  return genWasmEdge_Value(WasmEdge::RefVariant(Ref), TypeCode::ExternRef);
703
0
}
704
705
WASMEDGE_CAPI_EXPORT int32_t
706
0
WasmEdge_ValueGetI32(const WasmEdge_Value Val) noexcept {
707
0
  return WasmEdge::ValVariant::wrap<int32_t>(
708
0
             to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
709
0
      .get<int32_t>();
710
0
}
711
712
WASMEDGE_CAPI_EXPORT int64_t
713
0
WasmEdge_ValueGetI64(const WasmEdge_Value Val) noexcept {
714
0
  return WasmEdge::ValVariant::wrap<int64_t>(
715
0
             to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
716
0
      .get<int64_t>();
717
0
}
718
719
WASMEDGE_CAPI_EXPORT float
720
0
WasmEdge_ValueGetF32(const WasmEdge_Value Val) noexcept {
721
0
  return WasmEdge::ValVariant::wrap<float>(
722
0
             to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
723
0
      .get<float>();
724
0
}
725
726
WASMEDGE_CAPI_EXPORT double
727
0
WasmEdge_ValueGetF64(const WasmEdge_Value Val) noexcept {
728
0
  return WasmEdge::ValVariant::wrap<double>(
729
0
             to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
730
0
      .get<double>();
731
0
}
732
733
WASMEDGE_CAPI_EXPORT ::int128_t
734
0
WasmEdge_ValueGetV128(const WasmEdge_Value Val) noexcept {
735
0
  return to_int128_t(WasmEdge::ValVariant::wrap<WasmEdge::int128_t>(
736
0
                         to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
737
0
                         .get<WasmEdge::int128_t>());
738
0
}
739
740
WASMEDGE_CAPI_EXPORT bool
741
0
WasmEdge_ValueIsNullRef(const WasmEdge_Value Val) noexcept {
742
0
  return WasmEdge::ValVariant::wrap<WasmEdge::RefVariant>(
743
0
             to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
744
0
      .get<WasmEdge::RefVariant>()
745
0
      .isNull();
746
0
}
747
748
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionInstanceContext *
749
0
WasmEdge_ValueGetFuncRef(const WasmEdge_Value Val) noexcept {
750
0
  return toFuncCxt(WasmEdge::retrieveFuncRef(
751
0
      WasmEdge::ValVariant::wrap<WasmEdge::RefVariant>(
752
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
753
0
          .get<WasmEdge::RefVariant>()
754
0
          .getPtr<WasmEdge::Runtime::Instance::FunctionInstance>()));
755
0
}
756
757
WASMEDGE_CAPI_EXPORT void *
758
0
WasmEdge_ValueGetExternRef(const WasmEdge_Value Val) noexcept {
759
0
  return &WasmEdge::retrieveExternRef<uint32_t>(
760
0
      WasmEdge::ValVariant::wrap<WasmEdge::RefVariant>(
761
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Val.Value))
762
0
          .get<WasmEdge::RefVariant>());
763
0
}
764
765
// <<<<<<<< WasmEdge value functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
766
767
// >>>>>>>> WasmEdge string functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
768
769
WASMEDGE_CAPI_EXPORT WasmEdge_String
770
0
WasmEdge_StringCreateByCString(const char *Str) noexcept {
771
0
  if (Str) {
772
0
    return WasmEdge_StringCreateByBuffer(
773
0
        Str, static_cast<uint32_t>(std::strlen(Str)));
774
0
  }
775
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
776
0
}
777
778
WASMEDGE_CAPI_EXPORT WasmEdge_String
779
0
WasmEdge_StringCreateByBuffer(const char *Buf, const uint32_t Len) noexcept {
780
0
  try {
781
0
    if (Buf && Len) {
782
0
      char *Str = new char[Len];
783
0
      std::copy_n(Buf, Len, Str);
784
0
      return WasmEdge_String{/* Length */ Len, /* Buf */ Str};
785
0
    }
786
0
  } catch (...) {
787
0
    handleCAPIError();
788
0
  }
789
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
790
0
}
791
792
WASMEDGE_CAPI_EXPORT WasmEdge_String
793
0
WasmEdge_StringWrap(const char *Buf, const uint32_t Len) noexcept {
794
0
  return WasmEdge_String{/* Length */ Len, /* Buf */ Buf};
795
0
}
796
797
WASMEDGE_CAPI_EXPORT bool
798
WasmEdge_StringIsEqual(const WasmEdge_String Str1,
799
0
                       const WasmEdge_String Str2) noexcept {
800
0
  if (Str1.Length != Str2.Length) {
801
0
    return false;
802
0
  }
803
0
  return std::equal(Str1.Buf, Str1.Buf + Str1.Length, Str2.Buf);
804
0
}
805
806
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_StringCopy(const WasmEdge_String Str,
807
                                                  char *Buf,
808
0
                                                  const uint32_t Len) noexcept {
809
0
  uint32_t RealLength = std::min(Len, Str.Length);
810
0
  if (Buf) {
811
0
    std::memset(Buf, 0, Len);
812
0
    if (RealLength > 0) {
813
0
      std::copy_n(Str.Buf, RealLength, Buf);
814
0
    }
815
0
  }
816
0
  return RealLength;
817
0
}
818
819
0
WASMEDGE_CAPI_EXPORT void WasmEdge_StringDelete(WasmEdge_String Str) noexcept {
820
0
  if (Str.Buf) {
821
0
    delete[] Str.Buf;
822
0
  }
823
0
}
824
825
// <<<<<<<< WasmEdge string functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
826
827
// >>>>>>>> WasmEdge bytes functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
828
829
WASMEDGE_CAPI_EXPORT WasmEdge_Bytes
830
0
WasmEdge_BytesCreate(const uint8_t *Buf, const uint32_t Len) noexcept {
831
0
  try {
832
0
    if (Buf && Len) {
833
0
      uint8_t *Str = new uint8_t[Len];
834
0
      std::copy_n(Buf, Len, Str);
835
0
      return WasmEdge_Bytes{/* Length */ Len, /* Buf */ Str};
836
0
    }
837
0
  } catch (...) {
838
0
    handleCAPIError();
839
0
  }
840
0
  return WasmEdge_Bytes{/* Length */ 0, /* Buf */ nullptr};
841
0
}
842
843
WASMEDGE_CAPI_EXPORT WasmEdge_Bytes
844
0
WasmEdge_BytesWrap(const uint8_t *Buf, const uint32_t Len) noexcept {
845
0
  return WasmEdge_Bytes{/* Length */ Len, /* Buf */ Buf};
846
0
}
847
848
0
WASMEDGE_CAPI_EXPORT void WasmEdge_BytesDelete(WasmEdge_Bytes Bytes) noexcept {
849
0
  if (Bytes.Buf) {
850
0
    delete[] Bytes.Buf;
851
0
  }
852
0
}
853
854
// <<<<<<<< WasmEdge bytes functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
855
856
// >>>>>>>> WasmEdge result functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
857
858
WASMEDGE_CAPI_EXPORT bool
859
0
WasmEdge_ResultOK(const WasmEdge_Result Res) noexcept {
860
0
  if (WasmEdge_ResultGetCategory(Res) == WasmEdge_ErrCategory_WASM &&
861
0
      (static_cast<WasmEdge::ErrCode::Value>(WasmEdge_ResultGetCode(Res)) ==
862
0
           WasmEdge::ErrCode::Value::Success ||
863
0
       static_cast<WasmEdge::ErrCode::Value>(WasmEdge_ResultGetCode(Res)) ==
864
0
           WasmEdge::ErrCode::Value::Terminated)) {
865
0
    return true;
866
0
  } else {
867
0
    return false;
868
0
  }
869
0
}
870
871
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ResultGen(
872
0
    const enum WasmEdge_ErrCategory Category, const uint32_t Code) noexcept {
873
0
  return WasmEdge_Result{/* Code */ (static_cast<uint32_t>(Category) << 24) +
874
0
                         (Code & 0x00FFFFFFU)};
875
0
}
876
877
WASMEDGE_CAPI_EXPORT uint32_t
878
0
WasmEdge_ResultGetCode(const WasmEdge_Result Res) noexcept {
879
0
  return Res.Code & 0x00FFFFFFU;
880
0
}
881
882
WASMEDGE_CAPI_EXPORT WasmEdge_ErrCategory
883
0
WasmEdge_ResultGetCategory(const WasmEdge_Result Res) noexcept {
884
0
  return static_cast<WasmEdge_ErrCategory>(Res.Code >> 24);
885
0
}
886
887
WASMEDGE_CAPI_EXPORT const char *
888
0
WasmEdge_ResultGetMessage(const WasmEdge_Result Res) noexcept {
889
0
  if (WasmEdge_ResultGetCategory(Res) != WasmEdge_ErrCategory_WASM) {
890
0
    return WasmEdge::ErrCodeStr[WasmEdge::ErrCode::Value::UserDefError].data();
891
0
  }
892
0
  return WasmEdge::ErrCodeStr[static_cast<WasmEdge::ErrCode::Value>(
893
0
                                  WasmEdge_ResultGetCode(Res))]
894
0
      .data();
895
0
}
896
897
// <<<<<<<< WasmEdge result functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
898
899
// >>>>>>>> WasmEdge configure functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
900
901
WASMEDGE_CAPI_EXPORT WasmEdge_ConfigureContext *
902
0
WasmEdge_ConfigureCreate(void) noexcept {
903
0
  try {
904
0
    return new WasmEdge_ConfigureContext;
905
0
  } catch (...) {
906
0
    handleCAPIError();
907
0
    return nullptr;
908
0
  }
909
0
}
910
911
WASMEDGE_CAPI_EXPORT void
912
WasmEdge_ConfigureAddProposal(WasmEdge_ConfigureContext *Cxt,
913
0
                              const enum WasmEdge_Proposal Prop) noexcept {
914
0
  if (Cxt) {
915
0
    Cxt->Conf.addProposal(static_cast<WasmEdge::Proposal>(Prop));
916
0
  }
917
0
}
918
919
WASMEDGE_CAPI_EXPORT void
920
WasmEdge_ConfigureRemoveProposal(WasmEdge_ConfigureContext *Cxt,
921
0
                                 const enum WasmEdge_Proposal Prop) noexcept {
922
0
  if (Cxt) {
923
0
    Cxt->Conf.removeProposal(static_cast<WasmEdge::Proposal>(Prop));
924
0
  }
925
0
}
926
927
WASMEDGE_CAPI_EXPORT bool
928
WasmEdge_ConfigureHasProposal(const WasmEdge_ConfigureContext *Cxt,
929
0
                              const enum WasmEdge_Proposal Prop) noexcept {
930
0
  if (Cxt) {
931
0
    return Cxt->Conf.hasProposal(static_cast<WasmEdge::Proposal>(Prop));
932
0
  }
933
0
  return false;
934
0
}
935
936
WASMEDGE_CAPI_EXPORT void
937
WasmEdge_ConfigureSetWASMStandard(WasmEdge_ConfigureContext *Cxt,
938
0
                                  const enum WasmEdge_Standard Std) noexcept {
939
0
  if (Cxt) {
940
0
    Cxt->Conf.setWASMStandard(static_cast<WasmEdge::Standard>(Std));
941
0
  }
942
0
}
943
944
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureAddHostRegistration(
945
    WasmEdge_ConfigureContext *Cxt,
946
0
    const enum WasmEdge_HostRegistration Host) noexcept {
947
0
  if (Cxt) {
948
0
    Cxt->Conf.addHostRegistration(
949
0
        static_cast<WasmEdge::HostRegistration>(Host));
950
0
  }
951
0
}
952
953
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureRemoveHostRegistration(
954
    WasmEdge_ConfigureContext *Cxt,
955
0
    const enum WasmEdge_HostRegistration Host) noexcept {
956
0
  if (Cxt) {
957
0
    Cxt->Conf.removeHostRegistration(
958
0
        static_cast<WasmEdge::HostRegistration>(Host));
959
0
  }
960
0
}
961
962
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureHasHostRegistration(
963
    const WasmEdge_ConfigureContext *Cxt,
964
0
    const enum WasmEdge_HostRegistration Host) noexcept {
965
0
  if (Cxt) {
966
0
    return Cxt->Conf.hasHostRegistration(
967
0
        static_cast<WasmEdge::HostRegistration>(Host));
968
0
  }
969
0
  return false;
970
0
}
971
972
WASMEDGE_CAPI_EXPORT void
973
WasmEdge_ConfigureSetMaxMemoryPage(WasmEdge_ConfigureContext *Cxt,
974
0
                                   const uint64_t Page) noexcept {
975
0
  if (Cxt) {
976
0
    Cxt->Conf.getRuntimeConfigure().setMaxMemoryPage(Page);
977
0
  }
978
0
}
979
980
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_ConfigureGetMaxMemoryPage(
981
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
982
0
  if (Cxt) {
983
0
    return Cxt->Conf.getRuntimeConfigure().getMaxMemoryPage();
984
0
  }
985
0
  return 0;
986
0
}
987
988
WASMEDGE_CAPI_EXPORT void
989
WasmEdge_ConfigureSetRunMode(WasmEdge_ConfigureContext *Cxt,
990
0
                             const enum WasmEdge_RunMode Mode) noexcept {
991
0
  if (Cxt) {
992
0
    Cxt->Conf.getRuntimeConfigure().setRunMode(
993
0
        static_cast<WasmEdge::RunMode>(Mode));
994
0
  }
995
0
}
996
997
WASMEDGE_CAPI_EXPORT enum WasmEdge_RunMode
998
0
WasmEdge_ConfigureGetRunMode(const WasmEdge_ConfigureContext *Cxt) noexcept {
999
0
  if (Cxt) {
1000
0
    return static_cast<WasmEdge_RunMode>(
1001
0
        Cxt->Conf.getRuntimeConfigure().getRunMode());
1002
0
  }
1003
0
  return WasmEdge_RunMode_Interpreter;
1004
0
}
1005
1006
WASMEDGE_CAPI_EXPORT void
1007
WasmEdge_ConfigureSetForceInterpreter(WasmEdge_ConfigureContext *Cxt,
1008
0
                                      const bool IsForceInterpreter) noexcept {
1009
0
  if (Cxt) {
1010
0
    if (IsForceInterpreter) {
1011
0
      Cxt->Conf.getRuntimeConfigure().setRunMode(
1012
0
          WasmEdge::RunMode::Interpreter);
1013
0
    }
1014
    // Passing `false` is a no-op, preserving the historical "don't force"
1015
    // semantic.
1016
0
  }
1017
0
}
1018
1019
WASMEDGE_CAPI_EXPORT void
1020
WasmEdge_ConfigureSetAllowAFUNIX(WasmEdge_ConfigureContext *Cxt,
1021
0
                                 const bool EnableAFUNIX) noexcept {
1022
0
  if (Cxt) {
1023
0
    Cxt->Conf.getRuntimeConfigure().setAllowAFUNIX(EnableAFUNIX);
1024
0
  }
1025
0
}
1026
1027
WASMEDGE_CAPI_EXPORT bool
1028
0
WasmEdge_ConfigureIsAllowAFUNIX(const WasmEdge_ConfigureContext *Cxt) noexcept {
1029
0
  if (Cxt) {
1030
0
    return Cxt->Conf.getRuntimeConfigure().isAllowAFUNIX();
1031
0
  }
1032
0
  return false;
1033
0
}
1034
1035
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureIsForceInterpreter(
1036
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1037
0
  if (Cxt) {
1038
0
    return Cxt->Conf.getRuntimeConfigure().getRunMode() ==
1039
0
           WasmEdge::RunMode::Interpreter;
1040
0
  }
1041
0
  return false;
1042
0
}
1043
1044
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetOptimizationLevel(
1045
    WasmEdge_ConfigureContext *Cxt,
1046
0
    const enum WasmEdge_CompilerOptimizationLevel Level) noexcept {
1047
0
  if (Cxt) {
1048
0
    Cxt->Conf.getCompilerConfigure().setOptimizationLevel(
1049
0
        static_cast<WasmEdge::CompilerConfigure::OptimizationLevel>(Level));
1050
0
  }
1051
0
}
1052
1053
WASMEDGE_CAPI_EXPORT enum WasmEdge_CompilerOptimizationLevel
1054
WasmEdge_ConfigureCompilerGetOptimizationLevel(
1055
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1056
0
  if (Cxt) {
1057
0
    return static_cast<WasmEdge_CompilerOptimizationLevel>(
1058
0
        Cxt->Conf.getCompilerConfigure().getOptimizationLevel());
1059
0
  }
1060
0
  return WasmEdge_CompilerOptimizationLevel_O0;
1061
0
}
1062
1063
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetOutputFormat(
1064
    WasmEdge_ConfigureContext *Cxt,
1065
0
    const enum WasmEdge_CompilerOutputFormat Format) noexcept {
1066
0
  if (Cxt) {
1067
0
    Cxt->Conf.getCompilerConfigure().setOutputFormat(
1068
0
        static_cast<WasmEdge::CompilerConfigure::OutputFormat>(Format));
1069
0
  }
1070
0
}
1071
1072
WASMEDGE_CAPI_EXPORT enum WasmEdge_CompilerOutputFormat
1073
WasmEdge_ConfigureCompilerGetOutputFormat(
1074
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1075
0
  if (Cxt) {
1076
0
    return static_cast<WasmEdge_CompilerOutputFormat>(
1077
0
        Cxt->Conf.getCompilerConfigure().getOutputFormat());
1078
0
  }
1079
0
  return WasmEdge_CompilerOutputFormat_Wasm;
1080
0
}
1081
1082
WASMEDGE_CAPI_EXPORT void
1083
WasmEdge_ConfigureCompilerSetDumpIR(WasmEdge_ConfigureContext *Cxt,
1084
0
                                    const bool IsDump) noexcept {
1085
0
  if (Cxt) {
1086
0
    Cxt->Conf.getCompilerConfigure().setDumpIR(IsDump);
1087
0
  }
1088
0
}
1089
1090
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsDumpIR(
1091
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1092
0
  if (Cxt) {
1093
0
    return Cxt->Conf.getCompilerConfigure().isDumpIR();
1094
0
  }
1095
0
  return false;
1096
0
}
1097
1098
WASMEDGE_CAPI_EXPORT void
1099
WasmEdge_ConfigureCompilerSetGenericBinary(WasmEdge_ConfigureContext *Cxt,
1100
0
                                           const bool IsGeneric) noexcept {
1101
0
  if (Cxt) {
1102
0
    Cxt->Conf.getCompilerConfigure().setGenericBinary(IsGeneric);
1103
0
  }
1104
0
}
1105
1106
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsGenericBinary(
1107
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1108
0
  if (Cxt) {
1109
0
    return Cxt->Conf.getCompilerConfigure().isGenericBinary();
1110
0
  }
1111
0
  return false;
1112
0
}
1113
1114
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetInterruptible(
1115
0
    WasmEdge_ConfigureContext *Cxt, const bool IsInterruptible) noexcept {
1116
0
  if (Cxt) {
1117
0
    Cxt->Conf.getCompilerConfigure().setInterruptible(IsInterruptible);
1118
0
  }
1119
0
}
1120
1121
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsInterruptible(
1122
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1123
0
  if (Cxt) {
1124
0
    return Cxt->Conf.getCompilerConfigure().isInterruptible();
1125
0
  }
1126
0
  return false;
1127
0
}
1128
1129
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureStatisticsSetInstructionCounting(
1130
0
    WasmEdge_ConfigureContext *Cxt, const bool IsCount) noexcept {
1131
0
  if (Cxt) {
1132
0
    Cxt->Conf.getStatisticsConfigure().setInstructionCounting(IsCount);
1133
0
  }
1134
0
}
1135
1136
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsInstructionCounting(
1137
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1138
0
  if (Cxt) {
1139
0
    return Cxt->Conf.getStatisticsConfigure().isInstructionCounting();
1140
0
  }
1141
0
  return false;
1142
0
}
1143
1144
WASMEDGE_CAPI_EXPORT void
1145
WasmEdge_ConfigureStatisticsSetCostMeasuring(WasmEdge_ConfigureContext *Cxt,
1146
0
                                             const bool IsMeasure) noexcept {
1147
0
  if (Cxt) {
1148
0
    Cxt->Conf.getStatisticsConfigure().setCostMeasuring(IsMeasure);
1149
0
  }
1150
0
}
1151
1152
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsCostMeasuring(
1153
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1154
0
  if (Cxt) {
1155
0
    return Cxt->Conf.getStatisticsConfigure().isCostMeasuring();
1156
0
  }
1157
0
  return false;
1158
0
}
1159
1160
WASMEDGE_CAPI_EXPORT void
1161
WasmEdge_ConfigureStatisticsSetTimeMeasuring(WasmEdge_ConfigureContext *Cxt,
1162
0
                                             const bool IsMeasure) noexcept {
1163
0
  if (Cxt) {
1164
0
    Cxt->Conf.getStatisticsConfigure().setTimeMeasuring(IsMeasure);
1165
0
  }
1166
0
}
1167
1168
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsTimeMeasuring(
1169
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1170
0
  if (Cxt) {
1171
0
    return Cxt->Conf.getStatisticsConfigure().isTimeMeasuring();
1172
0
  }
1173
0
  return false;
1174
0
}
1175
1176
WASMEDGE_CAPI_EXPORT void
1177
0
WasmEdge_ConfigureDelete(WasmEdge_ConfigureContext *Cxt) noexcept {
1178
0
  delete Cxt;
1179
0
}
1180
1181
// <<<<<<<< WasmEdge configure functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1182
1183
// >>>>>>>> WasmEdge statistics functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1184
1185
WASMEDGE_CAPI_EXPORT WasmEdge_StatisticsContext *
1186
0
WasmEdge_StatisticsCreate(void) noexcept {
1187
0
  try {
1188
0
    return toStatCxt(new WasmEdge::Statistics::Statistics);
1189
0
  } catch (...) {
1190
0
    handleCAPIError();
1191
0
    return nullptr;
1192
0
  }
1193
0
}
1194
1195
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_StatisticsGetInstrCount(
1196
0
    const WasmEdge_StatisticsContext *Cxt) noexcept {
1197
0
  if (Cxt) {
1198
0
    return fromStatCxt(Cxt)->getInstrCount();
1199
0
  }
1200
0
  return 0;
1201
0
}
1202
1203
WASMEDGE_CAPI_EXPORT double WasmEdge_StatisticsGetInstrPerSecond(
1204
0
    const WasmEdge_StatisticsContext *Cxt) noexcept {
1205
0
  if (Cxt) {
1206
0
    return fromStatCxt(Cxt)->getInstrPerSecond();
1207
0
  }
1208
0
  return 0.0;
1209
0
}
1210
1211
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_StatisticsGetTotalCost(
1212
0
    const WasmEdge_StatisticsContext *Cxt) noexcept {
1213
0
  if (Cxt) {
1214
0
    return fromStatCxt(Cxt)->getTotalCost();
1215
0
  }
1216
0
  return 0;
1217
0
}
1218
1219
WASMEDGE_CAPI_EXPORT void
1220
WasmEdge_StatisticsSetCostTable(WasmEdge_StatisticsContext *Cxt,
1221
                                uint64_t *CostArr,
1222
0
                                const uint32_t Len) noexcept {
1223
0
  if (Cxt) {
1224
0
    fromStatCxt(Cxt)->setCostTable(genSpan(CostArr, Len));
1225
0
  }
1226
0
}
1227
1228
WASMEDGE_CAPI_EXPORT void
1229
WasmEdge_StatisticsSetCostLimit(WasmEdge_StatisticsContext *Cxt,
1230
0
                                const uint64_t Limit) noexcept {
1231
0
  if (Cxt) {
1232
0
    fromStatCxt(Cxt)->setCostLimit(Limit);
1233
0
  }
1234
0
}
1235
1236
WASMEDGE_CAPI_EXPORT void
1237
0
WasmEdge_StatisticsClear(WasmEdge_StatisticsContext *Cxt) noexcept {
1238
0
  if (Cxt) {
1239
0
    fromStatCxt(Cxt)->clear();
1240
0
  }
1241
0
}
1242
1243
WASMEDGE_CAPI_EXPORT void
1244
0
WasmEdge_StatisticsDelete(WasmEdge_StatisticsContext *Cxt) noexcept {
1245
0
  delete fromStatCxt(Cxt);
1246
0
}
1247
1248
// <<<<<<<< WasmEdge statistics functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1249
1250
// >>>>>>>> WasmEdge AST module functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1251
1252
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListImportsLength(
1253
0
    const WasmEdge_ASTModuleContext *Cxt) noexcept {
1254
0
  if (Cxt) {
1255
0
    return static_cast<uint32_t>(
1256
0
        fromASTModCxt(Cxt)->getImportSection().getContent().size());
1257
0
  }
1258
0
  return 0;
1259
0
}
1260
1261
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListImports(
1262
    const WasmEdge_ASTModuleContext *Cxt,
1263
0
    const WasmEdge_ImportTypeContext **Imports, const uint32_t Len) noexcept {
1264
0
  if (Cxt) {
1265
0
    const auto &ImpSec = fromASTModCxt(Cxt)->getImportSection().getContent();
1266
0
    if (Imports) {
1267
0
      for (uint32_t I = 0; I < Len && I < ImpSec.size(); I++) {
1268
0
        Imports[I] = toImpTypeCxt(&ImpSec[I]);
1269
0
      }
1270
0
    }
1271
0
    return static_cast<uint32_t>(ImpSec.size());
1272
0
  }
1273
0
  return 0;
1274
0
}
1275
1276
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListExportsLength(
1277
0
    const WasmEdge_ASTModuleContext *Cxt) noexcept {
1278
0
  if (Cxt) {
1279
0
    return static_cast<uint32_t>(
1280
0
        fromASTModCxt(Cxt)->getExportSection().getContent().size());
1281
0
  }
1282
0
  return 0;
1283
0
}
1284
1285
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListExports(
1286
    const WasmEdge_ASTModuleContext *Cxt,
1287
0
    const WasmEdge_ExportTypeContext **Exports, const uint32_t Len) noexcept {
1288
0
  if (Cxt) {
1289
0
    const auto &ExpSec = fromASTModCxt(Cxt)->getExportSection().getContent();
1290
0
    if (Exports) {
1291
0
      for (uint32_t I = 0; I < Len && I < ExpSec.size(); I++) {
1292
0
        Exports[I] = toExpTypeCxt(&ExpSec[I]);
1293
0
      }
1294
0
    }
1295
0
    return static_cast<uint32_t>(ExpSec.size());
1296
0
  }
1297
0
  return 0;
1298
0
}
1299
1300
WASMEDGE_CAPI_EXPORT void
1301
0
WasmEdge_ASTModuleDelete(WasmEdge_ASTModuleContext *Cxt) noexcept {
1302
0
  std::unique_ptr<WasmEdge::AST::Module> Own(fromASTModCxt(Cxt));
1303
0
}
1304
1305
// <<<<<<<< WasmEdge AST module functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1306
1307
// >>>>>>>> WasmEdge limit functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1308
1309
WASMEDGE_CAPI_EXPORT WasmEdge_LimitContext *
1310
0
WasmEdge_LimitCreate(const uint64_t Min, const bool Is64Bit) noexcept {
1311
0
  try {
1312
0
    return toLimitCxt(new WasmEdge::AST::Limit(Min, Is64Bit));
1313
0
  } catch (...) {
1314
0
    handleCAPIError();
1315
0
    return nullptr;
1316
0
  }
1317
0
}
1318
1319
WASMEDGE_CAPI_EXPORT WasmEdge_LimitContext *
1320
WasmEdge_LimitCreateWithMax(const uint64_t Min, const uint64_t Max,
1321
0
                            const bool Is64Bit, const bool IsShared) noexcept {
1322
0
  try {
1323
0
    return toLimitCxt(new WasmEdge::AST::Limit(Min, Max, Is64Bit, IsShared));
1324
0
  } catch (...) {
1325
0
    handleCAPIError();
1326
0
    return nullptr;
1327
0
  }
1328
0
}
1329
1330
WASMEDGE_CAPI_EXPORT uint64_t
1331
0
WasmEdge_LimitGetMin(const WasmEdge_LimitContext *Cxt) noexcept {
1332
0
  if (Cxt) {
1333
0
    return fromLimitCxt(Cxt)->getMin();
1334
0
  }
1335
0
  return 0;
1336
0
}
1337
1338
WASMEDGE_CAPI_EXPORT uint64_t
1339
0
WasmEdge_LimitGetMax(const WasmEdge_LimitContext *Cxt) noexcept {
1340
0
  if (Cxt) {
1341
0
    return fromLimitCxt(Cxt)->getMax();
1342
0
  }
1343
0
  return 0;
1344
0
}
1345
1346
WASMEDGE_CAPI_EXPORT bool
1347
0
WasmEdge_LimitHasMax(const WasmEdge_LimitContext *Cxt) noexcept {
1348
0
  if (Cxt) {
1349
0
    return fromLimitCxt(Cxt)->hasMax();
1350
0
  }
1351
0
  return false;
1352
0
}
1353
1354
WASMEDGE_CAPI_EXPORT bool
1355
0
WasmEdge_LimitIsShared(const WasmEdge_LimitContext *Cxt) noexcept {
1356
0
  if (Cxt) {
1357
0
    return fromLimitCxt(Cxt)->isShared();
1358
0
  }
1359
0
  return false;
1360
0
}
1361
1362
WASMEDGE_CAPI_EXPORT bool
1363
0
WasmEdge_LimitIs64Bit(const WasmEdge_LimitContext *Cxt) noexcept {
1364
0
  if (Cxt) {
1365
0
    return fromLimitCxt(Cxt)->is64();
1366
0
  }
1367
0
  return false;
1368
0
}
1369
1370
WASMEDGE_CAPI_EXPORT bool
1371
WasmEdge_LimitIsEqual(const WasmEdge_LimitContext *Cxt1,
1372
0
                      const WasmEdge_LimitContext *Cxt2) noexcept {
1373
0
  if (Cxt1 && Cxt2) {
1374
0
    const auto *Lim1 = fromLimitCxt(Cxt1);
1375
0
    const auto *Lim2 = fromLimitCxt(Cxt2);
1376
0
    if (Lim1->hasMax() && Lim2->hasMax()) {
1377
0
      return Lim1->getMin() == Lim2->getMin() &&
1378
0
             Lim1->getMax() == Lim2->getMax() &&
1379
0
             Lim1->getAddrType() == Lim2->getAddrType() &&
1380
0
             Lim1->isShared() == Lim2->isShared();
1381
0
    } else {
1382
0
      return Lim1->hasMax() == Lim2->hasMax() &&
1383
0
             Lim1->getMin() == Lim2->getMin() &&
1384
0
             Lim1->getAddrType() == Lim2->getAddrType() &&
1385
0
             Lim1->isShared() == Lim2->isShared();
1386
0
    }
1387
0
  }
1388
0
  return false;
1389
0
}
1390
1391
WASMEDGE_CAPI_EXPORT void
1392
0
WasmEdge_LimitDelete(WasmEdge_LimitContext *Cxt) noexcept {
1393
0
  delete fromLimitCxt(Cxt);
1394
0
}
1395
1396
// <<<<<<<< WasmEdge limit functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1397
1398
// >>>>>>>> WasmEdge function type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1399
1400
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionTypeContext *WasmEdge_FunctionTypeCreate(
1401
    const WasmEdge_ValType *ParamList, const uint32_t ParamLen,
1402
0
    const WasmEdge_ValType *ReturnList, const uint32_t ReturnLen) noexcept {
1403
0
  try {
1404
0
    auto *Cxt = new WasmEdge::AST::FunctionType;
1405
0
    if (ParamLen > 0) {
1406
0
      Cxt->getParamTypes().resize(ParamLen);
1407
0
    }
1408
0
    for (uint32_t I = 0; I < ParamLen; I++) {
1409
0
      Cxt->getParamTypes()[I] = genValType(ParamList[I]);
1410
0
    }
1411
0
    if (ReturnLen > 0) {
1412
0
      Cxt->getReturnTypes().resize(ReturnLen);
1413
0
    }
1414
0
    for (uint32_t I = 0; I < ReturnLen; I++) {
1415
0
      Cxt->getReturnTypes()[I] = genValType(ReturnList[I]);
1416
0
    }
1417
0
    return toFuncTypeCxt(Cxt);
1418
0
  } catch (...) {
1419
0
    handleCAPIError();
1420
0
    return nullptr;
1421
0
  }
1422
0
}
1423
1424
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetParametersLength(
1425
0
    const WasmEdge_FunctionTypeContext *Cxt) noexcept {
1426
0
  if (Cxt) {
1427
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getParamTypes().size());
1428
0
  }
1429
0
  return 0;
1430
0
}
1431
1432
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetParameters(
1433
    const WasmEdge_FunctionTypeContext *Cxt, WasmEdge_ValType *List,
1434
0
    const uint32_t Len) noexcept {
1435
0
  if (Cxt) {
1436
0
    for (uint32_t I = 0;
1437
0
         I < fromFuncTypeCxt(Cxt)->getParamTypes().size() && I < Len; I++) {
1438
0
      List[I] = genWasmEdge_ValType(fromFuncTypeCxt(Cxt)->getParamTypes()[I]);
1439
0
    }
1440
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getParamTypes().size());
1441
0
  }
1442
0
  return 0;
1443
0
}
1444
1445
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetReturnsLength(
1446
0
    const WasmEdge_FunctionTypeContext *Cxt) noexcept {
1447
0
  if (Cxt) {
1448
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getReturnTypes().size());
1449
0
  }
1450
0
  return 0;
1451
0
}
1452
1453
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetReturns(
1454
    const WasmEdge_FunctionTypeContext *Cxt, WasmEdge_ValType *List,
1455
0
    const uint32_t Len) noexcept {
1456
0
  if (Cxt) {
1457
0
    for (uint32_t I = 0;
1458
0
         I < fromFuncTypeCxt(Cxt)->getReturnTypes().size() && I < Len; I++) {
1459
0
      List[I] = genWasmEdge_ValType(fromFuncTypeCxt(Cxt)->getReturnTypes()[I]);
1460
0
    }
1461
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getReturnTypes().size());
1462
0
  }
1463
0
  return 0;
1464
0
}
1465
1466
WASMEDGE_CAPI_EXPORT void
1467
0
WasmEdge_FunctionTypeDelete(WasmEdge_FunctionTypeContext *Cxt) noexcept {
1468
0
  delete fromFuncTypeCxt(Cxt);
1469
0
}
1470
1471
// <<<<<<<< WasmEdge function type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1472
1473
// >>>>>>>> WasmEdge table type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1474
1475
WASMEDGE_CAPI_EXPORT WasmEdge_TableTypeContext *
1476
WasmEdge_TableTypeCreate(const WasmEdge_ValType RefType,
1477
0
                         const WasmEdge_LimitContext *Limit) noexcept {
1478
0
  try {
1479
0
    WasmEdge::ValType RT = genValType(RefType);
1480
0
    if (Limit && RT.isRefType()) {
1481
0
      return toTabTypeCxt(
1482
0
          new WasmEdge::AST::TableType(RT, *fromLimitCxt(Limit)));
1483
0
    }
1484
0
  } catch (...) {
1485
0
    handleCAPIError();
1486
0
  }
1487
0
  return nullptr;
1488
0
}
1489
1490
WASMEDGE_CAPI_EXPORT WasmEdge_ValType
1491
0
WasmEdge_TableTypeGetRefType(const WasmEdge_TableTypeContext *Cxt) noexcept {
1492
0
  if (Cxt) {
1493
0
    return genWasmEdge_ValType(fromTabTypeCxt(Cxt)->getRefType());
1494
0
  }
1495
0
  return WasmEdge_ValTypeGenFuncRef();
1496
0
}
1497
1498
WASMEDGE_CAPI_EXPORT const WasmEdge_LimitContext *
1499
0
WasmEdge_TableTypeGetLimit(const WasmEdge_TableTypeContext *Cxt) noexcept {
1500
0
  if (Cxt) {
1501
0
    return toLimitCxt(&fromTabTypeCxt(Cxt)->getLimit());
1502
0
  }
1503
0
  return nullptr;
1504
0
}
1505
1506
WASMEDGE_CAPI_EXPORT void
1507
0
WasmEdge_TableTypeDelete(WasmEdge_TableTypeContext *Cxt) noexcept {
1508
0
  delete fromTabTypeCxt(Cxt);
1509
0
}
1510
1511
// <<<<<<<< WasmEdge table type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1512
1513
// >>>>>>>> WasmEdge memory type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1514
1515
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryTypeContext *
1516
0
WasmEdge_MemoryTypeCreate(const WasmEdge_LimitContext *Limit) noexcept {
1517
0
  try {
1518
0
    if (Limit) {
1519
0
      return toMemTypeCxt(new WasmEdge::AST::MemoryType(*fromLimitCxt(Limit)));
1520
0
    }
1521
0
  } catch (...) {
1522
0
    handleCAPIError();
1523
0
  }
1524
0
  return nullptr;
1525
0
}
1526
1527
WASMEDGE_CAPI_EXPORT const WasmEdge_LimitContext *
1528
0
WasmEdge_MemoryTypeGetLimit(const WasmEdge_MemoryTypeContext *Cxt) noexcept {
1529
0
  if (Cxt) {
1530
0
    return toLimitCxt(&fromMemTypeCxt(Cxt)->getLimit());
1531
0
  }
1532
0
  return nullptr;
1533
0
}
1534
1535
WASMEDGE_CAPI_EXPORT void
1536
0
WasmEdge_MemoryTypeDelete(WasmEdge_MemoryTypeContext *Cxt) noexcept {
1537
0
  delete fromMemTypeCxt(Cxt);
1538
0
}
1539
1540
// <<<<<<<< WasmEdge memory type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1541
1542
// >>>>>>>> WasmEdge tag type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1543
1544
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
1545
0
WasmEdge_TagTypeGetFunctionType(const WasmEdge_TagTypeContext *Cxt) noexcept {
1546
0
  if (Cxt) {
1547
0
    const auto &CompType = fromTagTypeCxt(Cxt)->getDefType().getCompositeType();
1548
0
    if (CompType.isFunc()) {
1549
0
      return toFuncTypeCxt(&CompType.getFuncType());
1550
0
    }
1551
0
  }
1552
0
  return nullptr;
1553
0
}
1554
1555
// <<<<<<<< WasmEdge tag type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1556
1557
// >>>>>>>> WasmEdge global type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1558
1559
WASMEDGE_CAPI_EXPORT WasmEdge_GlobalTypeContext *
1560
WasmEdge_GlobalTypeCreate(const WasmEdge_ValType ValType,
1561
0
                          const enum WasmEdge_Mutability Mut) noexcept {
1562
0
  try {
1563
0
    return toGlobTypeCxt(new WasmEdge::AST::GlobalType(
1564
0
        genValType(ValType), static_cast<WasmEdge::ValMut>(Mut)));
1565
0
  } catch (...) {
1566
0
    handleCAPIError();
1567
0
    return nullptr;
1568
0
  }
1569
0
}
1570
1571
WASMEDGE_CAPI_EXPORT WasmEdge_ValType
1572
0
WasmEdge_GlobalTypeGetValType(const WasmEdge_GlobalTypeContext *Cxt) noexcept {
1573
0
  if (Cxt) {
1574
0
    return genWasmEdge_ValType(fromGlobTypeCxt(Cxt)->getValType());
1575
0
  }
1576
0
  return WasmEdge_ValTypeGenI32();
1577
0
}
1578
1579
WASMEDGE_CAPI_EXPORT enum WasmEdge_Mutability WasmEdge_GlobalTypeGetMutability(
1580
0
    const WasmEdge_GlobalTypeContext *Cxt) noexcept {
1581
0
  if (Cxt) {
1582
0
    return static_cast<WasmEdge_Mutability>(fromGlobTypeCxt(Cxt)->getValMut());
1583
0
  }
1584
0
  return WasmEdge_Mutability_Const;
1585
0
}
1586
1587
WASMEDGE_CAPI_EXPORT void
1588
0
WasmEdge_GlobalTypeDelete(WasmEdge_GlobalTypeContext *Cxt) noexcept {
1589
0
  delete fromGlobTypeCxt(Cxt);
1590
0
}
1591
1592
// <<<<<<<< WasmEdge global type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1593
1594
// >>>>>>>> WasmEdge import type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1595
1596
WASMEDGE_CAPI_EXPORT enum WasmEdge_ExternalType
1597
WasmEdge_ImportTypeGetExternalType(
1598
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1599
0
  if (Cxt) {
1600
0
    return static_cast<WasmEdge_ExternalType>(
1601
0
        fromImpTypeCxt(Cxt)->getExternalType());
1602
0
  }
1603
0
  return WasmEdge_ExternalType_Function;
1604
0
}
1605
1606
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ImportTypeGetModuleName(
1607
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1608
0
  if (Cxt) {
1609
0
    auto StrView = fromImpTypeCxt(Cxt)->getModuleName();
1610
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
1611
0
                           /* Buf */ StrView.data()};
1612
0
  }
1613
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
1614
0
}
1615
1616
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ImportTypeGetExternalName(
1617
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1618
0
  if (Cxt) {
1619
0
    auto StrView = fromImpTypeCxt(Cxt)->getExternalName();
1620
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
1621
0
                           /* Buf */ StrView.data()};
1622
0
  }
1623
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
1624
0
}
1625
1626
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
1627
WasmEdge_ImportTypeGetFunctionType(
1628
    const WasmEdge_ASTModuleContext *ASTCxt,
1629
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1630
0
  if (ASTCxt && Cxt &&
1631
0
      fromImpTypeCxt(Cxt)->getExternalType() ==
1632
0
          WasmEdge::ExternalType::Function) {
1633
0
    uint32_t Idx = fromImpTypeCxt(Cxt)->getExternalFuncTypeIdx();
1634
0
    auto SubTypes = fromASTModCxt(ASTCxt)->getTypeSection().getContent();
1635
0
    if (Idx < SubTypes.size() && SubTypes[Idx].getCompositeType().isFunc()) {
1636
0
      return toFuncTypeCxt(&(SubTypes[Idx].getCompositeType().getFuncType()));
1637
0
    }
1638
0
  }
1639
0
  return nullptr;
1640
0
}
1641
1642
WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext *
1643
WasmEdge_ImportTypeGetTableType(
1644
    const WasmEdge_ASTModuleContext *ASTCxt,
1645
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1646
0
  if (ASTCxt && Cxt &&
1647
0
      fromImpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Table) {
1648
0
    return toTabTypeCxt(&fromImpTypeCxt(Cxt)->getExternalTableType());
1649
0
  }
1650
0
  return nullptr;
1651
0
}
1652
1653
WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext *
1654
WasmEdge_ImportTypeGetMemoryType(
1655
    const WasmEdge_ASTModuleContext *ASTCxt,
1656
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1657
0
  if (ASTCxt && Cxt &&
1658
0
      fromImpTypeCxt(Cxt)->getExternalType() ==
1659
0
          WasmEdge::ExternalType::Memory) {
1660
0
    return toMemTypeCxt(&fromImpTypeCxt(Cxt)->getExternalMemoryType());
1661
0
  }
1662
0
  return nullptr;
1663
0
}
1664
1665
WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext *
1666
WasmEdge_ImportTypeGetTagType(const WasmEdge_ASTModuleContext *ASTCxt,
1667
0
                              const WasmEdge_ImportTypeContext *Cxt) noexcept {
1668
0
  if (ASTCxt && Cxt &&
1669
0
      fromImpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Tag) {
1670
0
    return toTagTypeCxt(&fromImpTypeCxt(Cxt)->getExternalTagType());
1671
0
  }
1672
0
  return nullptr;
1673
0
}
1674
1675
WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext *
1676
WasmEdge_ImportTypeGetGlobalType(
1677
    const WasmEdge_ASTModuleContext *ASTCxt,
1678
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1679
0
  if (ASTCxt && Cxt &&
1680
0
      fromImpTypeCxt(Cxt)->getExternalType() ==
1681
0
          WasmEdge::ExternalType::Global) {
1682
0
    return toGlobTypeCxt(&fromImpTypeCxt(Cxt)->getExternalGlobalType());
1683
0
  }
1684
0
  return nullptr;
1685
0
}
1686
1687
// <<<<<<<< WasmEdge import type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1688
1689
// >>>>>>>> WasmEdge export type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1690
1691
WASMEDGE_CAPI_EXPORT enum WasmEdge_ExternalType
1692
WasmEdge_ExportTypeGetExternalType(
1693
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1694
0
  if (Cxt) {
1695
0
    return static_cast<WasmEdge_ExternalType>(
1696
0
        fromExpTypeCxt(Cxt)->getExternalType());
1697
0
  }
1698
0
  return WasmEdge_ExternalType_Function;
1699
0
}
1700
1701
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ExportTypeGetExternalName(
1702
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1703
0
  if (Cxt) {
1704
0
    auto StrView = fromExpTypeCxt(Cxt)->getExternalName();
1705
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
1706
0
                           /* Buf */ StrView.data()};
1707
0
  }
1708
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
1709
0
}
1710
1711
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
1712
WasmEdge_ExportTypeGetFunctionType(
1713
    const WasmEdge_ASTModuleContext *ASTCxt,
1714
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1715
0
  try {
1716
0
    if (ASTCxt && Cxt &&
1717
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1718
0
            WasmEdge::ExternalType::Function) {
1719
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1720
0
      auto FuncIdxs = fromASTModCxt(ASTCxt)->getFunctionSection().getContent();
1721
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1722
1723
      // Indexing the import descriptions.
1724
0
      std::vector<uint32_t> ImpFuncs;
1725
0
      ImpFuncs.reserve(ImpDescs.size());
1726
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1727
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Function) {
1728
0
          ImpFuncs.push_back(I);
1729
0
        }
1730
0
      }
1731
      // Get the function type index.
1732
0
      uint32_t TypeIdx = 0;
1733
0
      if (ExtIdx < ImpFuncs.size()) {
1734
        // Imported function. Get the function type index from the import desc.
1735
0
        TypeIdx = ImpDescs[ImpFuncs[ExtIdx]].getExternalFuncTypeIdx();
1736
0
      } else if (ExtIdx < ImpFuncs.size() + FuncIdxs.size()) {
1737
        // Module owned function. Get the function type index from the section.
1738
0
        TypeIdx = FuncIdxs[ExtIdx - ImpFuncs.size()];
1739
0
      } else {
1740
        // Invalid function index.
1741
0
        return nullptr;
1742
0
      }
1743
      // Get the function type.
1744
0
      auto SubTypes = fromASTModCxt(ASTCxt)->getTypeSection().getContent();
1745
0
      if (TypeIdx < SubTypes.size() &&
1746
0
          SubTypes[TypeIdx].getCompositeType().isFunc()) {
1747
0
        return toFuncTypeCxt(
1748
0
            &(SubTypes[TypeIdx].getCompositeType().getFuncType()));
1749
0
      }
1750
0
    }
1751
0
  } catch (...) {
1752
0
    handleCAPIError();
1753
0
  }
1754
0
  return nullptr;
1755
0
}
1756
1757
WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext *
1758
WasmEdge_ExportTypeGetTableType(
1759
    const WasmEdge_ASTModuleContext *ASTCxt,
1760
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1761
0
  try {
1762
0
    if (ASTCxt && Cxt &&
1763
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1764
0
            WasmEdge::ExternalType::Table) {
1765
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1766
0
      auto TabDescs = fromASTModCxt(ASTCxt)->getTableSection().getContent();
1767
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1768
1769
      // Indexing the import descriptions.
1770
0
      std::vector<uint32_t> ImpTabs;
1771
0
      ImpTabs.reserve(ImpDescs.size());
1772
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1773
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Table) {
1774
0
          ImpTabs.push_back(I);
1775
0
        }
1776
0
      }
1777
      // Get the table type.
1778
0
      if (ExtIdx < ImpTabs.size()) {
1779
        // Imported table. Get the table type from the import desc.
1780
0
        return toTabTypeCxt(&ImpDescs[ImpTabs[ExtIdx]].getExternalTableType());
1781
0
      } else if (ExtIdx < ImpTabs.size() + TabDescs.size()) {
1782
        // Module owned table. Get the table type from the section.
1783
0
        return toTabTypeCxt(&TabDescs[ExtIdx - ImpTabs.size()].getTableType());
1784
0
      } else {
1785
        // Invalid table type index.
1786
0
        return nullptr;
1787
0
      }
1788
0
    }
1789
0
  } catch (...) {
1790
0
    handleCAPIError();
1791
0
  }
1792
0
  return nullptr;
1793
0
}
1794
1795
WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext *
1796
WasmEdge_ExportTypeGetMemoryType(
1797
    const WasmEdge_ASTModuleContext *ASTCxt,
1798
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1799
0
  try {
1800
0
    if (ASTCxt && Cxt &&
1801
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1802
0
            WasmEdge::ExternalType::Memory) {
1803
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1804
0
      auto MemTypes = fromASTModCxt(ASTCxt)->getMemorySection().getContent();
1805
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1806
1807
      // Indexing the import descriptions.
1808
0
      std::vector<uint32_t> ImpMems;
1809
0
      ImpMems.reserve(ImpDescs.size());
1810
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1811
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Memory) {
1812
0
          ImpMems.push_back(I);
1813
0
        }
1814
0
      }
1815
      // Get the memory type.
1816
0
      if (ExtIdx < ImpMems.size()) {
1817
        // Imported memory. Get the memory type from the import desc.
1818
0
        return toMemTypeCxt(&ImpDescs[ImpMems[ExtIdx]].getExternalMemoryType());
1819
0
      } else if (ExtIdx < ImpMems.size() + MemTypes.size()) {
1820
        // Module owned memory. Get the memory type from the section.
1821
0
        return toMemTypeCxt(&MemTypes[ExtIdx - ImpMems.size()]);
1822
0
      } else {
1823
        // Invalid memory type index.
1824
0
        return nullptr;
1825
0
      }
1826
0
    }
1827
0
  } catch (...) {
1828
0
    handleCAPIError();
1829
0
  }
1830
0
  return nullptr;
1831
0
}
1832
1833
WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext *
1834
WasmEdge_ExportTypeGetTagType(const WasmEdge_ASTModuleContext *ASTCxt,
1835
0
                              const WasmEdge_ExportTypeContext *Cxt) noexcept {
1836
0
  if (ASTCxt && Cxt &&
1837
0
      fromExpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Tag) {
1838
0
    uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1839
0
    const auto &ImpDescs =
1840
0
        fromASTModCxt(ASTCxt)->getImportSection().getContent();
1841
0
    for (auto &&ImpDesc : ImpDescs) {
1842
0
      if (ImpDesc.getExternalType() == WasmEdge::ExternalType::Tag) {
1843
0
        if (ExtIdx == 0) {
1844
0
          return toTagTypeCxt(&ImpDesc.getExternalTagType());
1845
0
        }
1846
0
        ExtIdx--;
1847
0
      }
1848
0
    }
1849
    // Get the tag type.
1850
0
    const auto &TagDescs = fromASTModCxt(ASTCxt)->getTagSection().getContent();
1851
0
    if (ExtIdx >= TagDescs.size()) {
1852
0
      return nullptr;
1853
0
    }
1854
0
    return toTagTypeCxt(&TagDescs[ExtIdx]);
1855
0
  }
1856
0
  return nullptr;
1857
0
}
1858
1859
WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext *
1860
WasmEdge_ExportTypeGetGlobalType(
1861
    const WasmEdge_ASTModuleContext *ASTCxt,
1862
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1863
0
  try {
1864
0
    if (ASTCxt && Cxt &&
1865
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1866
0
            WasmEdge::ExternalType::Global) {
1867
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1868
0
      auto GlobDescs = fromASTModCxt(ASTCxt)->getGlobalSection().getContent();
1869
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1870
1871
      // Indexing the import descriptions.
1872
0
      std::vector<uint32_t> ImpGlobs;
1873
0
      ImpGlobs.reserve(ImpDescs.size());
1874
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1875
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Global) {
1876
0
          ImpGlobs.push_back(I);
1877
0
        }
1878
0
      }
1879
      // Get the global type.
1880
0
      if (ExtIdx < ImpGlobs.size()) {
1881
        // Imported global. Get the global type from the import desc.
1882
0
        return toGlobTypeCxt(
1883
0
            &ImpDescs[ImpGlobs[ExtIdx]].getExternalGlobalType());
1884
0
      } else if (ExtIdx < ImpGlobs.size() + GlobDescs.size()) {
1885
        // Module owned global. Get the global type from the section.
1886
0
        return toGlobTypeCxt(
1887
0
            &GlobDescs[ExtIdx - ImpGlobs.size()].getGlobalType());
1888
0
      } else {
1889
        // Invalid global type index.
1890
0
        return nullptr;
1891
0
      }
1892
0
    }
1893
0
  } catch (...) {
1894
0
    handleCAPIError();
1895
0
  }
1896
0
  return nullptr;
1897
0
}
1898
1899
// <<<<<<<< WasmEdge export type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1900
1901
// >>>>>>>> WasmEdge AOT compiler functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1902
1903
WASMEDGE_CAPI_EXPORT WasmEdge_CompilerContext *
1904
WasmEdge_CompilerCreate(const WasmEdge_ConfigureContext *ConfCxt
1905
0
                        [[maybe_unused]]) noexcept {
1906
0
#ifdef WASMEDGE_USE_LLVM
1907
0
  try {
1908
    // Set force interpreter here to load instructions of function body
1909
    // forcibly.
1910
0
    if (ConfCxt) {
1911
0
      WasmEdge::Configure CopyConf(ConfCxt->Conf);
1912
0
      CopyConf.getRuntimeConfigure().setRunMode(WasmEdge::RunMode::Interpreter);
1913
0
      return new WasmEdge_CompilerContext(CopyConf);
1914
0
    } else {
1915
0
      WasmEdge::Configure CopyConf;
1916
0
      CopyConf.getRuntimeConfigure().setRunMode(WasmEdge::RunMode::Interpreter);
1917
0
      return new WasmEdge_CompilerContext(CopyConf);
1918
0
    }
1919
0
  } catch (...) {
1920
0
    handleCAPIError();
1921
0
    return nullptr;
1922
0
  }
1923
#else
1924
  return nullptr;
1925
#endif
1926
0
}
1927
1928
WASMEDGE_CAPI_EXPORT WasmEdge_Result
1929
WasmEdge_CompilerCompile(WasmEdge_CompilerContext *Cxt [[maybe_unused]],
1930
                         const char *InPath [[maybe_unused]],
1931
0
                         const char *OutPath [[maybe_unused]]) noexcept {
1932
0
#ifdef WASMEDGE_USE_LLVM
1933
0
  return wrap(
1934
0
      [&]() -> WasmEdge::Expect<void> {
1935
0
        std::filesystem::path InputPath = genPath(InPath);
1936
0
        std::filesystem::path OutputPath = genPath(OutPath);
1937
0
        std::vector<WasmEdge::Byte> Data;
1938
0
        std::unique_ptr<WasmEdge::AST::Module> Module;
1939
0
        return Cxt->Compiler.checkConfigure()
1940
0
            .and_then([&]() noexcept { return Cxt->Load.loadFile(InputPath); })
1941
0
            .and_then([&](auto Result) noexcept {
1942
0
              Data = std::move(Result);
1943
0
              return Cxt->Load.parseModule(Data);
1944
0
            })
1945
0
            .and_then([&](auto Result) noexcept {
1946
0
              Module = std::move(Result);
1947
0
              return Cxt->Valid.validate(*Module.get());
1948
0
            })
1949
0
            .and_then(
1950
0
                [&]() noexcept { return Cxt->Compiler.compile(*Module.get()); })
1951
0
            .and_then([&](auto Result) noexcept {
1952
0
              return Cxt->CodeGen.codegen(Data, std::move(Result), OutputPath);
1953
0
            });
1954
0
      },
1955
0
      EmptyThen, Cxt);
1956
#else
1957
  return genWasmEdge_Result(ErrCode::Value::AOTDisabled);
1958
#endif
1959
0
}
1960
1961
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_CompilerCompileFromBuffer(
1962
    WasmEdge_CompilerContext *Cxt, const uint8_t *InBuffer,
1963
0
    const uint64_t InBufferLen, const char *OutPath) noexcept {
1964
0
  return WasmEdge_CompilerCompileFromBytes(
1965
0
      Cxt, WasmEdge_BytesWrap(InBuffer, static_cast<uint32_t>(InBufferLen)),
1966
0
      OutPath);
1967
0
}
1968
1969
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_CompilerCompileFromBytes(
1970
    WasmEdge_CompilerContext *Cxt [[maybe_unused]],
1971
    const WasmEdge_Bytes Bytes [[maybe_unused]],
1972
0
    const char *OutPath [[maybe_unused]]) noexcept {
1973
0
#ifdef WASMEDGE_USE_LLVM
1974
0
  return wrap(
1975
0
      [&]() -> WasmEdge::Expect<void> {
1976
0
        std::filesystem::path OutputPath = genPath(OutPath);
1977
0
        auto Data = genSpan(Bytes.Buf, Bytes.Length);
1978
0
        std::unique_ptr<WasmEdge::AST::Module> Module;
1979
0
        return Cxt->Load.parseModule(Data)
1980
0
            .and_then([&](auto Result) noexcept {
1981
0
              Module = std::move(Result);
1982
0
              return Cxt->Valid.validate(*Module);
1983
0
            })
1984
0
            .and_then([&]() noexcept { return Cxt->Compiler.compile(*Module); })
1985
0
            .and_then([&](auto Result) noexcept {
1986
0
              return Cxt->CodeGen.codegen(Data, std::move(Result), OutputPath);
1987
0
            });
1988
0
      },
1989
0
      EmptyThen, Cxt);
1990
#else
1991
  return genWasmEdge_Result(ErrCode::Value::AOTDisabled);
1992
#endif
1993
0
}
1994
1995
WASMEDGE_CAPI_EXPORT void
1996
0
WasmEdge_CompilerDelete(WasmEdge_CompilerContext *Cxt) noexcept {
1997
0
  delete Cxt;
1998
0
}
1999
2000
// <<<<<<<< WasmEdge AOT compiler functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2001
2002
// >>>>>>>> WasmEdge loader functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2003
2004
WASMEDGE_CAPI_EXPORT WasmEdge_LoaderContext *
2005
0
WasmEdge_LoaderCreate(const WasmEdge_ConfigureContext *ConfCxt) noexcept {
2006
0
  try {
2007
0
    if (ConfCxt) {
2008
0
      return toLoaderCxt(new WasmEdge::Loader::Loader(
2009
0
          ConfCxt->Conf, &WasmEdge::Executor::Executor::Intrinsics));
2010
0
    } else {
2011
0
      return toLoaderCxt(new WasmEdge::Loader::Loader(
2012
0
          WasmEdge::Configure(), &WasmEdge::Executor::Executor::Intrinsics));
2013
0
    }
2014
0
  } catch (...) {
2015
0
    handleCAPIError();
2016
0
    return nullptr;
2017
0
  }
2018
0
}
2019
2020
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromFile(
2021
    WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module,
2022
0
    const char *Path) noexcept {
2023
0
  return wrap([&]() { return fromLoaderCxt(Cxt)->parseModule(genPath(Path)); },
2024
0
              [&](auto &&Res) { *Module = toASTModCxt((*Res).release()); }, Cxt,
2025
0
              Module);
2026
0
}
2027
2028
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromBuffer(
2029
    WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module,
2030
0
    const uint8_t *Buf, const uint32_t BufLen) noexcept {
2031
0
  return WasmEdge_LoaderParseFromBytes(Cxt, Module,
2032
0
                                       WasmEdge_BytesWrap(Buf, BufLen));
2033
0
}
2034
2035
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromBytes(
2036
    WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module,
2037
0
    const WasmEdge_Bytes Bytes) noexcept {
2038
0
  return wrap(
2039
0
      [&]() {
2040
0
        return fromLoaderCxt(Cxt)->parseModule(
2041
0
            genSpan(Bytes.Buf, Bytes.Length));
2042
0
      },
2043
0
      [&](auto &&Res) { *Module = toASTModCxt((*Res).release()); }, Cxt,
2044
0
      Module);
2045
0
}
2046
2047
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderSerializeASTModule(
2048
    WasmEdge_LoaderContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt,
2049
0
    WasmEdge_Bytes *Buf) noexcept {
2050
0
  return wrap(
2051
0
      [&]() {
2052
0
        return fromLoaderCxt(Cxt)->serializeModule(*fromASTModCxt(ASTCxt));
2053
0
      },
2054
0
      [&](auto &&Res) {
2055
0
        uint32_t Size = static_cast<uint32_t>((*Res).size());
2056
0
        uint8_t *Bytes = new uint8_t[Size];
2057
0
        std::copy_n((*Res).begin(), Size, Bytes);
2058
0
        Buf->Length = Size;
2059
0
        Buf->Buf = Bytes;
2060
0
      },
2061
0
      Cxt, ASTCxt, Buf);
2062
0
}
2063
2064
WASMEDGE_CAPI_EXPORT void
2065
0
WasmEdge_LoaderDelete(WasmEdge_LoaderContext *Cxt) noexcept {
2066
0
  delete fromLoaderCxt(Cxt);
2067
0
}
2068
2069
// <<<<<<<< WasmEdge loader functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2070
2071
// >>>>>>>> WasmEdge validator functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2072
2073
WASMEDGE_CAPI_EXPORT WasmEdge_ValidatorContext *
2074
0
WasmEdge_ValidatorCreate(const WasmEdge_ConfigureContext *ConfCxt) noexcept {
2075
0
  try {
2076
0
    if (ConfCxt) {
2077
0
      return toValidatorCxt(new WasmEdge::Validator::Validator(ConfCxt->Conf));
2078
0
    } else {
2079
0
      return toValidatorCxt(
2080
0
          new WasmEdge::Validator::Validator(WasmEdge::Configure()));
2081
0
    }
2082
0
  } catch (...) {
2083
0
    handleCAPIError();
2084
0
    return nullptr;
2085
0
  }
2086
0
}
2087
2088
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ValidatorValidate(
2089
    WasmEdge_ValidatorContext *Cxt,
2090
0
    const WasmEdge_ASTModuleContext *ModuleCxt) noexcept {
2091
0
  return wrap(
2092
0
      [&]() {
2093
0
        return fromValidatorCxt(Cxt)->validate(*fromASTModCxt(ModuleCxt));
2094
0
      },
2095
0
      EmptyThen, Cxt, ModuleCxt);
2096
0
}
2097
2098
WASMEDGE_CAPI_EXPORT void
2099
0
WasmEdge_ValidatorDelete(WasmEdge_ValidatorContext *Cxt) noexcept {
2100
0
  delete fromValidatorCxt(Cxt);
2101
0
}
2102
2103
// <<<<<<<< WasmEdge validator functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2104
2105
// >>>>>>>> WasmEdge executor functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2106
2107
WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext *
2108
WasmEdge_ExecutorCreate(const WasmEdge_ConfigureContext *ConfCxt,
2109
0
                        WasmEdge_StatisticsContext *StatCxt) noexcept {
2110
0
  try {
2111
0
    if (ConfCxt) {
2112
0
      if (StatCxt) {
2113
0
        return toExecutorCxt(new WasmEdge::Executor::Executor(
2114
0
            ConfCxt->Conf, fromStatCxt(StatCxt)));
2115
0
      } else {
2116
0
        return toExecutorCxt(new WasmEdge::Executor::Executor(ConfCxt->Conf));
2117
0
      }
2118
0
    } else {
2119
0
      if (StatCxt) {
2120
0
        return toExecutorCxt(new WasmEdge::Executor::Executor(
2121
0
            WasmEdge::Configure(), fromStatCxt(StatCxt)));
2122
0
      } else {
2123
0
        return toExecutorCxt(
2124
0
            new WasmEdge::Executor::Executor(WasmEdge::Configure()));
2125
0
      }
2126
0
    }
2127
0
  } catch (...) {
2128
0
    handleCAPIError();
2129
0
    return nullptr;
2130
0
  }
2131
0
}
2132
2133
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorInstantiate(
2134
    WasmEdge_ExecutorContext *Cxt, WasmEdge_ModuleInstanceContext **ModuleCxt,
2135
    WasmEdge_StoreContext *StoreCxt,
2136
0
    const WasmEdge_ASTModuleContext *ASTCxt) noexcept {
2137
0
  return wrap(
2138
0
      [&]() {
2139
0
        return fromExecutorCxt(Cxt)->instantiateModule(*fromStoreCxt(StoreCxt),
2140
0
                                                       *fromASTModCxt(ASTCxt));
2141
0
      },
2142
0
      [&](auto &&Res) { *ModuleCxt = toModCxt((*Res).release()); }, Cxt,
2143
0
      ModuleCxt, StoreCxt, ASTCxt);
2144
0
}
2145
2146
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegister(
2147
    WasmEdge_ExecutorContext *Cxt, WasmEdge_ModuleInstanceContext **ModuleCxt,
2148
    WasmEdge_StoreContext *StoreCxt, const WasmEdge_ASTModuleContext *ASTCxt,
2149
0
    const WasmEdge_String ModuleName) noexcept {
2150
0
  return wrap(
2151
0
      [&]() {
2152
0
        return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt),
2153
0
                                                    *fromASTModCxt(ASTCxt),
2154
0
                                                    genStrView(ModuleName));
2155
0
      },
2156
0
      [&](auto &&Res) { *ModuleCxt = toModCxt((*Res).release()); }, Cxt,
2157
0
      ModuleCxt, StoreCxt, ASTCxt);
2158
0
}
2159
2160
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegisterImport(
2161
    WasmEdge_ExecutorContext *Cxt, WasmEdge_StoreContext *StoreCxt,
2162
0
    const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept {
2163
0
  return wrap(
2164
0
      [&]() {
2165
0
        return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt),
2166
0
                                                    *fromModCxt(ImportCxt));
2167
0
      },
2168
0
      EmptyThen, Cxt, StoreCxt, ImportCxt);
2169
0
}
2170
2171
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegisterImportWithAlias(
2172
    WasmEdge_ExecutorContext *Cxt, WasmEdge_StoreContext *StoreCxt,
2173
    const WasmEdge_ModuleInstanceContext *ImportCxt,
2174
0
    const WasmEdge_String ModuleName) noexcept {
2175
0
  return wrap(
2176
0
      [&]() {
2177
0
        return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt),
2178
0
                                                    *fromModCxt(ImportCxt),
2179
0
                                                    genStrView(ModuleName));
2180
0
      },
2181
0
      EmptyThen, Cxt, StoreCxt, ImportCxt);
2182
0
}
2183
2184
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorInvoke(
2185
    WasmEdge_ExecutorContext *Cxt,
2186
    const WasmEdge_FunctionInstanceContext *FuncCxt,
2187
    const WasmEdge_Value *Params, const uint32_t ParamLen,
2188
0
    WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept {
2189
0
  try {
2190
0
    auto ParamPair = genParamPair(Params, ParamLen);
2191
0
    return wrap(
2192
0
        [&]() -> WasmEdge::Expect<std::vector<
2193
0
                  std::pair<WasmEdge::ValVariant, WasmEdge::ValType>>> {
2194
0
          return fromExecutorCxt(Cxt)->invoke(
2195
0
              fromFuncCxt(FuncCxt), ParamPair.first, ParamPair.second);
2196
0
        },
2197
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
2198
0
        Cxt, FuncCxt);
2199
0
  } catch (...) {
2200
0
    return handleCAPIError();
2201
0
  }
2202
0
}
2203
2204
WASMEDGE_CAPI_EXPORT WasmEdge_Async *
2205
WasmEdge_ExecutorAsyncInvoke(WasmEdge_ExecutorContext *Cxt,
2206
                             const WasmEdge_FunctionInstanceContext *FuncCxt,
2207
                             const WasmEdge_Value *Params,
2208
0
                             const uint32_t ParamLen) noexcept {
2209
0
  try {
2210
0
    if (Cxt && FuncCxt) {
2211
0
      auto ParamPair = genParamPair(Params, ParamLen);
2212
0
      return new WasmEdge_Async(fromExecutorCxt(Cxt)->asyncInvoke(
2213
0
          fromFuncCxt(FuncCxt), ParamPair.first, ParamPair.second));
2214
0
    }
2215
0
  } catch (...) {
2216
0
    handleCAPIError();
2217
0
  }
2218
0
  return nullptr;
2219
0
}
2220
2221
WASMEDGE_CAPI_EXPORT void
2222
0
WasmEdge_ExecutorDelete(WasmEdge_ExecutorContext *Cxt) noexcept {
2223
0
  delete fromExecutorCxt(Cxt);
2224
0
}
2225
2226
// <<<<<<<< WasmEdge executor functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2227
2228
// >>>>>>>> WasmEdge store functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2229
2230
WASMEDGE_CAPI_EXPORT WasmEdge_StoreContext *
2231
0
WasmEdge_StoreCreate(void) noexcept {
2232
0
  try {
2233
0
    return toStoreCxt(new WasmEdge::Runtime::StoreManager);
2234
0
  } catch (...) {
2235
0
    handleCAPIError();
2236
0
    return nullptr;
2237
0
  }
2238
0
}
2239
2240
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
2241
WasmEdge_StoreFindModule(const WasmEdge_StoreContext *Cxt,
2242
0
                         const WasmEdge_String Name) noexcept {
2243
0
  if (Cxt) {
2244
0
    return toModCxt(fromStoreCxt(Cxt)->findModule(genStrView(Name)));
2245
0
  }
2246
0
  return nullptr;
2247
0
}
2248
2249
WASMEDGE_CAPI_EXPORT uint32_t
2250
0
WasmEdge_StoreListModuleLength(const WasmEdge_StoreContext *Cxt) noexcept {
2251
0
  if (Cxt) {
2252
0
    return fromStoreCxt(Cxt)->getModuleListSize();
2253
0
  }
2254
0
  return 0;
2255
0
}
2256
2257
WASMEDGE_CAPI_EXPORT uint32_t
2258
WasmEdge_StoreListModule(const WasmEdge_StoreContext *Cxt,
2259
0
                         WasmEdge_String *Names, const uint32_t Len) noexcept {
2260
0
  if (Cxt) {
2261
0
    return fromStoreCxt(Cxt)->getModuleList(
2262
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2263
0
  }
2264
0
  return 0;
2265
0
}
2266
2267
WASMEDGE_CAPI_EXPORT void
2268
0
WasmEdge_StoreDelete(WasmEdge_StoreContext *Cxt) noexcept {
2269
0
  delete fromStoreCxt(Cxt);
2270
0
}
2271
2272
// <<<<<<<< WasmEdge store functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2273
2274
// >>>>>>>> WasmEdge module instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2275
2276
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
2277
0
WasmEdge_ModuleInstanceCreate(const WasmEdge_String ModuleName) noexcept {
2278
0
  try {
2279
0
    return toModCxt(new WasmEdge::Runtime::Instance::ModuleInstance(
2280
0
        genStrView(ModuleName)));
2281
0
  } catch (...) {
2282
0
    handleCAPIError();
2283
0
    return nullptr;
2284
0
  }
2285
0
}
2286
2287
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
2288
WasmEdge_ModuleInstanceCreateWASIWithFds(
2289
    const char *const *Args, const uint32_t ArgLen, const char *const *Envs,
2290
    const uint32_t EnvLen, const char *const *Preopens,
2291
    const uint32_t PreopenLen, const int32_t StdInFd, const int32_t StdOutFd,
2292
0
    const int32_t StdErrFd) noexcept {
2293
0
  try {
2294
0
    auto WasiMod = std::make_unique<WasmEdge::Host::WasiModule>();
2295
0
    WasmEdge_ModuleInstanceInitWASIWithFds(
2296
0
        toModCxt(WasiMod.get()), Args, ArgLen, Envs, EnvLen, Preopens,
2297
0
        PreopenLen, StdInFd, StdOutFd, StdErrFd);
2298
0
    return toModCxt(WasiMod.release());
2299
0
  } catch (...) {
2300
0
    handleCAPIError();
2301
0
    return nullptr;
2302
0
  }
2303
0
}
2304
2305
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
2306
WasmEdge_ModuleInstanceCreateWASI(const char *const *Args,
2307
                                  const uint32_t ArgLen,
2308
                                  const char *const *Envs,
2309
                                  const uint32_t EnvLen,
2310
                                  const char *const *Preopens,
2311
0
                                  const uint32_t PreopenLen) noexcept {
2312
0
  try {
2313
0
    auto WasiMod = std::make_unique<WasmEdge::Host::WasiModule>();
2314
0
    WasmEdge_ModuleInstanceInitWASI(toModCxt(WasiMod.get()), Args, ArgLen, Envs,
2315
0
                                    EnvLen, Preopens, PreopenLen);
2316
0
    return toModCxt(WasiMod.release());
2317
0
  } catch (...) {
2318
0
    handleCAPIError();
2319
0
    return nullptr;
2320
0
  }
2321
0
}
2322
2323
WASMEDGE_CAPI_EXPORT extern WasmEdge_ModuleInstanceContext *
2324
WasmEdge_ModuleInstanceCreateWithData(const WasmEdge_String ModuleName,
2325
                                      void *HostData,
2326
0
                                      void (*Finalizer)(void *)) noexcept {
2327
0
  try {
2328
0
    return toModCxt(new WasmEdge::Runtime::Instance::ModuleInstance(
2329
0
        genStrView(ModuleName), HostData, Finalizer));
2330
0
  } catch (...) {
2331
0
    handleCAPIError();
2332
0
    return nullptr;
2333
0
  }
2334
0
}
2335
2336
WASMEDGE_CAPI_EXPORT void WasmEdge_ModuleInstanceInitWASI(
2337
    WasmEdge_ModuleInstanceContext *Cxt, const char *const *Args,
2338
    const uint32_t ArgLen, const char *const *Envs, const uint32_t EnvLen,
2339
0
    const char *const *Preopens, const uint32_t PreopenLen) noexcept {
2340
0
  if (!Cxt) {
2341
0
    return;
2342
0
  }
2343
0
  auto *WasiMod = dynamic_cast<WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2344
0
  if (!WasiMod) {
2345
0
    return;
2346
0
  }
2347
0
  try {
2348
0
    std::vector<std::string> ArgVec, EnvVec, DirVec;
2349
0
    std::string ProgName;
2350
0
    if (Args) {
2351
0
      if (ArgLen > 0) {
2352
0
        ProgName = Args[0];
2353
0
      }
2354
0
      for (uint32_t I = 1; I < ArgLen; I++) {
2355
0
        ArgVec.emplace_back(Args[I]);
2356
0
      }
2357
0
    }
2358
0
    if (Envs) {
2359
0
      for (uint32_t I = 0; I < EnvLen; I++) {
2360
0
        EnvVec.emplace_back(Envs[I]);
2361
0
      }
2362
0
    }
2363
0
    if (Preopens) {
2364
0
      for (uint32_t I = 0; I < PreopenLen; I++) {
2365
0
        DirVec.emplace_back(Preopens[I]);
2366
0
      }
2367
0
    }
2368
0
    WasiMod->init(DirVec, ProgName, ArgVec, EnvVec);
2369
0
  } catch (...) {
2370
0
    handleCAPIError();
2371
0
  }
2372
0
}
2373
2374
WASMEDGE_CAPI_EXPORT void WasmEdge_ModuleInstanceInitWASIWithFds(
2375
    WasmEdge_ModuleInstanceContext *Cxt, const char *const *Args,
2376
    const uint32_t ArgLen, const char *const *Envs, const uint32_t EnvLen,
2377
    const char *const *Preopens, const uint32_t PreopenLen,
2378
    const int32_t StdInFd, const int32_t StdOutFd,
2379
0
    const int32_t StdErrFd) noexcept {
2380
0
  if (!Cxt) {
2381
0
    return;
2382
0
  }
2383
0
  auto *WasiMod = dynamic_cast<WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2384
0
  if (!WasiMod) {
2385
0
    return;
2386
0
  }
2387
0
  try {
2388
0
    std::vector<std::string> ArgVec, EnvVec, DirVec;
2389
0
    std::string ProgName;
2390
0
    if (Args) {
2391
0
      if (ArgLen > 0) {
2392
0
        ProgName = Args[0];
2393
0
      }
2394
0
      for (uint32_t I = 1; I < ArgLen; I++) {
2395
0
        ArgVec.emplace_back(Args[I]);
2396
0
      }
2397
0
    }
2398
0
    if (Envs) {
2399
0
      for (uint32_t I = 0; I < EnvLen; I++) {
2400
0
        EnvVec.emplace_back(Envs[I]);
2401
0
      }
2402
0
    }
2403
0
    if (Preopens) {
2404
0
      for (uint32_t I = 0; I < PreopenLen; I++) {
2405
0
        DirVec.emplace_back(Preopens[I]);
2406
0
      }
2407
0
    }
2408
0
    auto Result = WasiMod->initWithFds(DirVec, ProgName, ArgVec, EnvVec,
2409
0
                                       StdInFd, StdOutFd, StdErrFd);
2410
0
    if (!Result) {
2411
0
      spdlog::error("    Failed to initialize WASI environment: {}"sv,
2412
0
                    Result.error());
2413
0
    }
2414
0
  } catch (...) {
2415
0
    handleCAPIError();
2416
0
  }
2417
0
}
2418
2419
WASMEDGE_CAPI_EXPORT extern uint32_t
2420
WasmEdge_ModuleInstanceWASIGetNativeHandler(
2421
    const WasmEdge_ModuleInstanceContext *Cxt, int32_t Fd,
2422
0
    uint64_t *NativeHandler) noexcept {
2423
0
  if (!Cxt) {
2424
0
    return 1;
2425
0
  }
2426
0
  auto *WasiMod =
2427
0
      dynamic_cast<const WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2428
0
  if (!WasiMod) {
2429
0
    return 2;
2430
0
  }
2431
0
  auto Handler = WasiMod->getNativeHandler(Fd);
2432
0
  if (!Handler) {
2433
0
    return 2;
2434
0
  }
2435
0
  *NativeHandler = *Handler;
2436
0
  return 0;
2437
0
}
2438
2439
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceWASIGetExitCode(
2440
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2441
0
  if (!Cxt) {
2442
0
    return EXIT_FAILURE;
2443
0
  }
2444
0
  auto *WasiMod =
2445
0
      dynamic_cast<const WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2446
0
  if (!WasiMod) {
2447
0
    return EXIT_FAILURE;
2448
0
  }
2449
0
  return WasiMod->getExitCode();
2450
0
}
2451
2452
WASMEDGE_CAPI_EXPORT void
2453
WasmEdge_ModuleInstanceInitWasmEdgeProcess(const char *const *AllowedCmds,
2454
                                           const uint32_t CmdsLen,
2455
0
                                           const bool AllowAll) noexcept {
2456
0
  using namespace std::literals::string_view_literals;
2457
0
  if (const auto *Plugin =
2458
0
          WasmEdge::Plugin::Plugin::find("wasmedge_process"sv)) {
2459
0
    PO::ArgumentParser Parser;
2460
0
    Plugin->registerOptions(Parser);
2461
0
    Parser.set_raw_value<std::vector<std::string>>(
2462
0
        "allow-command"sv,
2463
0
        std::vector<std::string>(AllowedCmds, AllowedCmds + CmdsLen));
2464
0
    if (AllowAll) {
2465
0
      Parser.set_raw_value("allow-command-all"sv);
2466
0
    }
2467
0
  }
2468
0
}
2469
2470
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ModuleInstanceGetModuleName(
2471
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2472
0
  if (Cxt) {
2473
0
    auto StrView = fromModCxt(Cxt)->getModuleName();
2474
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
2475
0
                           /* Buf */ StrView.data()};
2476
0
  }
2477
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
2478
0
}
2479
2480
WASMEDGE_CAPI_EXPORT void *WasmEdge_ModuleInstanceGetHostData(
2481
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2482
0
  if (Cxt) {
2483
0
    return fromModCxt(Cxt)->getHostData();
2484
0
  }
2485
0
  return nullptr;
2486
0
}
2487
2488
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext *
2489
WasmEdge_ModuleInstanceFindFunction(const WasmEdge_ModuleInstanceContext *Cxt,
2490
0
                                    const WasmEdge_String Name) noexcept {
2491
0
  if (Cxt) {
2492
0
    return toFuncCxt(fromModCxt(Cxt)->findFuncExports(genStrView(Name)));
2493
0
  }
2494
0
  return nullptr;
2495
0
}
2496
2497
WASMEDGE_CAPI_EXPORT WasmEdge_TableInstanceContext *
2498
WasmEdge_ModuleInstanceFindTable(const WasmEdge_ModuleInstanceContext *Cxt,
2499
0
                                 const WasmEdge_String Name) noexcept {
2500
0
  if (Cxt) {
2501
0
    return toTabCxt(fromModCxt(Cxt)->findTableExports(genStrView(Name)));
2502
0
  }
2503
0
  return nullptr;
2504
0
}
2505
2506
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext *
2507
WasmEdge_ModuleInstanceFindMemory(const WasmEdge_ModuleInstanceContext *Cxt,
2508
0
                                  const WasmEdge_String Name) noexcept {
2509
0
  if (Cxt) {
2510
0
    return toMemCxt(fromModCxt(Cxt)->findMemoryExports(genStrView(Name)));
2511
0
  }
2512
0
  return nullptr;
2513
0
}
2514
2515
WASMEDGE_CAPI_EXPORT WasmEdge_TagInstanceContext *
2516
WasmEdge_ModuleInstanceFindTag(const WasmEdge_ModuleInstanceContext *Cxt,
2517
0
                               const WasmEdge_String Name) noexcept {
2518
0
  if (Cxt) {
2519
0
    return toTagCxt(fromModCxt(Cxt)->findTagExports(genStrView(Name)));
2520
0
  }
2521
0
  return nullptr;
2522
0
}
2523
2524
WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext *
2525
WasmEdge_ModuleInstanceFindGlobal(const WasmEdge_ModuleInstanceContext *Cxt,
2526
0
                                  const WasmEdge_String Name) noexcept {
2527
0
  if (Cxt) {
2528
0
    return toGlobCxt(fromModCxt(Cxt)->findGlobalExports(genStrView(Name)));
2529
0
  }
2530
0
  return nullptr;
2531
0
}
2532
2533
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListFunctionLength(
2534
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2535
0
  if (Cxt) {
2536
0
    return fromModCxt(Cxt)->getFuncExportNum();
2537
0
  }
2538
0
  return 0;
2539
0
}
2540
2541
WASMEDGE_CAPI_EXPORT extern uint32_t
2542
WasmEdge_ModuleInstanceListFunction(const WasmEdge_ModuleInstanceContext *Cxt,
2543
                                    WasmEdge_String *Names,
2544
0
                                    const uint32_t Len) noexcept {
2545
0
  if (Cxt) {
2546
0
    return fromModCxt(Cxt)->getFuncExports(
2547
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2548
0
  }
2549
0
  return 0;
2550
0
}
2551
2552
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTableLength(
2553
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2554
0
  if (Cxt) {
2555
0
    return fromModCxt(Cxt)->getTableExportNum();
2556
0
  }
2557
0
  return 0;
2558
0
}
2559
2560
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTable(
2561
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2562
0
    const uint32_t Len) noexcept {
2563
0
  if (Cxt) {
2564
0
    return fromModCxt(Cxt)->getTableExports(
2565
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2566
0
  }
2567
0
  return 0;
2568
0
}
2569
2570
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListMemoryLength(
2571
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2572
0
  if (Cxt) {
2573
0
    return fromModCxt(Cxt)->getMemoryExportNum();
2574
0
  }
2575
0
  return 0;
2576
0
}
2577
2578
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListMemory(
2579
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2580
0
    const uint32_t Len) noexcept {
2581
0
  if (Cxt) {
2582
0
    return fromModCxt(Cxt)->getMemoryExports(
2583
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2584
0
  }
2585
0
  return 0;
2586
0
}
2587
2588
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTagLength(
2589
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2590
0
  if (Cxt) {
2591
0
    return fromModCxt(Cxt)->getTagExportNum();
2592
0
  }
2593
0
  return 0;
2594
0
}
2595
2596
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTag(
2597
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2598
0
    const uint32_t Len) noexcept {
2599
0
  if (Cxt) {
2600
0
    return fromModCxt(Cxt)->getTagExports(
2601
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2602
0
  }
2603
0
  return 0;
2604
0
}
2605
2606
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListGlobalLength(
2607
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2608
0
  if (Cxt) {
2609
0
    return fromModCxt(Cxt)->getGlobalExportNum();
2610
0
  }
2611
0
  return 0;
2612
0
}
2613
2614
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListGlobal(
2615
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2616
0
    const uint32_t Len) noexcept {
2617
0
  if (Cxt) {
2618
0
    return fromModCxt(Cxt)->getGlobalExports(
2619
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2620
0
  }
2621
0
  return 0;
2622
0
}
2623
2624
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddFunction(
2625
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2626
0
    WasmEdge_FunctionInstanceContext *FuncCxt) noexcept {
2627
0
  return wrap(
2628
0
      [&]() -> WasmEdge::Expect<void> {
2629
0
        std::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance> Inst(
2630
0
            fromFuncCxt(FuncCxt));
2631
0
        auto Res =
2632
0
            fromModCxt(Cxt)->addHostFunc(genStrView(Name), std::move(Inst));
2633
0
        if (!Res) {
2634
0
          static_cast<void>(Inst.release());
2635
0
        }
2636
0
        return Res;
2637
0
      },
2638
0
      EmptyThen, Cxt, FuncCxt);
2639
0
}
2640
2641
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddTable(
2642
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2643
0
    WasmEdge_TableInstanceContext *TableCxt) noexcept {
2644
0
  return wrap(
2645
0
      [&]() -> WasmEdge::Expect<void> {
2646
0
        std::unique_ptr<WasmEdge::Runtime::Instance::TableInstance> Inst(
2647
0
            fromTabCxt(TableCxt));
2648
0
        auto Res =
2649
0
            fromModCxt(Cxt)->addHostTable(genStrView(Name), std::move(Inst));
2650
0
        if (!Res) {
2651
0
          static_cast<void>(Inst.release());
2652
0
        }
2653
0
        return Res;
2654
0
      },
2655
0
      EmptyThen, Cxt, TableCxt);
2656
0
}
2657
2658
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddMemory(
2659
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2660
0
    WasmEdge_MemoryInstanceContext *MemoryCxt) noexcept {
2661
0
  return wrap(
2662
0
      [&]() -> WasmEdge::Expect<void> {
2663
0
        std::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance> Inst(
2664
0
            fromMemCxt(MemoryCxt));
2665
0
        auto Res =
2666
0
            fromModCxt(Cxt)->addHostMemory(genStrView(Name), std::move(Inst));
2667
0
        if (!Res) {
2668
0
          static_cast<void>(Inst.release());
2669
0
        }
2670
0
        return Res;
2671
0
      },
2672
0
      EmptyThen, Cxt, MemoryCxt);
2673
0
}
2674
2675
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddGlobal(
2676
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2677
0
    WasmEdge_GlobalInstanceContext *GlobalCxt) noexcept {
2678
0
  return wrap(
2679
0
      [&]() -> WasmEdge::Expect<void> {
2680
0
        std::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance> Inst(
2681
0
            fromGlobCxt(GlobalCxt));
2682
0
        auto Res =
2683
0
            fromModCxt(Cxt)->addHostGlobal(genStrView(Name), std::move(Inst));
2684
0
        if (!Res) {
2685
0
          static_cast<void>(Inst.release());
2686
0
        }
2687
0
        return Res;
2688
0
      },
2689
0
      EmptyThen, Cxt, GlobalCxt);
2690
0
}
2691
2692
WASMEDGE_CAPI_EXPORT void
2693
0
WasmEdge_ModuleInstanceDelete(WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2694
0
  auto *Mod = fromModCxt(Cxt);
2695
0
  if (Mod) {
2696
0
    Mod->terminate();
2697
0
  }
2698
0
}
2699
2700
// <<<<<<<< WasmEdge module instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2701
2702
// >>>>>>>> WasmEdge function instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2703
2704
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext *
2705
WasmEdge_FunctionInstanceCreate(const WasmEdge_FunctionTypeContext *Type,
2706
                                WasmEdge_HostFunc_t HostFunc, void *Data,
2707
0
                                const uint64_t Cost) noexcept {
2708
0
  try {
2709
0
    if (Type && HostFunc) {
2710
0
      return toFuncCxt(new WasmEdge::Runtime::Instance::FunctionInstance(
2711
0
          std::make_unique<CAPIHostFunc>(fromFuncTypeCxt(Type), HostFunc, Data,
2712
0
                                         Cost)));
2713
0
    }
2714
0
  } catch (...) {
2715
0
    handleCAPIError();
2716
0
  }
2717
0
  return nullptr;
2718
0
}
2719
2720
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext *
2721
WasmEdge_FunctionInstanceCreateBinding(const WasmEdge_FunctionTypeContext *Type,
2722
                                       WasmEdge_WrapFunc_t WrapFunc,
2723
                                       void *Binding, void *Data,
2724
0
                                       const uint64_t Cost) noexcept {
2725
0
  try {
2726
0
    if (Type && WrapFunc) {
2727
0
      return toFuncCxt(new WasmEdge::Runtime::Instance::FunctionInstance(
2728
0
          std::make_unique<CAPIHostFunc>(fromFuncTypeCxt(Type), WrapFunc,
2729
0
                                         Binding, Data, Cost)));
2730
0
    }
2731
0
  } catch (...) {
2732
0
    handleCAPIError();
2733
0
  }
2734
0
  return nullptr;
2735
0
}
2736
2737
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
2738
WasmEdge_FunctionInstanceGetFunctionType(
2739
0
    const WasmEdge_FunctionInstanceContext *Cxt) noexcept {
2740
0
  if (Cxt) {
2741
0
    return toFuncTypeCxt(&fromFuncCxt(Cxt)->getFuncType());
2742
0
  }
2743
0
  return nullptr;
2744
0
}
2745
2746
WASMEDGE_CAPI_EXPORT extern const void *WasmEdge_FunctionInstanceGetData(
2747
0
    const WasmEdge_FunctionInstanceContext *Cxt) noexcept {
2748
0
  if (Cxt && fromFuncCxt(Cxt)->isHostFunction()) {
2749
0
    if (auto *HostFunc =
2750
0
            dynamic_cast<CAPIHostFunc *>(&fromFuncCxt(Cxt)->getHostFunc())) {
2751
0
      return HostFunc->getData();
2752
0
    }
2753
0
  }
2754
0
  return nullptr;
2755
0
}
2756
2757
WASMEDGE_CAPI_EXPORT void WasmEdge_FunctionInstanceDelete(
2758
0
    WasmEdge_FunctionInstanceContext *Cxt) noexcept {
2759
0
  delete fromFuncCxt(Cxt);
2760
0
}
2761
2762
// <<<<<<<< WasmEdge function instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2763
2764
// >>>>>>>> WasmEdge table instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2765
2766
WASMEDGE_CAPI_EXPORT WasmEdge_TableInstanceContext *
2767
WasmEdge_TableInstanceCreate(
2768
0
    const WasmEdge_TableTypeContext *TabType) noexcept {
2769
0
  try {
2770
0
    if (TabType) {
2771
0
      const AST::TableType &TType = *fromTabTypeCxt(TabType);
2772
0
      if (!TType.getRefType().isNullableRefType()) {
2773
0
        spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
2774
0
        return nullptr;
2775
0
      }
2776
0
      return toTabCxt(new WasmEdge::Runtime::Instance::TableInstance(TType));
2777
0
    }
2778
0
  } catch (...) {
2779
0
    handleCAPIError();
2780
0
  }
2781
0
  return nullptr;
2782
0
}
2783
2784
WASMEDGE_CAPI_EXPORT extern WasmEdge_TableInstanceContext *
2785
WasmEdge_TableInstanceCreateWithInit(const WasmEdge_TableTypeContext *TabType,
2786
0
                                     const WasmEdge_Value Value) noexcept {
2787
0
  try {
2788
0
    if (TabType) {
2789
      // Comparison of the value types needs the module instance to retrieve the
2790
      // function type index after applying the typed function reference
2791
      // proposal. It's impossible to do this without refactoring. Therefore
2792
      // simply match the FuncRef and ExternRef here.
2793
0
      const AST::TableType &TType = *fromTabTypeCxt(TabType);
2794
0
      WasmEdge::ValType GotType = genValType(Value.Type);
2795
0
      if (TType.getRefType().isFuncRefType() != GotType.isFuncRefType()) {
2796
0
        spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch);
2797
0
        spdlog::error(
2798
0
            WasmEdge::ErrInfo::InfoMismatch(TType.getRefType(), GotType));
2799
0
        return nullptr;
2800
0
      }
2801
0
      auto Val = WasmEdge::ValVariant(
2802
0
                     to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value))
2803
0
                     .get<WasmEdge::RefVariant>();
2804
0
      if (!TType.getRefType().isNullableRefType() && Val.isNull()) {
2805
0
        spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
2806
0
        return nullptr;
2807
0
      }
2808
0
      return toTabCxt(
2809
0
          new WasmEdge::Runtime::Instance::TableInstance(TType, Val));
2810
0
    }
2811
0
  } catch (...) {
2812
0
    handleCAPIError();
2813
0
  }
2814
0
  return nullptr;
2815
0
}
2816
2817
WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext *
2818
WasmEdge_TableInstanceGetTableType(
2819
0
    const WasmEdge_TableInstanceContext *Cxt) noexcept {
2820
0
  if (Cxt) {
2821
0
    return toTabTypeCxt(&fromTabCxt(Cxt)->getTableType());
2822
0
  }
2823
0
  return nullptr;
2824
0
}
2825
2826
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceGetData(
2827
    const WasmEdge_TableInstanceContext *Cxt, WasmEdge_Value *Data,
2828
0
    const uint64_t Offset) noexcept {
2829
0
  return wrap([&]() { return fromTabCxt(Cxt)->getRefAddr(Offset); },
2830
0
              [&Data, &Cxt](auto &&Res) {
2831
0
                *Data = genWasmEdge_Value(
2832
0
                    *Res, fromTabCxt(Cxt)->getTableType().getRefType());
2833
0
              },
2834
0
              Cxt, Data);
2835
0
}
2836
2837
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceSetData(
2838
    WasmEdge_TableInstanceContext *Cxt, WasmEdge_Value Data,
2839
0
    const uint64_t Offset) noexcept {
2840
0
  return wrap(
2841
0
      [&]() -> WasmEdge::Expect<void> {
2842
        // Comparison of the value types needs the module instance to retrieve
2843
        // the function type index after applying the typed function reference
2844
        // proposal. It's impossible to do this without refactoring. Therefore
2845
        // simply match the FuncRef and ExternRef here.
2846
0
        WasmEdge::ValType ExpType =
2847
0
            fromTabCxt(Cxt)->getTableType().getRefType();
2848
0
        WasmEdge::ValType GotType = genValType(Data.Type);
2849
0
        if (!GotType.isRefType() ||
2850
0
            ExpType.isFuncRefType() != GotType.isFuncRefType()) {
2851
0
          spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch);
2852
0
          spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType));
2853
0
          return Unexpect(WasmEdge::ErrCode::Value::RefTypeMismatch);
2854
0
        }
2855
0
        auto Val = WasmEdge::ValVariant(
2856
0
                       to_WasmEdge_128_t<WasmEdge::uint128_t>(Data.Value))
2857
0
                       .get<WasmEdge::RefVariant>();
2858
0
        if (!ExpType.isNullableRefType() && Val.isNull()) {
2859
          // If this table is not a nullable ref type, the data should not be
2860
          // null.
2861
0
          spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
2862
0
          return Unexpect(WasmEdge::ErrCode::Value::NonNullRequired);
2863
0
        }
2864
0
        return fromTabCxt(Cxt)->setRefAddr(Offset, Val);
2865
0
      },
2866
0
      EmptyThen, Cxt);
2867
0
}
2868
2869
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_TableInstanceGetSize(
2870
0
    const WasmEdge_TableInstanceContext *Cxt) noexcept {
2871
0
  if (Cxt) {
2872
0
    return fromTabCxt(Cxt)->getSize();
2873
0
  }
2874
0
  return 0;
2875
0
}
2876
2877
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceGrow(
2878
0
    WasmEdge_TableInstanceContext *Cxt, const uint64_t Size) noexcept {
2879
0
  return wrap(
2880
0
      [&]() -> WasmEdge::Expect<void> {
2881
0
        if (fromTabCxt(Cxt)->growTable(Size)) {
2882
0
          return {};
2883
0
        } else {
2884
0
          spdlog::error(WasmEdge::ErrCode::Value::TableOutOfBounds);
2885
0
          return WasmEdge::Unexpect(WasmEdge::ErrCode::Value::TableOutOfBounds);
2886
0
        }
2887
0
      },
2888
0
      EmptyThen, Cxt);
2889
0
}
2890
2891
WASMEDGE_CAPI_EXPORT void
2892
0
WasmEdge_TableInstanceDelete(WasmEdge_TableInstanceContext *Cxt) noexcept {
2893
0
  delete fromTabCxt(Cxt);
2894
0
}
2895
2896
// <<<<<<<< WasmEdge table instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2897
2898
// >>>>>>>> WasmEdge memory instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2899
2900
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext *
2901
WasmEdge_MemoryInstanceCreate(
2902
0
    const WasmEdge_MemoryTypeContext *MemType) noexcept {
2903
0
  try {
2904
0
    if (MemType) {
2905
0
      return toMemCxt(new WasmEdge::Runtime::Instance::MemoryInstance(
2906
0
          *fromMemTypeCxt(MemType)));
2907
0
    }
2908
0
  } catch (...) {
2909
0
    handleCAPIError();
2910
0
  }
2911
0
  return nullptr;
2912
0
}
2913
2914
WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext *
2915
WasmEdge_MemoryInstanceGetMemoryType(
2916
0
    const WasmEdge_MemoryInstanceContext *Cxt) noexcept {
2917
0
  if (Cxt) {
2918
0
    return toMemTypeCxt(&fromMemCxt(Cxt)->getMemoryType());
2919
0
  }
2920
0
  return nullptr;
2921
0
}
2922
2923
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceGetData(
2924
    const WasmEdge_MemoryInstanceContext *Cxt, uint8_t *Data,
2925
0
    const uint64_t Offset, const uint64_t Length) noexcept {
2926
0
  return wrap([&]() { return fromMemCxt(Cxt)->getBytes(Offset, Length); },
2927
0
              [&](auto &&Res) { std::copy_n((*Res).begin(), Length, Data); },
2928
0
              Cxt, Data);
2929
0
}
2930
2931
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceSetData(
2932
    WasmEdge_MemoryInstanceContext *Cxt, const uint8_t *Data,
2933
0
    const uint64_t Offset, const uint64_t Length) noexcept {
2934
0
  return wrap(
2935
0
      [&]() {
2936
0
        return fromMemCxt(Cxt)->setBytes(genSpan(Data, Length), Offset, 0,
2937
0
                                         Length);
2938
0
      },
2939
0
      EmptyThen, Cxt, Data);
2940
0
}
2941
2942
WASMEDGE_CAPI_EXPORT uint8_t *
2943
WasmEdge_MemoryInstanceGetPointer(WasmEdge_MemoryInstanceContext *Cxt,
2944
                                  const uint64_t Offset,
2945
0
                                  const uint64_t Length) noexcept {
2946
0
  if (Cxt) {
2947
0
    const auto S = fromMemCxt(Cxt)->getSpan<uint8_t>(Offset, Length);
2948
0
    if (S.size() == Length) {
2949
0
      return S.data();
2950
0
    }
2951
0
  }
2952
0
  return nullptr;
2953
0
}
2954
2955
WASMEDGE_CAPI_EXPORT const uint8_t *WasmEdge_MemoryInstanceGetPointerConst(
2956
    const WasmEdge_MemoryInstanceContext *Cxt, const uint64_t Offset,
2957
0
    const uint64_t Length) noexcept {
2958
0
  if (Cxt) {
2959
0
    const auto S = fromMemCxt(Cxt)->getSpan<const uint8_t>(Offset, Length);
2960
0
    if (S.size() == Length) {
2961
0
      return S.data();
2962
0
    }
2963
0
  }
2964
0
  return nullptr;
2965
0
}
2966
2967
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_MemoryInstanceGetPageSize(
2968
0
    const WasmEdge_MemoryInstanceContext *Cxt) noexcept {
2969
0
  if (Cxt) {
2970
0
    return fromMemCxt(Cxt)->getPageSize();
2971
0
  }
2972
0
  return 0;
2973
0
}
2974
2975
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceGrowPage(
2976
0
    WasmEdge_MemoryInstanceContext *Cxt, const uint64_t Page) noexcept {
2977
0
  return wrap(
2978
0
      [&]() -> WasmEdge::Expect<void> {
2979
0
        if (fromMemCxt(Cxt)->growPage(Page)) {
2980
0
          return {};
2981
0
        } else {
2982
0
          spdlog::error(WasmEdge::ErrCode::Value::MemoryOutOfBounds);
2983
0
          return WasmEdge::Unexpect(
2984
0
              WasmEdge::ErrCode::Value::MemoryOutOfBounds);
2985
0
        }
2986
0
      },
2987
0
      EmptyThen, Cxt);
2988
0
}
2989
2990
WASMEDGE_CAPI_EXPORT void
2991
0
WasmEdge_MemoryInstanceDelete(WasmEdge_MemoryInstanceContext *Cxt) noexcept {
2992
0
  delete fromMemCxt(Cxt);
2993
0
}
2994
2995
// <<<<<<<< WasmEdge memory instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2996
2997
// >>>>>>>> WasmEdge tag instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2998
2999
WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext *
3000
WasmEdge_TagInstanceGetTagType(
3001
0
    const WasmEdge_TagInstanceContext *Cxt) noexcept {
3002
0
  if (Cxt) {
3003
0
    return toTagTypeCxt(&fromTagCxt(Cxt)->getTagType());
3004
0
  }
3005
0
  return nullptr;
3006
0
}
3007
3008
// <<<<<<<< WasmEdge tag instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3009
3010
// >>>>>>>> WasmEdge global instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3011
3012
WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext *
3013
WasmEdge_GlobalInstanceCreate(const WasmEdge_GlobalTypeContext *GlobType,
3014
0
                              const WasmEdge_Value Value) noexcept {
3015
0
  try {
3016
0
    if (GlobType) {
3017
      // Comparison of the value types needs the module instance to retrieve the
3018
      // function type index after applying the typed function reference
3019
      // proposal. It's impossible to do this without refactoring. Therefore
3020
      // simply match the FuncRef and ExternRef here.
3021
0
      const AST::GlobalType &GType = *fromGlobTypeCxt(GlobType);
3022
0
      WasmEdge::ValType ExpType = GType.getValType();
3023
0
      WasmEdge::ValType GotType = genValType(Value.Type);
3024
0
      if (ExpType.isFuncRefType() != GotType.isFuncRefType()) {
3025
0
        spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
3026
0
        spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType));
3027
0
        return nullptr;
3028
0
      }
3029
3030
0
      WasmEdge::ValVariant Val =
3031
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value);
3032
0
      if (ExpType.isRefType()) {
3033
        // Reference type case.
3034
0
        if (!ExpType.isNullableRefType() &&
3035
0
            Val.get<WasmEdge::RefVariant>().isNull()) {
3036
          // If this global is not a nullable ref type, the data should not be
3037
          // null.
3038
0
          spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
3039
0
          return nullptr;
3040
0
        }
3041
0
      } else {
3042
        // Number type case.
3043
0
        if (ExpType != GotType) {
3044
0
          spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
3045
0
          return nullptr;
3046
0
        }
3047
0
      }
3048
0
      return toGlobCxt(
3049
0
          new WasmEdge::Runtime::Instance::GlobalInstance(GType, Val));
3050
0
    }
3051
0
  } catch (...) {
3052
0
    handleCAPIError();
3053
0
  }
3054
0
  return nullptr;
3055
0
}
3056
3057
WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext *
3058
WasmEdge_GlobalInstanceGetGlobalType(
3059
0
    const WasmEdge_GlobalInstanceContext *Cxt) noexcept {
3060
0
  if (Cxt) {
3061
0
    return toGlobTypeCxt(&fromGlobCxt(Cxt)->getGlobalType());
3062
0
  }
3063
0
  return nullptr;
3064
0
}
3065
3066
WASMEDGE_CAPI_EXPORT WasmEdge_Value WasmEdge_GlobalInstanceGetValue(
3067
0
    const WasmEdge_GlobalInstanceContext *Cxt) noexcept {
3068
0
  if (Cxt) {
3069
0
    return genWasmEdge_Value(fromGlobCxt(Cxt)->getValue(),
3070
0
                             fromGlobCxt(Cxt)->getGlobalType().getValType());
3071
0
  }
3072
0
  return genWasmEdge_Value(
3073
0
      WasmEdge::ValVariant(static_cast<WasmEdge::uint128_t>(0U)),
3074
0
      TypeCode::I32);
3075
0
}
3076
3077
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_GlobalInstanceSetValue(
3078
0
    WasmEdge_GlobalInstanceContext *Cxt, const WasmEdge_Value Value) noexcept {
3079
0
  return wrap(
3080
0
      [&]() -> WasmEdge::Expect<void> {
3081
0
        const auto &GlobType = fromGlobCxt(Cxt)->getGlobalType();
3082
0
        if (GlobType.getValMut() != WasmEdge::ValMut::Var) {
3083
0
          spdlog::error(WasmEdge::ErrCode::Value::SetValueToConst);
3084
0
          return Unexpect(WasmEdge::ErrCode::Value::SetValueToConst);
3085
0
        }
3086
3087
        // Comparison of the value types needs the module instance to retrieve
3088
        // the function type index after applying the typed function reference
3089
        // proposal. It's impossible to do this without refactoring. Therefore
3090
        // simply match the FuncRef and ExternRef here.
3091
0
        WasmEdge::ValType ExpType = GlobType.getValType();
3092
0
        WasmEdge::ValType GotType = genValType(Value.Type);
3093
0
        if (ExpType.isRefType() &&
3094
0
            ExpType.isFuncRefType() != GotType.isFuncRefType()) {
3095
0
          spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch);
3096
0
          spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType));
3097
0
          return Unexpect(WasmEdge::ErrCode::Value::RefTypeMismatch);
3098
0
        }
3099
3100
0
        WasmEdge::ValVariant Val =
3101
0
            to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value);
3102
0
        if (ExpType.isRefType()) {
3103
          // Reference type case.
3104
0
          if (!ExpType.isNullableRefType() &&
3105
0
              Val.get<WasmEdge::RefVariant>().isNull()) {
3106
            // If this global is not a nullable ref type, the data should not be
3107
            // null.
3108
0
            spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
3109
0
            return Unexpect(WasmEdge::ErrCode::Value::NonNullRequired);
3110
0
          }
3111
0
        } else {
3112
          // Number type case.
3113
0
          if (ExpType != GotType) {
3114
0
            spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
3115
0
            return Unexpect(WasmEdge::ErrCode::Value::SetValueErrorType);
3116
0
          }
3117
0
        }
3118
0
        fromGlobCxt(Cxt)->setValue(Val);
3119
0
        return {};
3120
0
      },
3121
0
      EmptyThen, Cxt);
3122
0
}
3123
3124
WASMEDGE_CAPI_EXPORT void
3125
0
WasmEdge_GlobalInstanceDelete(WasmEdge_GlobalInstanceContext *Cxt) noexcept {
3126
0
  delete fromGlobCxt(Cxt);
3127
0
}
3128
3129
// <<<<<<<< WasmEdge global instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3130
3131
// >>>>>>>> WasmEdge calling frame functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3132
3133
WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext *WasmEdge_CallingFrameGetExecutor(
3134
0
    const WasmEdge_CallingFrameContext *Cxt) noexcept {
3135
0
  if (Cxt) {
3136
0
    return toExecutorCxt(fromCallFrameCxt(Cxt)->getExecutor());
3137
0
  }
3138
0
  return nullptr;
3139
0
}
3140
3141
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
3142
WasmEdge_CallingFrameGetModuleInstance(
3143
0
    const WasmEdge_CallingFrameContext *Cxt) noexcept {
3144
0
  if (Cxt) {
3145
0
    return toModCxt(fromCallFrameCxt(Cxt)->getModule());
3146
0
  }
3147
0
  return nullptr;
3148
0
}
3149
3150
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext *
3151
WasmEdge_CallingFrameGetMemoryInstance(const WasmEdge_CallingFrameContext *Cxt,
3152
0
                                       const uint32_t Idx) noexcept {
3153
0
  if (Cxt) {
3154
0
    return toMemCxt(fromCallFrameCxt(Cxt)->getMemoryByIndex(Idx));
3155
0
  }
3156
0
  return nullptr;
3157
0
}
3158
3159
// <<<<<<<< WasmEdge calling frame functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3160
3161
// >>>>>>>> WasmEdge Async functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3162
3163
WASMEDGE_CAPI_EXPORT void
3164
0
WasmEdge_AsyncWait(const WasmEdge_Async *Cxt) noexcept {
3165
0
  if (Cxt) {
3166
0
    Cxt->Async.wait();
3167
0
  }
3168
0
}
3169
3170
WASMEDGE_CAPI_EXPORT bool
3171
WasmEdge_AsyncWaitFor(const WasmEdge_Async *Cxt,
3172
0
                      uint64_t Milliseconds) noexcept {
3173
0
  if (Cxt) {
3174
0
    return Cxt->Async.waitFor(std::chrono::milliseconds(Milliseconds));
3175
0
  }
3176
0
  return false;
3177
0
}
3178
3179
0
WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncCancel(WasmEdge_Async *Cxt) noexcept {
3180
0
  if (Cxt) {
3181
0
    Cxt->Async.cancel();
3182
0
  }
3183
0
}
3184
3185
WASMEDGE_CAPI_EXPORT uint32_t
3186
0
WasmEdge_AsyncGetReturnsLength(const WasmEdge_Async *Cxt) noexcept {
3187
0
  if (Cxt) {
3188
0
    if (auto Res = Cxt->Async.get()) {
3189
0
      return static_cast<uint32_t>((*Res).size());
3190
0
    }
3191
0
  }
3192
0
  return 0;
3193
0
}
3194
3195
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3196
WasmEdge_AsyncGet(const WasmEdge_Async *Cxt, WasmEdge_Value *Returns,
3197
0
                  const uint32_t ReturnLen) noexcept {
3198
0
  return wrap(
3199
0
      [&]() { return Cxt->Async.get(); },
3200
0
      [&](auto Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, Cxt);
3201
0
}
3202
3203
0
WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncDelete(WasmEdge_Async *Cxt) noexcept {
3204
0
  delete Cxt;
3205
0
}
3206
3207
// <<<<<<<< WasmEdge Async functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3208
3209
// >>>>>>>> WasmEdge VM functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3210
3211
WASMEDGE_CAPI_EXPORT WasmEdge_VMContext *
3212
WasmEdge_VMCreate(const WasmEdge_ConfigureContext *ConfCxt,
3213
0
                  WasmEdge_StoreContext *StoreCxt) noexcept {
3214
0
  try {
3215
0
    if (ConfCxt) {
3216
0
      if (StoreCxt) {
3217
0
        return new WasmEdge_VMContext(ConfCxt->Conf, *fromStoreCxt(StoreCxt));
3218
0
      } else {
3219
0
        return new WasmEdge_VMContext(ConfCxt->Conf);
3220
0
      }
3221
0
    } else {
3222
0
      if (StoreCxt) {
3223
0
        return new WasmEdge_VMContext(WasmEdge::Configure(),
3224
0
                                      *fromStoreCxt(StoreCxt));
3225
0
      } else {
3226
0
        return new WasmEdge_VMContext(WasmEdge::Configure());
3227
0
      }
3228
0
    }
3229
0
  } catch (...) {
3230
0
    handleCAPIError();
3231
0
    return nullptr;
3232
0
  }
3233
0
}
3234
3235
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromFile(
3236
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3237
0
    const char *Path) noexcept {
3238
0
  return wrap(
3239
0
      [&]() {
3240
0
        return Cxt->VM.registerModule(genStrView(ModuleName), genPath(Path));
3241
0
      },
3242
0
      EmptyThen, Cxt);
3243
0
}
3244
3245
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromBuffer(
3246
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3247
0
    const uint8_t *Buf, const uint32_t BufLen) noexcept {
3248
0
  return WasmEdge_VMRegisterModuleFromBytes(Cxt, ModuleName,
3249
0
                                            WasmEdge_BytesWrap(Buf, BufLen));
3250
0
}
3251
3252
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromBytes(
3253
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3254
0
    const WasmEdge_Bytes Bytes) noexcept {
3255
0
  return wrap(
3256
0
      [&]() {
3257
0
        return Cxt->VM.registerModule(genStrView(ModuleName),
3258
0
                                      genSpan(Bytes.Buf, Bytes.Length));
3259
0
      },
3260
0
      EmptyThen, Cxt);
3261
0
}
3262
3263
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromASTModule(
3264
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3265
0
    const WasmEdge_ASTModuleContext *ASTCxt) noexcept {
3266
0
  return wrap(
3267
0
      [&]() {
3268
0
        return Cxt->VM.registerModule(genStrView(ModuleName),
3269
0
                                      *fromASTModCxt(ASTCxt));
3270
0
      },
3271
0
      EmptyThen, Cxt, ASTCxt);
3272
0
}
3273
3274
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromImport(
3275
    WasmEdge_VMContext *Cxt,
3276
0
    const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept {
3277
0
  return wrap([&]() { return Cxt->VM.registerModule(*fromModCxt(ImportCxt)); },
3278
0
              EmptyThen, Cxt, ImportCxt);
3279
0
}
3280
3281
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3282
WasmEdge_VMRegisterModuleFromImportWithAlias(
3283
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3284
0
    const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept {
3285
0
  return wrap(
3286
0
      [&]() {
3287
0
        return Cxt->VM.registerModule(genStrView(ModuleName),
3288
0
                                      *fromModCxt(ImportCxt));
3289
0
      },
3290
0
      EmptyThen, Cxt, ImportCxt);
3291
0
}
3292
3293
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromFile(
3294
    WasmEdge_VMContext *Cxt, const char *Path, const WasmEdge_String FuncName,
3295
    const WasmEdge_Value *Params, const uint32_t ParamLen,
3296
0
    WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept {
3297
0
  try {
3298
0
    auto ParamPair = genParamPair(Params, ParamLen);
3299
0
    return wrap(
3300
0
        [&]() {
3301
0
          return Cxt->VM.runWasmFile(genPath(Path), genStrView(FuncName),
3302
0
                                     ParamPair.first, ParamPair.second);
3303
0
        },
3304
0
        [&](auto Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3305
0
        Cxt);
3306
0
  } catch (...) {
3307
0
    return handleCAPIError();
3308
0
  }
3309
0
}
3310
3311
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromBuffer(
3312
    WasmEdge_VMContext *Cxt, const uint8_t *Buf, const uint32_t BufLen,
3313
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3314
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3315
0
    const uint32_t ReturnLen) noexcept {
3316
0
  return WasmEdge_VMRunWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen),
3317
0
                                     FuncName, Params, ParamLen, Returns,
3318
0
                                     ReturnLen);
3319
0
}
3320
3321
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromBytes(
3322
    WasmEdge_VMContext *Cxt, const WasmEdge_Bytes Bytes,
3323
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3324
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3325
0
    const uint32_t ReturnLen) noexcept {
3326
0
  try {
3327
0
    auto ParamPair = genParamPair(Params, ParamLen);
3328
0
    return wrap(
3329
0
        [&]() {
3330
0
          return Cxt->VM.runWasmFile(genSpan(Bytes.Buf, Bytes.Length),
3331
0
                                     genStrView(FuncName), ParamPair.first,
3332
0
                                     ParamPair.second);
3333
0
        },
3334
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3335
0
        Cxt);
3336
0
  } catch (...) {
3337
0
    return handleCAPIError();
3338
0
  }
3339
0
}
3340
3341
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromASTModule(
3342
    WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt,
3343
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3344
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3345
0
    const uint32_t ReturnLen) noexcept {
3346
0
  try {
3347
0
    auto ParamPair = genParamPair(Params, ParamLen);
3348
0
    return wrap(
3349
0
        [&]() {
3350
0
          return Cxt->VM.runWasmFile(*fromASTModCxt(ASTCxt),
3351
0
                                     genStrView(FuncName), ParamPair.first,
3352
0
                                     ParamPair.second);
3353
0
        },
3354
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3355
0
        Cxt, ASTCxt);
3356
0
  } catch (...) {
3357
0
    return handleCAPIError();
3358
0
  }
3359
0
}
3360
3361
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromFile(
3362
    WasmEdge_VMContext *Cxt, const char *Path, const WasmEdge_String FuncName,
3363
0
    const WasmEdge_Value *Params, const uint32_t ParamLen) noexcept {
3364
0
  try {
3365
0
    auto ParamPair = genParamPair(Params, ParamLen);
3366
0
    if (Cxt) {
3367
0
      return new WasmEdge_Async(
3368
0
          Cxt->VM.asyncRunWasmFile(genPath(Path), genStrView(FuncName),
3369
0
                                   ParamPair.first, ParamPair.second));
3370
0
    }
3371
0
  } catch (...) {
3372
0
    handleCAPIError();
3373
0
  }
3374
0
  return nullptr;
3375
0
}
3376
3377
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromBuffer(
3378
    WasmEdge_VMContext *Cxt, const uint8_t *Buf, const uint32_t BufLen,
3379
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3380
0
    const uint32_t ParamLen) noexcept {
3381
0
  return WasmEdge_VMAsyncRunWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen),
3382
0
                                          FuncName, Params, ParamLen);
3383
0
}
3384
3385
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromBytes(
3386
    WasmEdge_VMContext *Cxt, const WasmEdge_Bytes Bytes,
3387
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3388
0
    const uint32_t ParamLen) noexcept {
3389
0
  try {
3390
0
    auto ParamPair = genParamPair(Params, ParamLen);
3391
0
    if (Cxt) {
3392
0
      return new WasmEdge_Async(Cxt->VM.asyncRunWasmFile(
3393
0
          genSpan(Bytes.Buf, Bytes.Length), genStrView(FuncName),
3394
0
          ParamPair.first, ParamPair.second));
3395
0
    }
3396
0
  } catch (...) {
3397
0
    handleCAPIError();
3398
0
  }
3399
0
  return nullptr;
3400
0
}
3401
3402
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromASTModule(
3403
    WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt,
3404
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3405
0
    const uint32_t ParamLen) noexcept {
3406
0
  try {
3407
0
    auto ParamPair = genParamPair(Params, ParamLen);
3408
0
    if (Cxt && ASTCxt) {
3409
0
      return new WasmEdge_Async(
3410
0
          Cxt->VM.asyncRunWasmFile(*fromASTModCxt(ASTCxt), genStrView(FuncName),
3411
0
                                   ParamPair.first, ParamPair.second));
3412
0
    }
3413
0
  } catch (...) {
3414
0
    handleCAPIError();
3415
0
  }
3416
0
  return nullptr;
3417
0
}
3418
3419
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMLoadWasmFromFile(
3420
0
    WasmEdge_VMContext *Cxt, const char *Path) noexcept {
3421
0
  return wrap([&]() { return Cxt->VM.loadWasm(genPath(Path)); }, EmptyThen,
3422
0
              Cxt);
3423
0
}
3424
3425
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3426
WasmEdge_VMLoadWasmFromBuffer(WasmEdge_VMContext *Cxt, const uint8_t *Buf,
3427
0
                              const uint32_t BufLen) noexcept {
3428
0
  return WasmEdge_VMLoadWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen));
3429
0
}
3430
3431
WASMEDGE_CAPI_EXPORT extern WasmEdge_Result
3432
WasmEdge_VMLoadWasmFromBytes(WasmEdge_VMContext *Cxt,
3433
0
                             const WasmEdge_Bytes Bytes) noexcept {
3434
0
  return wrap(
3435
0
      [&]() { return Cxt->VM.loadWasm(genSpan(Bytes.Buf, Bytes.Length)); },
3436
0
      EmptyThen, Cxt);
3437
0
}
3438
3439
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMLoadWasmFromASTModule(
3440
0
    WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt) noexcept {
3441
0
  return wrap([&]() { return Cxt->VM.loadWasm(*fromASTModCxt(ASTCxt)); },
3442
0
              EmptyThen, Cxt, ASTCxt);
3443
0
}
3444
3445
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3446
0
WasmEdge_VMValidate(WasmEdge_VMContext *Cxt) noexcept {
3447
0
  return wrap([&]() { return Cxt->VM.validate(); }, EmptyThen, Cxt);
3448
0
}
3449
3450
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3451
0
WasmEdge_VMInstantiate(WasmEdge_VMContext *Cxt) noexcept {
3452
0
  return wrap([&]() { return Cxt->VM.instantiate(); }, EmptyThen, Cxt);
3453
0
}
3454
3455
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3456
WasmEdge_VMExecute(WasmEdge_VMContext *Cxt, const WasmEdge_String FuncName,
3457
                   const WasmEdge_Value *Params, const uint32_t ParamLen,
3458
0
                   WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept {
3459
0
  try {
3460
0
    auto ParamPair = genParamPair(Params, ParamLen);
3461
0
    return wrap(
3462
0
        [&]() {
3463
0
          return Cxt->VM.execute(genStrView(FuncName), ParamPair.first,
3464
0
                                 ParamPair.second);
3465
0
        },
3466
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3467
0
        Cxt);
3468
0
  } catch (...) {
3469
0
    return handleCAPIError();
3470
0
  }
3471
0
}
3472
3473
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMExecuteRegistered(
3474
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3475
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3476
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3477
0
    const uint32_t ReturnLen) noexcept {
3478
0
  try {
3479
0
    auto ParamPair = genParamPair(Params, ParamLen);
3480
0
    return wrap(
3481
0
        [&]() {
3482
0
          return Cxt->VM.execute(genStrView(ModuleName), genStrView(FuncName),
3483
0
                                 ParamPair.first, ParamPair.second);
3484
0
        },
3485
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3486
0
        Cxt);
3487
0
  } catch (...) {
3488
0
    return handleCAPIError();
3489
0
  }
3490
0
}
3491
3492
WASMEDGE_CAPI_EXPORT WasmEdge_Async *
3493
WasmEdge_VMAsyncExecute(WasmEdge_VMContext *Cxt, const WasmEdge_String FuncName,
3494
                        const WasmEdge_Value *Params,
3495
0
                        const uint32_t ParamLen) noexcept {
3496
0
  try {
3497
0
    auto ParamPair = genParamPair(Params, ParamLen);
3498
0
    if (Cxt) {
3499
0
      return new WasmEdge_Async(Cxt->VM.asyncExecute(
3500
0
          genStrView(FuncName), ParamPair.first, ParamPair.second));
3501
0
    }
3502
0
  } catch (...) {
3503
0
    handleCAPIError();
3504
0
  }
3505
0
  return nullptr;
3506
0
}
3507
3508
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncExecuteRegistered(
3509
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3510
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3511
0
    const uint32_t ParamLen) noexcept {
3512
0
  try {
3513
0
    auto ParamPair = genParamPair(Params, ParamLen);
3514
0
    if (Cxt) {
3515
0
      return new WasmEdge_Async(
3516
0
          Cxt->VM.asyncExecute(genStrView(ModuleName), genStrView(FuncName),
3517
0
                               ParamPair.first, ParamPair.second));
3518
0
    }
3519
0
  } catch (...) {
3520
0
    handleCAPIError();
3521
0
  }
3522
0
  return nullptr;
3523
0
}
3524
3525
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
3526
WasmEdge_VMGetFunctionType(const WasmEdge_VMContext *Cxt,
3527
0
                           const WasmEdge_String FuncName) noexcept {
3528
0
  try {
3529
0
    if (Cxt) {
3530
0
      const auto FuncList = Cxt->VM.getFunctionList();
3531
0
      for (const auto &It : FuncList) {
3532
0
        if (It.first == genStrView(FuncName)) {
3533
0
          return toFuncTypeCxt(&It.second);
3534
0
        }
3535
0
      }
3536
0
    }
3537
0
  } catch (...) {
3538
0
    handleCAPIError();
3539
0
  }
3540
0
  return nullptr;
3541
0
}
3542
3543
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
3544
WasmEdge_VMGetFunctionTypeRegistered(const WasmEdge_VMContext *Cxt,
3545
                                     const WasmEdge_String ModuleName,
3546
0
                                     const WasmEdge_String FuncName) noexcept {
3547
0
  if (Cxt) {
3548
0
    const auto *ModInst =
3549
0
        Cxt->VM.getStoreManager().findModule(genStrView(ModuleName));
3550
0
    if (ModInst != nullptr) {
3551
0
      const auto *FuncInst = ModInst->findFuncExports(genStrView(FuncName));
3552
0
      if (FuncInst != nullptr) {
3553
0
        return toFuncTypeCxt(&FuncInst->getFuncType());
3554
0
      }
3555
0
    }
3556
0
  }
3557
0
  return nullptr;
3558
0
}
3559
3560
0
WASMEDGE_CAPI_EXPORT void WasmEdge_VMCleanup(WasmEdge_VMContext *Cxt) noexcept {
3561
0
  if (Cxt) {
3562
0
    Cxt->VM.cleanup();
3563
0
  }
3564
0
}
3565
3566
WASMEDGE_CAPI_EXPORT void
3567
WasmEdge_VMDeleteRegisteredModule(const WasmEdge_VMContext *Cxt,
3568
0
                                  const WasmEdge_String ModuleName) noexcept {
3569
0
  if (!Cxt || !ModuleName.Buf) {
3570
0
    return; // Invalid input
3571
0
  }
3572
3573
0
  auto &VM = const_cast<WasmEdge_VMContext *>(Cxt)->VM;
3574
0
  auto &StoreMgr = VM.getStoreManager();
3575
0
  auto NameStr = genStrView(ModuleName);
3576
3577
  // Unregister from VM
3578
0
  VM.unregisterModule(NameStr);
3579
3580
0
  if (StoreMgr.findModule(NameStr)) {
3581
    // Unregister from StoreManager
3582
0
    StoreMgr.unregisterModule(NameStr);
3583
0
  }
3584
0
}
3585
3586
WASMEDGE_CAPI_EXPORT void WasmEdge_VMForceDeleteRegisteredModule(
3587
0
    const WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName) noexcept {
3588
0
  WasmEdge_VMDeleteRegisteredModule(Cxt, ModuleName);
3589
0
}
3590
3591
WASMEDGE_CAPI_EXPORT uint32_t
3592
0
WasmEdge_VMGetFunctionListLength(const WasmEdge_VMContext *Cxt) noexcept {
3593
0
  try {
3594
0
    if (Cxt) {
3595
0
      return static_cast<uint32_t>(Cxt->VM.getFunctionList().size());
3596
0
    }
3597
0
  } catch (...) {
3598
0
    handleCAPIError();
3599
0
  }
3600
0
  return 0;
3601
0
}
3602
3603
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VMGetFunctionList(
3604
    const WasmEdge_VMContext *Cxt, WasmEdge_String *Names,
3605
    const WasmEdge_FunctionTypeContext **FuncTypes,
3606
0
    const uint32_t Len) noexcept {
3607
0
  if (Cxt) {
3608
    // Do not use VM::getFunctionList() here because it would allocate the
3609
    // returned function name strings.
3610
0
    const auto *ModInst = Cxt->VM.getActiveModule();
3611
0
    if (ModInst != nullptr) {
3612
0
      return ModInst->getFuncExports([&](const auto &FuncExp) {
3613
0
        uint32_t I = 0;
3614
0
        for (auto It = FuncExp.cbegin(); It != FuncExp.cend() && I < Len;
3615
0
             It++, I++) {
3616
0
          const auto *FuncInst = It->second;
3617
0
          const auto &FuncType = FuncInst->getFuncType();
3618
0
          if (Names) {
3619
0
            Names[I] = WasmEdge_String{
3620
0
                /* Length */ static_cast<uint32_t>(It->first.length()),
3621
0
                /* Buf */ It->first.data()};
3622
0
          }
3623
0
          if (FuncTypes) {
3624
0
            FuncTypes[I] = toFuncTypeCxt(&FuncType);
3625
0
          }
3626
0
        }
3627
0
        return static_cast<uint32_t>(FuncExp.size());
3628
0
      });
3629
0
    }
3630
0
  }
3631
0
  return 0;
3632
0
}
3633
3634
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
3635
WasmEdge_VMGetImportModuleContext(
3636
    const WasmEdge_VMContext *Cxt,
3637
0
    const enum WasmEdge_HostRegistration Reg) noexcept {
3638
0
  if (Cxt) {
3639
0
    return toModCxt(
3640
0
        Cxt->VM.getImportModule(static_cast<WasmEdge::HostRegistration>(Reg)));
3641
0
  }
3642
0
  return nullptr;
3643
0
}
3644
3645
WASMEDGE_CAPI_EXPORT uint32_t
3646
0
WasmEdge_VMListRegisteredModuleLength(const WasmEdge_VMContext *Cxt) noexcept {
3647
0
  if (Cxt) {
3648
0
    return Cxt->VM.getStoreManager().getModuleListSize();
3649
0
  }
3650
0
  return 0;
3651
0
}
3652
3653
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VMListRegisteredModule(
3654
    const WasmEdge_VMContext *Cxt, WasmEdge_String *Names,
3655
0
    const uint32_t Len) noexcept {
3656
0
  if (Cxt) {
3657
0
    return Cxt->VM.getStoreManager().getModuleList(
3658
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
3659
0
  }
3660
0
  return 0;
3661
0
}
3662
3663
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
3664
WasmEdge_VMGetRegisteredModule(const WasmEdge_VMContext *Cxt,
3665
0
                               const WasmEdge_String ModuleName) noexcept {
3666
0
  if (Cxt) {
3667
0
    return toModCxt(
3668
0
        Cxt->VM.getStoreManager().findModule(genStrView(ModuleName)));
3669
0
  }
3670
0
  return nullptr;
3671
0
}
3672
3673
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
3674
0
WasmEdge_VMGetActiveModule(const WasmEdge_VMContext *Cxt) noexcept {
3675
0
  if (Cxt) {
3676
0
    return toModCxt(Cxt->VM.getActiveModule());
3677
0
  }
3678
0
  return nullptr;
3679
0
}
3680
3681
WASMEDGE_CAPI_EXPORT WasmEdge_StoreContext *
3682
0
WasmEdge_VMGetStoreContext(WasmEdge_VMContext *Cxt) noexcept {
3683
0
  if (Cxt) {
3684
0
    return toStoreCxt(&Cxt->VM.getStoreManager());
3685
0
  }
3686
0
  return nullptr;
3687
0
}
3688
3689
WASMEDGE_CAPI_EXPORT WasmEdge_LoaderContext *
3690
0
WasmEdge_VMGetLoaderContext(WasmEdge_VMContext *Cxt) noexcept {
3691
0
  if (Cxt) {
3692
0
    return toLoaderCxt(&Cxt->VM.getLoader());
3693
0
  }
3694
0
  return nullptr;
3695
0
}
3696
3697
WASMEDGE_CAPI_EXPORT WasmEdge_ValidatorContext *
3698
0
WasmEdge_VMGetValidatorContext(WasmEdge_VMContext *Cxt) noexcept {
3699
0
  if (Cxt) {
3700
0
    return toValidatorCxt(&Cxt->VM.getValidator());
3701
0
  }
3702
0
  return nullptr;
3703
0
}
3704
3705
WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext *
3706
0
WasmEdge_VMGetExecutorContext(WasmEdge_VMContext *Cxt) noexcept {
3707
0
  if (Cxt) {
3708
0
    return toExecutorCxt(&Cxt->VM.getExecutor());
3709
0
  }
3710
0
  return nullptr;
3711
0
}
3712
3713
WASMEDGE_CAPI_EXPORT WasmEdge_StatisticsContext *
3714
0
WasmEdge_VMGetStatisticsContext(WasmEdge_VMContext *Cxt) noexcept {
3715
0
  if (Cxt) {
3716
0
    return toStatCxt(&Cxt->VM.getStatistics());
3717
0
  }
3718
0
  return nullptr;
3719
0
}
3720
3721
0
WASMEDGE_CAPI_EXPORT void WasmEdge_VMDelete(WasmEdge_VMContext *Cxt) noexcept {
3722
0
  delete Cxt;
3723
0
}
3724
3725
// <<<<<<<< WasmEdge VM functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3726
3727
// >>>>>>>> WasmEdge Driver functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3728
3729
#if WASMEDGE_OS_WINDOWS
3730
WASMEDGE_CAPI_EXPORT const char **
3731
WasmEdge_Driver_ArgvCreate(int Argc, const wchar_t *Argv[]) noexcept {
3732
  try {
3733
    const Span<const wchar_t *> Args(Argv, static_cast<size_t>(Argc));
3734
    const size_t PointerArraySize = static_cast<size_t>(Argc) * sizeof(char *);
3735
    size_t StringBufferSize = 0;
3736
    for (auto Arg : Args) {
3737
      const auto Res = std::max<size_t>(
3738
          static_cast<size_t>(winapi::WideCharToMultiByte(
3739
              winapi::CP_UTF8_, 0, Arg, -1, nullptr, 0, nullptr, nullptr)),
3740
          1u);
3741
      StringBufferSize += Res;
3742
    }
3743
    auto Buffer = std::make_unique<char[]>(PointerArraySize + StringBufferSize);
3744
    Span<char *> PointerArray(reinterpret_cast<char **>(Buffer.get()),
3745
                              static_cast<size_t>(Argc));
3746
    Span<char> StringBuffer(Buffer.get() + PointerArraySize, StringBufferSize);
3747
    for (auto Arg : Args) {
3748
      PointerArray[0] = StringBuffer.data();
3749
      PointerArray = PointerArray.subspan(1);
3750
      const auto Res = std::max<size_t>(
3751
          static_cast<size_t>(winapi::WideCharToMultiByte(
3752
              winapi::CP_UTF8_, 0, Arg, -1, StringBuffer.data(),
3753
              static_cast<int>(StringBuffer.size()), nullptr, nullptr)),
3754
          1);
3755
      StringBuffer = StringBuffer.subspan(Res);
3756
    }
3757
3758
    return reinterpret_cast<const char **>(Buffer.release());
3759
  } catch (...) {
3760
    handleCAPIError();
3761
    return nullptr;
3762
  }
3763
}
3764
3765
WASMEDGE_CAPI_EXPORT void
3766
WasmEdge_Driver_ArgvDelete(const char *Argv[]) noexcept {
3767
  std::unique_ptr<char[]> Buffer(reinterpret_cast<char *>(Argv));
3768
  Buffer.reset();
3769
}
3770
3771
WASMEDGE_CAPI_EXPORT void
3772
WasmEdge_Driver_SetConsoleOutputCPtoUTF8(void) noexcept {
3773
#if WINAPI_PARTITION_DESKTOP
3774
  winapi::SetConsoleOutputCP(winapi::CP_UTF8_);
3775
#endif
3776
}
3777
#endif
3778
3779
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Compiler(int Argc,
3780
0
                                                  const char *Argv[]) noexcept {
3781
0
  try {
3782
0
    return WasmEdge::Driver::UniTool(Argc, Argv,
3783
0
                                     WasmEdge::Driver::ToolType::Compiler);
3784
0
  } catch (...) {
3785
0
    handleCAPIError();
3786
0
    return EXIT_FAILURE;
3787
0
  }
3788
0
}
3789
3790
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Tool(int Argc,
3791
0
                                              const char *Argv[]) noexcept {
3792
0
  try {
3793
0
    return WasmEdge::Driver::UniTool(Argc, Argv,
3794
0
                                     WasmEdge::Driver::ToolType::Tool);
3795
0
  } catch (...) {
3796
0
    handleCAPIError();
3797
0
    return EXIT_FAILURE;
3798
0
  }
3799
0
}
3800
3801
#ifdef WASMEDGE_BUILD_WASI_NN_RPC
3802
WASMEDGE_CAPI_EXPORT int
3803
WasmEdge_Driver_WasiNNRPCServer(int Argc, const char *Argv[]) noexcept {
3804
  try {
3805
    return WasmEdge::Driver::WasiNNRPCServer(Argc, Argv);
3806
  } catch (...) {
3807
    handleCAPIError();
3808
    return EXIT_FAILURE;
3809
  }
3810
}
3811
#endif
3812
3813
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_UniTool(int Argc,
3814
0
                                                 const char *Argv[]) noexcept {
3815
0
  try {
3816
0
    return WasmEdge::Driver::UniTool(Argc, Argv,
3817
0
                                     WasmEdge::Driver::ToolType::All);
3818
0
  } catch (...) {
3819
0
    handleCAPIError();
3820
0
    return EXIT_FAILURE;
3821
0
  }
3822
0
}
3823
3824
#ifdef WASMEDGE_BUILD_FUZZING
3825
WASMEDGE_CAPI_EXPORT extern "C" int
3826
9.38k
WasmEdge_Driver_FuzzTool(const uint8_t *Data, size_t Size) noexcept {
3827
9.38k
  try {
3828
9.38k
    return WasmEdge::Driver::FuzzTool(Data, Size);
3829
9.38k
  } catch (...) {
3830
0
    handleCAPIError();
3831
0
    return EXIT_FAILURE;
3832
0
  }
3833
9.38k
}
3834
3835
WASMEDGE_CAPI_EXPORT extern "C" int
3836
1.59k
WasmEdge_Driver_FuzzPO(const uint8_t *Data, size_t Size) noexcept {
3837
1.59k
  try {
3838
1.59k
    return WasmEdge::Driver::FuzzPO(Data, Size);
3839
1.59k
  } catch (...) {
3840
0
    handleCAPIError();
3841
0
    return EXIT_FAILURE;
3842
0
  }
3843
1.59k
}
3844
#endif
3845
3846
// <<<<<<<< WasmEdge Driver functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3847
3848
// >>>>>>>> WasmEdge Plugin functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3849
3850
0
WASMEDGE_CAPI_EXPORT void WasmEdge_PluginLoadWithDefaultPaths(void) noexcept {
3851
0
  WasmEdge::Plugin::Plugin::loadFromDefaultPaths();
3852
0
}
3853
3854
WASMEDGE_CAPI_EXPORT void
3855
0
WasmEdge_PluginLoadFromPath(const char *Path) noexcept {
3856
  // Not genPath(): this is noexcept and absolute() can throw.
3857
0
  WasmEdge::Plugin::Plugin::load(std::filesystem::path(Path ? Path : ""));
3858
0
}
3859
3860
0
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_PluginListPluginsLength(void) noexcept {
3861
0
  return static_cast<uint32_t>(WasmEdge::Plugin::Plugin::plugins().size());
3862
0
}
3863
3864
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_PluginListPlugins(
3865
0
    WasmEdge_String *Names, const uint32_t Len) noexcept {
3866
0
  const auto &PList = WasmEdge::Plugin::Plugin::plugins();
3867
0
  if (Names) {
3868
0
    for (uint32_t I = 0; I < Len && I < PList.size(); I++) {
3869
0
      Names[I] = WasmEdge_String{
3870
0
          /* Length */ static_cast<uint32_t>(std::strlen(PList[I].name())),
3871
0
          /* Buf */ PList[I].name()};
3872
0
    }
3873
0
  }
3874
0
  return static_cast<uint32_t>(PList.size());
3875
0
}
3876
3877
WASMEDGE_CAPI_EXPORT const WasmEdge_PluginContext *
3878
0
WasmEdge_PluginFind(const WasmEdge_String Name) noexcept {
3879
0
  return toPluginCxt(WasmEdge::Plugin::Plugin::find(genStrView(Name)));
3880
0
}
3881
3882
WASMEDGE_CAPI_EXPORT WasmEdge_String
3883
0
WasmEdge_PluginGetPluginName(const WasmEdge_PluginContext *Cxt) noexcept {
3884
0
  if (Cxt) {
3885
0
    const char *Name = fromPluginCxt(Cxt)->name();
3886
0
    return WasmEdge_String{
3887
0
        /* Length */ static_cast<uint32_t>(std::strlen(Name)),
3888
0
        /* Buf */ Name};
3889
0
  }
3890
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
3891
0
}
3892
3893
WASMEDGE_CAPI_EXPORT uint32_t
3894
0
WasmEdge_PluginListModuleLength(const WasmEdge_PluginContext *Cxt) noexcept {
3895
0
  if (Cxt) {
3896
0
    return static_cast<uint32_t>(fromPluginCxt(Cxt)->modules().size());
3897
0
  }
3898
0
  return 0;
3899
0
}
3900
3901
WASMEDGE_CAPI_EXPORT uint32_t
3902
WasmEdge_PluginListModule(const WasmEdge_PluginContext *Cxt,
3903
0
                          WasmEdge_String *Names, const uint32_t Len) noexcept {
3904
0
  if (Cxt) {
3905
0
    auto MList = fromPluginCxt(Cxt)->modules();
3906
0
    if (Names) {
3907
0
      for (uint32_t I = 0; I < Len && I < MList.size(); I++) {
3908
0
        Names[I] = WasmEdge_String{
3909
0
            /* Length */ static_cast<uint32_t>(std::strlen(MList[I].name())),
3910
0
            /* Buf */ MList[I].name()};
3911
0
      }
3912
0
    }
3913
0
    return static_cast<uint32_t>(MList.size());
3914
0
  }
3915
0
  return 0;
3916
0
}
3917
3918
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
3919
WasmEdge_PluginCreateModule(const WasmEdge_PluginContext *Cxt,
3920
0
                            const WasmEdge_String ModuleName) noexcept {
3921
0
  try {
3922
0
    if (Cxt) {
3923
0
      if (const auto *PMod =
3924
0
              fromPluginCxt(Cxt)->findModule(genStrView(ModuleName));
3925
0
          PMod) {
3926
0
        return toModCxt(PMod->create().release());
3927
0
      }
3928
0
    }
3929
0
  } catch (...) {
3930
0
    handleCAPIError();
3931
0
  }
3932
0
  return nullptr;
3933
0
}
3934
3935
WASMEDGE_CAPI_EXPORT void
3936
WasmEdge_PluginInitWASINN(const char *const *NNPreloads,
3937
0
                          const uint32_t PreloadsLen) noexcept {
3938
0
  try {
3939
0
    using namespace std::literals::string_view_literals;
3940
0
    if (const auto *Plugin = WasmEdge::Plugin::Plugin::find("wasi_nn"sv)) {
3941
0
      PO::ArgumentParser Parser;
3942
0
      Plugin->registerOptions(Parser);
3943
0
      Parser.set_raw_value<std::vector<std::string>>(
3944
0
          "nn-preload"sv,
3945
0
          std::vector<std::string>(NNPreloads, NNPreloads + PreloadsLen));
3946
0
    }
3947
0
  } catch (...) {
3948
0
    handleCAPIError();
3949
0
  }
3950
0
}
3951
3952
// <<<<<<<< WasmEdge Plugin functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3953
3954
// >>>>>>>> WasmEdge Experimental functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3955
3956
WASMEDGE_CAPI_EXPORT void WasmEdge_ExecutorExperimentalRegisterPreHostFunction(
3957
0
    WasmEdge_ExecutorContext *Cxt, void *Data, void (*Func)(void *)) noexcept {
3958
0
  if (!Cxt) {
3959
0
    return;
3960
0
  }
3961
0
  fromExecutorCxt(Cxt)->registerPreHostFunction(Data, Func);
3962
0
}
3963
3964
WASMEDGE_CAPI_EXPORT void WasmEdge_ExecutorExperimentalRegisterPostHostFunction(
3965
0
    WasmEdge_ExecutorContext *Cxt, void *Data, void (*Func)(void *)) noexcept {
3966
0
  if (!Cxt) {
3967
0
    return;
3968
0
  }
3969
0
  fromExecutorCxt(Cxt)->registerPostHostFunction(Data, Func);
3970
0
}
3971
3972
// <<<<<<<< WasmEdge Experimental Functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3973
#ifdef __cplusplus
3974
} // extern "C"
3975
#endif