doc(runtime-c-api) Improve error functions' documentations.

This commit is contained in:
Ivan Enderlin
2020-01-20 14:23:52 +01:00
parent 9b483d3fe8
commit 384c501c93

View File

@ -22,16 +22,12 @@ pub(crate) fn take_last_error() -> Option<Box<dyn Error>> {
LAST_ERROR.with(|prev| prev.borrow_mut().take()) LAST_ERROR.with(|prev| prev.borrow_mut().take())
} }
/// Gets the length in bytes of the last error. /// Gets the length in bytes of the last error if any.
///
/// This can be used to dynamically allocate a buffer with the correct number of /// This can be used to dynamically allocate a buffer with the correct number of
/// bytes needed to store a message. /// bytes needed to store a message.
/// ///
/// # Example /// See `wasmer_last_error_message()` to get a full example.
///
/// ```c
/// int error_len = wasmer_last_error_length();
/// char *error_str = malloc(error_len);
/// ```
#[no_mangle] #[no_mangle]
pub extern "C" fn wasmer_last_error_length() -> c_int { pub extern "C" fn wasmer_last_error_length() -> c_int {
LAST_ERROR.with(|prev| match *prev.borrow() { LAST_ERROR.with(|prev| match *prev.borrow() {
@ -40,19 +36,33 @@ pub extern "C" fn wasmer_last_error_length() -> c_int {
}) })
} }
/// Stores the last error message into the provided buffer up to the given `length`. /// Gets the last error message if any into the provided buffer
/// The `length` parameter must be large enough to store the last error message. /// `buffer` up to the given `length`.
/// ///
/// Returns the length of the string in bytes. /// The `length` parameter must be large enough to store the last
/// Returns `-1` if an error occurs. /// error message. Ideally, the value should come from
/// `wasmer_last_error_length()`.
/// ///
/// # Example /// The function returns the length of the string in bytes, `-1` if an
/// error occurs. Potential errors are:
///
/// * The buffer is a null pointer,
/// * The buffer is too smal to hold the error message.
///
/// Note: The error message always has a trailing null character.
///
/// Example:
/// ///
/// ```c /// ```c
/// int error_len = wasmer_last_error_length(); /// int error_length = wasmer_last_error_length();
/// char *error_str = malloc(error_len); ///
/// wasmer_last_error_message(error_str, error_len); /// if (error_length > 0) {
/// printf("Error str: `%s`\n", error_str); /// char *error_message = malloc(error_length);
/// wasmer_last_error_message(error_message, error_length);
/// printf("Error message: `%s`\n", error_message);
/// } else {
/// printf("No error message\n");
/// }
/// ``` /// ```
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length: c_int) -> c_int { pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length: c_int) -> c_int {