Skip to content

Commit

Permalink
doc(runtime-c-api) Improve documentation of wasmer_instance_exports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Jan 20, 2020
1 parent 3efd7d1 commit 19627bd
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions lib/runtime-c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,48 @@ pub unsafe extern "C" fn wasmer_instance_call(
}
}

/// Gets Exports for the given instance
/// Gets all the exports of the given WebAssembly instance.
///
/// The caller owns the object and should call `wasmer_exports_destroy` to free it.
/// This function stores a Rust vector of exports into `exports` as an
/// opaque pointer of kind `wasmer_exports_t`.
///
/// As is, you can do anything with `exports` except using the
/// companion functions, like `wasmer_exports_len()`,
/// `wasmer_exports_get()` or `wasmer_export_kind()`. See the example below.
///
/// **Warning**: The caller owns the object and should call
/// `wasmer_exports_destroy()` to free it.
///
/// Example:
///
/// ```c
/// // Get the exports.
/// wasmer_exports_t *exports = NULL;
/// wasmer_instance_exports(instance, &exports);
///
/// // Get the number of exports.
/// int exports_length = wasmer_exports_len(exports);
/// printf("Number of exports: %d\n", exports_length);
///
/// // Read the first export.
/// wasmer_export_t *export = wasmer_exports_get(exports, 0);
///
/// // Get the kind of the export.
/// wasmer_import_export_kind export_kind = wasmer_export_kind(export);
///
/// // Assert it is a function (why not).
/// assert(export_kind == WASM_FUNCTION);
///
/// // Read the export name.
/// wasmer_byte_array name_bytes = wasmer_export_name(export);
///
/// assert(name_bytes.bytes_len == sizeof("sum") - 1);
/// assert(memcmp(name_bytes.bytes, "sum", sizeof("sum") - 1) == 0);
///
/// // Destroy the exports.
/// wasmer_exports_destroy(exports);
/// ```
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_instance_exports(
Expand All @@ -335,14 +374,17 @@ pub unsafe extern "C" fn wasmer_instance_exports(
) {
let instance_ref = &mut *(instance as *mut Instance);
let mut exports_vec: Vec<NamedExport> = Vec::with_capacity(instance_ref.exports().count());

for (name, export) in instance_ref.exports() {
exports_vec.push(NamedExport {
name: name.clone(),
export: export.clone(),
instance: instance as *mut Instance,
});
}

let named_exports: Box<NamedExports> = Box::new(NamedExports(exports_vec));

*exports = Box::into_raw(named_exports) as *mut wasmer_exports_t;
}

Expand Down

0 comments on commit 19627bd

Please sign in to comment.