orca/ext/bytebox/include/bytebox.h

171 lines
5.5 KiB
C
Raw Permalink Normal View History

2023-08-01 02:57:16 +00:00
// C interface for bytebox wasm runtime.
#include <stdint.h>
#include <stdbool.h>
struct bb_slice
{
char* data;
size_t length;
};
typedef struct bb_slice bb_slice;
enum bb_error
{
BB_ERROR_OK,
BB_ERROR_FAILED,
BB_ERROR_OUTOFMEMORY,
BB_ERROR_INVALIDPARAM,
BB_ERROR_UNKNOWNEXPORT,
2023-08-02 05:44:42 +00:00
BB_ERROR_UNKNOWNIMPORT,
BB_ERROR_INCOMPATIBLEIMPORT,
2023-08-03 15:00:13 +00:00
BB_ERROR_TRAP_DEBUG,
BB_ERROR_TRAP_UNREACHABLE,
BB_ERROR_TRAP_INTEGERDIVISIONBYZERO,
BB_ERROR_TRAP_INTEGEROVERFLOW,
BB_ERROR_TRAP_INDIRECTCALLTYPEMISMATCH,
BB_ERROR_TRAP_INVALIDINTEGERCONVERSION,
BB_ERROR_TRAP_OUTOFBOUNDSMEMORYACCESS,
BB_ERROR_TRAP_UNDEFINEDELEMENT,
BB_ERROR_TRAP_UNINITIALIZEDELEMENT,
BB_ERROR_TRAP_OUTOFBOUNDSTABLEACCESS,
BB_ERROR_TRAP_STACKEXHAUSTED,
2023-08-01 02:57:16 +00:00
};
typedef enum bb_error bb_error;
enum bb_valtype
{
BB_VALTYPE_I32,
BB_VALTYPE_I64,
BB_VALTYPE_F32,
BB_VALTYPE_F64,
};
typedef enum bb_valtype bb_valtype;
typedef float bb_v128[4];
union bb_val
{
int32_t i32_val;
int64_t i64_val;
float f32_val;
double f64_val;
bb_v128 v128_val;
uint32_t externref_val;
};
typedef union bb_val bb_val;
struct bb_module_definition_init_opts
{
const char* debug_name;
};
typedef struct bb_module_definition_init_opts bb_module_definition_init_opts;
typedef struct bb_module_definition bb_module_definition;
2023-08-02 05:44:42 +00:00
typedef struct bb_module_instance bb_module_instance;
2023-08-01 02:57:16 +00:00
typedef struct bb_import_package bb_import_package;
2023-08-03 15:00:13 +00:00
typedef void bb_host_function(void* userdata, bb_module_instance* module, const bb_val* params, bb_val* returns);
typedef void* bb_wasm_memory_resize(void* mem, size_t new_size_bytes, size_t old_size_bytes, void* userdata);
typedef void bb_wasm_memory_free(void* mem, size_t size_bytes, void* userdata);
2023-08-01 02:57:16 +00:00
struct bb_module_instance_instantiate_opts
{
2023-08-02 05:44:42 +00:00
bb_import_package** packages;
2023-08-01 02:57:16 +00:00
size_t num_packages;
2023-08-03 15:00:13 +00:00
struct {
bb_wasm_memory_resize* resize_callback;
bb_wasm_memory_free* free_callback;
void* userdata;
} wasm_memory_config;
2023-08-10 14:29:51 +00:00
size_t stack_size;
2023-08-01 02:57:16 +00:00
bool enable_debug;
};
typedef struct bb_module_instance_instantiate_opts bb_module_instance_instantiate_opts;
struct bb_module_instance_invoke_opts
{
bool trap_on_start;
};
typedef struct bb_module_instance_invoke_opts bb_module_instance_invoke_opts;
struct bb_func_handle
{
uint32_t index;
uint32_t type;
};
typedef struct bb_func_handle bb_func_handle;
struct bb_func_info
{
bb_valtype* params;
size_t num_params;
bb_valtype* returns;
size_t num_returns;
};
typedef struct bb_func_info bb_func_info;
2023-08-02 05:44:42 +00:00
enum bb_global_mut
{
BB_GLOBAL_MUT_IMMUTABLE,
BB_GLOBAL_MUT_MUTABLE,
};
typedef enum bb_global_mut bb_global_mut;
struct bb_global
{
bb_val* value;
bb_valtype type;
bb_global_mut mut;
};
typedef struct bb_global bb_global;
2023-08-03 15:00:13 +00:00
enum bb_debug_trace_mode
{
BB_DEBUG_TRACE_NONE,
BB_DEBUG_TRACE_FUNCTION,
BB_DEBUG_TRACE_INSTRUCTION,
};
typedef enum bb_debug_trace_mode bb_debug_trace_mode;
enum bb_debug_trap_mode
{
BB_DEBUG_TRAP_MODE_DISABLED,
BB_DEBUG_TRAP_MODE_ENABLED,
};
typedef enum bb_debug_trap_mode bb_debug_trap_mode;
2023-08-01 02:57:16 +00:00
// typedef void* bb_malloc_func(size_t size, void* userdata);
// typedef void* bb_realloc_func(void* mem, size_t size, void* userdata);
// typedef void bb_free_func(void* mem, void* userdata);
// void bb_set_memory_hooks(bb_alloc_func* alloc_func, bb_realloc_func* realloc_func, bb_free_func);
const char* bb_error_str(bb_error err);
2023-08-02 05:44:42 +00:00
bb_module_definition* bb_module_definition_init(bb_module_definition_init_opts opts);
2023-08-01 02:57:16 +00:00
void bb_module_definition_deinit(bb_module_definition* definition);
bb_error bb_module_definition_decode(bb_module_definition* definition, const char* data, size_t length);
bb_slice bb_module_definition_get_custom_section(const bb_module_definition* definition, const char* name);
2023-08-02 05:44:42 +00:00
bb_import_package* bb_import_package_init(const char* name);
2023-08-01 02:57:16 +00:00
void bb_import_package_deinit(bb_import_package* package); // only deinit when all module_instances using the package have been deinited
bb_error bb_import_package_add_function(bb_import_package* package, bb_host_function* func, const char* export_name, bb_valtype* params, size_t num_params, bb_valtype* returns, size_t num_returns, void* userdata);
2023-08-03 15:00:13 +00:00
void bb_set_debug_trace_mode(bb_debug_trace_mode mode);
2023-08-02 05:44:42 +00:00
bb_module_instance* bb_module_instance_init(bb_module_definition* definition);
2023-08-01 02:57:16 +00:00
void bb_module_instance_deinit(bb_module_instance* instance);
bb_error bb_module_instance_instantiate(bb_module_instance* instance, bb_module_instance_instantiate_opts opts);
2023-08-02 05:44:42 +00:00
bb_error bb_module_instance_find_func(bb_module_instance* instance, const char* func_name, bb_func_handle* out_handle);
2023-08-01 02:57:16 +00:00
bb_func_info bb_module_instance_func_info(bb_module_instance* instance, bb_func_handle handle);
bb_error bb_module_instance_invoke(bb_module_instance* instance, bb_func_handle, const bb_val* params, size_t num_params, bb_val* returns, size_t num_returns, bb_module_instance_invoke_opts opts);
bb_error bb_module_instance_resume(bb_module_instance* instance, bb_val* returns, size_t num_returns);
bb_error bb_module_instance_step(bb_module_instance* instance, bb_val* returns, size_t num_returns);
bb_error bb_module_instance_debug_set_trap(bb_module_instance* instance, uint32_t address, bb_debug_trap_mode trap_mode);
void* bb_module_instance_mem(bb_module_instance* instance, size_t offset, size_t length);
bb_slice bb_module_instance_mem_all(bb_module_instance* instance);
2023-08-03 15:00:13 +00:00
bb_error bb_module_instance_mem_grow(bb_module_instance* instance, size_t num_pages);
2023-08-02 05:44:42 +00:00
bb_global bb_module_instance_find_global(bb_module_instance* instance, const char* global_name);
bool bb_func_handle_isvalid(bb_func_handle handle);