@@ -6,14 +6,14 @@ use crate::{
6
6
ssa:: {
7
7
acir_gen:: { acir_mem:: AcirMem , internal_var_cache:: InternalVarCache , InternalVar } ,
8
8
context:: SsaContext ,
9
- mem:: { self , ArrayId } ,
9
+ mem:: { self , ArrayId , MemArray } ,
10
10
node:: NodeId ,
11
11
} ,
12
12
Evaluator ,
13
13
} ;
14
14
15
15
// Returns a variable corresponding to the element at the provided index in the array
16
- // Returns None if index is constant and out-of-bound.
16
+ // Returns an error if index is constant and out-of-bound.
17
17
pub ( crate ) fn evaluate (
18
18
array_id : ArrayId ,
19
19
index : NodeId ,
@@ -25,11 +25,21 @@ pub(crate) fn evaluate(
25
25
) -> Result < InternalVar , RuntimeError > {
26
26
let mem_array = & ctx. mem [ array_id] ;
27
27
let index = var_cache. get_or_compute_internal_var_unwrap ( index, evaluator, ctx) ;
28
+ evaluate_with ( mem_array, & index, acir_mem, location, evaluator)
29
+ }
28
30
31
+ // Same as evaluate(), but using MemArray and InternalVar instead of ArrayId and NodeId
32
+ pub ( crate ) fn evaluate_with (
33
+ array : & MemArray ,
34
+ index : & InternalVar ,
35
+ acir_mem : & mut AcirMem ,
36
+ location : Option < Location > ,
37
+ evaluator : & mut Evaluator ,
38
+ ) -> Result < InternalVar , RuntimeError > {
29
39
if let Some ( idx) = index. to_const ( ) {
30
40
let idx = mem:: Memory :: as_u32 ( idx) ;
31
41
// Check to see if the index has gone out of bounds
32
- let array_length = mem_array . len ;
42
+ let array_length = array . len ;
33
43
if idx >= array_length {
34
44
return Err ( RuntimeError {
35
45
location,
@@ -40,13 +50,13 @@ pub(crate) fn evaluate(
40
50
} ) ;
41
51
}
42
52
43
- let array_element = acir_mem. load_array_element_constant_index ( mem_array , idx) ;
53
+ let array_element = acir_mem. load_array_element_constant_index ( array , idx) ;
44
54
if let Some ( element) = array_element {
45
55
return Ok ( element) ;
46
56
}
47
57
}
48
58
49
59
let w = evaluator. add_witness_to_cs ( ) ;
50
- acir_mem. add_to_trace ( & array_id , index. to_expression ( ) , w. into ( ) , Expression :: zero ( ) ) ;
60
+ acir_mem. add_to_trace ( & array . id , index. to_expression ( ) , w. into ( ) , Expression :: zero ( ) ) ;
51
61
Ok ( InternalVar :: from_witness ( w) )
52
62
}
0 commit comments