@@ -3,10 +3,9 @@ use acvm::{
3
3
pwg:: ForeignCallWaitInfo ,
4
4
} ;
5
5
use iter_extended:: vecmap;
6
- use noirc_abi:: { decode_string_value, input_parser:: InputValueDisplay , AbiType } ;
7
- use regex:: { Captures , Regex } ;
6
+ use noirc_printable_type:: PrintableValueDisplay ;
8
7
9
- use crate :: errors :: ForeignCallError ;
8
+ use crate :: NargoError ;
10
9
11
10
/// This enumeration represents the Brillig foreign calls that are natively supported by nargo.
12
11
/// After resolution of a foreign call, nargo will restart execution of the ACVM
@@ -43,7 +42,7 @@ impl ForeignCall {
43
42
pub ( crate ) fn execute (
44
43
foreign_call : & ForeignCallWaitInfo ,
45
44
show_output : bool ,
46
- ) -> Result < ForeignCallResult , ForeignCallError > {
45
+ ) -> Result < ForeignCallResult , NargoError > {
47
46
let foreign_call_name = foreign_call. function . as_str ( ) ;
48
47
match Self :: lookup ( foreign_call_name) {
49
48
Some ( ForeignCall :: Println ) => {
@@ -78,90 +77,9 @@ impl ForeignCall {
78
77
}
79
78
}
80
79
81
- fn execute_println ( foreign_call_inputs : & [ Vec < Value > ] ) -> Result < ( ) , ForeignCallError > {
82
- let ( is_fmt_str, foreign_call_inputs) =
83
- foreign_call_inputs. split_last ( ) . ok_or ( ForeignCallError :: MissingForeignCallInputs ) ?;
84
-
85
- let output_string = if is_fmt_str[ 0 ] . to_field ( ) . is_one ( ) {
86
- convert_fmt_string_inputs ( foreign_call_inputs) ?
87
- } else {
88
- convert_string_inputs ( foreign_call_inputs) ?
89
- } ;
90
- println ! ( "{output_string}" ) ;
80
+ fn execute_println ( foreign_call_inputs : & [ Vec < Value > ] ) -> Result < ( ) , NargoError > {
81
+ let display_values: PrintableValueDisplay = foreign_call_inputs. try_into ( ) ?;
82
+ println ! ( "{display_values}" ) ;
91
83
Ok ( ( ) )
92
84
}
93
85
}
94
-
95
- fn convert_string_inputs ( foreign_call_inputs : & [ Vec < Value > ] ) -> Result < String , ForeignCallError > {
96
- // Fetch the abi type from the foreign call input
97
- // The remaining input values should hold what is to be printed
98
- let ( abi_type_as_values, input_values) =
99
- foreign_call_inputs. split_last ( ) . ok_or ( ForeignCallError :: MissingForeignCallInputs ) ?;
100
- let abi_type = fetch_abi_type ( abi_type_as_values) ?;
101
-
102
- // We must use a flat map here as each value in a struct will be in a separate input value
103
- let mut input_values_as_fields =
104
- input_values. iter ( ) . flat_map ( |values| vecmap ( values, |value| value. to_field ( ) ) ) ;
105
-
106
- let input_value_display =
107
- InputValueDisplay :: try_from_fields ( & mut input_values_as_fields, abi_type) ?;
108
-
109
- Ok ( input_value_display. to_string ( ) )
110
- }
111
-
112
- fn convert_fmt_string_inputs (
113
- foreign_call_inputs : & [ Vec < Value > ] ,
114
- ) -> Result < String , ForeignCallError > {
115
- let ( message_as_values, input_and_abi_values) =
116
- foreign_call_inputs. split_first ( ) . ok_or ( ForeignCallError :: MissingForeignCallInputs ) ?;
117
-
118
- let message_as_fields = vecmap ( message_as_values, |value| value. to_field ( ) ) ;
119
- let message_as_string = decode_string_value ( & message_as_fields) ;
120
-
121
- let ( num_values, input_and_abi_values) =
122
- input_and_abi_values. split_first ( ) . ok_or ( ForeignCallError :: MissingForeignCallInputs ) ?;
123
-
124
- let mut output_strings = Vec :: new ( ) ;
125
- let num_values = num_values[ 0 ] . to_field ( ) . to_u128 ( ) as usize ;
126
-
127
- let mut abi_types = Vec :: new ( ) ;
128
- for abi_values in input_and_abi_values. iter ( ) . skip ( input_and_abi_values. len ( ) - num_values) {
129
- let abi_type = fetch_abi_type ( abi_values) ?;
130
- abi_types. push ( abi_type) ;
131
- }
132
-
133
- for i in 0 ..num_values {
134
- let abi_type = & abi_types[ i] ;
135
- let type_size = abi_type. field_count ( ) as usize ;
136
-
137
- let mut input_values_as_fields = input_and_abi_values[ i..( i + type_size) ]
138
- . iter ( )
139
- . flat_map ( |values| vecmap ( values, |value| value. to_field ( ) ) ) ;
140
-
141
- let input_value_display =
142
- InputValueDisplay :: try_from_fields ( & mut input_values_as_fields, abi_type. clone ( ) ) ?;
143
-
144
- output_strings. push ( input_value_display. to_string ( ) ) ;
145
- }
146
-
147
- let mut output_strings_iter = output_strings. into_iter ( ) ;
148
- let re = Regex :: new ( r"\{([a-zA-Z0-9_]+)\}" )
149
- . expect ( "ICE: an invalid regex pattern was used for checking format strings" ) ;
150
-
151
- let formatted_str = re. replace_all ( & message_as_string, |_: & Captures | {
152
- output_strings_iter
153
- . next ( )
154
- . expect ( "ICE: there are more regex matches than fields supplied to the format string" )
155
- } ) ;
156
-
157
- Ok ( formatted_str. into_owned ( ) )
158
- }
159
-
160
- fn fetch_abi_type ( abi_type_as_values : & [ Value ] ) -> Result < AbiType , ForeignCallError > {
161
- let abi_type_as_fields = vecmap ( abi_type_as_values, |value| value. to_field ( ) ) ;
162
- let abi_type_as_string = decode_string_value ( & abi_type_as_fields) ;
163
- let abi_type: AbiType = serde_json:: from_str ( & abi_type_as_string)
164
- . map_err ( |err| ForeignCallError :: InputParserError ( err. into ( ) ) ) ?;
165
-
166
- Ok ( abi_type)
167
- }
0 commit comments