Coverage Report

Created: 2025-07-01 06:18

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