Coverage Report

Created: 2025-11-16 06:42

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