pub const C_HEADER: &'static str = "// Generic implementation of a WIT world with a static set of functions.\n//\n// This is the header file for the dynamic library generated by\n// `wasm-tools component wit-dylib`. This header describes in-memory data\n// structures that are generated as well as functions that the dynamic library\n// is expected to export.\n//\n// At a high level a `wit_t` provides type information during component\n// initialization and then `wit_dylib_export_call` is used as the entrypoint to\n// invoke functions. Various other `wit_dylib_*` intrinsics will receive indices\n// relative to the original `wit_t` value.\n\n#ifndef WIT_INTERPRETER_H\n#define WIT_INTERPRETER_H\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdbool.h>\n\ntypedef uint32_t wit_type_t;\n\n#define WIT_TYPE_KIND(ty) ((ty) & 0xff)\n#define WIT_TYPE_INDEX(ty) ((ty) >> 8)\n\n#define WIT_TYPE_U8 0\n#define WIT_TYPE_U16 1\n#define WIT_TYPE_U32 2\n#define WIT_TYPE_U64 3\n#define WIT_TYPE_S8 4\n#define WIT_TYPE_S16 5\n#define WIT_TYPE_S32 6\n#define WIT_TYPE_S64 7\n#define WIT_TYPE_BOOL 8\n#define WIT_TYPE_CHAR 9\n#define WIT_TYPE_F32 10\n#define WIT_TYPE_F64 11\n#define WIT_TYPE_STRING 12\n#define WIT_TYPE_ERROR_CONTEXT 13\n#define WIT_TYPE_RECORD 14\n#define WIT_TYPE_OWN 15\n#define WIT_TYPE_BORROW 16\n#define WIT_TYPE_FLAGS 17\n#define WIT_TYPE_TUPLE 18\n#define WIT_TYPE_VARIANT 19\n#define WIT_TYPE_ENUM 20\n#define WIT_TYPE_OPTION 21\n#define WIT_TYPE_RESULT 22\n#define WIT_TYPE_LIST 23\n#define WIT_TYPE_FIXED_LENGTH_LIST 24\n#define WIT_TYPE_FUTURE 25\n#define WIT_TYPE_STREAM 26\n#define WIT_TYPE_ALIAS 27\n#define WIT_TYPE_EMPTY 0xff\n\ntypedef void(*wit_import_fn_t)(void* cx);\ntypedef uint32_t(*wit_import_async_fn_t)(void* cx, void *abi_area);\ntypedef void(*wit_import_async_lift_fn_t)(void* cx, void *abi_area);\ntypedef void(*wit_export_task_return_fn_t)(void* cx);\n\ntypedef struct wit_import_func {\n const char *interface;\n const char *name;\n\n // If this function is imported for synchronous invocation, this field will\n // be non-null.\n //\n // This function pointer takes a single `void*` argument which is the\n // context for the call. The context will be passed to various\n // `wit_dylib_*` intrinsics below for pushing/popping values from the stack\n // contained within `cx`.\n //\n // If this function is imported as an async function, then this field will\n // be null.\n wit_import_fn_t impl;\n\n // If this function is imported for asynchronous invocation these two\n // fields will be non-null.\n //\n // The `async_impl` field is a function pointer that starts the async\n // import. This function call takes a `void *cx` just like `impl` above,\n // and it must stay alive for the entire invocation of the async imported\n // function. The second parameter of `async_impl` is an in-memory\n // allocation that must be of `async_abi_area_size` bytes aligned to\n // `async_abi_area_align`. This must also live for the duration of the\n // entire call and will be used to store canonical ABI values/results.\n // The return value from `async_impl` is the component-model status code\n // for the import\'s return value.\n //\n // The `async_lift_impl` field can be used once the component model\n // indicates that the function call is complete. The two parameters to\n // `async_lift_impl` as the same as `async_impl`. The `async_lift_impl`\n // function will use `wit_dylib_push_*` to translate from the canonical ABI\n // onto the stack within `cx`.\n //\n // These two fields are null for sync imported functions.\n wit_import_async_fn_t async_impl;\n wit_import_async_lift_fn_t async_lift_impl;\n\n size_t nparams;\n const wit_type_t *params;\n wit_type_t result;\n\n // Only meaningful if `async_impl` is non-null, otherwise\n // `async_abi_area_{size,align}` are set to zero.\n size_t async_abi_area_size;\n size_t async_abi_area_align;\n} wit_import_func_t;\n\ntypedef struct wit_export_func {\n const char *interface;\n const char *name;\n\n // For exported functions which are exported as `async` this is the\n // `task.return` intrinsic to invoke.\n //\n // This function takes a single parameter which is a `void *cx` which is\n // used when passing to `wit_dylib_pop_*` functions. This must be used\n // to indicate the return value of an async function.\n wit_export_task_return_fn_t task_return;\n\n size_t nparams;\n const wit_type_t *params;\n wit_type_t result;\n} wit_export_func_t;\n\ntypedef void(*wit_resource_drop_t)(uint32_t);\ntypedef uint32_t(*wit_resource_new_t)(size_t);\ntypedef size_t(*wit_resource_rep_t)(uint32_t);\n\ntypedef struct wit_resource {\n const char *interface;\n const char *name;\n wit_resource_drop_t drop;\n wit_resource_new_t new; // nullable\n wit_resource_rep_t rep; // nullable\n} wit_resource_t;\n\ntypedef struct wit_field {\n const char *name;\n wit_type_t ty;\n} wit_field_t;\n\ntypedef struct wit_record {\n const char *interface;\n const char *name;\n size_t nfields;\n const wit_field_t *fields;\n} wit_record_t;\n\ntypedef struct wit_flags {\n const char *interface;\n const char *name;\n size_t nnames;\n const char **names;\n} wit_flags_t;\n\ntypedef struct wit_tuple {\n const char *interface;\n const char *name;\n size_t ntypes;\n const wit_type_t *types;\n} wit_tuple_t;\n\ntypedef struct wit_case {\n const char *name;\n wit_type_t ty;\n} wit_case_t;\n\ntypedef struct wit_variant {\n const char *interface;\n const char *name;\n size_t ncases;\n const wit_case_t *cases;\n} wit_variant_t;\n\ntypedef struct wit_enum {\n const char *interface;\n const char *name;\n size_t nnames;\n const char **names;\n} wit_enum_t;\n\ntypedef struct wit_option {\n const char *interface;\n const char *name;\n wit_type_t ty;\n} wit_option_t;\n\ntypedef struct wit_result {\n const char *interface;\n const char *name;\n wit_type_t ok;\n wit_type_t err;\n} wit_result_t;\n\ntypedef struct wit_list {\n const char *interface;\n const char *name;\n wit_type_t ty;\n} wit_list_t;\n\ntypedef struct wit_fixed_length_list {\n const char *interface;\n const char *name;\n size_t size;\n wit_type_t ty;\n} wit_fixed_length_list_t;\n\ntypedef void(*wit_lift_fn_t)(void* cx, const void *buffer);\ntypedef void(*wit_lower_fn_t)(void* cx, void *buffer);\n\ntypedef uint64_t(*wit_future_new_fn_t)();\ntypedef uint32_t(*wit_future_read_fn_t)(uint32_t future, void *buffer);\ntypedef uint32_t(*wit_future_write_fn_t)(uint32_t future, const void *buffer);\ntypedef uint32_t(*wit_future_cancel_read_fn_t)(uint32_t future);\ntypedef uint32_t(*wit_future_cancel_write_fn_t)(uint32_t future);\ntypedef void(*wit_future_drop_readable_fn_t)(uint32_t future);\ntypedef void(*wit_future_drop_writable_fn_t)(uint32_t future);\n\ntypedef struct wit_future {\n const char *interface;\n const char *name;\n wit_type_t ty;\n wit_future_new_fn_t new;\n wit_future_read_fn_t read;\n wit_future_write_fn_t write;\n wit_future_cancel_read_fn_t cancel_read;\n wit_future_cancel_write_fn_t cancel_write;\n wit_future_drop_readable_fn_t drop_readable;\n wit_future_drop_writable_fn_t drop_writable;\n wit_lift_fn_t lift;\n wit_lower_fn_t lower;\n size_t abi_payload_size;\n size_t abi_payload_align;\n} wit_future_t;\n\ntypedef uint64_t(*wit_stream_new_fn_t)();\ntypedef uint32_t(*wit_stream_read_fn_t)(uint32_t stream, void *buffer, size_t count);\ntypedef uint32_t(*wit_stream_write_fn_t)(uint32_t stream, const void *buffer, size_t count);\ntypedef uint32_t(*wit_stream_cancel_read_fn_t)(uint32_t stream);\ntypedef uint32_t(*wit_stream_cancel_write_fn_t)(uint32_t stream);\ntypedef void(*wit_stream_drop_writable_fn_t)(uint32_t stream);\ntypedef void(*wit_stream_drop_readable_fn_t)(uint32_t stream);\n\ntypedef struct wit_stream {\n const char *interface;\n const char *name;\n wit_type_t ty;\n wit_stream_new_fn_t new;\n wit_stream_read_fn_t read;\n wit_stream_write_fn_t write;\n wit_stream_cancel_read_fn_t cancel_read;\n wit_stream_cancel_write_fn_t cancel_write;\n wit_stream_drop_readable_fn_t drop_readable;\n wit_stream_drop_writable_fn_t drop_writable;\n wit_lift_fn_t lift;\n wit_lower_fn_t lower;\n size_t abi_payload_size;\n size_t abi_payload_align;\n} wit_stream_t;\n\ntypedef struct wit_alias {\n const char *interface;\n const char *name;\n wit_type_t ty;\n} wit_alias_t;\n\n#define WIT_CURRENT_VERSION 2\n\ntypedef struct wit {\n uint32_t version; // `WIT_V*`\n\n size_t num_import_funcs;\n const wit_import_func_t *import_funcs;\n size_t num_export_funcs;\n const wit_export_func_t *export_funcs;\n size_t num_resources;\n const wit_resource_t *resources;\n size_t num_records;\n const wit_record_t *records;\n size_t num_flags;\n const wit_flags_t *flags;\n size_t num_tuples;\n const wit_tuple_t *tuples;\n size_t num_variants;\n const wit_variant_t *variants;\n size_t num_enums;\n const wit_enum_t *enums;\n size_t num_options;\n const wit_option_t *options;\n size_t num_results;\n const wit_result_t *results;\n size_t num_lists;\n const wit_list_t *lists;\n size_t num_fixed_length_lists;\n const wit_fixed_length_list_t *fixed_length_lists;\n size_t num_futures;\n const wit_future_t *futures;\n size_t num_streams;\n const wit_stream_t *streams;\n size_t num_aliases;\n const wit_alias_t *aliases;\n} wit_t;\n\n// Invoked during `__wasm_call_ctors` with an in-memory `wit_t` data structure.\n//\n// The pointer provided lives for the lifetime of the entire program so it\'s\n// safe to store this pointer.\nvoid wit_dylib_initialize(const wit_t* wit);\n\n// Generic byte deallocation function.\n//\n// This function will deallocate the `ptr` provided which was previously\n// allocated with `cabi_realloc` which has `byte_size` bytes and `align`\n// alignment.\n//\n// Note that if `defer` is set to `true` then this deallocation should happen\n// when `cx` is deallocated, not right now. If `defer` is set to `false` then\n// the deallocation can happen right now. The `defer` flag will be set\n// when a list is translated into the canonical ABI format when passing to an\n// import call or returning from an export. In this situation the deallocation\n// needs to happen after the ABI value is read, such as after the import call\n// or during post-return of the export.\nvoid wit_dylib_dealloc_bytes(void *cx, void *ptr, size_t byte_size, size_t align, bool defer);\n\n// Entrypoints for WIT exports.\n//\n// When an exported WIT function is called first `wit_dylib_export_start` will\n// be invoked with `which` as an index into the `wit_t` provided as part of\n// `wit_dylib_initialize`. The returned pointer is then passed as a contextual\n// argument to everything below.\n//\n// The `wit_dylib_export_call` function is invoked once arguments have all\n// been pushed into `cx`. The top of the stack of `cx` is the last argument of\n// the function invocation. Once the call returns the result is pulled out of\n// the stack of `cx` through the `*_pop_*` functions below.\n//\n// The `post-return` function will invoke `wit_dylib_export_finish` to clean up\n// any allocations or such.\nvoid *wit_dylib_export_start(size_t which);\nvoid wit_dylib_export_call(void *cx, size_t which);\nvoid wit_dylib_export_finish(void *cx, size_t which);\n\n// Entrypoint for WIT resource destructors.\n//\n// The `ty` points to `wit->resources` and `handle` is the value being\n// destroyed.\nvoid wit_dylib_resource_dtor(size_t ty, size_t handle);\n\n// =============================================================================\n// Converting between WIT and language types.\n//\n// The functions below are used for converting a component model WIT value to a\n// language\'s specific representation of a value. This is modeled as a\n// stack-machine of sorts within a `cx` argument passed around to all functions.\n// For example all \"push\" functions take a WIT value and push the\n// language-specific representation onto `cx`\'s internal stack. Composite types\n// such as records will both pop and push to the stack.\n\nvoid wit_dylib_push_bool(void *cx, bool val);\nvoid wit_dylib_push_char(void *cx, uint32_t val);\nvoid wit_dylib_push_u8(void *cx, uint8_t val);\nvoid wit_dylib_push_s8(void *cx, int8_t val);\nvoid wit_dylib_push_u16(void *cx, uint16_t val);\nvoid wit_dylib_push_s16(void *cx, int16_t val);\nvoid wit_dylib_push_u32(void *cx, uint32_t val);\nvoid wit_dylib_push_s32(void *cx, int32_t val);\nvoid wit_dylib_push_u64(void *cx, uint64_t val);\nvoid wit_dylib_push_s64(void *cx, int64_t val);\nvoid wit_dylib_push_f32(void *cx, float val);\nvoid wit_dylib_push_f64(void *cx, double val);\nvoid wit_dylib_push_flags(void *cx, size_t ty, uint32_t flags);\nvoid wit_dylib_push_enum(void *cx, size_t ty, uint32_t enum_);\nvoid wit_dylib_push_borrow(void *cx, size_t ty, uint32_t handle);\nvoid wit_dylib_push_own(void *cx, size_t ty, uint32_t handle);\nvoid wit_dylib_push_future(void *cx, size_t ty, uint32_t handle);\nvoid wit_dylib_push_stream(void *cx, size_t ty, uint32_t handle);\n// Note that `bytes` and `len` are allocated by `cabi_realloc` and thus this\n// function is required to take ownership of the values.\nvoid wit_dylib_push_string(void *cx, uint8_t *bytes, size_t len);\n// Records and tuples pop fields from the stack of `cx`. The top entry of the\n// stack is the last field, the next entry is the next-to-last field, and so on.\nvoid wit_dylib_push_record(void *cx, size_t ty);\nvoid wit_dylib_push_tuple(void *cx, size_t ty);\n// Variants (and options/results) have their payload, if necessary, on the\n// stack. If the `discr` case has a payload this needs to be popped. The end\n// result of this, the final language value, is pushed to the stack.\nvoid wit_dylib_push_option(void *cx, size_t ty, uint32_t discr);\nvoid wit_dylib_push_result(void *cx, size_t ty, uint32_t discr);\nvoid wit_dylib_push_variant(void *cx, size_t ty, uint32_t discr);\n// If this function returns 0 then it means that `bytes`/`len` need to be pushed\n// one-by-one. If a true value is returned then it\'s assume that `bytes` and\n// `len` is now owned by the engine.\n//\n// Note that `bytes` was allocated with `cabi_realloc` and thus represents an\n// owned allocation. This function takes ownership if a nonzero value is\n// returned, otherwise the generated bindings will clean it up.\n//\n// If this function returns false then a list with `len` capacity should be\n// pushed to the stack of `cx`. In this situation `wit_dylib_list_append` will\n// be called element-by-element to pop an element from the stack and then push\n// it onto the list which is then at the top of the stack.\nbool wit_dylib_push_list(void *cx, size_t ty, uint8_t *bytes, size_t len);\nvoid wit_dylib_list_append(void *cx, size_t ty);\n\nuint8_t wit_dylib_pop_u8(void *cx);\nuint16_t wit_dylib_pop_u16(void *cx);\nuint32_t wit_dylib_pop_u32(void *cx);\nuint64_t wit_dylib_pop_u64(void *cx);\nint8_t wit_dylib_pop_s8(void *cx);\nint16_t wit_dylib_pop_s16(void *cx);\nint32_t wit_dylib_pop_s32(void *cx);\nint64_t wit_dylib_pop_s64(void *cx);\nfloat wit_dylib_pop_f32(void *cx);\ndouble wit_dylib_pop_f64(void *cx);\nbool wit_dylib_pop_bool(void *cx);\nuint32_t wit_dylib_pop_char(void *cx);\nuint32_t wit_dylib_pop_borrow(void *cx, size_t ty);\nuint32_t wit_dylib_pop_own(void *cx, size_t ty);\nuint32_t wit_dylib_pop_enum(void *cx, size_t ty);\nuint32_t wit_dylib_pop_flags(void *cx, size_t ty);\nuint32_t wit_dylib_pop_future(void *cx, size_t ty);\nuint32_t wit_dylib_pop_stream(void *cx, size_t ty);\n// When popping a string from the stack the `ptr` argument should be set to the\n// location of the string in memory, and the `size_t` return value is the byte\n// length of the string.\nsize_t wit_dylib_pop_string(void *cx, char **ptr);\n// When popping a variant from the stack the language value is first removed.\n// The discriminant of the value is returned, and if there is a payload for the\n// case then it\'s pushed back onto the stack.\nuint32_t wit_dylib_pop_option(void *cx, size_t type_index);\nuint32_t wit_dylib_pop_result(void *cx, size_t type_index);\nuint32_t wit_dylib_pop_variant(void *cx, size_t type_index);\n// When a record or tuple is popped that means that the language\'s\n// representation is being destructured. This pops a language value from the\n// stack and then pushes the fields back to the stack. The last field should be\n// pushed first meaning that the top entry of the stack after this is the first\n// field of the record or tuple.\nvoid wit_dylib_pop_record(void *cx, size_t ty);\nvoid wit_dylib_pop_tuple(void *cx, size_t ty);\n// When a list is popped from the stack the engine returns the pointer/length\n// through this intrinsic.\n//\n// Note that `ptr` must be written in this function, and `NULL` has a special\n// value. Regardless the return value of this function is the element length of\n// the list that was popped from the stack.\n//\n// If this function returns a non-null pointer through the `ptr` field that\n// means that the data is already in canonical ABI format (for example\n// `list<u8>` is just a sequential list of bytes). In this situation the\n// list should be popped from the stack and popping will continue with any\n// further values from here.\n//\n// If this function returns a null pointer through the `ptr` field then it\n// means that the data for this list is not in the canonical ABI format. That\n// means that it\'s required to translate elements one-by-one. In this situation\n// the list is popped from the stack and an iterator over the list is pushed\n// to the stack. Calls to `wit_dylib_pop_iter_next` are used to then extract\n// a single element from the iterator and push it onto the stack. Once\n// translation of the list is finished `wit_dylib_pop_iter` will be used to\n// remove the iterator from the stack.\nsize_t wit_dylib_pop_list(void *cx, size_t ty, void **ptr);\nvoid wit_dylib_pop_iter_next(void *cx, size_t ty);\nvoid wit_dylib_pop_iter(void *cx, size_t ty);\n\n#endif\n";