Coverage Report

Created: 2026-11-09 06:49

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_ConfigureSetMaxStackSize(WasmEdge_ConfigureContext *Cxt,
990
0
                                  const uint64_t Size) noexcept {
991
0
  if (Cxt) {
992
0
    Cxt->Conf.getRuntimeConfigure().setMaxStackSize(Size);
993
0
  }
994
0
}
995
996
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_ConfigureGetMaxStackSize(
997
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
998
0
  if (Cxt) {
999
0
    return Cxt->Conf.getRuntimeConfigure().getMaxStackSize();
1000
0
  }
1001
0
  return 0;
1002
0
}
1003
1004
WASMEDGE_CAPI_EXPORT void
1005
WasmEdge_ConfigureSetRunMode(WasmEdge_ConfigureContext *Cxt,
1006
0
                             const enum WasmEdge_RunMode Mode) noexcept {
1007
0
  if (Cxt) {
1008
0
    Cxt->Conf.getRuntimeConfigure().setRunMode(
1009
0
        static_cast<WasmEdge::RunMode>(Mode));
1010
0
  }
1011
0
}
1012
1013
WASMEDGE_CAPI_EXPORT enum WasmEdge_RunMode
1014
0
WasmEdge_ConfigureGetRunMode(const WasmEdge_ConfigureContext *Cxt) noexcept {
1015
0
  if (Cxt) {
1016
0
    return static_cast<WasmEdge_RunMode>(
1017
0
        Cxt->Conf.getRuntimeConfigure().getRunMode());
1018
0
  }
1019
0
  return WasmEdge_RunMode_Interpreter;
1020
0
}
1021
1022
WASMEDGE_CAPI_EXPORT void
1023
WasmEdge_ConfigureSetForceInterpreter(WasmEdge_ConfigureContext *Cxt,
1024
0
                                      const bool IsForceInterpreter) noexcept {
1025
0
  if (Cxt) {
1026
0
    if (IsForceInterpreter) {
1027
0
      Cxt->Conf.getRuntimeConfigure().setRunMode(
1028
0
          WasmEdge::RunMode::Interpreter);
1029
0
    }
1030
    // Passing `false` is a no-op, preserving the historical "don't force"
1031
    // semantic.
1032
0
  }
1033
0
}
1034
1035
WASMEDGE_CAPI_EXPORT void
1036
WasmEdge_ConfigureSetAllowAFUNIX(WasmEdge_ConfigureContext *Cxt,
1037
0
                                 const bool EnableAFUNIX) noexcept {
1038
0
  if (Cxt) {
1039
0
    Cxt->Conf.getRuntimeConfigure().setAllowAFUNIX(EnableAFUNIX);
1040
0
  }
1041
0
}
1042
1043
WASMEDGE_CAPI_EXPORT bool
1044
0
WasmEdge_ConfigureIsAllowAFUNIX(const WasmEdge_ConfigureContext *Cxt) noexcept {
1045
0
  if (Cxt) {
1046
0
    return Cxt->Conf.getRuntimeConfigure().isAllowAFUNIX();
1047
0
  }
1048
0
  return false;
1049
0
}
1050
1051
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureIsForceInterpreter(
1052
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1053
0
  if (Cxt) {
1054
0
    return Cxt->Conf.getRuntimeConfigure().getRunMode() ==
1055
0
           WasmEdge::RunMode::Interpreter;
1056
0
  }
1057
0
  return false;
1058
0
}
1059
1060
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetOptimizationLevel(
1061
    WasmEdge_ConfigureContext *Cxt,
1062
0
    const enum WasmEdge_CompilerOptimizationLevel Level) noexcept {
1063
0
  if (Cxt) {
1064
0
    Cxt->Conf.getCompilerConfigure().setOptimizationLevel(
1065
0
        static_cast<WasmEdge::CompilerConfigure::OptimizationLevel>(Level));
1066
0
  }
1067
0
}
1068
1069
WASMEDGE_CAPI_EXPORT enum WasmEdge_CompilerOptimizationLevel
1070
WasmEdge_ConfigureCompilerGetOptimizationLevel(
1071
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1072
0
  if (Cxt) {
1073
0
    return static_cast<WasmEdge_CompilerOptimizationLevel>(
1074
0
        Cxt->Conf.getCompilerConfigure().getOptimizationLevel());
1075
0
  }
1076
0
  return WasmEdge_CompilerOptimizationLevel_O0;
1077
0
}
1078
1079
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetOutputFormat(
1080
    WasmEdge_ConfigureContext *Cxt,
1081
0
    const enum WasmEdge_CompilerOutputFormat Format) noexcept {
1082
0
  if (Cxt) {
1083
0
    Cxt->Conf.getCompilerConfigure().setOutputFormat(
1084
0
        static_cast<WasmEdge::CompilerConfigure::OutputFormat>(Format));
1085
0
  }
1086
0
}
1087
1088
WASMEDGE_CAPI_EXPORT enum WasmEdge_CompilerOutputFormat
1089
WasmEdge_ConfigureCompilerGetOutputFormat(
1090
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1091
0
  if (Cxt) {
1092
0
    return static_cast<WasmEdge_CompilerOutputFormat>(
1093
0
        Cxt->Conf.getCompilerConfigure().getOutputFormat());
1094
0
  }
1095
0
  return WasmEdge_CompilerOutputFormat_Wasm;
1096
0
}
1097
1098
WASMEDGE_CAPI_EXPORT void
1099
WasmEdge_ConfigureCompilerSetDumpIR(WasmEdge_ConfigureContext *Cxt,
1100
0
                                    const bool IsDump) noexcept {
1101
0
  if (Cxt) {
1102
0
    Cxt->Conf.getCompilerConfigure().setDumpIR(IsDump);
1103
0
  }
1104
0
}
1105
1106
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsDumpIR(
1107
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1108
0
  if (Cxt) {
1109
0
    return Cxt->Conf.getCompilerConfigure().isDumpIR();
1110
0
  }
1111
0
  return false;
1112
0
}
1113
1114
WASMEDGE_CAPI_EXPORT void
1115
WasmEdge_ConfigureCompilerSetGenericBinary(WasmEdge_ConfigureContext *Cxt,
1116
0
                                           const bool IsGeneric) noexcept {
1117
0
  if (Cxt) {
1118
0
    Cxt->Conf.getCompilerConfigure().setGenericBinary(IsGeneric);
1119
0
  }
1120
0
}
1121
1122
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsGenericBinary(
1123
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1124
0
  if (Cxt) {
1125
0
    return Cxt->Conf.getCompilerConfigure().isGenericBinary();
1126
0
  }
1127
0
  return false;
1128
0
}
1129
1130
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureCompilerSetInterruptible(
1131
0
    WasmEdge_ConfigureContext *Cxt, const bool IsInterruptible) noexcept {
1132
0
  if (Cxt) {
1133
0
    Cxt->Conf.getCompilerConfigure().setInterruptible(IsInterruptible);
1134
0
  }
1135
0
}
1136
1137
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureCompilerIsInterruptible(
1138
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1139
0
  if (Cxt) {
1140
0
    return Cxt->Conf.getCompilerConfigure().isInterruptible();
1141
0
  }
1142
0
  return false;
1143
0
}
1144
1145
WASMEDGE_CAPI_EXPORT void WasmEdge_ConfigureStatisticsSetInstructionCounting(
1146
0
    WasmEdge_ConfigureContext *Cxt, const bool IsCount) noexcept {
1147
0
  if (Cxt) {
1148
0
    Cxt->Conf.getStatisticsConfigure().setInstructionCounting(IsCount);
1149
0
  }
1150
0
}
1151
1152
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsInstructionCounting(
1153
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1154
0
  if (Cxt) {
1155
0
    return Cxt->Conf.getStatisticsConfigure().isInstructionCounting();
1156
0
  }
1157
0
  return false;
1158
0
}
1159
1160
WASMEDGE_CAPI_EXPORT void
1161
WasmEdge_ConfigureStatisticsSetCostMeasuring(WasmEdge_ConfigureContext *Cxt,
1162
0
                                             const bool IsMeasure) noexcept {
1163
0
  if (Cxt) {
1164
0
    Cxt->Conf.getStatisticsConfigure().setCostMeasuring(IsMeasure);
1165
0
  }
1166
0
}
1167
1168
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsCostMeasuring(
1169
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1170
0
  if (Cxt) {
1171
0
    return Cxt->Conf.getStatisticsConfigure().isCostMeasuring();
1172
0
  }
1173
0
  return false;
1174
0
}
1175
1176
WASMEDGE_CAPI_EXPORT void
1177
WasmEdge_ConfigureStatisticsSetTimeMeasuring(WasmEdge_ConfigureContext *Cxt,
1178
0
                                             const bool IsMeasure) noexcept {
1179
0
  if (Cxt) {
1180
0
    Cxt->Conf.getStatisticsConfigure().setTimeMeasuring(IsMeasure);
1181
0
  }
1182
0
}
1183
1184
WASMEDGE_CAPI_EXPORT bool WasmEdge_ConfigureStatisticsIsTimeMeasuring(
1185
0
    const WasmEdge_ConfigureContext *Cxt) noexcept {
1186
0
  if (Cxt) {
1187
0
    return Cxt->Conf.getStatisticsConfigure().isTimeMeasuring();
1188
0
  }
1189
0
  return false;
1190
0
}
1191
1192
WASMEDGE_CAPI_EXPORT void
1193
0
WasmEdge_ConfigureDelete(WasmEdge_ConfigureContext *Cxt) noexcept {
1194
0
  delete Cxt;
1195
0
}
1196
1197
// <<<<<<<< WasmEdge configure functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1198
1199
// >>>>>>>> WasmEdge statistics functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1200
1201
WASMEDGE_CAPI_EXPORT WasmEdge_StatisticsContext *
1202
0
WasmEdge_StatisticsCreate(void) noexcept {
1203
0
  try {
1204
0
    return toStatCxt(new WasmEdge::Statistics::Statistics);
1205
0
  } catch (...) {
1206
0
    handleCAPIError();
1207
0
    return nullptr;
1208
0
  }
1209
0
}
1210
1211
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_StatisticsGetInstrCount(
1212
0
    const WasmEdge_StatisticsContext *Cxt) noexcept {
1213
0
  if (Cxt) {
1214
0
    return fromStatCxt(Cxt)->getInstrCount();
1215
0
  }
1216
0
  return 0;
1217
0
}
1218
1219
WASMEDGE_CAPI_EXPORT double WasmEdge_StatisticsGetInstrPerSecond(
1220
0
    const WasmEdge_StatisticsContext *Cxt) noexcept {
1221
0
  if (Cxt) {
1222
0
    return fromStatCxt(Cxt)->getInstrPerSecond();
1223
0
  }
1224
0
  return 0.0;
1225
0
}
1226
1227
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_StatisticsGetTotalCost(
1228
0
    const WasmEdge_StatisticsContext *Cxt) noexcept {
1229
0
  if (Cxt) {
1230
0
    return fromStatCxt(Cxt)->getTotalCost();
1231
0
  }
1232
0
  return 0;
1233
0
}
1234
1235
WASMEDGE_CAPI_EXPORT void
1236
WasmEdge_StatisticsSetCostTable(WasmEdge_StatisticsContext *Cxt,
1237
                                uint64_t *CostArr,
1238
0
                                const uint32_t Len) noexcept {
1239
0
  if (Cxt) {
1240
0
    fromStatCxt(Cxt)->setCostTable(genSpan(CostArr, Len));
1241
0
  }
1242
0
}
1243
1244
WASMEDGE_CAPI_EXPORT void
1245
WasmEdge_StatisticsSetCostLimit(WasmEdge_StatisticsContext *Cxt,
1246
0
                                const uint64_t Limit) noexcept {
1247
0
  if (Cxt) {
1248
0
    fromStatCxt(Cxt)->setCostLimit(Limit);
1249
0
  }
1250
0
}
1251
1252
WASMEDGE_CAPI_EXPORT void
1253
0
WasmEdge_StatisticsClear(WasmEdge_StatisticsContext *Cxt) noexcept {
1254
0
  if (Cxt) {
1255
0
    fromStatCxt(Cxt)->clear();
1256
0
  }
1257
0
}
1258
1259
WASMEDGE_CAPI_EXPORT void
1260
0
WasmEdge_StatisticsDelete(WasmEdge_StatisticsContext *Cxt) noexcept {
1261
0
  delete fromStatCxt(Cxt);
1262
0
}
1263
1264
// <<<<<<<< WasmEdge statistics functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1265
1266
// >>>>>>>> WasmEdge AST module functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1267
1268
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListImportsLength(
1269
0
    const WasmEdge_ASTModuleContext *Cxt) noexcept {
1270
0
  if (Cxt) {
1271
0
    return static_cast<uint32_t>(
1272
0
        fromASTModCxt(Cxt)->getImportSection().getContent().size());
1273
0
  }
1274
0
  return 0;
1275
0
}
1276
1277
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListImports(
1278
    const WasmEdge_ASTModuleContext *Cxt,
1279
0
    const WasmEdge_ImportTypeContext **Imports, const uint32_t Len) noexcept {
1280
0
  if (Cxt) {
1281
0
    const auto &ImpSec = fromASTModCxt(Cxt)->getImportSection().getContent();
1282
0
    if (Imports) {
1283
0
      for (uint32_t I = 0; I < Len && I < ImpSec.size(); I++) {
1284
0
        Imports[I] = toImpTypeCxt(&ImpSec[I]);
1285
0
      }
1286
0
    }
1287
0
    return static_cast<uint32_t>(ImpSec.size());
1288
0
  }
1289
0
  return 0;
1290
0
}
1291
1292
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListExportsLength(
1293
0
    const WasmEdge_ASTModuleContext *Cxt) noexcept {
1294
0
  if (Cxt) {
1295
0
    return static_cast<uint32_t>(
1296
0
        fromASTModCxt(Cxt)->getExportSection().getContent().size());
1297
0
  }
1298
0
  return 0;
1299
0
}
1300
1301
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ASTModuleListExports(
1302
    const WasmEdge_ASTModuleContext *Cxt,
1303
0
    const WasmEdge_ExportTypeContext **Exports, const uint32_t Len) noexcept {
1304
0
  if (Cxt) {
1305
0
    const auto &ExpSec = fromASTModCxt(Cxt)->getExportSection().getContent();
1306
0
    if (Exports) {
1307
0
      for (uint32_t I = 0; I < Len && I < ExpSec.size(); I++) {
1308
0
        Exports[I] = toExpTypeCxt(&ExpSec[I]);
1309
0
      }
1310
0
    }
1311
0
    return static_cast<uint32_t>(ExpSec.size());
1312
0
  }
1313
0
  return 0;
1314
0
}
1315
1316
WASMEDGE_CAPI_EXPORT void
1317
0
WasmEdge_ASTModuleDelete(WasmEdge_ASTModuleContext *Cxt) noexcept {
1318
0
  std::unique_ptr<WasmEdge::AST::Module> Own(fromASTModCxt(Cxt));
1319
0
}
1320
1321
// <<<<<<<< WasmEdge AST module functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1322
1323
// >>>>>>>> WasmEdge limit functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1324
1325
WASMEDGE_CAPI_EXPORT WasmEdge_LimitContext *
1326
0
WasmEdge_LimitCreate(const uint64_t Min, const bool Is64Bit) noexcept {
1327
0
  try {
1328
0
    return toLimitCxt(new WasmEdge::AST::Limit(Min, Is64Bit));
1329
0
  } catch (...) {
1330
0
    handleCAPIError();
1331
0
    return nullptr;
1332
0
  }
1333
0
}
1334
1335
WASMEDGE_CAPI_EXPORT WasmEdge_LimitContext *
1336
WasmEdge_LimitCreateWithMax(const uint64_t Min, const uint64_t Max,
1337
0
                            const bool Is64Bit, const bool IsShared) noexcept {
1338
0
  try {
1339
0
    return toLimitCxt(new WasmEdge::AST::Limit(Min, Max, Is64Bit, IsShared));
1340
0
  } catch (...) {
1341
0
    handleCAPIError();
1342
0
    return nullptr;
1343
0
  }
1344
0
}
1345
1346
WASMEDGE_CAPI_EXPORT uint64_t
1347
0
WasmEdge_LimitGetMin(const WasmEdge_LimitContext *Cxt) noexcept {
1348
0
  if (Cxt) {
1349
0
    return fromLimitCxt(Cxt)->getMin();
1350
0
  }
1351
0
  return 0;
1352
0
}
1353
1354
WASMEDGE_CAPI_EXPORT uint64_t
1355
0
WasmEdge_LimitGetMax(const WasmEdge_LimitContext *Cxt) noexcept {
1356
0
  if (Cxt) {
1357
0
    return fromLimitCxt(Cxt)->getMax();
1358
0
  }
1359
0
  return 0;
1360
0
}
1361
1362
WASMEDGE_CAPI_EXPORT bool
1363
0
WasmEdge_LimitHasMax(const WasmEdge_LimitContext *Cxt) noexcept {
1364
0
  if (Cxt) {
1365
0
    return fromLimitCxt(Cxt)->hasMax();
1366
0
  }
1367
0
  return false;
1368
0
}
1369
1370
WASMEDGE_CAPI_EXPORT bool
1371
0
WasmEdge_LimitIsShared(const WasmEdge_LimitContext *Cxt) noexcept {
1372
0
  if (Cxt) {
1373
0
    return fromLimitCxt(Cxt)->isShared();
1374
0
  }
1375
0
  return false;
1376
0
}
1377
1378
WASMEDGE_CAPI_EXPORT bool
1379
0
WasmEdge_LimitIs64Bit(const WasmEdge_LimitContext *Cxt) noexcept {
1380
0
  if (Cxt) {
1381
0
    return fromLimitCxt(Cxt)->is64();
1382
0
  }
1383
0
  return false;
1384
0
}
1385
1386
WASMEDGE_CAPI_EXPORT bool
1387
WasmEdge_LimitIsEqual(const WasmEdge_LimitContext *Cxt1,
1388
0
                      const WasmEdge_LimitContext *Cxt2) noexcept {
1389
0
  if (Cxt1 && Cxt2) {
1390
0
    const auto *Lim1 = fromLimitCxt(Cxt1);
1391
0
    const auto *Lim2 = fromLimitCxt(Cxt2);
1392
0
    if (Lim1->hasMax() && Lim2->hasMax()) {
1393
0
      return Lim1->getMin() == Lim2->getMin() &&
1394
0
             Lim1->getMax() == Lim2->getMax() &&
1395
0
             Lim1->getAddrType() == Lim2->getAddrType() &&
1396
0
             Lim1->isShared() == Lim2->isShared();
1397
0
    } else {
1398
0
      return Lim1->hasMax() == Lim2->hasMax() &&
1399
0
             Lim1->getMin() == Lim2->getMin() &&
1400
0
             Lim1->getAddrType() == Lim2->getAddrType() &&
1401
0
             Lim1->isShared() == Lim2->isShared();
1402
0
    }
1403
0
  }
1404
0
  return false;
1405
0
}
1406
1407
WASMEDGE_CAPI_EXPORT void
1408
0
WasmEdge_LimitDelete(WasmEdge_LimitContext *Cxt) noexcept {
1409
0
  delete fromLimitCxt(Cxt);
1410
0
}
1411
1412
// <<<<<<<< WasmEdge limit functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1413
1414
// >>>>>>>> WasmEdge function type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1415
1416
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionTypeContext *WasmEdge_FunctionTypeCreate(
1417
    const WasmEdge_ValType *ParamList, const uint32_t ParamLen,
1418
0
    const WasmEdge_ValType *ReturnList, const uint32_t ReturnLen) noexcept {
1419
0
  try {
1420
0
    auto *Cxt = new WasmEdge::AST::FunctionType;
1421
0
    if (ParamLen > 0) {
1422
0
      Cxt->getParamTypes().resize(ParamLen);
1423
0
    }
1424
0
    for (uint32_t I = 0; I < ParamLen; I++) {
1425
0
      Cxt->getParamTypes()[I] = genValType(ParamList[I]);
1426
0
    }
1427
0
    if (ReturnLen > 0) {
1428
0
      Cxt->getReturnTypes().resize(ReturnLen);
1429
0
    }
1430
0
    for (uint32_t I = 0; I < ReturnLen; I++) {
1431
0
      Cxt->getReturnTypes()[I] = genValType(ReturnList[I]);
1432
0
    }
1433
0
    return toFuncTypeCxt(Cxt);
1434
0
  } catch (...) {
1435
0
    handleCAPIError();
1436
0
    return nullptr;
1437
0
  }
1438
0
}
1439
1440
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetParametersLength(
1441
0
    const WasmEdge_FunctionTypeContext *Cxt) noexcept {
1442
0
  if (Cxt) {
1443
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getParamTypes().size());
1444
0
  }
1445
0
  return 0;
1446
0
}
1447
1448
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetParameters(
1449
    const WasmEdge_FunctionTypeContext *Cxt, WasmEdge_ValType *List,
1450
0
    const uint32_t Len) noexcept {
1451
0
  if (Cxt) {
1452
0
    for (uint32_t I = 0;
1453
0
         I < fromFuncTypeCxt(Cxt)->getParamTypes().size() && I < Len; I++) {
1454
0
      List[I] = genWasmEdge_ValType(fromFuncTypeCxt(Cxt)->getParamTypes()[I]);
1455
0
    }
1456
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getParamTypes().size());
1457
0
  }
1458
0
  return 0;
1459
0
}
1460
1461
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetReturnsLength(
1462
0
    const WasmEdge_FunctionTypeContext *Cxt) noexcept {
1463
0
  if (Cxt) {
1464
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getReturnTypes().size());
1465
0
  }
1466
0
  return 0;
1467
0
}
1468
1469
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_FunctionTypeGetReturns(
1470
    const WasmEdge_FunctionTypeContext *Cxt, WasmEdge_ValType *List,
1471
0
    const uint32_t Len) noexcept {
1472
0
  if (Cxt) {
1473
0
    for (uint32_t I = 0;
1474
0
         I < fromFuncTypeCxt(Cxt)->getReturnTypes().size() && I < Len; I++) {
1475
0
      List[I] = genWasmEdge_ValType(fromFuncTypeCxt(Cxt)->getReturnTypes()[I]);
1476
0
    }
1477
0
    return static_cast<uint32_t>(fromFuncTypeCxt(Cxt)->getReturnTypes().size());
1478
0
  }
1479
0
  return 0;
1480
0
}
1481
1482
WASMEDGE_CAPI_EXPORT void
1483
0
WasmEdge_FunctionTypeDelete(WasmEdge_FunctionTypeContext *Cxt) noexcept {
1484
0
  delete fromFuncTypeCxt(Cxt);
1485
0
}
1486
1487
// <<<<<<<< WasmEdge function type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1488
1489
// >>>>>>>> WasmEdge table type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1490
1491
WASMEDGE_CAPI_EXPORT WasmEdge_TableTypeContext *
1492
WasmEdge_TableTypeCreate(const WasmEdge_ValType RefType,
1493
0
                         const WasmEdge_LimitContext *Limit) noexcept {
1494
0
  try {
1495
0
    WasmEdge::ValType RT = genValType(RefType);
1496
0
    if (Limit && RT.isRefType()) {
1497
0
      return toTabTypeCxt(
1498
0
          new WasmEdge::AST::TableType(RT, *fromLimitCxt(Limit)));
1499
0
    }
1500
0
  } catch (...) {
1501
0
    handleCAPIError();
1502
0
  }
1503
0
  return nullptr;
1504
0
}
1505
1506
WASMEDGE_CAPI_EXPORT WasmEdge_ValType
1507
0
WasmEdge_TableTypeGetRefType(const WasmEdge_TableTypeContext *Cxt) noexcept {
1508
0
  if (Cxt) {
1509
0
    return genWasmEdge_ValType(fromTabTypeCxt(Cxt)->getRefType());
1510
0
  }
1511
0
  return WasmEdge_ValTypeGenFuncRef();
1512
0
}
1513
1514
WASMEDGE_CAPI_EXPORT const WasmEdge_LimitContext *
1515
0
WasmEdge_TableTypeGetLimit(const WasmEdge_TableTypeContext *Cxt) noexcept {
1516
0
  if (Cxt) {
1517
0
    return toLimitCxt(&fromTabTypeCxt(Cxt)->getLimit());
1518
0
  }
1519
0
  return nullptr;
1520
0
}
1521
1522
WASMEDGE_CAPI_EXPORT void
1523
0
WasmEdge_TableTypeDelete(WasmEdge_TableTypeContext *Cxt) noexcept {
1524
0
  delete fromTabTypeCxt(Cxt);
1525
0
}
1526
1527
// <<<<<<<< WasmEdge table type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1528
1529
// >>>>>>>> WasmEdge memory type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1530
1531
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryTypeContext *
1532
0
WasmEdge_MemoryTypeCreate(const WasmEdge_LimitContext *Limit) noexcept {
1533
0
  try {
1534
0
    if (Limit) {
1535
0
      return toMemTypeCxt(new WasmEdge::AST::MemoryType(*fromLimitCxt(Limit)));
1536
0
    }
1537
0
  } catch (...) {
1538
0
    handleCAPIError();
1539
0
  }
1540
0
  return nullptr;
1541
0
}
1542
1543
WASMEDGE_CAPI_EXPORT const WasmEdge_LimitContext *
1544
0
WasmEdge_MemoryTypeGetLimit(const WasmEdge_MemoryTypeContext *Cxt) noexcept {
1545
0
  if (Cxt) {
1546
0
    return toLimitCxt(&fromMemTypeCxt(Cxt)->getLimit());
1547
0
  }
1548
0
  return nullptr;
1549
0
}
1550
1551
WASMEDGE_CAPI_EXPORT void
1552
0
WasmEdge_MemoryTypeDelete(WasmEdge_MemoryTypeContext *Cxt) noexcept {
1553
0
  delete fromMemTypeCxt(Cxt);
1554
0
}
1555
1556
// <<<<<<<< WasmEdge memory type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1557
1558
// >>>>>>>> WasmEdge tag type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1559
1560
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
1561
0
WasmEdge_TagTypeGetFunctionType(const WasmEdge_TagTypeContext *Cxt) noexcept {
1562
0
  if (Cxt) {
1563
0
    const auto &CompType = fromTagTypeCxt(Cxt)->getDefType().getCompositeType();
1564
0
    if (CompType.isFunc()) {
1565
0
      return toFuncTypeCxt(&CompType.getFuncType());
1566
0
    }
1567
0
  }
1568
0
  return nullptr;
1569
0
}
1570
1571
// <<<<<<<< WasmEdge tag type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1572
1573
// >>>>>>>> WasmEdge global type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1574
1575
WASMEDGE_CAPI_EXPORT WasmEdge_GlobalTypeContext *
1576
WasmEdge_GlobalTypeCreate(const WasmEdge_ValType ValType,
1577
0
                          const enum WasmEdge_Mutability Mut) noexcept {
1578
0
  try {
1579
0
    return toGlobTypeCxt(new WasmEdge::AST::GlobalType(
1580
0
        genValType(ValType), static_cast<WasmEdge::ValMut>(Mut)));
1581
0
  } catch (...) {
1582
0
    handleCAPIError();
1583
0
    return nullptr;
1584
0
  }
1585
0
}
1586
1587
WASMEDGE_CAPI_EXPORT WasmEdge_ValType
1588
0
WasmEdge_GlobalTypeGetValType(const WasmEdge_GlobalTypeContext *Cxt) noexcept {
1589
0
  if (Cxt) {
1590
0
    return genWasmEdge_ValType(fromGlobTypeCxt(Cxt)->getValType());
1591
0
  }
1592
0
  return WasmEdge_ValTypeGenI32();
1593
0
}
1594
1595
WASMEDGE_CAPI_EXPORT enum WasmEdge_Mutability WasmEdge_GlobalTypeGetMutability(
1596
0
    const WasmEdge_GlobalTypeContext *Cxt) noexcept {
1597
0
  if (Cxt) {
1598
0
    return static_cast<WasmEdge_Mutability>(fromGlobTypeCxt(Cxt)->getValMut());
1599
0
  }
1600
0
  return WasmEdge_Mutability_Const;
1601
0
}
1602
1603
WASMEDGE_CAPI_EXPORT void
1604
0
WasmEdge_GlobalTypeDelete(WasmEdge_GlobalTypeContext *Cxt) noexcept {
1605
0
  delete fromGlobTypeCxt(Cxt);
1606
0
}
1607
1608
// <<<<<<<< WasmEdge global type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1609
1610
// >>>>>>>> WasmEdge import type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1611
1612
WASMEDGE_CAPI_EXPORT enum WasmEdge_ExternalType
1613
WasmEdge_ImportTypeGetExternalType(
1614
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1615
0
  if (Cxt) {
1616
0
    return static_cast<WasmEdge_ExternalType>(
1617
0
        fromImpTypeCxt(Cxt)->getExternalType());
1618
0
  }
1619
0
  return WasmEdge_ExternalType_Function;
1620
0
}
1621
1622
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ImportTypeGetModuleName(
1623
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1624
0
  if (Cxt) {
1625
0
    auto StrView = fromImpTypeCxt(Cxt)->getModuleName();
1626
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
1627
0
                           /* Buf */ StrView.data()};
1628
0
  }
1629
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
1630
0
}
1631
1632
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ImportTypeGetExternalName(
1633
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1634
0
  if (Cxt) {
1635
0
    auto StrView = fromImpTypeCxt(Cxt)->getExternalName();
1636
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
1637
0
                           /* Buf */ StrView.data()};
1638
0
  }
1639
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
1640
0
}
1641
1642
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
1643
WasmEdge_ImportTypeGetFunctionType(
1644
    const WasmEdge_ASTModuleContext *ASTCxt,
1645
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1646
0
  if (ASTCxt && Cxt &&
1647
0
      fromImpTypeCxt(Cxt)->getExternalType() ==
1648
0
          WasmEdge::ExternalType::Function) {
1649
0
    uint32_t Idx = fromImpTypeCxt(Cxt)->getExternalFuncTypeIdx();
1650
0
    auto SubTypes = fromASTModCxt(ASTCxt)->getTypeSection().getContent();
1651
0
    if (Idx < SubTypes.size() && SubTypes[Idx].getCompositeType().isFunc()) {
1652
0
      return toFuncTypeCxt(&(SubTypes[Idx].getCompositeType().getFuncType()));
1653
0
    }
1654
0
  }
1655
0
  return nullptr;
1656
0
}
1657
1658
WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext *
1659
WasmEdge_ImportTypeGetTableType(
1660
    const WasmEdge_ASTModuleContext *ASTCxt,
1661
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1662
0
  if (ASTCxt && Cxt &&
1663
0
      fromImpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Table) {
1664
0
    return toTabTypeCxt(&fromImpTypeCxt(Cxt)->getExternalTableType());
1665
0
  }
1666
0
  return nullptr;
1667
0
}
1668
1669
WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext *
1670
WasmEdge_ImportTypeGetMemoryType(
1671
    const WasmEdge_ASTModuleContext *ASTCxt,
1672
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1673
0
  if (ASTCxt && Cxt &&
1674
0
      fromImpTypeCxt(Cxt)->getExternalType() ==
1675
0
          WasmEdge::ExternalType::Memory) {
1676
0
    return toMemTypeCxt(&fromImpTypeCxt(Cxt)->getExternalMemoryType());
1677
0
  }
1678
0
  return nullptr;
1679
0
}
1680
1681
WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext *
1682
WasmEdge_ImportTypeGetTagType(const WasmEdge_ASTModuleContext *ASTCxt,
1683
0
                              const WasmEdge_ImportTypeContext *Cxt) noexcept {
1684
0
  if (ASTCxt && Cxt &&
1685
0
      fromImpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Tag) {
1686
0
    return toTagTypeCxt(&fromImpTypeCxt(Cxt)->getExternalTagType());
1687
0
  }
1688
0
  return nullptr;
1689
0
}
1690
1691
WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext *
1692
WasmEdge_ImportTypeGetGlobalType(
1693
    const WasmEdge_ASTModuleContext *ASTCxt,
1694
0
    const WasmEdge_ImportTypeContext *Cxt) noexcept {
1695
0
  if (ASTCxt && Cxt &&
1696
0
      fromImpTypeCxt(Cxt)->getExternalType() ==
1697
0
          WasmEdge::ExternalType::Global) {
1698
0
    return toGlobTypeCxt(&fromImpTypeCxt(Cxt)->getExternalGlobalType());
1699
0
  }
1700
0
  return nullptr;
1701
0
}
1702
1703
// <<<<<<<< WasmEdge import type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1704
1705
// >>>>>>>> WasmEdge export type functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1706
1707
WASMEDGE_CAPI_EXPORT enum WasmEdge_ExternalType
1708
WasmEdge_ExportTypeGetExternalType(
1709
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1710
0
  if (Cxt) {
1711
0
    return static_cast<WasmEdge_ExternalType>(
1712
0
        fromExpTypeCxt(Cxt)->getExternalType());
1713
0
  }
1714
0
  return WasmEdge_ExternalType_Function;
1715
0
}
1716
1717
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ExportTypeGetExternalName(
1718
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1719
0
  if (Cxt) {
1720
0
    auto StrView = fromExpTypeCxt(Cxt)->getExternalName();
1721
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
1722
0
                           /* Buf */ StrView.data()};
1723
0
  }
1724
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
1725
0
}
1726
1727
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
1728
WasmEdge_ExportTypeGetFunctionType(
1729
    const WasmEdge_ASTModuleContext *ASTCxt,
1730
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1731
0
  try {
1732
0
    if (ASTCxt && Cxt &&
1733
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1734
0
            WasmEdge::ExternalType::Function) {
1735
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1736
0
      auto FuncIdxs = fromASTModCxt(ASTCxt)->getFunctionSection().getContent();
1737
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1738
1739
      // Indexing the import descriptions.
1740
0
      std::vector<uint32_t> ImpFuncs;
1741
0
      ImpFuncs.reserve(ImpDescs.size());
1742
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1743
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Function) {
1744
0
          ImpFuncs.push_back(I);
1745
0
        }
1746
0
      }
1747
      // Get the function type index.
1748
0
      uint32_t TypeIdx = 0;
1749
0
      if (ExtIdx < ImpFuncs.size()) {
1750
        // Imported function. Get the function type index from the import desc.
1751
0
        TypeIdx = ImpDescs[ImpFuncs[ExtIdx]].getExternalFuncTypeIdx();
1752
0
      } else if (ExtIdx < ImpFuncs.size() + FuncIdxs.size()) {
1753
        // Module owned function. Get the function type index from the section.
1754
0
        TypeIdx = FuncIdxs[ExtIdx - ImpFuncs.size()];
1755
0
      } else {
1756
        // Invalid function index.
1757
0
        return nullptr;
1758
0
      }
1759
      // Get the function type.
1760
0
      auto SubTypes = fromASTModCxt(ASTCxt)->getTypeSection().getContent();
1761
0
      if (TypeIdx < SubTypes.size() &&
1762
0
          SubTypes[TypeIdx].getCompositeType().isFunc()) {
1763
0
        return toFuncTypeCxt(
1764
0
            &(SubTypes[TypeIdx].getCompositeType().getFuncType()));
1765
0
      }
1766
0
    }
1767
0
  } catch (...) {
1768
0
    handleCAPIError();
1769
0
  }
1770
0
  return nullptr;
1771
0
}
1772
1773
WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext *
1774
WasmEdge_ExportTypeGetTableType(
1775
    const WasmEdge_ASTModuleContext *ASTCxt,
1776
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1777
0
  try {
1778
0
    if (ASTCxt && Cxt &&
1779
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1780
0
            WasmEdge::ExternalType::Table) {
1781
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1782
0
      auto TabDescs = fromASTModCxt(ASTCxt)->getTableSection().getContent();
1783
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1784
1785
      // Indexing the import descriptions.
1786
0
      std::vector<uint32_t> ImpTabs;
1787
0
      ImpTabs.reserve(ImpDescs.size());
1788
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1789
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Table) {
1790
0
          ImpTabs.push_back(I);
1791
0
        }
1792
0
      }
1793
      // Get the table type.
1794
0
      if (ExtIdx < ImpTabs.size()) {
1795
        // Imported table. Get the table type from the import desc.
1796
0
        return toTabTypeCxt(&ImpDescs[ImpTabs[ExtIdx]].getExternalTableType());
1797
0
      } else if (ExtIdx < ImpTabs.size() + TabDescs.size()) {
1798
        // Module owned table. Get the table type from the section.
1799
0
        return toTabTypeCxt(&TabDescs[ExtIdx - ImpTabs.size()].getTableType());
1800
0
      } else {
1801
        // Invalid table type index.
1802
0
        return nullptr;
1803
0
      }
1804
0
    }
1805
0
  } catch (...) {
1806
0
    handleCAPIError();
1807
0
  }
1808
0
  return nullptr;
1809
0
}
1810
1811
WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext *
1812
WasmEdge_ExportTypeGetMemoryType(
1813
    const WasmEdge_ASTModuleContext *ASTCxt,
1814
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1815
0
  try {
1816
0
    if (ASTCxt && Cxt &&
1817
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1818
0
            WasmEdge::ExternalType::Memory) {
1819
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1820
0
      auto MemTypes = fromASTModCxt(ASTCxt)->getMemorySection().getContent();
1821
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1822
1823
      // Indexing the import descriptions.
1824
0
      std::vector<uint32_t> ImpMems;
1825
0
      ImpMems.reserve(ImpDescs.size());
1826
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1827
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Memory) {
1828
0
          ImpMems.push_back(I);
1829
0
        }
1830
0
      }
1831
      // Get the memory type.
1832
0
      if (ExtIdx < ImpMems.size()) {
1833
        // Imported memory. Get the memory type from the import desc.
1834
0
        return toMemTypeCxt(&ImpDescs[ImpMems[ExtIdx]].getExternalMemoryType());
1835
0
      } else if (ExtIdx < ImpMems.size() + MemTypes.size()) {
1836
        // Module owned memory. Get the memory type from the section.
1837
0
        return toMemTypeCxt(&MemTypes[ExtIdx - ImpMems.size()]);
1838
0
      } else {
1839
        // Invalid memory type index.
1840
0
        return nullptr;
1841
0
      }
1842
0
    }
1843
0
  } catch (...) {
1844
0
    handleCAPIError();
1845
0
  }
1846
0
  return nullptr;
1847
0
}
1848
1849
WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext *
1850
WasmEdge_ExportTypeGetTagType(const WasmEdge_ASTModuleContext *ASTCxt,
1851
0
                              const WasmEdge_ExportTypeContext *Cxt) noexcept {
1852
0
  if (ASTCxt && Cxt &&
1853
0
      fromExpTypeCxt(Cxt)->getExternalType() == WasmEdge::ExternalType::Tag) {
1854
0
    uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1855
0
    const auto &ImpDescs =
1856
0
        fromASTModCxt(ASTCxt)->getImportSection().getContent();
1857
0
    for (auto &&ImpDesc : ImpDescs) {
1858
0
      if (ImpDesc.getExternalType() == WasmEdge::ExternalType::Tag) {
1859
0
        if (ExtIdx == 0) {
1860
0
          return toTagTypeCxt(&ImpDesc.getExternalTagType());
1861
0
        }
1862
0
        ExtIdx--;
1863
0
      }
1864
0
    }
1865
    // Get the tag type.
1866
0
    const auto &TagDescs = fromASTModCxt(ASTCxt)->getTagSection().getContent();
1867
0
    if (ExtIdx >= TagDescs.size()) {
1868
0
      return nullptr;
1869
0
    }
1870
0
    return toTagTypeCxt(&TagDescs[ExtIdx]);
1871
0
  }
1872
0
  return nullptr;
1873
0
}
1874
1875
WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext *
1876
WasmEdge_ExportTypeGetGlobalType(
1877
    const WasmEdge_ASTModuleContext *ASTCxt,
1878
0
    const WasmEdge_ExportTypeContext *Cxt) noexcept {
1879
0
  try {
1880
0
    if (ASTCxt && Cxt &&
1881
0
        fromExpTypeCxt(Cxt)->getExternalType() ==
1882
0
            WasmEdge::ExternalType::Global) {
1883
0
      auto ImpDescs = fromASTModCxt(ASTCxt)->getImportSection().getContent();
1884
0
      auto GlobDescs = fromASTModCxt(ASTCxt)->getGlobalSection().getContent();
1885
0
      uint32_t ExtIdx = fromExpTypeCxt(Cxt)->getExternalIndex();
1886
1887
      // Indexing the import descriptions.
1888
0
      std::vector<uint32_t> ImpGlobs;
1889
0
      ImpGlobs.reserve(ImpDescs.size());
1890
0
      for (uint32_t I = 0; I < ImpDescs.size(); I++) {
1891
0
        if (ImpDescs[I].getExternalType() == WasmEdge::ExternalType::Global) {
1892
0
          ImpGlobs.push_back(I);
1893
0
        }
1894
0
      }
1895
      // Get the global type.
1896
0
      if (ExtIdx < ImpGlobs.size()) {
1897
        // Imported global. Get the global type from the import desc.
1898
0
        return toGlobTypeCxt(
1899
0
            &ImpDescs[ImpGlobs[ExtIdx]].getExternalGlobalType());
1900
0
      } else if (ExtIdx < ImpGlobs.size() + GlobDescs.size()) {
1901
        // Module owned global. Get the global type from the section.
1902
0
        return toGlobTypeCxt(
1903
0
            &GlobDescs[ExtIdx - ImpGlobs.size()].getGlobalType());
1904
0
      } else {
1905
        // Invalid global type index.
1906
0
        return nullptr;
1907
0
      }
1908
0
    }
1909
0
  } catch (...) {
1910
0
    handleCAPIError();
1911
0
  }
1912
0
  return nullptr;
1913
0
}
1914
1915
// <<<<<<<< WasmEdge export type functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1916
1917
// >>>>>>>> WasmEdge AOT compiler functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1918
1919
WASMEDGE_CAPI_EXPORT WasmEdge_CompilerContext *
1920
WasmEdge_CompilerCreate(const WasmEdge_ConfigureContext *ConfCxt
1921
0
                        [[maybe_unused]]) noexcept {
1922
0
#ifdef WASMEDGE_USE_LLVM
1923
0
  try {
1924
    // Set force interpreter here to load instructions of function body
1925
    // forcibly.
1926
0
    if (ConfCxt) {
1927
0
      WasmEdge::Configure CopyConf(ConfCxt->Conf);
1928
0
      CopyConf.getRuntimeConfigure().setRunMode(WasmEdge::RunMode::Interpreter);
1929
0
      return new WasmEdge_CompilerContext(CopyConf);
1930
0
    } else {
1931
0
      WasmEdge::Configure CopyConf;
1932
0
      CopyConf.getRuntimeConfigure().setRunMode(WasmEdge::RunMode::Interpreter);
1933
0
      return new WasmEdge_CompilerContext(CopyConf);
1934
0
    }
1935
0
  } catch (...) {
1936
0
    handleCAPIError();
1937
0
    return nullptr;
1938
0
  }
1939
#else
1940
  return nullptr;
1941
#endif
1942
0
}
1943
1944
WASMEDGE_CAPI_EXPORT WasmEdge_Result
1945
WasmEdge_CompilerCompile(WasmEdge_CompilerContext *Cxt [[maybe_unused]],
1946
                         const char *InPath [[maybe_unused]],
1947
0
                         const char *OutPath [[maybe_unused]]) noexcept {
1948
0
#ifdef WASMEDGE_USE_LLVM
1949
0
  return wrap(
1950
0
      [&]() -> WasmEdge::Expect<void> {
1951
0
        std::filesystem::path InputPath = genPath(InPath);
1952
0
        std::filesystem::path OutputPath = genPath(OutPath);
1953
0
        std::vector<WasmEdge::Byte> Data;
1954
0
        std::unique_ptr<WasmEdge::AST::Module> Module;
1955
0
        return Cxt->Compiler.checkConfigure()
1956
0
            .and_then([&]() noexcept { return Cxt->Load.loadFile(InputPath); })
1957
0
            .and_then([&](auto Result) noexcept {
1958
0
              Data = std::move(Result);
1959
0
              return Cxt->Load.parseModule(Data);
1960
0
            })
1961
0
            .and_then([&](auto Result) noexcept {
1962
0
              Module = std::move(Result);
1963
0
              return Cxt->Valid.validate(*Module.get());
1964
0
            })
1965
0
            .and_then(
1966
0
                [&]() noexcept { return Cxt->Compiler.compile(*Module.get()); })
1967
0
            .and_then([&](auto Result) noexcept {
1968
0
              return Cxt->CodeGen.codegen(Data, std::move(Result), OutputPath);
1969
0
            });
1970
0
      },
1971
0
      EmptyThen, Cxt);
1972
#else
1973
  return genWasmEdge_Result(ErrCode::Value::AOTDisabled);
1974
#endif
1975
0
}
1976
1977
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_CompilerCompileFromBuffer(
1978
    WasmEdge_CompilerContext *Cxt, const uint8_t *InBuffer,
1979
0
    const uint64_t InBufferLen, const char *OutPath) noexcept {
1980
0
  return WasmEdge_CompilerCompileFromBytes(
1981
0
      Cxt, WasmEdge_BytesWrap(InBuffer, static_cast<uint32_t>(InBufferLen)),
1982
0
      OutPath);
1983
0
}
1984
1985
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_CompilerCompileFromBytes(
1986
    WasmEdge_CompilerContext *Cxt [[maybe_unused]],
1987
    const WasmEdge_Bytes Bytes [[maybe_unused]],
1988
0
    const char *OutPath [[maybe_unused]]) noexcept {
1989
0
#ifdef WASMEDGE_USE_LLVM
1990
0
  return wrap(
1991
0
      [&]() -> WasmEdge::Expect<void> {
1992
0
        std::filesystem::path OutputPath = genPath(OutPath);
1993
0
        auto Data = genSpan(Bytes.Buf, Bytes.Length);
1994
0
        std::unique_ptr<WasmEdge::AST::Module> Module;
1995
0
        return Cxt->Load.parseModule(Data)
1996
0
            .and_then([&](auto Result) noexcept {
1997
0
              Module = std::move(Result);
1998
0
              return Cxt->Valid.validate(*Module);
1999
0
            })
2000
0
            .and_then([&]() noexcept { return Cxt->Compiler.compile(*Module); })
2001
0
            .and_then([&](auto Result) noexcept {
2002
0
              return Cxt->CodeGen.codegen(Data, std::move(Result), OutputPath);
2003
0
            });
2004
0
      },
2005
0
      EmptyThen, Cxt);
2006
#else
2007
  return genWasmEdge_Result(ErrCode::Value::AOTDisabled);
2008
#endif
2009
0
}
2010
2011
WASMEDGE_CAPI_EXPORT void
2012
0
WasmEdge_CompilerDelete(WasmEdge_CompilerContext *Cxt) noexcept {
2013
0
  delete Cxt;
2014
0
}
2015
2016
// <<<<<<<< WasmEdge AOT compiler functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2017
2018
// >>>>>>>> WasmEdge loader functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2019
2020
WASMEDGE_CAPI_EXPORT WasmEdge_LoaderContext *
2021
0
WasmEdge_LoaderCreate(const WasmEdge_ConfigureContext *ConfCxt) noexcept {
2022
0
  try {
2023
0
    if (ConfCxt) {
2024
0
      return toLoaderCxt(new WasmEdge::Loader::Loader(
2025
0
          ConfCxt->Conf, &WasmEdge::Executor::Executor::Intrinsics));
2026
0
    } else {
2027
0
      return toLoaderCxt(new WasmEdge::Loader::Loader(
2028
0
          WasmEdge::Configure(), &WasmEdge::Executor::Executor::Intrinsics));
2029
0
    }
2030
0
  } catch (...) {
2031
0
    handleCAPIError();
2032
0
    return nullptr;
2033
0
  }
2034
0
}
2035
2036
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromFile(
2037
    WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module,
2038
0
    const char *Path) noexcept {
2039
0
  return wrap([&]() { return fromLoaderCxt(Cxt)->parseModule(genPath(Path)); },
2040
0
              [&](auto &&Res) { *Module = toASTModCxt((*Res).release()); }, Cxt,
2041
0
              Module);
2042
0
}
2043
2044
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromBuffer(
2045
    WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module,
2046
0
    const uint8_t *Buf, const uint32_t BufLen) noexcept {
2047
0
  return WasmEdge_LoaderParseFromBytes(Cxt, Module,
2048
0
                                       WasmEdge_BytesWrap(Buf, BufLen));
2049
0
}
2050
2051
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderParseFromBytes(
2052
    WasmEdge_LoaderContext *Cxt, WasmEdge_ASTModuleContext **Module,
2053
0
    const WasmEdge_Bytes Bytes) noexcept {
2054
0
  return wrap(
2055
0
      [&]() {
2056
0
        return fromLoaderCxt(Cxt)->parseModule(
2057
0
            genSpan(Bytes.Buf, Bytes.Length));
2058
0
      },
2059
0
      [&](auto &&Res) { *Module = toASTModCxt((*Res).release()); }, Cxt,
2060
0
      Module);
2061
0
}
2062
2063
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_LoaderSerializeASTModule(
2064
    WasmEdge_LoaderContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt,
2065
0
    WasmEdge_Bytes *Buf) noexcept {
2066
0
  return wrap(
2067
0
      [&]() {
2068
0
        return fromLoaderCxt(Cxt)->serializeModule(*fromASTModCxt(ASTCxt));
2069
0
      },
2070
0
      [&](auto &&Res) {
2071
0
        uint32_t Size = static_cast<uint32_t>((*Res).size());
2072
0
        uint8_t *Bytes = new uint8_t[Size];
2073
0
        std::copy_n((*Res).begin(), Size, Bytes);
2074
0
        Buf->Length = Size;
2075
0
        Buf->Buf = Bytes;
2076
0
      },
2077
0
      Cxt, ASTCxt, Buf);
2078
0
}
2079
2080
WASMEDGE_CAPI_EXPORT void
2081
0
WasmEdge_LoaderDelete(WasmEdge_LoaderContext *Cxt) noexcept {
2082
0
  delete fromLoaderCxt(Cxt);
2083
0
}
2084
2085
// <<<<<<<< WasmEdge loader functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2086
2087
// >>>>>>>> WasmEdge validator functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2088
2089
WASMEDGE_CAPI_EXPORT WasmEdge_ValidatorContext *
2090
0
WasmEdge_ValidatorCreate(const WasmEdge_ConfigureContext *ConfCxt) noexcept {
2091
0
  try {
2092
0
    if (ConfCxt) {
2093
0
      return toValidatorCxt(new WasmEdge::Validator::Validator(ConfCxt->Conf));
2094
0
    } else {
2095
0
      return toValidatorCxt(
2096
0
          new WasmEdge::Validator::Validator(WasmEdge::Configure()));
2097
0
    }
2098
0
  } catch (...) {
2099
0
    handleCAPIError();
2100
0
    return nullptr;
2101
0
  }
2102
0
}
2103
2104
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ValidatorValidate(
2105
    WasmEdge_ValidatorContext *Cxt,
2106
0
    const WasmEdge_ASTModuleContext *ModuleCxt) noexcept {
2107
0
  return wrap(
2108
0
      [&]() {
2109
0
        return fromValidatorCxt(Cxt)->validate(*fromASTModCxt(ModuleCxt));
2110
0
      },
2111
0
      EmptyThen, Cxt, ModuleCxt);
2112
0
}
2113
2114
WASMEDGE_CAPI_EXPORT void
2115
0
WasmEdge_ValidatorDelete(WasmEdge_ValidatorContext *Cxt) noexcept {
2116
0
  delete fromValidatorCxt(Cxt);
2117
0
}
2118
2119
// <<<<<<<< WasmEdge validator functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2120
2121
// >>>>>>>> WasmEdge executor functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2122
2123
WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext *
2124
WasmEdge_ExecutorCreate(const WasmEdge_ConfigureContext *ConfCxt,
2125
0
                        WasmEdge_StatisticsContext *StatCxt) noexcept {
2126
0
  try {
2127
0
    if (ConfCxt) {
2128
0
      if (StatCxt) {
2129
0
        return toExecutorCxt(new WasmEdge::Executor::Executor(
2130
0
            ConfCxt->Conf, fromStatCxt(StatCxt)));
2131
0
      } else {
2132
0
        return toExecutorCxt(new WasmEdge::Executor::Executor(ConfCxt->Conf));
2133
0
      }
2134
0
    } else {
2135
0
      if (StatCxt) {
2136
0
        return toExecutorCxt(new WasmEdge::Executor::Executor(
2137
0
            WasmEdge::Configure(), fromStatCxt(StatCxt)));
2138
0
      } else {
2139
0
        return toExecutorCxt(
2140
0
            new WasmEdge::Executor::Executor(WasmEdge::Configure()));
2141
0
      }
2142
0
    }
2143
0
  } catch (...) {
2144
0
    handleCAPIError();
2145
0
    return nullptr;
2146
0
  }
2147
0
}
2148
2149
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorInstantiate(
2150
    WasmEdge_ExecutorContext *Cxt, WasmEdge_ModuleInstanceContext **ModuleCxt,
2151
    WasmEdge_StoreContext *StoreCxt,
2152
0
    const WasmEdge_ASTModuleContext *ASTCxt) noexcept {
2153
0
  return wrap(
2154
0
      [&]() {
2155
0
        return fromExecutorCxt(Cxt)->instantiateModule(*fromStoreCxt(StoreCxt),
2156
0
                                                       *fromASTModCxt(ASTCxt));
2157
0
      },
2158
0
      [&](auto &&Res) { *ModuleCxt = toModCxt((*Res).release()); }, Cxt,
2159
0
      ModuleCxt, StoreCxt, ASTCxt);
2160
0
}
2161
2162
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegister(
2163
    WasmEdge_ExecutorContext *Cxt, WasmEdge_ModuleInstanceContext **ModuleCxt,
2164
    WasmEdge_StoreContext *StoreCxt, const WasmEdge_ASTModuleContext *ASTCxt,
2165
0
    const WasmEdge_String ModuleName) noexcept {
2166
0
  return wrap(
2167
0
      [&]() {
2168
0
        return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt),
2169
0
                                                    *fromASTModCxt(ASTCxt),
2170
0
                                                    genStrView(ModuleName));
2171
0
      },
2172
0
      [&](auto &&Res) { *ModuleCxt = toModCxt((*Res).release()); }, Cxt,
2173
0
      ModuleCxt, StoreCxt, ASTCxt);
2174
0
}
2175
2176
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegisterImport(
2177
    WasmEdge_ExecutorContext *Cxt, WasmEdge_StoreContext *StoreCxt,
2178
0
    const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept {
2179
0
  return wrap(
2180
0
      [&]() {
2181
0
        return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt),
2182
0
                                                    *fromModCxt(ImportCxt));
2183
0
      },
2184
0
      EmptyThen, Cxt, StoreCxt, ImportCxt);
2185
0
}
2186
2187
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorRegisterImportWithAlias(
2188
    WasmEdge_ExecutorContext *Cxt, WasmEdge_StoreContext *StoreCxt,
2189
    const WasmEdge_ModuleInstanceContext *ImportCxt,
2190
0
    const WasmEdge_String ModuleName) noexcept {
2191
0
  return wrap(
2192
0
      [&]() {
2193
0
        return fromExecutorCxt(Cxt)->registerModule(*fromStoreCxt(StoreCxt),
2194
0
                                                    *fromModCxt(ImportCxt),
2195
0
                                                    genStrView(ModuleName));
2196
0
      },
2197
0
      EmptyThen, Cxt, StoreCxt, ImportCxt);
2198
0
}
2199
2200
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ExecutorInvoke(
2201
    WasmEdge_ExecutorContext *Cxt,
2202
    const WasmEdge_FunctionInstanceContext *FuncCxt,
2203
    const WasmEdge_Value *Params, const uint32_t ParamLen,
2204
0
    WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept {
2205
0
  try {
2206
0
    auto ParamPair = genParamPair(Params, ParamLen);
2207
0
    return wrap(
2208
0
        [&]() -> WasmEdge::Expect<std::vector<
2209
0
                  std::pair<WasmEdge::ValVariant, WasmEdge::ValType>>> {
2210
0
          return fromExecutorCxt(Cxt)->invoke(
2211
0
              fromFuncCxt(FuncCxt), ParamPair.first, ParamPair.second);
2212
0
        },
2213
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
2214
0
        Cxt, FuncCxt);
2215
0
  } catch (...) {
2216
0
    return handleCAPIError();
2217
0
  }
2218
0
}
2219
2220
WASMEDGE_CAPI_EXPORT WasmEdge_Async *
2221
WasmEdge_ExecutorAsyncInvoke(WasmEdge_ExecutorContext *Cxt,
2222
                             const WasmEdge_FunctionInstanceContext *FuncCxt,
2223
                             const WasmEdge_Value *Params,
2224
0
                             const uint32_t ParamLen) noexcept {
2225
0
  try {
2226
0
    if (Cxt && FuncCxt) {
2227
0
      auto ParamPair = genParamPair(Params, ParamLen);
2228
0
      return new WasmEdge_Async(fromExecutorCxt(Cxt)->asyncInvoke(
2229
0
          fromFuncCxt(FuncCxt), ParamPair.first, ParamPair.second));
2230
0
    }
2231
0
  } catch (...) {
2232
0
    handleCAPIError();
2233
0
  }
2234
0
  return nullptr;
2235
0
}
2236
2237
WASMEDGE_CAPI_EXPORT void
2238
0
WasmEdge_ExecutorDelete(WasmEdge_ExecutorContext *Cxt) noexcept {
2239
0
  delete fromExecutorCxt(Cxt);
2240
0
}
2241
2242
// <<<<<<<< WasmEdge executor functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2243
2244
// >>>>>>>> WasmEdge store functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2245
2246
WASMEDGE_CAPI_EXPORT WasmEdge_StoreContext *
2247
0
WasmEdge_StoreCreate(void) noexcept {
2248
0
  try {
2249
0
    return toStoreCxt(new WasmEdge::Runtime::StoreManager);
2250
0
  } catch (...) {
2251
0
    handleCAPIError();
2252
0
    return nullptr;
2253
0
  }
2254
0
}
2255
2256
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
2257
WasmEdge_StoreFindModule(const WasmEdge_StoreContext *Cxt,
2258
0
                         const WasmEdge_String Name) noexcept {
2259
0
  if (Cxt) {
2260
0
    return toModCxt(fromStoreCxt(Cxt)->findModule(genStrView(Name)));
2261
0
  }
2262
0
  return nullptr;
2263
0
}
2264
2265
WASMEDGE_CAPI_EXPORT uint32_t
2266
0
WasmEdge_StoreListModuleLength(const WasmEdge_StoreContext *Cxt) noexcept {
2267
0
  if (Cxt) {
2268
0
    return fromStoreCxt(Cxt)->getModuleListSize();
2269
0
  }
2270
0
  return 0;
2271
0
}
2272
2273
WASMEDGE_CAPI_EXPORT uint32_t
2274
WasmEdge_StoreListModule(const WasmEdge_StoreContext *Cxt,
2275
0
                         WasmEdge_String *Names, const uint32_t Len) noexcept {
2276
0
  if (Cxt) {
2277
0
    return fromStoreCxt(Cxt)->getModuleList(
2278
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2279
0
  }
2280
0
  return 0;
2281
0
}
2282
2283
WASMEDGE_CAPI_EXPORT void
2284
0
WasmEdge_StoreDelete(WasmEdge_StoreContext *Cxt) noexcept {
2285
0
  delete fromStoreCxt(Cxt);
2286
0
}
2287
2288
// <<<<<<<< WasmEdge store functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2289
2290
// >>>>>>>> WasmEdge module instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2291
2292
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
2293
0
WasmEdge_ModuleInstanceCreate(const WasmEdge_String ModuleName) noexcept {
2294
0
  try {
2295
0
    return toModCxt(new WasmEdge::Runtime::Instance::ModuleInstance(
2296
0
        genStrView(ModuleName)));
2297
0
  } catch (...) {
2298
0
    handleCAPIError();
2299
0
    return nullptr;
2300
0
  }
2301
0
}
2302
2303
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
2304
WasmEdge_ModuleInstanceCreateWASIWithFds(
2305
    const char *const *Args, const uint32_t ArgLen, const char *const *Envs,
2306
    const uint32_t EnvLen, const char *const *Preopens,
2307
    const uint32_t PreopenLen, const int32_t StdInFd, const int32_t StdOutFd,
2308
0
    const int32_t StdErrFd) noexcept {
2309
0
  try {
2310
0
    auto WasiMod = std::make_unique<WasmEdge::Host::WasiModule>();
2311
0
    WasmEdge_ModuleInstanceInitWASIWithFds(
2312
0
        toModCxt(WasiMod.get()), Args, ArgLen, Envs, EnvLen, Preopens,
2313
0
        PreopenLen, StdInFd, StdOutFd, StdErrFd);
2314
0
    return toModCxt(WasiMod.release());
2315
0
  } catch (...) {
2316
0
    handleCAPIError();
2317
0
    return nullptr;
2318
0
  }
2319
0
}
2320
2321
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
2322
WasmEdge_ModuleInstanceCreateWASI(const char *const *Args,
2323
                                  const uint32_t ArgLen,
2324
                                  const char *const *Envs,
2325
                                  const uint32_t EnvLen,
2326
                                  const char *const *Preopens,
2327
0
                                  const uint32_t PreopenLen) noexcept {
2328
0
  try {
2329
0
    auto WasiMod = std::make_unique<WasmEdge::Host::WasiModule>();
2330
0
    WasmEdge_ModuleInstanceInitWASI(toModCxt(WasiMod.get()), Args, ArgLen, Envs,
2331
0
                                    EnvLen, Preopens, PreopenLen);
2332
0
    return toModCxt(WasiMod.release());
2333
0
  } catch (...) {
2334
0
    handleCAPIError();
2335
0
    return nullptr;
2336
0
  }
2337
0
}
2338
2339
WASMEDGE_CAPI_EXPORT extern WasmEdge_ModuleInstanceContext *
2340
WasmEdge_ModuleInstanceCreateWithData(const WasmEdge_String ModuleName,
2341
                                      void *HostData,
2342
0
                                      void (*Finalizer)(void *)) noexcept {
2343
0
  try {
2344
0
    return toModCxt(new WasmEdge::Runtime::Instance::ModuleInstance(
2345
0
        genStrView(ModuleName), HostData, Finalizer));
2346
0
  } catch (...) {
2347
0
    handleCAPIError();
2348
0
    return nullptr;
2349
0
  }
2350
0
}
2351
2352
WASMEDGE_CAPI_EXPORT void WasmEdge_ModuleInstanceInitWASI(
2353
    WasmEdge_ModuleInstanceContext *Cxt, const char *const *Args,
2354
    const uint32_t ArgLen, const char *const *Envs, const uint32_t EnvLen,
2355
0
    const char *const *Preopens, const uint32_t PreopenLen) noexcept {
2356
0
  if (!Cxt) {
2357
0
    return;
2358
0
  }
2359
0
  auto *WasiMod = dynamic_cast<WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2360
0
  if (!WasiMod) {
2361
0
    return;
2362
0
  }
2363
0
  try {
2364
0
    std::vector<std::string> ArgVec, EnvVec, DirVec;
2365
0
    std::string ProgName;
2366
0
    if (Args) {
2367
0
      if (ArgLen > 0) {
2368
0
        ProgName = Args[0];
2369
0
      }
2370
0
      for (uint32_t I = 1; I < ArgLen; I++) {
2371
0
        ArgVec.emplace_back(Args[I]);
2372
0
      }
2373
0
    }
2374
0
    if (Envs) {
2375
0
      for (uint32_t I = 0; I < EnvLen; I++) {
2376
0
        EnvVec.emplace_back(Envs[I]);
2377
0
      }
2378
0
    }
2379
0
    if (Preopens) {
2380
0
      for (uint32_t I = 0; I < PreopenLen; I++) {
2381
0
        DirVec.emplace_back(Preopens[I]);
2382
0
      }
2383
0
    }
2384
0
    WasiMod->init(DirVec, ProgName, ArgVec, EnvVec);
2385
0
  } catch (...) {
2386
0
    handleCAPIError();
2387
0
  }
2388
0
}
2389
2390
WASMEDGE_CAPI_EXPORT void WasmEdge_ModuleInstanceInitWASIWithFds(
2391
    WasmEdge_ModuleInstanceContext *Cxt, const char *const *Args,
2392
    const uint32_t ArgLen, const char *const *Envs, const uint32_t EnvLen,
2393
    const char *const *Preopens, const uint32_t PreopenLen,
2394
    const int32_t StdInFd, const int32_t StdOutFd,
2395
0
    const int32_t StdErrFd) noexcept {
2396
0
  if (!Cxt) {
2397
0
    return;
2398
0
  }
2399
0
  auto *WasiMod = dynamic_cast<WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2400
0
  if (!WasiMod) {
2401
0
    return;
2402
0
  }
2403
0
  try {
2404
0
    std::vector<std::string> ArgVec, EnvVec, DirVec;
2405
0
    std::string ProgName;
2406
0
    if (Args) {
2407
0
      if (ArgLen > 0) {
2408
0
        ProgName = Args[0];
2409
0
      }
2410
0
      for (uint32_t I = 1; I < ArgLen; I++) {
2411
0
        ArgVec.emplace_back(Args[I]);
2412
0
      }
2413
0
    }
2414
0
    if (Envs) {
2415
0
      for (uint32_t I = 0; I < EnvLen; I++) {
2416
0
        EnvVec.emplace_back(Envs[I]);
2417
0
      }
2418
0
    }
2419
0
    if (Preopens) {
2420
0
      for (uint32_t I = 0; I < PreopenLen; I++) {
2421
0
        DirVec.emplace_back(Preopens[I]);
2422
0
      }
2423
0
    }
2424
0
    auto Result = WasiMod->initWithFds(DirVec, ProgName, ArgVec, EnvVec,
2425
0
                                       StdInFd, StdOutFd, StdErrFd);
2426
0
    if (!Result) {
2427
0
      spdlog::error("    Failed to initialize WASI environment: {}"sv,
2428
0
                    Result.error());
2429
0
    }
2430
0
  } catch (...) {
2431
0
    handleCAPIError();
2432
0
  }
2433
0
}
2434
2435
WASMEDGE_CAPI_EXPORT extern uint32_t
2436
WasmEdge_ModuleInstanceWASIGetNativeHandler(
2437
    const WasmEdge_ModuleInstanceContext *Cxt, int32_t Fd,
2438
0
    uint64_t *NativeHandler) noexcept {
2439
0
  if (!Cxt) {
2440
0
    return 1;
2441
0
  }
2442
0
  auto *WasiMod =
2443
0
      dynamic_cast<const WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2444
0
  if (!WasiMod) {
2445
0
    return 2;
2446
0
  }
2447
0
  auto Handler = WasiMod->getNativeHandler(Fd);
2448
0
  if (!Handler) {
2449
0
    return 2;
2450
0
  }
2451
0
  *NativeHandler = *Handler;
2452
0
  return 0;
2453
0
}
2454
2455
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceWASIGetExitCode(
2456
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2457
0
  if (!Cxt) {
2458
0
    return EXIT_FAILURE;
2459
0
  }
2460
0
  auto *WasiMod =
2461
0
      dynamic_cast<const WasmEdge::Host::WasiModule *>(fromModCxt(Cxt));
2462
0
  if (!WasiMod) {
2463
0
    return EXIT_FAILURE;
2464
0
  }
2465
0
  return WasiMod->getExitCode();
2466
0
}
2467
2468
WASMEDGE_CAPI_EXPORT void
2469
WasmEdge_ModuleInstanceInitWasmEdgeProcess(const char *const *AllowedCmds,
2470
                                           const uint32_t CmdsLen,
2471
0
                                           const bool AllowAll) noexcept {
2472
0
  using namespace std::literals::string_view_literals;
2473
0
  if (const auto *Plugin =
2474
0
          WasmEdge::Plugin::Plugin::find("wasmedge_process"sv)) {
2475
0
    PO::ArgumentParser Parser;
2476
0
    Plugin->registerOptions(Parser);
2477
0
    Parser.set_raw_value<std::vector<std::string>>(
2478
0
        "allow-command"sv,
2479
0
        std::vector<std::string>(AllowedCmds, AllowedCmds + CmdsLen));
2480
0
    if (AllowAll) {
2481
0
      Parser.set_raw_value("allow-command-all"sv);
2482
0
    }
2483
0
  }
2484
0
}
2485
2486
WASMEDGE_CAPI_EXPORT WasmEdge_String WasmEdge_ModuleInstanceGetModuleName(
2487
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2488
0
  if (Cxt) {
2489
0
    auto StrView = fromModCxt(Cxt)->getModuleName();
2490
0
    return WasmEdge_String{/* Length */ static_cast<uint32_t>(StrView.length()),
2491
0
                           /* Buf */ StrView.data()};
2492
0
  }
2493
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
2494
0
}
2495
2496
WASMEDGE_CAPI_EXPORT void *WasmEdge_ModuleInstanceGetHostData(
2497
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2498
0
  if (Cxt) {
2499
0
    return fromModCxt(Cxt)->getHostData();
2500
0
  }
2501
0
  return nullptr;
2502
0
}
2503
2504
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext *
2505
WasmEdge_ModuleInstanceFindFunction(const WasmEdge_ModuleInstanceContext *Cxt,
2506
0
                                    const WasmEdge_String Name) noexcept {
2507
0
  if (Cxt) {
2508
0
    return toFuncCxt(fromModCxt(Cxt)->findFuncExports(genStrView(Name)));
2509
0
  }
2510
0
  return nullptr;
2511
0
}
2512
2513
WASMEDGE_CAPI_EXPORT WasmEdge_TableInstanceContext *
2514
WasmEdge_ModuleInstanceFindTable(const WasmEdge_ModuleInstanceContext *Cxt,
2515
0
                                 const WasmEdge_String Name) noexcept {
2516
0
  if (Cxt) {
2517
0
    return toTabCxt(fromModCxt(Cxt)->findTableExports(genStrView(Name)));
2518
0
  }
2519
0
  return nullptr;
2520
0
}
2521
2522
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext *
2523
WasmEdge_ModuleInstanceFindMemory(const WasmEdge_ModuleInstanceContext *Cxt,
2524
0
                                  const WasmEdge_String Name) noexcept {
2525
0
  if (Cxt) {
2526
0
    return toMemCxt(fromModCxt(Cxt)->findMemoryExports(genStrView(Name)));
2527
0
  }
2528
0
  return nullptr;
2529
0
}
2530
2531
WASMEDGE_CAPI_EXPORT WasmEdge_TagInstanceContext *
2532
WasmEdge_ModuleInstanceFindTag(const WasmEdge_ModuleInstanceContext *Cxt,
2533
0
                               const WasmEdge_String Name) noexcept {
2534
0
  if (Cxt) {
2535
0
    return toTagCxt(fromModCxt(Cxt)->findTagExports(genStrView(Name)));
2536
0
  }
2537
0
  return nullptr;
2538
0
}
2539
2540
WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext *
2541
WasmEdge_ModuleInstanceFindGlobal(const WasmEdge_ModuleInstanceContext *Cxt,
2542
0
                                  const WasmEdge_String Name) noexcept {
2543
0
  if (Cxt) {
2544
0
    return toGlobCxt(fromModCxt(Cxt)->findGlobalExports(genStrView(Name)));
2545
0
  }
2546
0
  return nullptr;
2547
0
}
2548
2549
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListFunctionLength(
2550
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2551
0
  if (Cxt) {
2552
0
    return fromModCxt(Cxt)->getFuncExportNum();
2553
0
  }
2554
0
  return 0;
2555
0
}
2556
2557
WASMEDGE_CAPI_EXPORT extern uint32_t
2558
WasmEdge_ModuleInstanceListFunction(const WasmEdge_ModuleInstanceContext *Cxt,
2559
                                    WasmEdge_String *Names,
2560
0
                                    const uint32_t Len) noexcept {
2561
0
  if (Cxt) {
2562
0
    return fromModCxt(Cxt)->getFuncExports(
2563
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2564
0
  }
2565
0
  return 0;
2566
0
}
2567
2568
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTableLength(
2569
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2570
0
  if (Cxt) {
2571
0
    return fromModCxt(Cxt)->getTableExportNum();
2572
0
  }
2573
0
  return 0;
2574
0
}
2575
2576
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTable(
2577
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2578
0
    const uint32_t Len) noexcept {
2579
0
  if (Cxt) {
2580
0
    return fromModCxt(Cxt)->getTableExports(
2581
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2582
0
  }
2583
0
  return 0;
2584
0
}
2585
2586
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListMemoryLength(
2587
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2588
0
  if (Cxt) {
2589
0
    return fromModCxt(Cxt)->getMemoryExportNum();
2590
0
  }
2591
0
  return 0;
2592
0
}
2593
2594
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListMemory(
2595
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2596
0
    const uint32_t Len) noexcept {
2597
0
  if (Cxt) {
2598
0
    return fromModCxt(Cxt)->getMemoryExports(
2599
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2600
0
  }
2601
0
  return 0;
2602
0
}
2603
2604
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTagLength(
2605
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2606
0
  if (Cxt) {
2607
0
    return fromModCxt(Cxt)->getTagExportNum();
2608
0
  }
2609
0
  return 0;
2610
0
}
2611
2612
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListTag(
2613
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2614
0
    const uint32_t Len) noexcept {
2615
0
  if (Cxt) {
2616
0
    return fromModCxt(Cxt)->getTagExports(
2617
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2618
0
  }
2619
0
  return 0;
2620
0
}
2621
2622
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListGlobalLength(
2623
0
    const WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2624
0
  if (Cxt) {
2625
0
    return fromModCxt(Cxt)->getGlobalExportNum();
2626
0
  }
2627
0
  return 0;
2628
0
}
2629
2630
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_ModuleInstanceListGlobal(
2631
    const WasmEdge_ModuleInstanceContext *Cxt, WasmEdge_String *Names,
2632
0
    const uint32_t Len) noexcept {
2633
0
  if (Cxt) {
2634
0
    return fromModCxt(Cxt)->getGlobalExports(
2635
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
2636
0
  }
2637
0
  return 0;
2638
0
}
2639
2640
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddFunction(
2641
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2642
0
    WasmEdge_FunctionInstanceContext *FuncCxt) noexcept {
2643
0
  return wrap(
2644
0
      [&]() -> WasmEdge::Expect<void> {
2645
0
        std::unique_ptr<WasmEdge::Runtime::Instance::FunctionInstance> Inst(
2646
0
            fromFuncCxt(FuncCxt));
2647
0
        auto Res =
2648
0
            fromModCxt(Cxt)->addHostFunc(genStrView(Name), std::move(Inst));
2649
0
        if (!Res) {
2650
0
          static_cast<void>(Inst.release());
2651
0
        }
2652
0
        return Res;
2653
0
      },
2654
0
      EmptyThen, Cxt, FuncCxt);
2655
0
}
2656
2657
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddTable(
2658
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2659
0
    WasmEdge_TableInstanceContext *TableCxt) noexcept {
2660
0
  return wrap(
2661
0
      [&]() -> WasmEdge::Expect<void> {
2662
0
        std::unique_ptr<WasmEdge::Runtime::Instance::TableInstance> Inst(
2663
0
            fromTabCxt(TableCxt));
2664
0
        auto Res =
2665
0
            fromModCxt(Cxt)->addHostTable(genStrView(Name), std::move(Inst));
2666
0
        if (!Res) {
2667
0
          static_cast<void>(Inst.release());
2668
0
        }
2669
0
        return Res;
2670
0
      },
2671
0
      EmptyThen, Cxt, TableCxt);
2672
0
}
2673
2674
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddMemory(
2675
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2676
0
    WasmEdge_MemoryInstanceContext *MemoryCxt) noexcept {
2677
0
  return wrap(
2678
0
      [&]() -> WasmEdge::Expect<void> {
2679
0
        std::unique_ptr<WasmEdge::Runtime::Instance::MemoryInstance> Inst(
2680
0
            fromMemCxt(MemoryCxt));
2681
0
        auto Res =
2682
0
            fromModCxt(Cxt)->addHostMemory(genStrView(Name), std::move(Inst));
2683
0
        if (!Res) {
2684
0
          static_cast<void>(Inst.release());
2685
0
        }
2686
0
        return Res;
2687
0
      },
2688
0
      EmptyThen, Cxt, MemoryCxt);
2689
0
}
2690
2691
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_ModuleInstanceAddGlobal(
2692
    WasmEdge_ModuleInstanceContext *Cxt, const WasmEdge_String Name,
2693
0
    WasmEdge_GlobalInstanceContext *GlobalCxt) noexcept {
2694
0
  return wrap(
2695
0
      [&]() -> WasmEdge::Expect<void> {
2696
0
        std::unique_ptr<WasmEdge::Runtime::Instance::GlobalInstance> Inst(
2697
0
            fromGlobCxt(GlobalCxt));
2698
0
        auto Res =
2699
0
            fromModCxt(Cxt)->addHostGlobal(genStrView(Name), std::move(Inst));
2700
0
        if (!Res) {
2701
0
          static_cast<void>(Inst.release());
2702
0
        }
2703
0
        return Res;
2704
0
      },
2705
0
      EmptyThen, Cxt, GlobalCxt);
2706
0
}
2707
2708
WASMEDGE_CAPI_EXPORT void
2709
0
WasmEdge_ModuleInstanceDelete(WasmEdge_ModuleInstanceContext *Cxt) noexcept {
2710
0
  auto *Mod = fromModCxt(Cxt);
2711
0
  if (Mod) {
2712
0
    Mod->terminate();
2713
0
  }
2714
0
}
2715
2716
// <<<<<<<< WasmEdge module instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2717
2718
// >>>>>>>> WasmEdge function instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2719
2720
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext *
2721
WasmEdge_FunctionInstanceCreate(const WasmEdge_FunctionTypeContext *Type,
2722
                                WasmEdge_HostFunc_t HostFunc, void *Data,
2723
0
                                const uint64_t Cost) noexcept {
2724
0
  try {
2725
0
    if (Type && HostFunc) {
2726
0
      return toFuncCxt(new WasmEdge::Runtime::Instance::FunctionInstance(
2727
0
          std::make_unique<CAPIHostFunc>(fromFuncTypeCxt(Type), HostFunc, Data,
2728
0
                                         Cost)));
2729
0
    }
2730
0
  } catch (...) {
2731
0
    handleCAPIError();
2732
0
  }
2733
0
  return nullptr;
2734
0
}
2735
2736
WASMEDGE_CAPI_EXPORT WasmEdge_FunctionInstanceContext *
2737
WasmEdge_FunctionInstanceCreateBinding(const WasmEdge_FunctionTypeContext *Type,
2738
                                       WasmEdge_WrapFunc_t WrapFunc,
2739
                                       void *Binding, void *Data,
2740
0
                                       const uint64_t Cost) noexcept {
2741
0
  try {
2742
0
    if (Type && WrapFunc) {
2743
0
      return toFuncCxt(new WasmEdge::Runtime::Instance::FunctionInstance(
2744
0
          std::make_unique<CAPIHostFunc>(fromFuncTypeCxt(Type), WrapFunc,
2745
0
                                         Binding, Data, Cost)));
2746
0
    }
2747
0
  } catch (...) {
2748
0
    handleCAPIError();
2749
0
  }
2750
0
  return nullptr;
2751
0
}
2752
2753
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
2754
WasmEdge_FunctionInstanceGetFunctionType(
2755
0
    const WasmEdge_FunctionInstanceContext *Cxt) noexcept {
2756
0
  if (Cxt) {
2757
0
    return toFuncTypeCxt(&fromFuncCxt(Cxt)->getFuncType());
2758
0
  }
2759
0
  return nullptr;
2760
0
}
2761
2762
WASMEDGE_CAPI_EXPORT extern const void *WasmEdge_FunctionInstanceGetData(
2763
0
    const WasmEdge_FunctionInstanceContext *Cxt) noexcept {
2764
0
  if (Cxt && fromFuncCxt(Cxt)->isHostFunction()) {
2765
0
    if (auto *HostFunc =
2766
0
            dynamic_cast<CAPIHostFunc *>(&fromFuncCxt(Cxt)->getHostFunc())) {
2767
0
      return HostFunc->getData();
2768
0
    }
2769
0
  }
2770
0
  return nullptr;
2771
0
}
2772
2773
WASMEDGE_CAPI_EXPORT void WasmEdge_FunctionInstanceDelete(
2774
0
    WasmEdge_FunctionInstanceContext *Cxt) noexcept {
2775
0
  delete fromFuncCxt(Cxt);
2776
0
}
2777
2778
// <<<<<<<< WasmEdge function instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2779
2780
// >>>>>>>> WasmEdge table instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2781
2782
WASMEDGE_CAPI_EXPORT WasmEdge_TableInstanceContext *
2783
WasmEdge_TableInstanceCreate(
2784
0
    const WasmEdge_TableTypeContext *TabType) noexcept {
2785
0
  try {
2786
0
    if (TabType) {
2787
0
      const AST::TableType &TType = *fromTabTypeCxt(TabType);
2788
0
      if (!TType.getRefType().isNullableRefType()) {
2789
0
        spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
2790
0
        return nullptr;
2791
0
      }
2792
0
      return toTabCxt(new WasmEdge::Runtime::Instance::TableInstance(TType));
2793
0
    }
2794
0
  } catch (...) {
2795
0
    handleCAPIError();
2796
0
  }
2797
0
  return nullptr;
2798
0
}
2799
2800
WASMEDGE_CAPI_EXPORT extern WasmEdge_TableInstanceContext *
2801
WasmEdge_TableInstanceCreateWithInit(const WasmEdge_TableTypeContext *TabType,
2802
0
                                     const WasmEdge_Value Value) noexcept {
2803
0
  try {
2804
0
    if (TabType) {
2805
      // Comparison of the value types needs the module instance to retrieve the
2806
      // function type index after applying the typed function reference
2807
      // proposal. It's impossible to do this without refactoring. Therefore
2808
      // simply match the FuncRef and ExternRef here.
2809
0
      const AST::TableType &TType = *fromTabTypeCxt(TabType);
2810
0
      WasmEdge::ValType GotType = genValType(Value.Type);
2811
0
      if (TType.getRefType().isFuncRefType() != GotType.isFuncRefType()) {
2812
0
        spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch);
2813
0
        spdlog::error(
2814
0
            WasmEdge::ErrInfo::InfoMismatch(TType.getRefType(), GotType));
2815
0
        return nullptr;
2816
0
      }
2817
0
      auto Val = WasmEdge::ValVariant(
2818
0
                     to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value))
2819
0
                     .get<WasmEdge::RefVariant>();
2820
0
      if (!TType.getRefType().isNullableRefType() && Val.isNull()) {
2821
0
        spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
2822
0
        return nullptr;
2823
0
      }
2824
0
      return toTabCxt(
2825
0
          new WasmEdge::Runtime::Instance::TableInstance(TType, Val));
2826
0
    }
2827
0
  } catch (...) {
2828
0
    handleCAPIError();
2829
0
  }
2830
0
  return nullptr;
2831
0
}
2832
2833
WASMEDGE_CAPI_EXPORT const WasmEdge_TableTypeContext *
2834
WasmEdge_TableInstanceGetTableType(
2835
0
    const WasmEdge_TableInstanceContext *Cxt) noexcept {
2836
0
  if (Cxt) {
2837
0
    return toTabTypeCxt(&fromTabCxt(Cxt)->getTableType());
2838
0
  }
2839
0
  return nullptr;
2840
0
}
2841
2842
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceGetData(
2843
    const WasmEdge_TableInstanceContext *Cxt, WasmEdge_Value *Data,
2844
0
    const uint64_t Offset) noexcept {
2845
0
  return wrap([&]() { return fromTabCxt(Cxt)->getRefAddr(Offset); },
2846
0
              [&Data, &Cxt](auto &&Res) {
2847
0
                *Data = genWasmEdge_Value(
2848
0
                    *Res, fromTabCxt(Cxt)->getTableType().getRefType());
2849
0
              },
2850
0
              Cxt, Data);
2851
0
}
2852
2853
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceSetData(
2854
    WasmEdge_TableInstanceContext *Cxt, WasmEdge_Value Data,
2855
0
    const uint64_t Offset) noexcept {
2856
0
  return wrap(
2857
0
      [&]() -> WasmEdge::Expect<void> {
2858
        // Comparison of the value types needs the module instance to retrieve
2859
        // the function type index after applying the typed function reference
2860
        // proposal. It's impossible to do this without refactoring. Therefore
2861
        // simply match the FuncRef and ExternRef here.
2862
0
        WasmEdge::ValType ExpType =
2863
0
            fromTabCxt(Cxt)->getTableType().getRefType();
2864
0
        WasmEdge::ValType GotType = genValType(Data.Type);
2865
0
        if (!GotType.isRefType() ||
2866
0
            ExpType.isFuncRefType() != GotType.isFuncRefType()) {
2867
0
          spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch);
2868
0
          spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType));
2869
0
          return Unexpect(WasmEdge::ErrCode::Value::RefTypeMismatch);
2870
0
        }
2871
0
        auto Val = WasmEdge::ValVariant(
2872
0
                       to_WasmEdge_128_t<WasmEdge::uint128_t>(Data.Value))
2873
0
                       .get<WasmEdge::RefVariant>();
2874
0
        if (!ExpType.isNullableRefType() && Val.isNull()) {
2875
          // If this table is not a nullable ref type, the data should not be
2876
          // null.
2877
0
          spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
2878
0
          return Unexpect(WasmEdge::ErrCode::Value::NonNullRequired);
2879
0
        }
2880
0
        return fromTabCxt(Cxt)->setRefAddr(Offset, Val);
2881
0
      },
2882
0
      EmptyThen, Cxt);
2883
0
}
2884
2885
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_TableInstanceGetSize(
2886
0
    const WasmEdge_TableInstanceContext *Cxt) noexcept {
2887
0
  if (Cxt) {
2888
0
    return fromTabCxt(Cxt)->getSize();
2889
0
  }
2890
0
  return 0;
2891
0
}
2892
2893
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_TableInstanceGrow(
2894
0
    WasmEdge_TableInstanceContext *Cxt, const uint64_t Size) noexcept {
2895
0
  return wrap(
2896
0
      [&]() -> WasmEdge::Expect<void> {
2897
0
        if (fromTabCxt(Cxt)->growTable(Size)) {
2898
0
          return {};
2899
0
        } else {
2900
0
          spdlog::error(WasmEdge::ErrCode::Value::TableOutOfBounds);
2901
0
          return WasmEdge::Unexpect(WasmEdge::ErrCode::Value::TableOutOfBounds);
2902
0
        }
2903
0
      },
2904
0
      EmptyThen, Cxt);
2905
0
}
2906
2907
WASMEDGE_CAPI_EXPORT void
2908
0
WasmEdge_TableInstanceDelete(WasmEdge_TableInstanceContext *Cxt) noexcept {
2909
0
  delete fromTabCxt(Cxt);
2910
0
}
2911
2912
// <<<<<<<< WasmEdge table instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2913
2914
// >>>>>>>> WasmEdge memory instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2915
2916
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext *
2917
WasmEdge_MemoryInstanceCreate(
2918
0
    const WasmEdge_MemoryTypeContext *MemType) noexcept {
2919
0
  try {
2920
0
    if (MemType) {
2921
0
      return toMemCxt(new WasmEdge::Runtime::Instance::MemoryInstance(
2922
0
          *fromMemTypeCxt(MemType)));
2923
0
    }
2924
0
  } catch (...) {
2925
0
    handleCAPIError();
2926
0
  }
2927
0
  return nullptr;
2928
0
}
2929
2930
WASMEDGE_CAPI_EXPORT const WasmEdge_MemoryTypeContext *
2931
WasmEdge_MemoryInstanceGetMemoryType(
2932
0
    const WasmEdge_MemoryInstanceContext *Cxt) noexcept {
2933
0
  if (Cxt) {
2934
0
    return toMemTypeCxt(&fromMemCxt(Cxt)->getMemoryType());
2935
0
  }
2936
0
  return nullptr;
2937
0
}
2938
2939
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceGetData(
2940
    const WasmEdge_MemoryInstanceContext *Cxt, uint8_t *Data,
2941
0
    const uint64_t Offset, const uint64_t Length) noexcept {
2942
0
  return wrap([&]() { return fromMemCxt(Cxt)->getBytes(Offset, Length); },
2943
0
              [&](auto &&Res) { std::copy_n((*Res).begin(), Length, Data); },
2944
0
              Cxt, Data);
2945
0
}
2946
2947
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceSetData(
2948
    WasmEdge_MemoryInstanceContext *Cxt, const uint8_t *Data,
2949
0
    const uint64_t Offset, const uint64_t Length) noexcept {
2950
0
  return wrap(
2951
0
      [&]() {
2952
0
        return fromMemCxt(Cxt)->setBytes(genSpan(Data, Length), Offset, 0,
2953
0
                                         Length);
2954
0
      },
2955
0
      EmptyThen, Cxt, Data);
2956
0
}
2957
2958
WASMEDGE_CAPI_EXPORT uint8_t *
2959
WasmEdge_MemoryInstanceGetPointer(WasmEdge_MemoryInstanceContext *Cxt,
2960
                                  const uint64_t Offset,
2961
0
                                  const uint64_t Length) noexcept {
2962
0
  if (Cxt) {
2963
0
    const auto S = fromMemCxt(Cxt)->getSpan<uint8_t>(Offset, Length);
2964
0
    if (S.size() == Length) {
2965
0
      return S.data();
2966
0
    }
2967
0
  }
2968
0
  return nullptr;
2969
0
}
2970
2971
WASMEDGE_CAPI_EXPORT const uint8_t *WasmEdge_MemoryInstanceGetPointerConst(
2972
    const WasmEdge_MemoryInstanceContext *Cxt, const uint64_t Offset,
2973
0
    const uint64_t Length) noexcept {
2974
0
  if (Cxt) {
2975
0
    const auto S = fromMemCxt(Cxt)->getSpan<const uint8_t>(Offset, Length);
2976
0
    if (S.size() == Length) {
2977
0
      return S.data();
2978
0
    }
2979
0
  }
2980
0
  return nullptr;
2981
0
}
2982
2983
WASMEDGE_CAPI_EXPORT uint64_t WasmEdge_MemoryInstanceGetPageSize(
2984
0
    const WasmEdge_MemoryInstanceContext *Cxt) noexcept {
2985
0
  if (Cxt) {
2986
0
    return fromMemCxt(Cxt)->getPageSize();
2987
0
  }
2988
0
  return 0;
2989
0
}
2990
2991
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_MemoryInstanceGrowPage(
2992
0
    WasmEdge_MemoryInstanceContext *Cxt, const uint64_t Page) noexcept {
2993
0
  return wrap(
2994
0
      [&]() -> WasmEdge::Expect<void> {
2995
0
        if (fromMemCxt(Cxt)->growPage(Page)) {
2996
0
          return {};
2997
0
        } else {
2998
0
          spdlog::error(WasmEdge::ErrCode::Value::MemoryOutOfBounds);
2999
0
          return WasmEdge::Unexpect(
3000
0
              WasmEdge::ErrCode::Value::MemoryOutOfBounds);
3001
0
        }
3002
0
      },
3003
0
      EmptyThen, Cxt);
3004
0
}
3005
3006
WASMEDGE_CAPI_EXPORT void
3007
0
WasmEdge_MemoryInstanceDelete(WasmEdge_MemoryInstanceContext *Cxt) noexcept {
3008
0
  delete fromMemCxt(Cxt);
3009
0
}
3010
3011
// <<<<<<<< WasmEdge memory instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3012
3013
// >>>>>>>> WasmEdge tag instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3014
3015
WASMEDGE_CAPI_EXPORT const WasmEdge_TagTypeContext *
3016
WasmEdge_TagInstanceGetTagType(
3017
0
    const WasmEdge_TagInstanceContext *Cxt) noexcept {
3018
0
  if (Cxt) {
3019
0
    return toTagTypeCxt(&fromTagCxt(Cxt)->getTagType());
3020
0
  }
3021
0
  return nullptr;
3022
0
}
3023
3024
// <<<<<<<< WasmEdge tag instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3025
3026
// >>>>>>>> WasmEdge global instance functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3027
3028
WASMEDGE_CAPI_EXPORT WasmEdge_GlobalInstanceContext *
3029
WasmEdge_GlobalInstanceCreate(const WasmEdge_GlobalTypeContext *GlobType,
3030
0
                              const WasmEdge_Value Value) noexcept {
3031
0
  try {
3032
0
    if (GlobType) {
3033
      // Comparison of the value types needs the module instance to retrieve the
3034
      // function type index after applying the typed function reference
3035
      // proposal. It's impossible to do this without refactoring. Therefore
3036
      // simply match the FuncRef and ExternRef here.
3037
0
      const AST::GlobalType &GType = *fromGlobTypeCxt(GlobType);
3038
0
      WasmEdge::ValType ExpType = GType.getValType();
3039
0
      WasmEdge::ValType GotType = genValType(Value.Type);
3040
0
      if (ExpType.isFuncRefType() != GotType.isFuncRefType()) {
3041
0
        spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
3042
0
        spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType));
3043
0
        return nullptr;
3044
0
      }
3045
3046
0
      WasmEdge::ValVariant Val =
3047
0
          to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value);
3048
0
      if (ExpType.isRefType()) {
3049
        // Reference type case.
3050
0
        if (!ExpType.isNullableRefType() &&
3051
0
            Val.get<WasmEdge::RefVariant>().isNull()) {
3052
          // If this global is not a nullable ref type, the data should not be
3053
          // null.
3054
0
          spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
3055
0
          return nullptr;
3056
0
        }
3057
0
      } else {
3058
        // Number type case.
3059
0
        if (ExpType != GotType) {
3060
0
          spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
3061
0
          return nullptr;
3062
0
        }
3063
0
      }
3064
0
      return toGlobCxt(
3065
0
          new WasmEdge::Runtime::Instance::GlobalInstance(GType, Val));
3066
0
    }
3067
0
  } catch (...) {
3068
0
    handleCAPIError();
3069
0
  }
3070
0
  return nullptr;
3071
0
}
3072
3073
WASMEDGE_CAPI_EXPORT const WasmEdge_GlobalTypeContext *
3074
WasmEdge_GlobalInstanceGetGlobalType(
3075
0
    const WasmEdge_GlobalInstanceContext *Cxt) noexcept {
3076
0
  if (Cxt) {
3077
0
    return toGlobTypeCxt(&fromGlobCxt(Cxt)->getGlobalType());
3078
0
  }
3079
0
  return nullptr;
3080
0
}
3081
3082
WASMEDGE_CAPI_EXPORT WasmEdge_Value WasmEdge_GlobalInstanceGetValue(
3083
0
    const WasmEdge_GlobalInstanceContext *Cxt) noexcept {
3084
0
  if (Cxt) {
3085
0
    return genWasmEdge_Value(fromGlobCxt(Cxt)->getValue(),
3086
0
                             fromGlobCxt(Cxt)->getGlobalType().getValType());
3087
0
  }
3088
0
  return genWasmEdge_Value(
3089
0
      WasmEdge::ValVariant(static_cast<WasmEdge::uint128_t>(0U)),
3090
0
      TypeCode::I32);
3091
0
}
3092
3093
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_GlobalInstanceSetValue(
3094
0
    WasmEdge_GlobalInstanceContext *Cxt, const WasmEdge_Value Value) noexcept {
3095
0
  return wrap(
3096
0
      [&]() -> WasmEdge::Expect<void> {
3097
0
        const auto &GlobType = fromGlobCxt(Cxt)->getGlobalType();
3098
0
        if (GlobType.getValMut() != WasmEdge::ValMut::Var) {
3099
0
          spdlog::error(WasmEdge::ErrCode::Value::SetValueToConst);
3100
0
          return Unexpect(WasmEdge::ErrCode::Value::SetValueToConst);
3101
0
        }
3102
3103
        // Comparison of the value types needs the module instance to retrieve
3104
        // the function type index after applying the typed function reference
3105
        // proposal. It's impossible to do this without refactoring. Therefore
3106
        // simply match the FuncRef and ExternRef here.
3107
0
        WasmEdge::ValType ExpType = GlobType.getValType();
3108
0
        WasmEdge::ValType GotType = genValType(Value.Type);
3109
0
        if (ExpType.isRefType() &&
3110
0
            ExpType.isFuncRefType() != GotType.isFuncRefType()) {
3111
0
          spdlog::error(WasmEdge::ErrCode::Value::RefTypeMismatch);
3112
0
          spdlog::error(WasmEdge::ErrInfo::InfoMismatch(ExpType, GotType));
3113
0
          return Unexpect(WasmEdge::ErrCode::Value::RefTypeMismatch);
3114
0
        }
3115
3116
0
        WasmEdge::ValVariant Val =
3117
0
            to_WasmEdge_128_t<WasmEdge::uint128_t>(Value.Value);
3118
0
        if (ExpType.isRefType()) {
3119
          // Reference type case.
3120
0
          if (!ExpType.isNullableRefType() &&
3121
0
              Val.get<WasmEdge::RefVariant>().isNull()) {
3122
            // If this global is not a nullable ref type, the data should not be
3123
            // null.
3124
0
            spdlog::error(WasmEdge::ErrCode::Value::NonNullRequired);
3125
0
            return Unexpect(WasmEdge::ErrCode::Value::NonNullRequired);
3126
0
          }
3127
0
        } else {
3128
          // Number type case.
3129
0
          if (ExpType != GotType) {
3130
0
            spdlog::error(WasmEdge::ErrCode::Value::SetValueErrorType);
3131
0
            return Unexpect(WasmEdge::ErrCode::Value::SetValueErrorType);
3132
0
          }
3133
0
        }
3134
0
        fromGlobCxt(Cxt)->setValue(Val);
3135
0
        return {};
3136
0
      },
3137
0
      EmptyThen, Cxt);
3138
0
}
3139
3140
WASMEDGE_CAPI_EXPORT void
3141
0
WasmEdge_GlobalInstanceDelete(WasmEdge_GlobalInstanceContext *Cxt) noexcept {
3142
0
  delete fromGlobCxt(Cxt);
3143
0
}
3144
3145
// <<<<<<<< WasmEdge global instance functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3146
3147
// >>>>>>>> WasmEdge calling frame functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3148
3149
WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext *WasmEdge_CallingFrameGetExecutor(
3150
0
    const WasmEdge_CallingFrameContext *Cxt) noexcept {
3151
0
  if (Cxt) {
3152
0
    return toExecutorCxt(fromCallFrameCxt(Cxt)->getExecutor());
3153
0
  }
3154
0
  return nullptr;
3155
0
}
3156
3157
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
3158
WasmEdge_CallingFrameGetModuleInstance(
3159
0
    const WasmEdge_CallingFrameContext *Cxt) noexcept {
3160
0
  if (Cxt) {
3161
0
    return toModCxt(fromCallFrameCxt(Cxt)->getModule());
3162
0
  }
3163
0
  return nullptr;
3164
0
}
3165
3166
WASMEDGE_CAPI_EXPORT WasmEdge_MemoryInstanceContext *
3167
WasmEdge_CallingFrameGetMemoryInstance(const WasmEdge_CallingFrameContext *Cxt,
3168
0
                                       const uint32_t Idx) noexcept {
3169
0
  if (Cxt) {
3170
0
    return toMemCxt(fromCallFrameCxt(Cxt)->getMemoryByIndex(Idx));
3171
0
  }
3172
0
  return nullptr;
3173
0
}
3174
3175
// <<<<<<<< WasmEdge calling frame functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3176
3177
// >>>>>>>> WasmEdge Async functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3178
3179
WASMEDGE_CAPI_EXPORT void
3180
0
WasmEdge_AsyncWait(const WasmEdge_Async *Cxt) noexcept {
3181
0
  if (Cxt) {
3182
0
    Cxt->Async.wait();
3183
0
  }
3184
0
}
3185
3186
WASMEDGE_CAPI_EXPORT bool
3187
WasmEdge_AsyncWaitFor(const WasmEdge_Async *Cxt,
3188
0
                      uint64_t Milliseconds) noexcept {
3189
0
  if (Cxt) {
3190
0
    return Cxt->Async.waitFor(std::chrono::milliseconds(Milliseconds));
3191
0
  }
3192
0
  return false;
3193
0
}
3194
3195
0
WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncCancel(WasmEdge_Async *Cxt) noexcept {
3196
0
  if (Cxt) {
3197
0
    Cxt->Async.cancel();
3198
0
  }
3199
0
}
3200
3201
WASMEDGE_CAPI_EXPORT uint32_t
3202
0
WasmEdge_AsyncGetReturnsLength(const WasmEdge_Async *Cxt) noexcept {
3203
0
  if (Cxt) {
3204
0
    if (auto Res = Cxt->Async.get()) {
3205
0
      return static_cast<uint32_t>((*Res).size());
3206
0
    }
3207
0
  }
3208
0
  return 0;
3209
0
}
3210
3211
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3212
WasmEdge_AsyncGet(const WasmEdge_Async *Cxt, WasmEdge_Value *Returns,
3213
0
                  const uint32_t ReturnLen) noexcept {
3214
0
  return wrap(
3215
0
      [&]() { return Cxt->Async.get(); },
3216
0
      [&](auto Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); }, Cxt);
3217
0
}
3218
3219
0
WASMEDGE_CAPI_EXPORT void WasmEdge_AsyncDelete(WasmEdge_Async *Cxt) noexcept {
3220
0
  delete Cxt;
3221
0
}
3222
3223
// <<<<<<<< WasmEdge Async functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3224
3225
// >>>>>>>> WasmEdge VM functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3226
3227
WASMEDGE_CAPI_EXPORT WasmEdge_VMContext *
3228
WasmEdge_VMCreate(const WasmEdge_ConfigureContext *ConfCxt,
3229
0
                  WasmEdge_StoreContext *StoreCxt) noexcept {
3230
0
  try {
3231
0
    if (ConfCxt) {
3232
0
      if (StoreCxt) {
3233
0
        return new WasmEdge_VMContext(ConfCxt->Conf, *fromStoreCxt(StoreCxt));
3234
0
      } else {
3235
0
        return new WasmEdge_VMContext(ConfCxt->Conf);
3236
0
      }
3237
0
    } else {
3238
0
      if (StoreCxt) {
3239
0
        return new WasmEdge_VMContext(WasmEdge::Configure(),
3240
0
                                      *fromStoreCxt(StoreCxt));
3241
0
      } else {
3242
0
        return new WasmEdge_VMContext(WasmEdge::Configure());
3243
0
      }
3244
0
    }
3245
0
  } catch (...) {
3246
0
    handleCAPIError();
3247
0
    return nullptr;
3248
0
  }
3249
0
}
3250
3251
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromFile(
3252
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3253
0
    const char *Path) noexcept {
3254
0
  return wrap(
3255
0
      [&]() {
3256
0
        return Cxt->VM.registerModule(genStrView(ModuleName), genPath(Path));
3257
0
      },
3258
0
      EmptyThen, Cxt);
3259
0
}
3260
3261
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromBuffer(
3262
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3263
0
    const uint8_t *Buf, const uint32_t BufLen) noexcept {
3264
0
  return WasmEdge_VMRegisterModuleFromBytes(Cxt, ModuleName,
3265
0
                                            WasmEdge_BytesWrap(Buf, BufLen));
3266
0
}
3267
3268
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromBytes(
3269
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3270
0
    const WasmEdge_Bytes Bytes) noexcept {
3271
0
  return wrap(
3272
0
      [&]() {
3273
0
        return Cxt->VM.registerModule(genStrView(ModuleName),
3274
0
                                      genSpan(Bytes.Buf, Bytes.Length));
3275
0
      },
3276
0
      EmptyThen, Cxt);
3277
0
}
3278
3279
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromASTModule(
3280
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3281
0
    const WasmEdge_ASTModuleContext *ASTCxt) noexcept {
3282
0
  return wrap(
3283
0
      [&]() {
3284
0
        return Cxt->VM.registerModule(genStrView(ModuleName),
3285
0
                                      *fromASTModCxt(ASTCxt));
3286
0
      },
3287
0
      EmptyThen, Cxt, ASTCxt);
3288
0
}
3289
3290
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRegisterModuleFromImport(
3291
    WasmEdge_VMContext *Cxt,
3292
0
    const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept {
3293
0
  return wrap([&]() { return Cxt->VM.registerModule(*fromModCxt(ImportCxt)); },
3294
0
              EmptyThen, Cxt, ImportCxt);
3295
0
}
3296
3297
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3298
WasmEdge_VMRegisterModuleFromImportWithAlias(
3299
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3300
0
    const WasmEdge_ModuleInstanceContext *ImportCxt) noexcept {
3301
0
  return wrap(
3302
0
      [&]() {
3303
0
        return Cxt->VM.registerModule(genStrView(ModuleName),
3304
0
                                      *fromModCxt(ImportCxt));
3305
0
      },
3306
0
      EmptyThen, Cxt, ImportCxt);
3307
0
}
3308
3309
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromFile(
3310
    WasmEdge_VMContext *Cxt, const char *Path, const WasmEdge_String FuncName,
3311
    const WasmEdge_Value *Params, const uint32_t ParamLen,
3312
0
    WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept {
3313
0
  try {
3314
0
    auto ParamPair = genParamPair(Params, ParamLen);
3315
0
    return wrap(
3316
0
        [&]() {
3317
0
          return Cxt->VM.runWasmFile(genPath(Path), genStrView(FuncName),
3318
0
                                     ParamPair.first, ParamPair.second);
3319
0
        },
3320
0
        [&](auto Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3321
0
        Cxt);
3322
0
  } catch (...) {
3323
0
    return handleCAPIError();
3324
0
  }
3325
0
}
3326
3327
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromBuffer(
3328
    WasmEdge_VMContext *Cxt, const uint8_t *Buf, const uint32_t BufLen,
3329
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3330
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3331
0
    const uint32_t ReturnLen) noexcept {
3332
0
  return WasmEdge_VMRunWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen),
3333
0
                                     FuncName, Params, ParamLen, Returns,
3334
0
                                     ReturnLen);
3335
0
}
3336
3337
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromBytes(
3338
    WasmEdge_VMContext *Cxt, const WasmEdge_Bytes Bytes,
3339
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3340
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3341
0
    const uint32_t ReturnLen) noexcept {
3342
0
  try {
3343
0
    auto ParamPair = genParamPair(Params, ParamLen);
3344
0
    return wrap(
3345
0
        [&]() {
3346
0
          return Cxt->VM.runWasmFile(genSpan(Bytes.Buf, Bytes.Length),
3347
0
                                     genStrView(FuncName), ParamPair.first,
3348
0
                                     ParamPair.second);
3349
0
        },
3350
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3351
0
        Cxt);
3352
0
  } catch (...) {
3353
0
    return handleCAPIError();
3354
0
  }
3355
0
}
3356
3357
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMRunWasmFromASTModule(
3358
    WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt,
3359
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3360
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3361
0
    const uint32_t ReturnLen) noexcept {
3362
0
  try {
3363
0
    auto ParamPair = genParamPair(Params, ParamLen);
3364
0
    return wrap(
3365
0
        [&]() {
3366
0
          return Cxt->VM.runWasmFile(*fromASTModCxt(ASTCxt),
3367
0
                                     genStrView(FuncName), ParamPair.first,
3368
0
                                     ParamPair.second);
3369
0
        },
3370
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3371
0
        Cxt, ASTCxt);
3372
0
  } catch (...) {
3373
0
    return handleCAPIError();
3374
0
  }
3375
0
}
3376
3377
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromFile(
3378
    WasmEdge_VMContext *Cxt, const char *Path, const WasmEdge_String FuncName,
3379
0
    const WasmEdge_Value *Params, const uint32_t ParamLen) noexcept {
3380
0
  try {
3381
0
    auto ParamPair = genParamPair(Params, ParamLen);
3382
0
    if (Cxt) {
3383
0
      return new WasmEdge_Async(
3384
0
          Cxt->VM.asyncRunWasmFile(genPath(Path), genStrView(FuncName),
3385
0
                                   ParamPair.first, ParamPair.second));
3386
0
    }
3387
0
  } catch (...) {
3388
0
    handleCAPIError();
3389
0
  }
3390
0
  return nullptr;
3391
0
}
3392
3393
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromBuffer(
3394
    WasmEdge_VMContext *Cxt, const uint8_t *Buf, const uint32_t BufLen,
3395
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3396
0
    const uint32_t ParamLen) noexcept {
3397
0
  return WasmEdge_VMAsyncRunWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen),
3398
0
                                          FuncName, Params, ParamLen);
3399
0
}
3400
3401
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromBytes(
3402
    WasmEdge_VMContext *Cxt, const WasmEdge_Bytes Bytes,
3403
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3404
0
    const uint32_t ParamLen) noexcept {
3405
0
  try {
3406
0
    auto ParamPair = genParamPair(Params, ParamLen);
3407
0
    if (Cxt) {
3408
0
      return new WasmEdge_Async(Cxt->VM.asyncRunWasmFile(
3409
0
          genSpan(Bytes.Buf, Bytes.Length), genStrView(FuncName),
3410
0
          ParamPair.first, ParamPair.second));
3411
0
    }
3412
0
  } catch (...) {
3413
0
    handleCAPIError();
3414
0
  }
3415
0
  return nullptr;
3416
0
}
3417
3418
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncRunWasmFromASTModule(
3419
    WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt,
3420
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3421
0
    const uint32_t ParamLen) noexcept {
3422
0
  try {
3423
0
    auto ParamPair = genParamPair(Params, ParamLen);
3424
0
    if (Cxt && ASTCxt) {
3425
0
      return new WasmEdge_Async(
3426
0
          Cxt->VM.asyncRunWasmFile(*fromASTModCxt(ASTCxt), genStrView(FuncName),
3427
0
                                   ParamPair.first, ParamPair.second));
3428
0
    }
3429
0
  } catch (...) {
3430
0
    handleCAPIError();
3431
0
  }
3432
0
  return nullptr;
3433
0
}
3434
3435
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMLoadWasmFromFile(
3436
0
    WasmEdge_VMContext *Cxt, const char *Path) noexcept {
3437
0
  return wrap([&]() { return Cxt->VM.loadWasm(genPath(Path)); }, EmptyThen,
3438
0
              Cxt);
3439
0
}
3440
3441
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3442
WasmEdge_VMLoadWasmFromBuffer(WasmEdge_VMContext *Cxt, const uint8_t *Buf,
3443
0
                              const uint32_t BufLen) noexcept {
3444
0
  return WasmEdge_VMLoadWasmFromBytes(Cxt, WasmEdge_BytesWrap(Buf, BufLen));
3445
0
}
3446
3447
WASMEDGE_CAPI_EXPORT extern WasmEdge_Result
3448
WasmEdge_VMLoadWasmFromBytes(WasmEdge_VMContext *Cxt,
3449
0
                             const WasmEdge_Bytes Bytes) noexcept {
3450
0
  return wrap(
3451
0
      [&]() { return Cxt->VM.loadWasm(genSpan(Bytes.Buf, Bytes.Length)); },
3452
0
      EmptyThen, Cxt);
3453
0
}
3454
3455
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMLoadWasmFromASTModule(
3456
0
    WasmEdge_VMContext *Cxt, const WasmEdge_ASTModuleContext *ASTCxt) noexcept {
3457
0
  return wrap([&]() { return Cxt->VM.loadWasm(*fromASTModCxt(ASTCxt)); },
3458
0
              EmptyThen, Cxt, ASTCxt);
3459
0
}
3460
3461
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3462
0
WasmEdge_VMValidate(WasmEdge_VMContext *Cxt) noexcept {
3463
0
  return wrap([&]() { return Cxt->VM.validate(); }, EmptyThen, Cxt);
3464
0
}
3465
3466
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3467
0
WasmEdge_VMInstantiate(WasmEdge_VMContext *Cxt) noexcept {
3468
0
  return wrap([&]() { return Cxt->VM.instantiate(); }, EmptyThen, Cxt);
3469
0
}
3470
3471
WASMEDGE_CAPI_EXPORT WasmEdge_Result
3472
WasmEdge_VMExecute(WasmEdge_VMContext *Cxt, const WasmEdge_String FuncName,
3473
                   const WasmEdge_Value *Params, const uint32_t ParamLen,
3474
0
                   WasmEdge_Value *Returns, const uint32_t ReturnLen) noexcept {
3475
0
  try {
3476
0
    auto ParamPair = genParamPair(Params, ParamLen);
3477
0
    return wrap(
3478
0
        [&]() {
3479
0
          return Cxt->VM.execute(genStrView(FuncName), ParamPair.first,
3480
0
                                 ParamPair.second);
3481
0
        },
3482
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3483
0
        Cxt);
3484
0
  } catch (...) {
3485
0
    return handleCAPIError();
3486
0
  }
3487
0
}
3488
3489
WASMEDGE_CAPI_EXPORT WasmEdge_Result WasmEdge_VMExecuteRegistered(
3490
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3491
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3492
    const uint32_t ParamLen, WasmEdge_Value *Returns,
3493
0
    const uint32_t ReturnLen) noexcept {
3494
0
  try {
3495
0
    auto ParamPair = genParamPair(Params, ParamLen);
3496
0
    return wrap(
3497
0
        [&]() {
3498
0
          return Cxt->VM.execute(genStrView(ModuleName), genStrView(FuncName),
3499
0
                                 ParamPair.first, ParamPair.second);
3500
0
        },
3501
0
        [&](auto &&Res) { fillWasmEdge_ValueArr(*Res, Returns, ReturnLen); },
3502
0
        Cxt);
3503
0
  } catch (...) {
3504
0
    return handleCAPIError();
3505
0
  }
3506
0
}
3507
3508
WASMEDGE_CAPI_EXPORT WasmEdge_Async *
3509
WasmEdge_VMAsyncExecute(WasmEdge_VMContext *Cxt, const WasmEdge_String FuncName,
3510
                        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(Cxt->VM.asyncExecute(
3516
0
          genStrView(FuncName), ParamPair.first, ParamPair.second));
3517
0
    }
3518
0
  } catch (...) {
3519
0
    handleCAPIError();
3520
0
  }
3521
0
  return nullptr;
3522
0
}
3523
3524
WASMEDGE_CAPI_EXPORT WasmEdge_Async *WasmEdge_VMAsyncExecuteRegistered(
3525
    WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName,
3526
    const WasmEdge_String FuncName, const WasmEdge_Value *Params,
3527
0
    const uint32_t ParamLen) noexcept {
3528
0
  try {
3529
0
    auto ParamPair = genParamPair(Params, ParamLen);
3530
0
    if (Cxt) {
3531
0
      return new WasmEdge_Async(
3532
0
          Cxt->VM.asyncExecute(genStrView(ModuleName), genStrView(FuncName),
3533
0
                               ParamPair.first, ParamPair.second));
3534
0
    }
3535
0
  } catch (...) {
3536
0
    handleCAPIError();
3537
0
  }
3538
0
  return nullptr;
3539
0
}
3540
3541
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
3542
WasmEdge_VMGetFunctionType(const WasmEdge_VMContext *Cxt,
3543
0
                           const WasmEdge_String FuncName) noexcept {
3544
0
  try {
3545
0
    if (Cxt) {
3546
0
      const auto FuncList = Cxt->VM.getFunctionList();
3547
0
      for (const auto &It : FuncList) {
3548
0
        if (It.first == genStrView(FuncName)) {
3549
0
          return toFuncTypeCxt(&It.second);
3550
0
        }
3551
0
      }
3552
0
    }
3553
0
  } catch (...) {
3554
0
    handleCAPIError();
3555
0
  }
3556
0
  return nullptr;
3557
0
}
3558
3559
WASMEDGE_CAPI_EXPORT const WasmEdge_FunctionTypeContext *
3560
WasmEdge_VMGetFunctionTypeRegistered(const WasmEdge_VMContext *Cxt,
3561
                                     const WasmEdge_String ModuleName,
3562
0
                                     const WasmEdge_String FuncName) noexcept {
3563
0
  if (Cxt) {
3564
0
    const auto *ModInst =
3565
0
        Cxt->VM.getStoreManager().findModule(genStrView(ModuleName));
3566
0
    if (ModInst != nullptr) {
3567
0
      const auto *FuncInst = ModInst->findFuncExports(genStrView(FuncName));
3568
0
      if (FuncInst != nullptr) {
3569
0
        return toFuncTypeCxt(&FuncInst->getFuncType());
3570
0
      }
3571
0
    }
3572
0
  }
3573
0
  return nullptr;
3574
0
}
3575
3576
0
WASMEDGE_CAPI_EXPORT void WasmEdge_VMCleanup(WasmEdge_VMContext *Cxt) noexcept {
3577
0
  if (Cxt) {
3578
0
    Cxt->VM.cleanup();
3579
0
  }
3580
0
}
3581
3582
WASMEDGE_CAPI_EXPORT void
3583
WasmEdge_VMDeleteRegisteredModule(const WasmEdge_VMContext *Cxt,
3584
0
                                  const WasmEdge_String ModuleName) noexcept {
3585
0
  if (!Cxt || !ModuleName.Buf) {
3586
0
    return; // Invalid input
3587
0
  }
3588
3589
0
  auto &VM = const_cast<WasmEdge_VMContext *>(Cxt)->VM;
3590
0
  auto &StoreMgr = VM.getStoreManager();
3591
0
  auto NameStr = genStrView(ModuleName);
3592
3593
  // Unregister from VM
3594
0
  VM.unregisterModule(NameStr);
3595
3596
0
  if (StoreMgr.findModule(NameStr)) {
3597
    // Unregister from StoreManager
3598
0
    StoreMgr.unregisterModule(NameStr);
3599
0
  }
3600
0
}
3601
3602
WASMEDGE_CAPI_EXPORT void WasmEdge_VMForceDeleteRegisteredModule(
3603
0
    const WasmEdge_VMContext *Cxt, const WasmEdge_String ModuleName) noexcept {
3604
0
  WasmEdge_VMDeleteRegisteredModule(Cxt, ModuleName);
3605
0
}
3606
3607
WASMEDGE_CAPI_EXPORT uint32_t
3608
0
WasmEdge_VMGetFunctionListLength(const WasmEdge_VMContext *Cxt) noexcept {
3609
0
  try {
3610
0
    if (Cxt) {
3611
0
      return static_cast<uint32_t>(Cxt->VM.getFunctionList().size());
3612
0
    }
3613
0
  } catch (...) {
3614
0
    handleCAPIError();
3615
0
  }
3616
0
  return 0;
3617
0
}
3618
3619
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VMGetFunctionList(
3620
    const WasmEdge_VMContext *Cxt, WasmEdge_String *Names,
3621
    const WasmEdge_FunctionTypeContext **FuncTypes,
3622
0
    const uint32_t Len) noexcept {
3623
0
  if (Cxt) {
3624
    // Do not use VM::getFunctionList() here because it would allocate the
3625
    // returned function name strings.
3626
0
    const auto *ModInst = Cxt->VM.getActiveModule();
3627
0
    if (ModInst != nullptr) {
3628
0
      return ModInst->getFuncExports([&](const auto &FuncExp) {
3629
0
        uint32_t I = 0;
3630
0
        for (auto It = FuncExp.cbegin(); It != FuncExp.cend() && I < Len;
3631
0
             It++, I++) {
3632
0
          const auto *FuncInst = It->second;
3633
0
          const auto &FuncType = FuncInst->getFuncType();
3634
0
          if (Names) {
3635
0
            Names[I] = WasmEdge_String{
3636
0
                /* Length */ static_cast<uint32_t>(It->first.length()),
3637
0
                /* Buf */ It->first.data()};
3638
0
          }
3639
0
          if (FuncTypes) {
3640
0
            FuncTypes[I] = toFuncTypeCxt(&FuncType);
3641
0
          }
3642
0
        }
3643
0
        return static_cast<uint32_t>(FuncExp.size());
3644
0
      });
3645
0
    }
3646
0
  }
3647
0
  return 0;
3648
0
}
3649
3650
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
3651
WasmEdge_VMGetImportModuleContext(
3652
    const WasmEdge_VMContext *Cxt,
3653
0
    const enum WasmEdge_HostRegistration Reg) noexcept {
3654
0
  if (Cxt) {
3655
0
    return toModCxt(
3656
0
        Cxt->VM.getImportModule(static_cast<WasmEdge::HostRegistration>(Reg)));
3657
0
  }
3658
0
  return nullptr;
3659
0
}
3660
3661
WASMEDGE_CAPI_EXPORT uint32_t
3662
0
WasmEdge_VMListRegisteredModuleLength(const WasmEdge_VMContext *Cxt) noexcept {
3663
0
  if (Cxt) {
3664
0
    return Cxt->VM.getStoreManager().getModuleListSize();
3665
0
  }
3666
0
  return 0;
3667
0
}
3668
3669
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_VMListRegisteredModule(
3670
    const WasmEdge_VMContext *Cxt, WasmEdge_String *Names,
3671
0
    const uint32_t Len) noexcept {
3672
0
  if (Cxt) {
3673
0
    return Cxt->VM.getStoreManager().getModuleList(
3674
0
        [&](auto &Map) { return fillMap(Map, Names, Len); });
3675
0
  }
3676
0
  return 0;
3677
0
}
3678
3679
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
3680
WasmEdge_VMGetRegisteredModule(const WasmEdge_VMContext *Cxt,
3681
0
                               const WasmEdge_String ModuleName) noexcept {
3682
0
  if (Cxt) {
3683
0
    return toModCxt(
3684
0
        Cxt->VM.getStoreManager().findModule(genStrView(ModuleName)));
3685
0
  }
3686
0
  return nullptr;
3687
0
}
3688
3689
WASMEDGE_CAPI_EXPORT const WasmEdge_ModuleInstanceContext *
3690
0
WasmEdge_VMGetActiveModule(const WasmEdge_VMContext *Cxt) noexcept {
3691
0
  if (Cxt) {
3692
0
    return toModCxt(Cxt->VM.getActiveModule());
3693
0
  }
3694
0
  return nullptr;
3695
0
}
3696
3697
WASMEDGE_CAPI_EXPORT WasmEdge_StoreContext *
3698
0
WasmEdge_VMGetStoreContext(WasmEdge_VMContext *Cxt) noexcept {
3699
0
  if (Cxt) {
3700
0
    return toStoreCxt(&Cxt->VM.getStoreManager());
3701
0
  }
3702
0
  return nullptr;
3703
0
}
3704
3705
WASMEDGE_CAPI_EXPORT WasmEdge_LoaderContext *
3706
0
WasmEdge_VMGetLoaderContext(WasmEdge_VMContext *Cxt) noexcept {
3707
0
  if (Cxt) {
3708
0
    return toLoaderCxt(&Cxt->VM.getLoader());
3709
0
  }
3710
0
  return nullptr;
3711
0
}
3712
3713
WASMEDGE_CAPI_EXPORT WasmEdge_ValidatorContext *
3714
0
WasmEdge_VMGetValidatorContext(WasmEdge_VMContext *Cxt) noexcept {
3715
0
  if (Cxt) {
3716
0
    return toValidatorCxt(&Cxt->VM.getValidator());
3717
0
  }
3718
0
  return nullptr;
3719
0
}
3720
3721
WASMEDGE_CAPI_EXPORT WasmEdge_ExecutorContext *
3722
0
WasmEdge_VMGetExecutorContext(WasmEdge_VMContext *Cxt) noexcept {
3723
0
  if (Cxt) {
3724
0
    return toExecutorCxt(&Cxt->VM.getExecutor());
3725
0
  }
3726
0
  return nullptr;
3727
0
}
3728
3729
WASMEDGE_CAPI_EXPORT WasmEdge_StatisticsContext *
3730
0
WasmEdge_VMGetStatisticsContext(WasmEdge_VMContext *Cxt) noexcept {
3731
0
  if (Cxt) {
3732
0
    return toStatCxt(&Cxt->VM.getStatistics());
3733
0
  }
3734
0
  return nullptr;
3735
0
}
3736
3737
0
WASMEDGE_CAPI_EXPORT void WasmEdge_VMDelete(WasmEdge_VMContext *Cxt) noexcept {
3738
0
  delete Cxt;
3739
0
}
3740
3741
// <<<<<<<< WasmEdge VM functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3742
3743
// >>>>>>>> WasmEdge Driver functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3744
3745
#if WASMEDGE_OS_WINDOWS
3746
WASMEDGE_CAPI_EXPORT const char **
3747
WasmEdge_Driver_ArgvCreate(int Argc, const wchar_t *Argv[]) noexcept {
3748
  try {
3749
    const Span<const wchar_t *> Args(Argv, static_cast<size_t>(Argc));
3750
    const size_t PointerArraySize = static_cast<size_t>(Argc) * sizeof(char *);
3751
    size_t StringBufferSize = 0;
3752
    for (auto Arg : Args) {
3753
      const auto Res = std::max<size_t>(
3754
          static_cast<size_t>(winapi::WideCharToMultiByte(
3755
              winapi::CP_UTF8_, 0, Arg, -1, nullptr, 0, nullptr, nullptr)),
3756
          1u);
3757
      StringBufferSize += Res;
3758
    }
3759
    auto Buffer = std::make_unique<char[]>(PointerArraySize + StringBufferSize);
3760
    Span<char *> PointerArray(reinterpret_cast<char **>(Buffer.get()),
3761
                              static_cast<size_t>(Argc));
3762
    Span<char> StringBuffer(Buffer.get() + PointerArraySize, StringBufferSize);
3763
    for (auto Arg : Args) {
3764
      PointerArray[0] = StringBuffer.data();
3765
      PointerArray = PointerArray.subspan(1);
3766
      const auto Res = std::max<size_t>(
3767
          static_cast<size_t>(winapi::WideCharToMultiByte(
3768
              winapi::CP_UTF8_, 0, Arg, -1, StringBuffer.data(),
3769
              static_cast<int>(StringBuffer.size()), nullptr, nullptr)),
3770
          1);
3771
      StringBuffer = StringBuffer.subspan(Res);
3772
    }
3773
3774
    return reinterpret_cast<const char **>(Buffer.release());
3775
  } catch (...) {
3776
    handleCAPIError();
3777
    return nullptr;
3778
  }
3779
}
3780
3781
WASMEDGE_CAPI_EXPORT void
3782
WasmEdge_Driver_ArgvDelete(const char *Argv[]) noexcept {
3783
  std::unique_ptr<char[]> Buffer(reinterpret_cast<char *>(Argv));
3784
  Buffer.reset();
3785
}
3786
3787
WASMEDGE_CAPI_EXPORT void
3788
WasmEdge_Driver_SetConsoleOutputCPtoUTF8(void) noexcept {
3789
#if WINAPI_PARTITION_DESKTOP
3790
  winapi::SetConsoleOutputCP(winapi::CP_UTF8_);
3791
#endif
3792
}
3793
#endif
3794
3795
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Compiler(int Argc,
3796
0
                                                  const char *Argv[]) noexcept {
3797
0
  try {
3798
0
    return WasmEdge::Driver::UniTool(Argc, Argv,
3799
0
                                     WasmEdge::Driver::ToolType::Compiler);
3800
0
  } catch (...) {
3801
0
    handleCAPIError();
3802
0
    return EXIT_FAILURE;
3803
0
  }
3804
0
}
3805
3806
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_Tool(int Argc,
3807
0
                                              const char *Argv[]) noexcept {
3808
0
  try {
3809
0
    return WasmEdge::Driver::UniTool(Argc, Argv,
3810
0
                                     WasmEdge::Driver::ToolType::Tool);
3811
0
  } catch (...) {
3812
0
    handleCAPIError();
3813
0
    return EXIT_FAILURE;
3814
0
  }
3815
0
}
3816
3817
#ifdef WASMEDGE_BUILD_WASI_NN_RPC
3818
WASMEDGE_CAPI_EXPORT int
3819
WasmEdge_Driver_WasiNNRPCServer(int Argc, const char *Argv[]) noexcept {
3820
  try {
3821
    return WasmEdge::Driver::WasiNNRPCServer(Argc, Argv);
3822
  } catch (...) {
3823
    handleCAPIError();
3824
    return EXIT_FAILURE;
3825
  }
3826
}
3827
#endif
3828
3829
WASMEDGE_CAPI_EXPORT int WasmEdge_Driver_UniTool(int Argc,
3830
0
                                                 const char *Argv[]) noexcept {
3831
0
  try {
3832
0
    return WasmEdge::Driver::UniTool(Argc, Argv,
3833
0
                                     WasmEdge::Driver::ToolType::All);
3834
0
  } catch (...) {
3835
0
    handleCAPIError();
3836
0
    return EXIT_FAILURE;
3837
0
  }
3838
0
}
3839
3840
#ifdef WASMEDGE_BUILD_FUZZING
3841
WASMEDGE_CAPI_EXPORT extern "C" int
3842
9.22k
WasmEdge_Driver_FuzzTool(const uint8_t *Data, size_t Size) noexcept {
3843
9.22k
  try {
3844
9.22k
    return WasmEdge::Driver::FuzzTool(Data, Size);
3845
9.22k
  } catch (...) {
3846
0
    handleCAPIError();
3847
0
    return EXIT_FAILURE;
3848
0
  }
3849
9.22k
}
3850
3851
WASMEDGE_CAPI_EXPORT extern "C" int
3852
1.61k
WasmEdge_Driver_FuzzPO(const uint8_t *Data, size_t Size) noexcept {
3853
1.61k
  try {
3854
1.61k
    return WasmEdge::Driver::FuzzPO(Data, Size);
3855
1.61k
  } catch (...) {
3856
0
    handleCAPIError();
3857
0
    return EXIT_FAILURE;
3858
0
  }
3859
1.61k
}
3860
#endif
3861
3862
// <<<<<<<< WasmEdge Driver functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3863
3864
// >>>>>>>> WasmEdge Plugin functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3865
3866
0
WASMEDGE_CAPI_EXPORT void WasmEdge_PluginLoadWithDefaultPaths(void) noexcept {
3867
0
  WasmEdge::Plugin::Plugin::loadFromDefaultPaths();
3868
0
}
3869
3870
WASMEDGE_CAPI_EXPORT void
3871
0
WasmEdge_PluginLoadFromPath(const char *Path) noexcept {
3872
  // Not genPath(): this is noexcept and absolute() can throw.
3873
0
  WasmEdge::Plugin::Plugin::load(std::filesystem::path(Path ? Path : ""));
3874
0
}
3875
3876
0
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_PluginListPluginsLength(void) noexcept {
3877
0
  return static_cast<uint32_t>(WasmEdge::Plugin::Plugin::plugins().size());
3878
0
}
3879
3880
WASMEDGE_CAPI_EXPORT uint32_t WasmEdge_PluginListPlugins(
3881
0
    WasmEdge_String *Names, const uint32_t Len) noexcept {
3882
0
  const auto &PList = WasmEdge::Plugin::Plugin::plugins();
3883
0
  if (Names) {
3884
0
    for (uint32_t I = 0; I < Len && I < PList.size(); I++) {
3885
0
      Names[I] = WasmEdge_String{
3886
0
          /* Length */ static_cast<uint32_t>(std::strlen(PList[I].name())),
3887
0
          /* Buf */ PList[I].name()};
3888
0
    }
3889
0
  }
3890
0
  return static_cast<uint32_t>(PList.size());
3891
0
}
3892
3893
WASMEDGE_CAPI_EXPORT const WasmEdge_PluginContext *
3894
0
WasmEdge_PluginFind(const WasmEdge_String Name) noexcept {
3895
0
  return toPluginCxt(WasmEdge::Plugin::Plugin::find(genStrView(Name)));
3896
0
}
3897
3898
WASMEDGE_CAPI_EXPORT WasmEdge_String
3899
0
WasmEdge_PluginGetPluginName(const WasmEdge_PluginContext *Cxt) noexcept {
3900
0
  if (Cxt) {
3901
0
    const char *Name = fromPluginCxt(Cxt)->name();
3902
0
    return WasmEdge_String{
3903
0
        /* Length */ static_cast<uint32_t>(std::strlen(Name)),
3904
0
        /* Buf */ Name};
3905
0
  }
3906
0
  return WasmEdge_String{/* Length */ 0, /* Buf */ nullptr};
3907
0
}
3908
3909
WASMEDGE_CAPI_EXPORT uint32_t
3910
0
WasmEdge_PluginListModuleLength(const WasmEdge_PluginContext *Cxt) noexcept {
3911
0
  if (Cxt) {
3912
0
    return static_cast<uint32_t>(fromPluginCxt(Cxt)->modules().size());
3913
0
  }
3914
0
  return 0;
3915
0
}
3916
3917
WASMEDGE_CAPI_EXPORT uint32_t
3918
WasmEdge_PluginListModule(const WasmEdge_PluginContext *Cxt,
3919
0
                          WasmEdge_String *Names, const uint32_t Len) noexcept {
3920
0
  if (Cxt) {
3921
0
    auto MList = fromPluginCxt(Cxt)->modules();
3922
0
    if (Names) {
3923
0
      for (uint32_t I = 0; I < Len && I < MList.size(); I++) {
3924
0
        Names[I] = WasmEdge_String{
3925
0
            /* Length */ static_cast<uint32_t>(std::strlen(MList[I].name())),
3926
0
            /* Buf */ MList[I].name()};
3927
0
      }
3928
0
    }
3929
0
    return static_cast<uint32_t>(MList.size());
3930
0
  }
3931
0
  return 0;
3932
0
}
3933
3934
WASMEDGE_CAPI_EXPORT WasmEdge_ModuleInstanceContext *
3935
WasmEdge_PluginCreateModule(const WasmEdge_PluginContext *Cxt,
3936
0
                            const WasmEdge_String ModuleName) noexcept {
3937
0
  try {
3938
0
    if (Cxt) {
3939
0
      if (const auto *PMod =
3940
0
              fromPluginCxt(Cxt)->findModule(genStrView(ModuleName));
3941
0
          PMod) {
3942
0
        return toModCxt(PMod->create().release());
3943
0
      }
3944
0
    }
3945
0
  } catch (...) {
3946
0
    handleCAPIError();
3947
0
  }
3948
0
  return nullptr;
3949
0
}
3950
3951
WASMEDGE_CAPI_EXPORT void
3952
WasmEdge_PluginInitWASINN(const char *const *NNPreloads,
3953
0
                          const uint32_t PreloadsLen) noexcept {
3954
0
  try {
3955
0
    using namespace std::literals::string_view_literals;
3956
0
    if (const auto *Plugin = WasmEdge::Plugin::Plugin::find("wasi_nn"sv)) {
3957
0
      PO::ArgumentParser Parser;
3958
0
      Plugin->registerOptions(Parser);
3959
0
      Parser.set_raw_value<std::vector<std::string>>(
3960
0
          "nn-preload"sv,
3961
0
          std::vector<std::string>(NNPreloads, NNPreloads + PreloadsLen));
3962
0
    }
3963
0
  } catch (...) {
3964
0
    handleCAPIError();
3965
0
  }
3966
0
}
3967
3968
// <<<<<<<< WasmEdge Plugin functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3969
3970
// >>>>>>>> WasmEdge Experimental functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3971
3972
WASMEDGE_CAPI_EXPORT void WasmEdge_ExecutorExperimentalRegisterPreHostFunction(
3973
0
    WasmEdge_ExecutorContext *Cxt, void *Data, void (*Func)(void *)) noexcept {
3974
0
  if (!Cxt) {
3975
0
    return;
3976
0
  }
3977
0
  fromExecutorCxt(Cxt)->registerPreHostFunction(Data, Func);
3978
0
}
3979
3980
WASMEDGE_CAPI_EXPORT void WasmEdge_ExecutorExperimentalRegisterPostHostFunction(
3981
0
    WasmEdge_ExecutorContext *Cxt, void *Data, void (*Func)(void *)) noexcept {
3982
0
  if (!Cxt) {
3983
0
    return;
3984
0
  }
3985
0
  fromExecutorCxt(Cxt)->registerPostHostFunction(Data, Func);
3986
0
}
3987
3988
// <<<<<<<< WasmEdge Experimental Functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3989
#ifdef __cplusplus
3990
} // extern "C"
3991
#endif