@@ -22,16 +22,12 @@ pub(crate) fn take_last_error() -> Option<Box<dyn Error>> {
22
22
LAST_ERROR . with ( |prev| prev. borrow_mut ( ) . take ( ) )
23
23
}
24
24
25
- /// Gets the length in bytes of the last error.
25
+ /// Gets the length in bytes of the last error if any.
26
+ ///
26
27
/// This can be used to dynamically allocate a buffer with the correct number of
27
28
/// bytes needed to store a message.
28
29
///
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.
35
31
#[ no_mangle]
36
32
pub extern "C" fn wasmer_last_error_length ( ) -> c_int {
37
33
LAST_ERROR . with ( |prev| match * prev. borrow ( ) {
@@ -40,19 +36,33 @@ pub extern "C" fn wasmer_last_error_length() -> c_int {
40
36
} )
41
37
}
42
38
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` .
45
41
///
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()`.
48
45
///
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:
50
55
///
51
56
/// ```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
+ /// }
56
66
/// ```
57
67
#[ no_mangle]
58
68
pub unsafe extern "C" fn wasmer_last_error_message ( buffer : * mut c_char , length : c_int ) -> c_int {
0 commit comments