Coverage Report

Created: 2025-09-27 06:27

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