@@ -7803,6 +7803,15 @@ mod error_mapping {
7803
7803
/// usually triggered by `(unwrap!...)` and `(unwrap-err!...)`.
7804
7804
ShortReturnExpectedValue = 12 ,
7805
7805
7806
+ /// Indicates an attempt to use a function with the wrong amount of arguments
7807
+ ArgumentCountMismatch = 13 ,
7808
+
7809
+ /// Indicates an attempt to use a function with too few arguments
7810
+ ArgumentCountAtLeast = 14 ,
7811
+
7812
+ /// Indicates an attempt to use a function with too many arguments
7813
+ ArgumentCountAtMost = 15 ,
7814
+
7806
7815
/// A catch-all for errors that are not mapped to specific error codes.
7807
7816
/// This might be used for unexpected or unclassified errors.
7808
7817
NotMapped = 99 ,
@@ -7825,6 +7834,9 @@ mod error_mapping {
7825
7834
10 => ErrorMap :: ShortReturnExpectedValueResponse ,
7826
7835
11 => ErrorMap :: ShortReturnExpectedValueOptional ,
7827
7836
12 => ErrorMap :: ShortReturnExpectedValue ,
7837
+ 13 => ErrorMap :: ArgumentCountMismatch ,
7838
+ 14 => ErrorMap :: ArgumentCountAtLeast ,
7839
+ 15 => ErrorMap :: ArgumentCountAtMost ,
7828
7840
_ => ErrorMap :: NotMapped ,
7829
7841
}
7830
7842
}
@@ -7948,8 +7960,7 @@ mod error_mapping {
7948
7960
Error :: Runtime ( RuntimeErrorType :: UnwrapFailure , Some ( Vec :: new ( ) ) )
7949
7961
}
7950
7962
ErrorMap :: ShortReturnAssertionFailure => {
7951
- let clarity_val =
7952
- short_return_value ( & instance, & mut store, epoch_id, clarity_version) ;
7963
+ let clarity_val = short_return_value ( & instance, & mut store, epoch_id, clarity_version) ;
7953
7964
Error :: ShortReturn ( ShortReturnType :: AssertionFailed ( clarity_val) )
7954
7965
}
7955
7966
ErrorMap :: ArithmeticPowError => Error :: Runtime (
@@ -7976,23 +7987,35 @@ mod error_mapping {
7976
7987
Error :: Unchecked ( CheckErrors :: NameAlreadyUsed ( arg_name) )
7977
7988
}
7978
7989
ErrorMap :: ShortReturnExpectedValueResponse => {
7979
- let clarity_val =
7980
- short_return_value ( & instance, & mut store, epoch_id, clarity_version) ;
7990
+ let clarity_val = short_return_value ( & instance, & mut store, epoch_id, clarity_version) ;
7981
7991
Error :: ShortReturn ( ShortReturnType :: ExpectedValue ( Value :: Response (
7982
7992
ResponseData {
7983
7993
committed : false ,
7984
7994
data : Box :: new ( clarity_val) ,
7985
7995
} ,
7986
7996
) ) )
7987
7997
}
7988
- ErrorMap :: ShortReturnExpectedValueOptional => Error :: ShortReturn (
7989
- ShortReturnType :: ExpectedValue ( Value :: Optional ( OptionalData { data : None } ) ) ,
7990
- ) ,
7998
+ ErrorMap :: ShortReturnExpectedValueOptional => {
7999
+ Error :: ShortReturn ( ShortReturnType :: ExpectedValue ( Value :: Optional (
8000
+ clarity:: vm:: types:: OptionalData { data : None } ,
8001
+ ) ) )
8002
+ }
7991
8003
ErrorMap :: ShortReturnExpectedValue => {
7992
- let clarity_val =
7993
- short_return_value ( & instance, & mut store, epoch_id, clarity_version) ;
8004
+ let clarity_val = short_return_value ( & instance, & mut store, epoch_id, clarity_version) ;
7994
8005
Error :: ShortReturn ( ShortReturnType :: ExpectedValue ( clarity_val) )
7995
8006
}
8007
+ ErrorMap :: ArgumentCountMismatch => {
8008
+ let ( expected, got) = get_runtime_error_arg_lengths ( & instance, & mut store) ;
8009
+ Error :: Unchecked ( CheckErrors :: IncorrectArgumentCount ( expected, got) )
8010
+ }
8011
+ ErrorMap :: ArgumentCountAtLeast => {
8012
+ let ( expected, got) = get_runtime_error_arg_lengths ( & instance, & mut store) ;
8013
+ Error :: Unchecked ( CheckErrors :: RequiresAtLeastArguments ( expected, got) )
8014
+ }
8015
+ ErrorMap :: ArgumentCountAtMost => {
8016
+ let ( expected, got) = get_runtime_error_arg_lengths ( & instance, & mut store) ;
8017
+ Error :: Unchecked ( CheckErrors :: RequiresAtMostArguments ( expected, got) )
8018
+ }
7996
8019
_ => panic ! ( "Runtime error code {} not supported" , runtime_error_code) ,
7997
8020
}
7998
8021
}
@@ -8014,6 +8037,28 @@ mod error_mapping {
8014
8037
. unwrap_or_else ( || panic ! ( "Could not find ${} global with i32 value" , name) )
8015
8038
}
8016
8039
8040
+ /// Retrieves the expected and actual argument counts from a byte-encoded string.
8041
+ ///
8042
+ /// This function interprets a string as a sequence of bytes, where the first 4 bytes
8043
+ /// represent the expected number of arguments, and the bytes at positions 16 to 19
8044
+ /// represent the actual number of arguments received. It converts these byte sequences
8045
+ /// into `usize` values and returns them as a tuple.
8046
+ ///
8047
+ /// # Returns
8048
+ ///
8049
+ /// A tuple `(expected, got)` where:
8050
+ /// - `expected` is the number of arguments expected.
8051
+ /// - `got` is the number of arguments actually received.
8052
+ fn extract_expected_and_got ( bytes : & [ u8 ] ) -> ( usize , usize ) {
8053
+ // Assuming the first 4 bytes represent the expected value
8054
+ let expected = u32:: from_le_bytes ( [ bytes[ 0 ] , bytes[ 1 ] , bytes[ 2 ] , bytes[ 3 ] ] ) as usize ;
8055
+
8056
+ // Assuming the next 4 bytes represent the got value
8057
+ let got = u32:: from_le_bytes ( [ bytes[ 4 ] , bytes[ 5 ] , bytes[ 6 ] , bytes[ 7 ] ] ) as usize ;
8058
+
8059
+ ( expected, got)
8060
+ }
8061
+
8017
8062
/// Retrieves and deserializes a Clarity value from WebAssembly memory in the context of a short return.
8018
8063
///
8019
8064
/// This function is used to extract a Clarity value that has been stored in WebAssembly memory
@@ -8047,4 +8092,33 @@ mod error_mapping {
8047
8092
read_from_wasm_indirect ( memory, store, & value_ty, val_offset, * epoch_id)
8048
8093
. unwrap_or_else ( |e| panic ! ( "Could not read thrown value from memory: {}" , e) )
8049
8094
}
8095
+
8096
+ /// Retrieves the argument lengths from the runtime error global variables.
8097
+ ///
8098
+ /// This function reads the global variables `runtime-error-arg-offset` and `runtime-error-arg-len`
8099
+ /// from the WebAssembly instance and constructs a string representing the argument lengths.
8100
+ ///
8101
+ /// # Returns
8102
+ ///
8103
+ /// A string representing the argument lengths.
8104
+ fn get_runtime_error_arg_lengths (
8105
+ instance : & Instance ,
8106
+ store : & mut impl AsContextMut ,
8107
+ ) -> ( usize , usize ) {
8108
+ let runtime_error_arg_offset = get_global_i32 ( instance, store, "runtime-error-arg-offset" ) ;
8109
+ let runtime_error_arg_len = get_global_i32 ( instance, store, "runtime-error-arg-len" ) ;
8110
+
8111
+ let memory = instance
8112
+ . get_memory ( & mut * store, "memory" )
8113
+ . unwrap_or_else ( || panic ! ( "Could not find wasm instance memory" ) ) ;
8114
+ let arg_lengths = read_bytes_from_wasm (
8115
+ memory,
8116
+ store,
8117
+ runtime_error_arg_offset,
8118
+ runtime_error_arg_len,
8119
+ )
8120
+ . unwrap_or_else ( |e| panic ! ( "Could not recover arg_lengths: {e}" ) ) ;
8121
+
8122
+ extract_expected_and_got ( & arg_lengths)
8123
+ }
8050
8124
}
0 commit comments