Skip to content

Commit 0064c42

Browse files
committed
Implemented runtime remove and insert functions for dynamic arrays in move and added runtime test
1 parent 7dafdad commit 0064c42

File tree

7 files changed

+93
-2
lines changed

7 files changed

+93
-2
lines changed

src/moveir/call.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::ir::{MoveIRExpression, MoveIRFunctionCall};
55
use crate::ast::calls::FunctionArgument;
66
use crate::ast::{Expression, ExternalCall, FunctionCall, Identifier};
77
use crate::environment::{CallableInformation, FunctionCallMatchResult};
8+
use crate::moveir::MovePosition;
89

910
pub(crate) struct MoveExternalCall {
1011
pub external_call: ExternalCall,

src/moveir/identifier.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ impl MoveIdentifier {
6161
if let Some(identifier_type) = function_context
6262
.scope_context
6363
.type_for(&self.identifier.token)
64-
{
64+
{
6565
if identifier_type.is_currency_type(&libra::currency()) && f_call {
6666
return MoveIRExpression::Transfer(MoveIRTransfer::Move(Box::from(ir_identifier)));
6767
}
6868
if identifier_type.is_currency_type(&libra::currency()) {
6969
return ir_identifier;
7070
}
71+
7172
if identifier_type.is_inout_type() && identifier_type.is_user_defined_type() {
7273
if f_call {
7374
return MoveIRExpression::Transfer(MoveIRTransfer::Move(Box::from(

src/moveir/preprocessor/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,14 @@ impl Visitor for MovePreProcessor {
956956
MovePreProcessor::CALLER_PROTECTIONS_PARAM,
957957
));
958958
}
959+
960+
if call.identifier.token.eq("Flint_array_remove") {
961+
// TODO: change to element type
962+
call.identifier.token = "Flint_array_remove<u64>".to_string();
963+
} else if call.identifier.token.eq("Flint_array_insert") {
964+
call.identifier.token = "Flint_array_insert<u64>".to_string();
965+
}
966+
959967
return Ok(());
960968
}
961969

@@ -1229,7 +1237,7 @@ impl Visitor for MovePreProcessor {
12291237
) -> VResult {
12301238
let mut borrow_local = false;
12311239
let function_argument = _t.clone();
1232-
let mut expression;
1240+
let mut expression = function_argument.expression.clone();
12331241
if let Expression::InoutExpression(i) = function_argument.expression.clone() {
12341242
expression = *i.expression;
12351243

@@ -1250,6 +1258,11 @@ impl Visitor for MovePreProcessor {
12501258
&scope,
12511259
);
12521260

1261+
if let Type::ArrayType(_) = expression_type {
1262+
_t.expression = expression;
1263+
return Ok(());
1264+
}
1265+
12531266
if !expression_type.is_currency_type(&_ctx.target.currency)
12541267
&& !expression_type.is_external_resource(_ctx.environment.clone())
12551268
{
@@ -1284,6 +1297,7 @@ impl Visitor for MovePreProcessor {
12841297
}
12851298

12861299
_t.expression = expression;
1300+
dbg!(_t.clone());
12871301
Ok(())
12881302
}
12891303

src/moveir/preprocessor/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ pub fn pre_assign(
642642
}
643643
};
644644

645+
dbg!(declaration.clone());
646+
dbg!(expression.clone());
645647
if struct_is_mutable_reference(&mut expression, &temp_identifier, ctx) {
646648
ctx.pre_statements
647649
.push(Statement::Expression(Expression::BinaryExpression(

src/moveir/runtime_function.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,34 @@ impl MoveRuntimeFunction {
8888
8989
return;
9090
}
91+
92+
public Flint_array_remove<S: copyable>(nums: &mut vector<S>, i: u64) {
93+
let result: S;
94+
result = Vector.remove<S>(move(nums), move(i));
95+
_ = move(result);
96+
return;
97+
}
98+
99+
public Flint_array_length<S: copyable>(nums: vector<S>): u64 {
100+
let length: u64;
101+
length = Vector.length<S>(&nums);
102+
return move(length);
103+
}
104+
105+
public Flint_array_insert<S: copyable>(nums: &mut vector<S>, i: u64, value: S) {
106+
let length: u64;
107+
let index: u64;
108+
index = move(i);
109+
Vector.push_back<S>(copy(nums), copy(value));
110+
length = Self.Flint_array_length<S>(*copy(nums));
111+
112+
while(copy(index) < copy(length) - 1) {
113+
Vector.swap<S>(copy(nums), copy(index), copy(length) - 1);
114+
index = copy(index) + 1;
115+
}
116+
117+
return;
118+
}
91119
"
92120
.to_string()
93121
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
contract DynamicArrays {
2+
var nums: [Int] = [1, 2, 3]
3+
}
4+
5+
DynamicArrays :: (any) {
6+
public init() {}
7+
8+
public func get(x: Int) -> Int {
9+
return nums[x]
10+
}
11+
12+
public func set(i: Int, val: Int) mutates(nums) {
13+
nums[i] = val
14+
}
15+
16+
public func insert(i: Int, val: Int) mutates(nums) {
17+
Flint_array_insert(&nums, i, val)
18+
}
19+
20+
public func remove(i: Int) mutates(nums) {
21+
Flint_array_remove(&nums, i)
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {{default}}.DynamicArrays;
2+
import 0x1.Signer;
3+
4+
main(account: &signer) {
5+
DynamicArrays.publish(copy(account));
6+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 0, copy(account)) == 1, 1);
7+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 1, copy(account)) == 2, 2);
8+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 2, copy(account)) == 3, 3);
9+
10+
DynamicArrays.remove(Signer.address_of(copy(account)), 0, copy(account));
11+
12+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 0, copy(account)) == 2, 4);
13+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 1, copy(account)) == 3, 5);
14+
15+
DynamicArrays.insert(Signer.address_of(copy(account)), 0, 5, copy(account));
16+
17+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 0, copy(account)) == 5, 6);
18+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 1, copy(account)) == 2, 6);
19+
assert(DynamicArrays.get(Signer.address_of(copy(account)), 2, copy(account)) == 3, 6);
20+
21+
return;
22+
}

0 commit comments

Comments
 (0)