F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
spacewasm.h
Go to the documentation of this file.
1 /*
2  * spacewasm.h — C API for the SpaceWasm WebAssembly interpreter.
3  * Generated by cbindgen
4  */
5 
6 #ifndef SPACEWASM_H
7 #define SPACEWASM_H
8 
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <stddef.h>
12 
13 #if !defined(SPACEWASM_NORETURN) && defined(__has_c_attribute)
14 # if __has_c_attribute(noreturn)
15 # define SPACEWASM_NORETURN [[noreturn]] /* Standard C23 attribute */
16 # endif
17 #endif
18 
19 #if !defined(SPACEWASM_NORETURN)
20 # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
21 # define SPACEWASM_NORETURN _Noreturn /* Standard C11 keyword */
22 # elif defined(__GNUC__) || defined(__clang__)
23 # define SPACEWASM_NORETURN __attribute__((noreturn)) /* GCC / Clang extension */
24 # elif defined(_MSC_VER)
25 # define SPACEWASM_NORETURN __declspec(noreturn) /* MSVC extension */
26 # else
27 # define SPACEWASM_NORETURN /* Fallback: do nothing on unsupported compilers */
28 # endif
29 #endif
30 
31 #define CustomSection_MAX_NAME_LENGTH 32
32 
33 /*
34  Operation status returned by most `spacewasm_*` functions.
35  [`spacewasm_status_t::SPACEWASM_OK`] (0) means success.
36  */
38 #if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
39  : int32_t
40 #endif // defined(__cplusplus) || __STDC_VERSION__ >= 202311L
41  {
148 };
149 #ifndef __cplusplus
150 #if __STDC_VERSION__ >= 202311L
152 #else
153 typedef int32_t spacewasm_status_t;
154 #endif // __STDC_VERSION__ >= 202311L
155 #endif // __cplusplus
156 
157 /*
158  Result of a C host function call.
159  */
161 #if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
162  : int32_t
163 #endif // defined(__cplusplus) || __STDC_VERSION__ >= 202311L
164  {
165  /*
166  Continue, do not return a value
167  */
169  /*
170  Continue; populate `out_result` if the function has a result type.
171  */
173  /*
174  Trap the interpreter.
175  */
177  /*
178  Pause the interpreter (cooperative yield).
179  */
181 };
182 #ifndef __cplusplus
183 #if __STDC_VERSION__ >= 202311L
185 #else
187 #endif // __STDC_VERSION__ >= 202311L
188 #endif // __cplusplus
189 
190 /*
191  FFI-safe value type tag. Matches the ordering of [`spacewasm::ValType`].
192  */
194 #if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
195  : uint8_t
196 #endif // defined(__cplusplus) || __STDC_VERSION__ >= 202311L
197  {
202 };
203 #ifndef __cplusplus
204 #if __STDC_VERSION__ >= 202311L
206 #else
207 typedef uint8_t spacewasm_valtype_t;
208 #endif // __STDC_VERSION__ >= 202311L
209 #endif // __cplusplus
210 
211 /*
212  Outcome of a [`spacewasm_read_fn_t`] call, written by the callback.
213  */
215 #if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
216  : int32_t
217 #endif // defined(__cplusplus) || __STDC_VERSION__ >= 202311L
218  {
219  /*
220  A chunk of `*out_len` bytes was written to the buffer. `out_len == 0`
221  also signals end-of-stream.
222  */
224  /*
225  End of stream; no more bytes.
226  */
228  /*
229  An I/O error occurred; loading fails with `SPACEWASM_ERR_READER_ERROR`.
230  */
232 };
233 #ifndef __cplusplus
234 #if __STDC_VERSION__ >= 202311L
236 #else
237 typedef int32_t spacewasm_read_result_t;
238 #endif // __STDC_VERSION__ >= 202311L
239 #endif // __cplusplus
240 
241 /*
242  Outcome of a call to `spacewasm_run`.
243  */
245 #if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
246  : int32_t
247 #endif // defined(__cplusplus) || __STDC_VERSION__ >= 202311L
248  {
253 };
254 #ifndef __cplusplus
255 #if __STDC_VERSION__ >= 202311L
257 #else
258 typedef int32_t spacewasm_run_status_t;
259 #endif // __STDC_VERSION__ >= 202311L
260 #endif // __cplusplus
261 
262 /*
263  Reason accompanying a trap (`out_trap`). Mirrors [`spacewasm::TrapReason`],
264  with an extra [`SPACEWASM_TRAP_NONE`] (`-1`) written when no trap occurred.
265  */
267 #if defined(__cplusplus) || __STDC_VERSION__ >= 202311L
268  : int32_t
269 #endif // defined(__cplusplus) || __STDC_VERSION__ >= 202311L
270  {
271  /*
272  No trap occurred (the run finished, paused, or ran out of fuel).
273  */
275  /*
276  Triggered by an `unreachable` instruction.
277  */
279  /*
280  A host function noted an unrecoverable failure.
281  */
283  /*
284  Integer or floating-point division by zero.
285  */
287  /*
288  An indirect call index was out of the table's range.
289  */
291  /*
292  The function type in an indirect call did not match the pointer's type.
293  */
295  /*
296  An indirect call referenced an uninitialized table element.
297  */
299  /*
300  An imported global could not be read.
301  */
303  /*
304  An imported global could not be set.
305  */
307  /*
308  A memory allocation (e.g. `memory.grow`) ran out of memory.
309  */
311  /*
312  `memory.grow` failed because a host function holds the memory.
313  */
315  /*
316  A memory operation was out of bounds.
317  */
319  /*
320  Ran out of stack space.
321  */
323  /*
324  The result of an operation was unrepresentable (e.g. converting Inf).
325  */
327  /*
328  Signed division caused integer overflow.
329  */
331  /*
332  Attempted to convert NaN to an integer.
333  */
335  /*
336  The IR decoder reached an opcode or immediate it cannot interpret.
337  */
339 };
340 #ifndef __cplusplus
341 #if __STDC_VERSION__ >= 202311L
343 #else
344 typedef int32_t spacewasm_trap_t;
345 #endif // __STDC_VERSION__ >= 202311L
346 #endif // __cplusplus
347 
348 /*
349  A struct holding the alloc, realloc, dealloc, userdata pointers to adapt
350  the C API to the Rc<dyn WasmMemoryAllocator> API.
351 
352  This struct is reference counted and deallocated once all modules using this allocator
353  are dropped.
354  */
356 
357 /*
358  Handle holding the SpaceWasm engine and compiled IR code.
359  This handle is used for holding and executing the SpaceWasm interpreter.
360  */
361 typedef struct spacewasm_t spacewasm_t;
362 
364 
365 /*
366  Opaque handle passed to C host callbacks, wrapping a borrowed core
367  [`Engine`]. Valid only for the duration of the call.
368  */
370 
371 /*
372  Allocate `size` bytes aligned to `align`. Return NULL on failure.
373  */
374 typedef uint8_t *(*spacewasm_alloc_fn_t)(void *userdata, size_t size, size_t align);
375 
376 /*
377  Resize the `old_size`-byte allocation at `ptr` (alignment `align`) to
378  `new_size` bytes, moving the contents if needed. Return NULL on failure.
379  */
380 typedef uint8_t *(*spacewasm_realloc_fn_t)(void *userdata,
381  uint8_t *ptr,
382  size_t old_size,
383  size_t new_size,
384  size_t align);
385 
386 /*
387  Free the `size`-byte allocation at `ptr` (alignment `align`).
388  */
389 typedef void (*spacewasm_dealloc_fn_t)(void *userdata, uint8_t *ptr, size_t size, size_t align);
390 
391 typedef struct spacewasm_host_t {
393  size_t capacity;
394  size_t len;
396 
397 /*
398  FFI-safe union of the four WebAssembly 1.0 value payloads.
399  */
401  int32_t i32_;
402  int64_t i64_;
403  float f32_;
404  double f64_;
406 
407 /*
408  FFI-safe tagged value. `tag` selects the active `u` field.
409  */
410 typedef struct spacewasm_value_t {
414 
415 /*
416  Callback signature for a host function implemented in C. `caller` is an
417  opaque handle for `spacewasm_mem_*`; write `out_result` iff returning a value.
418  */
420  void *userdata,
421  const struct spacewasm_value_t *params,
422  size_t n_params,
423  struct spacewasm_value_t *out_result);
424 
425 /*
426  C callback that supplies the next chunk of a Wasm module. The callback owns
427  the buffer: it sets `*out_buf` to point at the next chunk and `*out_len` to
428  its length (0 == EOF). The chunk must stay valid until the next call to the
429  callback (or until loading completes).
430  */
432  const uint8_t **out_buf,
433  size_t *out_len);
434 
435 /*
436  FFI-safe mirror of [`CompilerOptions`], controlling how guest
437  modules loaded onto a store are compiled. Passed to [`spacewasm_new`].
438  */
440  /*
441  Allow compiling `memory.grow` instructions. When `false`, a module using
442  `memory.grow` is rejected at load time.
443  */
445  /*
446  Maximum number of iterations to resolve during a control-flow backpatch.
447  Bounds compile time on pathological modules at the cost of rejecting some
448  valid programs. Set to 0 for unlimited iterations.
449  */
451  /*
452  Maximum number of compiled code pages allowed across all modules loaded
453  onto the store.
454  */
457 
458 /*
459  Allocate `size` bytes aligned to `align`. Return NULL on failure. Per page allocation.
460  */
461 typedef uint8_t *(*spacewasm_global_alloc_fn_t)(void *userdata, size_t size, size_t align);
462 
463 /*
464  Free the `size`-byte allocation at `ptr` (alignment `align`). Per page deallocation.
465  */
466 typedef void (*spacewasm_global_dealloc_fn_t)(void *userdata,
467  uint8_t *ptr,
468  size_t size,
469  size_t align);
470 
471 
472 
473 #ifdef __cplusplus
474 extern "C" {
475 #endif // __cplusplus
476 
477 /*
478  Create a guest linear-memory allocator from three C callbacks, returning an
479  opaque handle (or null if any callback is null or allocation fails). The
480  handle is passed to [`spacewasm_load_module`] and must be released with
481  [`spacewasm_allocator_destroy`]. `userdata` is passed to every callback.
482  */
484  spacewasm_realloc_fn_t realloc,
485  spacewasm_dealloc_fn_t dealloc,
486  void *userdata);
487 
488 /*
489  Destroy an allocator handle. No-op on null. Any loaded module keeps its own
490  reference to the underlying allocator, so destroying the handle after loading
491  is safe.
492 
493  # Safety
494  `allocator` must be a live handle from [`spacewasm_allocator_new`], not
495  already destroyed.
496  */
497 void spacewasm_allocator_destroy(struct spacewasm_allocator_t *allocator);
498 
499 /*
500  Create a new host module vector of `len` size
501 
502  # Safety
503  `dest` must be null or a valid, live pointer to write the new host vector into.
504  */
505 spacewasm_status_t spacewasm_host_new(size_t len, struct spacewasm_host_t *dest);
506 
507 /*
508  Add a host module named `name` sized for `max_functions` functions and
509  `max_globals` globals, writing its index to `out_idx` (if non-null).
510 
511  # Safety
512  `host` must be live; all C strings valid and NUL-terminated.
513  */
515  const char *name,
516  size_t max_functions,
517  size_t max_globals,
518  uint32_t *out_idx);
519 
520 /*
521  Register a host function `name` in host module `module_idx`, with parameter
522  and return signatures given by `params_sig`/`returns_sig` and implemented by
523  callback `f` (passed `userdata` on each call).
524 
525  # Safety
526  `host` must be live; all C strings valid and NUL-terminated.
527  */
529  uint32_t module_idx,
530  const char *name,
531  const char *params_sig,
532  const char *returns_sig,
534  void *userdata);
535 
536 /*
537  Load a guest module named `name` onto an existing engine by streaming its
538  bytes through the `read` callback. The callback owns the buffer backing each
539  chunk (see [`spacewasm_read_fn_t`]). This does not run the module's start
540  function; resolve it with [`spacewasm_module_start`] and invoke it with
541  [`spacewasm_invoke`] for that. `allocator` supplies the
542  guest linear memory (see [`spacewasm_allocator_new`]). Writes the new module's
543  index to `out_module_idx` (if non-null). May be called repeatedly to load
544  several modules onto the same engine.
545 
546  # Safety
547  `engine` and `allocator` must be live handles; `read` a valid callback;
548  `out_module_idx` null or valid.
549  */
551  const char *name,
552  spacewasm_read_fn_t read,
553  void *read_userdata,
554  struct spacewasm_allocator_t *allocator,
555  uint32_t *out_module_idx);
556 
557 /*
558  Consume the host module vector `host` and finish it into an engine handle,
559  written to `out_engine`. The engine is sized with a `stack_size`-word guest
560  stack (each word is 4 bytes), room for `max_modules` guest modules (<= 256), and compiles guest
561  modules according to `options` (code-page budget, `memory.grow` support,
562  backpatch bound). No guest module is loaded yet; use
563  [`spacewasm_load_module`] to load one or more.
564 
565  `host` may be null to create an engine with no host modules.
566 
567  # Ownership of `host`
568 
569  The host vector is consumed on every path *except* the two argument-validation
570  failures that are rejected before `host` is read:
571 
572  * [`spacewasm_status_t::SPACEWASM_ERR_NULL_ARG`] — `out_engine` is null.
573  * [`spacewasm_status_t::SPACEWASM_ERR_VEC_TOO_LONG`] — `max_modules` exceeds 256.
574 
575  On those two the caller still owns `host` and must
576  [`spacewasm_host_destroy`] it. Every other outcome, success or failure,
577  consumes it. Concretely: check for these two codes before deciding whether to
578  destroy the vector.
579 
580  # Safety
581  `host` must be null or a live handle from [`spacewasm_host_new`], not already
582  consumed/destroyed; `out_engine` must be a valid pointer.
583  */
585  size_t stack_size,
586  size_t max_modules,
587  struct spacewasm_compiler_options_t options,
588  struct spacewasm_t **out_engine);
589 
590 /*
591  Destroy a host vector that was never consumed into an engine. No-op on null.
592 
593  # Safety
594  `host` must be null or a live unconsumed handle from [`spacewasm_host_new`].
595  */
596 void spacewasm_host_destroy(struct spacewasm_host_t *host);
597 
598 /*
599  Find a module with a given name in the engine.
600 
601  # Safety
602  `engine` must be live; `name` valid; `out_index` valid.
603  */
605  const char *name,
606  uint32_t *out_index);
607 
608 /*
609  Look up the exported function named `name` in module `module_idx` and write
610  its index to `out_index`.
611 
612  # Safety
613  `engine` must be live; `name` valid; `out_index` valid.
614  */
616  uint32_t module_idx,
617  const char *name,
618  uint32_t *out_index);
619 
620 /*
621  Look up the start function of module `module_idx` and write its location to
622  `out_module_idx` and `out_func_index`.
623 
624  Start functions are never host functions (that is rejected at load time), so
625  the result is always a directly-invokable Wasm function. The written indices
626  can be passed straight to [`spacewasm_invoke`] followed by [`spacewasm_run`],
627  exactly as you would an exported function. Note that `out_module_idx` may
628  differ from `module_idx` when the start is an imported (cross-module)
629  function.
630 
631  Returns [`spacewasm_status_t::SPACEWASM_OK`] and populates the outputs when
632  the module declares a start function. Returns
633  [`spacewasm_status_t::SPACEWASM_ERR_NOT_FOUND`] when `module_idx` is out of
634  range or the module has no start function, in which case there is nothing to
635  invoke.
636 
637  # Safety
638  `engine` must be live; `out_module_idx` and `out_func_index` valid.
639  */
641  uint32_t module_idx,
642  uint32_t *out_module_idx,
643  uint32_t *out_func_index);
644 
645 /*
646  Check that function `func_index` of module `module_idx` has the signature
647  described by `params_sig` and `returns_sig`.
648 
649  Signatures use the same alphabet as [`spacewasm_add_host_function`]:
650  `i` (i32), `I` (i64), `f` (f32), `d` (f64). For example, a function
651  `(i32, i32) -> i32` matches `params_sig = "ii"`, `returns_sig = "i"`.
652 
653  Returns [`spacewasm_status_t::SPACEWASM_OK`] when the signature matches.
654  Returns [`spacewasm_status_t::SPACEWASM_ERR_PARAM_LEN_MISMATCH`] when the
655  parameter or return count differs, and
656  [`spacewasm_status_t::SPACEWASM_ERR_PARAM_TYPE_MISMATCH`] when a type at some
657  position differs. Returns [`spacewasm_status_t::SPACEWASM_ERR_NOT_FOUND`]
658  when `module_idx` or `func_index` is out of range. When a signature string
659  contains a character other than `iIfd` it returns
660  [`spacewasm_status_t::SPACEWASM_ERR_BAD_ARG`], and when it declares more than
661  `MAX_HOST_FUNCTION_PARAMS` entries it returns
662  [`spacewasm_status_t::SPACEWASM_ERR_FUNCTION_PARAMETERS_TOO_LARGE`].
663 
664  # Safety
665  `engine` must be live; all C strings valid and NUL-terminated.
666  */
668  uint32_t module_idx,
669  uint32_t func_index,
670  const char *params_sig,
671  const char *returns_sig);
672 
673 /*
674  Look up the global exported as `name` in module `module_idx` and write its
675  index to `out_index`. The written index addresses the module's own globals
676  and can be passed straight to [`spacewasm_get_global`] and
677  [`spacewasm_set_global`].
678 
679  Only globals defined by module `module_idx` itself are resolvable this way:
680  if the export re-exports a global imported from another (guest or host)
681  module, this returns [`spacewasm_status_t::SPACEWASM_ERR_NOT_FOUND`], exactly
682  as [`spacewasm_find_export_func`] does for imported functions. Reach such a
683  global through the module that defines it.
684 
685  Returns [`spacewasm_status_t::SPACEWASM_ERR_NOT_FOUND`] when `module_idx` is
686  out of range or the module exports no matching, locally-defined global.
687 
688  # Safety
689  `engine` must be live; `name` valid; `out_index` valid.
690  */
692  uint32_t module_idx,
693  const char *name,
694  uint32_t *out_index);
695 
696 /*
697  Read global `global_index` of module `module_idx` into `out`, tagged with the
698  global's declared value type. `global_index` addresses the module's own
699  globals, as returned by [`spacewasm_find_global`].
700 
701  Returns [`spacewasm_status_t::SPACEWASM_ERR_NOT_FOUND`] when `module_idx` or
702  `global_index` is out of range.
703 
704  # Safety
705  `engine` must be live; `out` valid.
706  */
708  uint32_t module_idx,
709  uint32_t global_index,
710  struct spacewasm_value_t *out);
711 
712 /*
713  Write `value` into global `global_index` of module `module_idx`.
714  `global_index` addresses the module's own globals, as returned by
715  [`spacewasm_find_global`].
716 
717  The tag of `value` must match the global's declared value type, and the
718  global must be mutable.
719 
720  Returns [`spacewasm_status_t::SPACEWASM_ERR_NOT_FOUND`] when `module_idx` or
721  `global_index` is out of range,
722  [`spacewasm_status_t::SPACEWASM_ERR_GLOBAL_TYPE_MISMATCH`] when the value type
723  does not match the global, and
724  [`spacewasm_status_t::SPACEWASM_ERR_GLOBAL_NOT_MUTABLE`] when the global is
725  declared `const`.
726 
727  # Safety
728  `engine` must be a live handle.
729  */
731  uint32_t module_idx,
732  uint32_t global_index,
733  struct spacewasm_value_t value);
734 
735 /*
736  Set up a call to exported function `func_index` of module `module_idx` with
737  the `n` arguments in `params`. Does not run the function; drive execution
738  with [`spacewasm_run`].
739 
740  # Safety
741  `engine` must be live; `params` valid for `n` entries.
742  */
744  uint32_t module_idx,
745  uint32_t func_index,
746  const struct spacewasm_value_t *params,
747  size_t n);
748 
749 /*
750  Run the pending invocation for up to `fuel` units of work, writing any trap
751  to `out_trap`. Returns whether the call finished, trapped, or ran out of fuel.
752 
753  # Safety
754  `engine` must be live; `out_trap` null or valid.
755  */
757  size_t fuel,
758  spacewasm_trap_t *out_trap);
759 
760 /*
761  Resume the interpreter from a paused state (no return value).
762 
763  Returns [`spacewasm_status_t::SPACEWASM_ERR_WRONG_STATE`] if the engine is not
764  paused, and [`spacewasm_status_t::SPACEWASM_ERR_PARAM_TYPE_MISMATCH`] if the
765  paused host function declared a result — use [`spacewasm_resume_value`]
766  instead. On the latter the pause is preserved, so the call can be retried.
767 
768  # Safety
769  `engine` must be live.
770  */
772 
773 /*
774  Resume the interpreter from a paused state.
775  This function will also push a value to the interpreter stack
776  as the return value of the host function that requested a pause.
777 
778  Returns [`spacewasm_status_t::SPACEWASM_ERR_BAD_ARG`] if `resume_value.tag` is
779  not a valid [`spacewasm_valtype_t`],
780  [`spacewasm_status_t::SPACEWASM_ERR_WRONG_STATE`] if the engine is not paused,
781  and [`spacewasm_status_t::SPACEWASM_ERR_PARAM_TYPE_MISMATCH`] if the value's
782  type does not match the paused host function's declared result. In the
783  mismatch case the pause is preserved and no stack state changes, so the call
784  can be retried with a correctly typed value.
785 
786  # Safety
787  `engine` must be live.
788  */
790  struct spacewasm_value_t resume_value);
791 
792 /*
793  Reset the engine back to an idle state, discarding any in-progress or
794  completed call: the program counter, stack pointers, and pending result are
795  cleared, and guest linear memory and the table are reset to their zero
796  state. Loaded modules remain loaded. Use this to abandon a paused or
797  out-of-fuel call, or to run a fresh invocation from a clean slate.
798 
799  # Safety
800  `engine` must be a live handle.
801  */
803 
804 /*
805  Fetch the result of the last completed call, coerced to `expected`, into
806  `out`.
807 
808  # Silent type coercion
809 
810  The core engine stores a completed call's result as an untagged
811  [`RawValue`]. The function signature must be checked before invoking and the return
812  value must be extracted (in this function) using the proper `expected` type.
813 
814  # Safety
815  `engine` must be live; `out` valid.
816  */
818  spacewasm_valtype_t expected,
819  struct spacewasm_value_t *out);
820 
821 /*
822  Destroy an engine and free its resources. No-op on null.
823 
824  # Safety
825  `engine` must be a live handle, not already destroyed.
826  */
827 void spacewasm_destroy(struct spacewasm_t *engine);
828 
829 /*
830  Read `len` bytes of guest linear memory starting at `addr` into `dst`.
831  Intended for use from within a host function.
832 
833  # Safety
834  `caller` must be a live caller handle; `dst` valid for `len`.
835  */
837  uint32_t addr,
838  uint8_t *dst,
839  size_t len);
840 
841 /*
842  Write `len` bytes from `src` to guest linear memory starting at `addr`.
843  Intended for use from within a host function.
844 
845  # Safety
846  `caller` must be a live caller handle; `src` valid for `len`.
847  */
849  uint32_t addr,
850  const uint8_t *src,
851  size_t len);
852 
853 /*
854  Report the size of guest linear memory in pages. Intended for use from
855  within a host function.
856 
857  # Safety
858  `caller` must be a live caller handle; `out_pages` must be a valid pointer.
859  */
860 spacewasm_status_t spacewasm_mem_size(struct spacewasm_caller_t *caller, uint32_t *out_pages);
861 
862 /*
863  Handle a spacewasm Rust panic. This function should not return
864 
865  # Arguments
866 
867  * `filename`: Filename where panic occurred (not null terminated)
868  * `filename_len`: Length of filename (could be 0)
869  * `line`: Line number where panic occured
870  * `msg`: Panic message if it could be extracted (not null terminated)
871  * `len`: Length of panic message (zero if empty)
872 
873  returns: !
874  */
875 SPACEWASM_NORETURN extern void spacewasm_panic(const uint8_t *filename,
876  size_t filename_len,
877  uint32_t line,
878  const uint8_t *msg,
879  size_t len);
880 
881 /*
882  Install the process-wide heap allocator backing the interpreter.
883  `alloc`/`dealloc` are called at *page* granularity.
884 
885  # Safety
886  `alloc`/`dealloc` must remain valid for the lifetime of the process and
887  honor the requested size/alignment. `userdata` must outlive all allocations.
888  */
891  void *userdata);
892 
893 #ifdef __cplusplus
894 } // extern "C"
895 #endif // __cplusplus
896 
897 #endif /* SPACEWASM_H */
struct spacewasm_host_module_t * ptr
Definition: spacewasm.h:392
union spacewasm_value_payload_t u
Definition: spacewasm.h:412
void spacewasm_host_destroy(struct spacewasm_host_t *host)
spacewasm_run_status_t spacewasm_run(struct spacewasm_t *engine, size_t fuel, spacewasm_trap_t *out_trap)
spacewasm_status_t spacewasm_mem_write(struct spacewasm_caller_t *caller, uint32_t addr, const uint8_t *src, size_t len)
struct spacewasm_allocator_t * spacewasm_allocator_new(spacewasm_alloc_fn_t alloc, spacewasm_realloc_fn_t realloc, spacewasm_dealloc_fn_t dealloc, void *userdata)
uint8_t *(* spacewasm_realloc_fn_t)(void *userdata, uint8_t *ptr, size_t old_size, size_t new_size, size_t align)
Definition: spacewasm.h:380
void(* spacewasm_global_dealloc_fn_t)(void *userdata, uint8_t *ptr, size_t size, size_t align)
Definition: spacewasm.h:466
spacewasm_status_t spacewasm_module_start(struct spacewasm_t *engine, uint32_t module_idx, uint32_t *out_module_idx, uint32_t *out_func_index)
spacewasm_status_t spacewasm_load_module(struct spacewasm_t *engine, const char *name, spacewasm_read_fn_t read, void *read_userdata, struct spacewasm_allocator_t *allocator, uint32_t *out_module_idx)
struct spacewasm_caller_t spacewasm_caller_t
Definition: spacewasm.h:369
spacewasm_status_t spacewasm_get_global(struct spacewasm_t *engine, uint32_t module_idx, uint32_t global_index, struct spacewasm_value_t *out)
spacewasm_status_t spacewasm_add_host_function(struct spacewasm_host_t *host, uint32_t module_idx, const char *name, const char *params_sig, const char *returns_sig, spacewasm_host_fn_t f, void *userdata)
spacewasm_read_result_t(* spacewasm_read_fn_t)(void *userdata, const uint8_t **out_buf, size_t *out_len)
Definition: spacewasm.h:431
spacewasm_run_status_t
Definition: spacewasm.h:244
spacewasm_status_t spacewasm_mem_read(struct spacewasm_caller_t *caller, uint32_t addr, uint8_t *dst, size_t len)
spacewasm_status_t spacewasm_find_export_func(struct spacewasm_t *engine, uint32_t module_idx, const char *name, uint32_t *out_index)
uint8_t *(* spacewasm_global_alloc_fn_t)(void *userdata, size_t size, size_t align)
Definition: spacewasm.h:461
union spacewasm_value_payload_t spacewasm_value_payload_t
spacewasm_trap_t
Definition: spacewasm.h:266
spacewasm_status_t spacewasm_set_global_allocator(spacewasm_global_alloc_fn_t alloc, spacewasm_global_dealloc_fn_t dealloc, void *userdata)
struct spacewasm_host_module_t spacewasm_host_module_t
Definition: spacewasm.h:363
struct spacewasm_compiler_options_t spacewasm_compiler_options_t
spacewasm_status_t spacewasm_get_result(struct spacewasm_t *engine, spacewasm_valtype_t expected, struct spacewasm_value_t *out)
spacewasm_status_t spacewasm_resume_value(struct spacewasm_t *engine, struct spacewasm_value_t resume_value)
spacewasm_valtype_t tag
Definition: spacewasm.h:411
spacewasm_status_t spacewasm_add_host_module(struct spacewasm_host_t *host, const char *name, size_t max_functions, size_t max_globals, uint32_t *out_idx)
spacewasm_status_t spacewasm_find_global(struct spacewasm_t *engine, uint32_t module_idx, const char *name, uint32_t *out_index)
spacewasm_hostcall_result_t
Definition: spacewasm.h:160
uint8_t *(* spacewasm_alloc_fn_t)(void *userdata, size_t size, size_t align)
Definition: spacewasm.h:374
spacewasm_valtype_t
Definition: spacewasm.h:193
spacewasm_status_t spacewasm_new(struct spacewasm_host_t *host, size_t stack_size, size_t max_modules, struct spacewasm_compiler_options_t options, struct spacewasm_t **out_engine)
struct spacewasm_t spacewasm_t
Definition: spacewasm.h:361
#define SPACEWASM_NORETURN
Definition: spacewasm.h:27
spacewasm_status_t spacewasm_reset(struct spacewasm_t *engine)
spacewasm_status_t
Definition: spacewasm.h:37
spacewasm_status_t spacewasm_check_func_signature(struct spacewasm_t *engine, uint32_t module_idx, uint32_t func_index, const char *params_sig, const char *returns_sig)
spacewasm_status_t spacewasm_host_new(size_t len, struct spacewasm_host_t *dest)
spacewasm_status_t spacewasm_invoke(struct spacewasm_t *engine, uint32_t module_idx, uint32_t func_index, const struct spacewasm_value_t *params, size_t n)
void spacewasm_destroy(struct spacewasm_t *engine)
SPACEWASM_NORETURN void spacewasm_panic(const uint8_t *filename, size_t filename_len, uint32_t line, const uint8_t *msg, size_t len)
spacewasm_status_t spacewasm_find_module(struct spacewasm_t *engine, const char *name, uint32_t *out_index)
spacewasm_hostcall_result_t(* spacewasm_host_fn_t)(struct spacewasm_caller_t *caller, void *userdata, const struct spacewasm_value_t *params, size_t n_params, struct spacewasm_value_t *out_result)
Definition: spacewasm.h:419
struct spacewasm_host_t spacewasm_host_t
spacewasm_status_t spacewasm_set_global(struct spacewasm_t *engine, uint32_t module_idx, uint32_t global_index, struct spacewasm_value_t value)
void spacewasm_allocator_destroy(struct spacewasm_allocator_t *allocator)
struct spacewasm_value_t spacewasm_value_t
spacewasm_read_result_t
Definition: spacewasm.h:214
struct spacewasm_allocator_t spacewasm_allocator_t
Definition: spacewasm.h:355
spacewasm_status_t spacewasm_resume(struct spacewasm_t *engine)
spacewasm_status_t spacewasm_mem_size(struct spacewasm_caller_t *caller, uint32_t *out_pages)
void(* spacewasm_dealloc_fn_t)(void *userdata, uint8_t *ptr, size_t size, size_t align)
Definition: spacewasm.h:389