wasmer-c-api 1.0.0

Wasmer C API library
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// The Wasmer C/C++ header file compatible with the `wasm-c-api` standard API.
// This file is generated by lib/c-api/build.rs.

#if !defined(WASMER_WASM_H_PRELUDE)

#define WASMER_WASM_H_PRELUDE

// Define the `ARCH_X86_X64` constant.
#if defined(MSVC) && defined(_M_AMD64)
#  define ARCH_X86_64
#elif (defined(GCC) || defined(__GNUC__) || defined(__clang__)) && defined(__x86_64__)
#  define ARCH_X86_64
#endif

// Compatibility with non-Clang compilers.
#if !defined(__has_attribute)
#  define __has_attribute(x) 0
#endif

// Compatibility with non-Clang compilers.
#if !defined(__has_declspec_attribute)
#  define __has_declspec_attribute(x) 0
#endif

// Define the `DEPRECATED` macro.
#if defined(GCC) || defined(__GNUC__) || __has_attribute(deprecated)
#  define DEPRECATED(message) __attribute__((deprecated(message)))
#elif defined(MSVC) || __has_declspec_attribute(deprecated)
#  define DEPRECATED(message) __declspec(deprecated(message))
#endif

// The `jit` feature has been enabled for this build.
#define WASMER_JIT_ENABLED

// The `compiler` feature has been enabled for this build.
#define WASMER_COMPILER_ENABLED

// The `wasi` feature has been enabled for this build.
#define WASMER_WASI_ENABLED

// This file corresponds to the following Wasmer version.
#define WASMER_VERSION "1.0.0"
#define WASMER_VERSION_MAJOR 1
#define WASMER_VERSION_MINOR 0
#define WASMER_VERSION_PATCH 0
#define WASMER_VERSION_PRE ""

#endif // WASMER_WASM_H_PRELUDE


//
// OK, here we go. The code below is automatically generated.
//


#ifndef WASMER_WASM_H
#define WASMER_WASM_H

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "wasm.h"

#if defined(WASMER_COMPILER_ENABLED)
/**
 * Kind of compilers that can be used by the engines.
 *
 * This is a Wasmer-specific type with Wasmer-specific functions for
 * manipulating it.
 */
typedef enum {
  /**
   * Variant to represent the Cranelift compiler. See the
   * [`wasmer_compiler_cranelift`] Rust crate.
   */
  CRANELIFT = 0,
  /**
   * Variant to represent the LLVM compiler. See the
   * [`wasmer_compiler_llvm`] Rust crate.
   */
  LLVM = 1,
  /**
   * Variant to represent the Singlepass compiler. See the
   * [`wasmer_compiler_singlepass`] Rust crate.
   */
  SINGLEPASS = 2,
} wasmer_compiler_t;
#endif

/**
 * Kind of engines that can be used by the store.
 *
 * This is a Wasmer-specific type with Wasmer-specific functions for
 * manipulating it.
 */
typedef enum {
  /**
   * Variant to represent the JIT engine. See the
   * [`wasmer_engine_jit`] Rust crate.
   */
  JIT = 0,
  /**
   * Variant to represent the Native engine. See the
   * [`wasmer_engine_native`] Rust crate.
   */
  NATIVE = 1,
  /**
   * Variant to represent the Object File engine. See the
   * [`wasmer_engine_object_file`] Rust crate.
   */
  OBJECT_FILE = 2,
} wasmer_engine_t;

#if defined(WASMER_WASI_ENABLED)
typedef struct wasi_config_t wasi_config_t;
#endif

#if defined(WASMER_WASI_ENABLED)
typedef struct wasi_env_t wasi_env_t;
#endif

#if defined(WASMER_WASI_ENABLED)
typedef struct wasi_version_t wasi_version_t;
#endif

#if defined(WASMER_WASI_ENABLED)
void wasi_config_arg(wasi_config_t *config, const char *arg);
#endif

#if defined(WASMER_WASI_ENABLED)
void wasi_config_env(wasi_config_t *config, const char *key, const char *value);
#endif

#if defined(WASMER_WASI_ENABLED)
void wasi_config_inherit_stderr(wasi_config_t *config);
#endif

#if defined(WASMER_WASI_ENABLED)
void wasi_config_inherit_stdin(wasi_config_t *config);
#endif

#if defined(WASMER_WASI_ENABLED)
void wasi_config_inherit_stdout(wasi_config_t *config);
#endif

#if defined(WASMER_WASI_ENABLED)
bool wasi_config_mapdir(wasi_config_t *config, const char *alias, const char *dir);
#endif

#if defined(WASMER_WASI_ENABLED)
wasi_config_t *wasi_config_new(const char *program_name);
#endif

#if defined(WASMER_WASI_ENABLED)
bool wasi_config_preopen_dir(wasi_config_t *config, const char *dir);
#endif

#if defined(WASMER_WASI_ENABLED)
void wasi_env_delete(wasi_env_t *_state);
#endif

#if defined(WASMER_WASI_ENABLED)
/**
 * Takes ownership over the `wasi_config_t`.
 */
wasi_env_t *wasi_env_new(wasi_config_t *config);
#endif

#if defined(WASMER_WASI_ENABLED)
intptr_t wasi_env_read_stderr(wasi_env_t *env, char *buffer, uintptr_t buffer_len);
#endif

#if defined(WASMER_WASI_ENABLED)
intptr_t wasi_env_read_stdout(wasi_env_t *env, char *buffer, uintptr_t buffer_len);
#endif

#if defined(WASMER_WASI_ENABLED)
/**
 * This function is deprecated. You may safely remove all calls to it and everything
 * will continue to work.
 */
bool wasi_env_set_instance(wasi_env_t *env, const wasm_instance_t *instance);
#endif

#if defined(WASMER_WASI_ENABLED)
/**
 * This function is deprecated. You may safely remove all calls to it and everything
 * will continue to work.
 */
void wasi_env_set_memory(wasi_env_t *env, const wasm_memory_t *memory);
#endif

#if defined(WASMER_WASI_ENABLED)
/**
 * Takes ownership of `wasi_env_t`.
 */
bool wasi_get_imports(const wasm_store_t *store,
                      const wasm_module_t *module,
                      const wasi_env_t *wasi_env,
                      wasm_extern_vec_t *imports);
#endif

#if defined(WASMER_WASI_ENABLED)
wasm_func_t *wasi_get_start_function(wasm_instance_t *instance);
#endif

#if defined(WASMER_WASI_ENABLED)
wasi_version_t wasi_get_wasi_version(const wasm_module_t *module);
#endif

#if defined(WASMER_COMPILER_ENABLED)
/**
 * Updates the configuration to specify a particular compiler to use.
 *
 * This is a Wasmer-specific function.
 *
 * # Example
 *
 * ```rust,no_run
 * # use inline_c::assert_c;
 * # fn main() {
 * #    (assert_c! {
 * # #include "tests/wasmer_wasm.h"
 * #
 * int main() {
 *     // Create the configuration.
 *     wasm_config_t* config = wasm_config_new();
 *
 *     // Use the Cranelift compiler.
 *     wasm_config_set_compiler(config, CRANELIFT);
 *
 *     // Create the engine.
 *     wasm_engine_t* engine = wasm_engine_new_with_config(config);
 *
 *     // Check we have an engine!
 *     assert(engine);
 *
 *     // Free everything.
 *     wasm_engine_delete(engine);
 *
 *     return 0;
 * }
 * #    })
 * #    .success();
 * # }
 * ```
 */
void wasm_config_set_compiler(wasm_config_t *config, wasmer_compiler_t compiler);
#endif

/**
 * Updates the configuration to specify a particular engine to use.
 *
 * This is a Wasmer-specific function.
 *
 * # Example
 *
 * ```rust,no_run
 * # use inline_c::assert_c;
 * # fn main() {
 * #    (assert_c! {
 * # #include "tests/wasmer_wasm.h"
 * #
 * int main() {
 *     // Create the configuration.
 *     wasm_config_t* config = wasm_config_new();
 *
 *     // Use the JIT engine.
 *     wasm_config_set_engine(config, JIT);
 *
 *     // Create the engine.
 *     wasm_engine_t* engine = wasm_engine_new_with_config(config);
 *
 *     // Check we have an engine!
 *     assert(engine);
 *
 *     // Free everything.
 *     wasm_engine_delete(engine);
 *
 *     return 0;
 * }
 * #    })
 * #    .success();
 * # }
 * ```
 */
void wasm_config_set_engine(wasm_config_t *config, wasmer_engine_t engine);

/**
 * Non-standard Wasmer-specific API to get the module's name,
 * otherwise `out->size` is set to `0` and `out->data` to `NULL`.
 *
 * # Example
 *
 * ```rust
 * # use inline_c::assert_c;
 * # fn main() {
 * #    (assert_c! {
 * # #include "tests/wasmer_wasm.h"
 * #
 * int main() {
 *     // Create the engine and the store.
 *     wasm_engine_t* engine = wasm_engine_new();
 *     wasm_store_t* store = wasm_store_new(engine);
 *
 *     // Create a WebAssembly module from a WAT definition.
 *     wasm_byte_vec_t wat;
 *     wasmer_byte_vec_new_from_string(&wat, "(module $moduleName)");
 *     //                                             ^~~~~~~~~~~ that's the name!
 *     wasm_byte_vec_t wasm;
 *     wat2wasm(&wat, &wasm);
 *
 *     // Create the module.
 *     wasm_module_t* module = wasm_module_new(store, &wasm);
 *
 *     // Read the module's name.
 *     wasm_name_t name;
 *     wasm_module_name(module, &name);
 *
 *     // It works!
 *     wasmer_assert_name(&name, "moduleName");
 *
 *     // Free everything.
 *     wasm_byte_vec_delete(&name);
 *     wasm_module_delete(module);
 *     wasm_byte_vec_delete(&wasm);
 *     wasm_byte_vec_delete(&wat);
 *     wasm_store_delete(store);
 *     wasm_engine_delete(engine);
 *
 *     return 0;
 * }
 * #    })
 * #    .success();
 * # }
 * ```
 */
void wasm_module_name(const wasm_module_t *module, wasm_name_t *out);

/**
 * Non-standard Wasmer-specific API to set the module's name. The
 * function returns `true` if the name has been updated, `false`
 * otherwise.
 *
 * # Example
 *
 * ```rust
 * # use inline_c::assert_c;
 * # fn main() {
 * #    (assert_c! {
 * # #include "tests/wasmer_wasm.h"
 * #
 * int main() {
 *     // Create the engine and the store.
 *     wasm_engine_t* engine = wasm_engine_new();
 *     wasm_store_t* store = wasm_store_new(engine);
 *
 *     // Create a WebAssembly module from a WAT definition.
 *     wasm_byte_vec_t wat;
 *     wasmer_byte_vec_new_from_string(&wat, "(module)");
 *     wasm_byte_vec_t wasm;
 *     wat2wasm(&wat, &wasm);
 *
 *     // Create the module.
 *     wasm_module_t* module = wasm_module_new(store, &wasm);
 *
 *     // Read the module's name. There is none for the moment.
 *     {
 *         wasm_name_t name;
 *         wasm_module_name(module, &name);
 *
 *         assert(name.size == 0);
 *     }
 *
 *     // So, let's set a new name.
 *     {
 *         wasm_name_t name;
 *         wasmer_byte_vec_new_from_string(&name, "hello");
 *         wasm_module_set_name(module, &name);
 *     }
 *
 *     // And now, let's see the new name.
 *     {
 *         wasm_name_t name;
 *         wasm_module_name(module, &name);
 *
 *         // It works!
 *         wasmer_assert_name(&name, "hello");
 *
 *         wasm_byte_vec_delete(&name);
 *     }
 *
 *     // Free everything.
 *     wasm_module_delete(module);
 *     wasm_byte_vec_delete(&wasm);
 *     wasm_byte_vec_delete(&wat);
 *     wasm_store_delete(store);
 *     wasm_engine_delete(engine);
 *
 *     return 0;
 * }
 * #    })
 * #    .success();
 * # }
 * ```
 */
bool wasm_module_set_name(wasm_module_t *module, const wasm_name_t *name);

/**
 * Gets the length in bytes of the last error if any, zero otherwise.
 *
 * This can be used to dynamically allocate a buffer with the correct number of
 * bytes needed to store a message.
 *
 * # Example
 *
 * See this module's documentation to get a complete example.
 */
int wasmer_last_error_length(void);

/**
 * Gets the last error message if any into the provided buffer
 * `buffer` up to the given `length`.
 *
 * The `length` parameter must be large enough to store the last
 * error message. Ideally, the value should come from
 * [`wasmer_last_error_length`].
 *
 * The function returns the length of the string in bytes, `-1` if an
 * error occurs. Potential errors are:
 *
 *  * The `buffer` is a null pointer,
 *  * The `buffer` is too small to hold the error message.
 *
 * Note: The error message always has a trailing NUL character.
 *
 * Important note: If the provided `buffer` is non-null, once this
 * function has been called, regardless whether it fails or succeeds,
 * the error is cleared.
 *
 * # Example
 *
 * See this module's documentation to get a complete example.
 */
int wasmer_last_error_message(char *buffer, int length);

/**
 * Get the version of the Wasmer C API.
 *
 * The `.h` files already define variables like `WASMER_VERSION*`,
 * but if this file is unreachable, one can use this function to
 * retrieve the full semver version of the Wasmer C API.
 *
 * The returned string is statically allocated. It must _not_ be
 * freed!
 *
 * # Example
 *
 * See the module's documentation.
 */
const char *wasmer_version(void);

/**
 * Get the major version of the Wasmer C API.
 *
 * See [`wasmer_version`] to learn more.
 *
 * # Example
 *
 * ```rust
 * # use inline_c::assert_c;
 * # fn main() {
 * #    (assert_c! {
 * # #include "tests/wasmer_wasm.h"
 * #
 * int main() {
 *     // Get and print the version components.
 *     uint8_t version_major = wasmer_version_major();
 *     uint8_t version_minor = wasmer_version_minor();
 *     uint8_t version_patch = wasmer_version_patch();
 *
 *     printf("%d.%d.%d", version_major, version_minor, version_patch);
 *
 *     return 0;
 * }
 * #    })
 * #    .success()
 * #    .stdout(
 * #         format!(
 * #             "{}.{}.{}",
 * #             env!("CARGO_PKG_VERSION_MAJOR"),
 * #             env!("CARGO_PKG_VERSION_MINOR"),
 * #             env!("CARGO_PKG_VERSION_PATCH")
 * #         )
 * #     );
 * # }
 * ```
 */
uint8_t wasmer_version_major(void);

/**
 * Get the minor version of the Wasmer C API.
 *
 * See [`wasmer_version_major`] to learn more and get an example.
 */
uint8_t wasmer_version_minor(void);

/**
 * Get the patch version of the Wasmer C API.
 *
 * See [`wasmer_version_major`] to learn more and get an example.
 */
uint8_t wasmer_version_patch(void);

/**
 * Get the minor version of the Wasmer C API.
 *
 * See [`wasmer_version_major`] to learn more.
 *
 * The returned string is statically allocated. It must _not_ be
 * freed!
 *
 * # Example
 *
 * ```rust
 * # use inline_c::assert_c;
 * # fn main() {
 * #    (assert_c! {
 * # #include "tests/wasmer_wasm.h"
 * #
 * int main() {
 *     // Get and print the pre version.
 *     const char* version_pre = wasmer_version_pre();
 *     printf("%s", version_pre);
 *
 *     // No need to free the string. It's statically allocated on
 *     // the Rust side.
 *
 *     return 0;
 * }
 * #    })
 * #    .success()
 * #    .stdout(env!("CARGO_PKG_VERSION_PRE"));
 * # }
 * ```
 */
const char *wasmer_version_pre(void);

/**
 * Parses in-memory bytes as either the WAT format, or a binary Wasm
 * module. This is wasmer-specific.
 *
 * In case of failure, `wat2wasm` sets the `out->data = NULL` and `out->size = 0`.
 *
 * # Example
 *
 * See the module's documentation.
 */
void wat2wasm(const wasm_byte_vec_t *wat, wasm_byte_vec_t *out);

#endif /* WASMER_WASM_H */