|
11 | 11 | extern "C" { |
12 | 12 | #endif |
13 | 13 |
|
| 14 | +/// The opaque data type representing a module. |
| 15 | +typedef struct FizzyModule FizzyModule; |
| 16 | + |
| 17 | +/// The opaque data type representing an instance (instantiated module). |
| 18 | +typedef struct FizzyInstance FizzyInstance; |
| 19 | + |
| 20 | +/// The data type representing numeric values. |
| 21 | +/// |
| 22 | +/// i64 member is used to represent values of both i32 and i64 type. |
| 23 | +union FizzyValue |
| 24 | +{ |
| 25 | + uint64_t i64; |
| 26 | + float f32; |
| 27 | + double f64; |
| 28 | +}; |
| 29 | + |
| 30 | +/// Result of execution of a function. |
| 31 | +typedef struct FizzyExecutionResult |
| 32 | +{ |
| 33 | + /// Whether execution ended with a trap. |
| 34 | + bool trapped; |
| 35 | + /// Whether function returned a value. Valid only if trapped equals false. |
| 36 | + bool has_value; |
| 37 | + /// Value returned from a function. |
| 38 | + /// Valid only if trapped equals false and has_value equals true. |
| 39 | + union FizzyValue value; |
| 40 | +} FizzyExecutionResult; |
| 41 | + |
| 42 | + |
| 43 | +/// Pointer to external function. |
| 44 | +/// |
| 45 | +/// @param context Opaque pointer to execution context. |
| 46 | +/// @param instance Pointer to module instance. |
| 47 | +/// @param args Pointer to the argument array. Can be NULL iff args_size equals 0. |
| 48 | +/// @param args_size Size of the argument array. |
| 49 | +/// @param depth Call stack depth. |
| 50 | +typedef FizzyExecutionResult (*FizzyExternalFn)(void* context, FizzyInstance* instance, |
| 51 | + const union FizzyValue* args, size_t args_size, int depth); |
| 52 | + |
| 53 | +/// External function. |
| 54 | +typedef struct FizzyExternalFunction |
| 55 | +{ |
| 56 | + // TODO function type |
| 57 | + |
| 58 | + /// Pointer to function. |
| 59 | + FizzyExternalFn function; |
| 60 | + /// Opaque pointer to execution context, that will be passed to function. |
| 61 | + void* context; |
| 62 | +} FizzyExternalFunction; |
| 63 | + |
| 64 | +/// Validate binary module. |
14 | 65 | bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size); |
15 | 66 |
|
| 67 | +/// Parse binary module. |
| 68 | +/// |
| 69 | +/// @returns non-NULL pointer to module in case of success, NULL otherwise. |
| 70 | +const FizzyModule* fizzy_parse(const uint8_t* wasm_binary, size_t wasm_binary_size); |
| 71 | + |
| 72 | +/// Free resources associated with the module. |
| 73 | +/// |
| 74 | +/// Should be called unless @p module was passed to fizzy_instantiate. |
| 75 | +/// If passed pointer is NULL, has no effect. |
| 76 | +void fizzy_free_module(const FizzyModule* module); |
| 77 | + |
| 78 | +/// Instantiate a module. |
| 79 | +/// Takes ownership of module, i.e. @p module is invalidated after this call. |
| 80 | +/// |
| 81 | +/// @param module Pointer to module. |
| 82 | +/// @param imported_functions Pointer to the imported function array. Can be NULL iff |
| 83 | +/// imported_functions_size equals 0. |
| 84 | +/// @param imported_functions_size Size of the imported function array. Can be zero. |
| 85 | +/// @returns non-NULL pointer to instance in case of success, NULL otherwise. |
| 86 | +/// |
| 87 | +/// @note |
| 88 | +/// Function expects @a imported_functions to be in the order of imports defined in the module. |
| 89 | +/// No validation is done on the number of functions passed in, nor on their order. |
| 90 | +/// When number of passed functions or their order is different from the one defined by the |
| 91 | +/// module, behaviour is undefined. |
| 92 | +FizzyInstance* fizzy_instantiate(const FizzyModule* module, |
| 93 | + const FizzyExternalFunction* imported_functions, size_t imported_functions_size); |
| 94 | + |
| 95 | +/// Free resources associated with the instance. |
| 96 | +/// If passed pointer is NULL, has no effect. |
| 97 | +void fizzy_free_instance(FizzyInstance* instance); |
| 98 | + |
| 99 | +/// Execute module function. |
| 100 | +/// |
| 101 | +/// @param instance Pointer to module instance. |
| 102 | +/// @param args Pointer to the argument array. Can be NULL if function has 0 inputs. |
| 103 | +/// @param depth Call stack depth. |
| 104 | +/// |
| 105 | +/// @note |
| 106 | +/// No validation is done on the number of arguments passed in @p args, nor on their types. |
| 107 | +/// When number of passed arguments or their types are different from the ones defined by the |
| 108 | +/// function type, behaviour is undefined. |
| 109 | +FizzyExecutionResult fizzy_execute( |
| 110 | + FizzyInstance* instance, uint32_t func_idx, const union FizzyValue* args, int depth); |
| 111 | + |
16 | 112 | #ifdef __cplusplus |
17 | 113 | } |
18 | 114 | #endif |
0 commit comments