Skip to content

Commit 5266116

Browse files
committed
Merge remote-tracking branch 'origin/master' into feature/unified-exceptions
2 parents dc21636 + 328d538 commit 5266116

File tree

8 files changed

+814
-144
lines changed

8 files changed

+814
-144
lines changed

lib/runtime-c-api/src/error.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ pub(crate) fn take_last_error() -> Option<Box<dyn Error>> {
2222
LAST_ERROR.with(|prev| prev.borrow_mut().take())
2323
}
2424

25-
/// Gets the length in bytes of the last error.
25+
/// Gets the length in bytes of the last error if any.
26+
///
2627
/// This can be used to dynamically allocate a buffer with the correct number of
2728
/// bytes needed to store a message.
2829
///
29-
/// # Example
30-
///
31-
/// ```c
32-
/// int error_len = wasmer_last_error_length();
33-
/// char *error_str = malloc(error_len);
34-
/// ```
30+
/// See `wasmer_last_error_message()` to get a full example.
3531
#[no_mangle]
3632
pub extern "C" fn wasmer_last_error_length() -> c_int {
3733
LAST_ERROR.with(|prev| match *prev.borrow() {
@@ -40,19 +36,33 @@ pub extern "C" fn wasmer_last_error_length() -> c_int {
4036
})
4137
}
4238

43-
/// Stores the last error message into the provided buffer up to the given `length`.
44-
/// The `length` parameter must be large enough to store the last error message.
39+
/// Gets the last error message if any into the provided buffer
40+
/// `buffer` up to the given `length`.
4541
///
46-
/// Returns the length of the string in bytes.
47-
/// Returns `-1` if an error occurs.
42+
/// The `length` parameter must be large enough to store the last
43+
/// error message. Ideally, the value should come from
44+
/// `wasmer_last_error_length()`.
4845
///
49-
/// # Example
46+
/// The function returns the length of the string in bytes, `-1` if an
47+
/// error occurs. Potential errors are:
48+
///
49+
/// * The buffer is a null pointer,
50+
/// * The buffer is too smal to hold the error message.
51+
///
52+
/// Note: The error message always has a trailing null character.
53+
///
54+
/// Example:
5055
///
5156
/// ```c
52-
/// int error_len = wasmer_last_error_length();
53-
/// char *error_str = malloc(error_len);
54-
/// wasmer_last_error_message(error_str, error_len);
55-
/// printf("Error str: `%s`\n", error_str);
57+
/// int error_length = wasmer_last_error_length();
58+
///
59+
/// if (error_length > 0) {
60+
/// char *error_message = malloc(error_length);
61+
/// wasmer_last_error_message(error_message, error_length);
62+
/// printf("Error message: `%s`\n", error_message);
63+
/// } else {
64+
/// printf("No error message\n");
65+
/// }
5666
/// ```
5767
#[no_mangle]
5868
pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length: c_int) -> c_int {

lib/runtime-c-api/src/export.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ pub struct wasmer_export_func_t;
4343
/// exposed to C.
4444
pub(crate) struct NamedExports(pub Vec<NamedExport>);
4545

46-
/// Opaque pointer to `NamedExports`.
46+
/// Opaque pointer to the opaque structure `crate::NamedExports`,
47+
/// which is a wrapper around a vector of the opaque structure
48+
/// `crate::NamedExport`.
49+
///
50+
/// Check the `wasmer_instance_exports()` function to learn more.
4751
#[repr(C)]
4852
#[derive(Clone)]
4953
pub struct wasmer_exports_t;
@@ -91,9 +95,16 @@ pub union wasmer_import_export_value {
9195
// ================
9296
// Do not modify these values without updating the `TryFrom` implementation below
9397
pub enum wasmer_import_export_kind {
98+
/// The export/import is a function.
9499
WASM_FUNCTION = 0,
100+
101+
/// The export/import is a global.
95102
WASM_GLOBAL = 1,
103+
104+
/// The export/import is a memory.
96105
WASM_MEMORY = 2,
106+
107+
/// The export/import is a table.
97108
WASM_TABLE = 3,
98109
}
99110

@@ -201,7 +212,23 @@ pub unsafe extern "C" fn wasmer_export_descriptor_kind(
201212
named_export_descriptor.kind.clone()
202213
}
203214

204-
/// Frees the memory for the given exports
215+
/// Frees the memory for the given exports.
216+
///
217+
/// Check the `wasmer_instance_exports()` function to get a complete
218+
/// example.
219+
///
220+
/// If `exports` is a null pointer, this function does nothing.
221+
///
222+
/// Example:
223+
///
224+
/// ```c
225+
/// // Get some exports.
226+
/// wasmer_exports_t *exports = NULL;
227+
/// wasmer_instance_exports(instance, &exports);
228+
///
229+
/// // Destroy the exports.
230+
/// wasmer_exports_destroy(exports);
231+
/// ```
205232
#[allow(clippy::cast_ptr_alignment)]
206233
#[no_mangle]
207234
pub extern "C" fn wasmer_exports_destroy(exports: *mut wasmer_exports_t) {

0 commit comments

Comments
 (0)