1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use move_binary_format::errors::PartialVMResult;
use move_core_types::gas_schedule::GasAlgebra;
use move_vm_runtime::native_functions::NativeContext;
use move_vm_types::{
    gas_schedule::NativeCostIndex,
    loaded_data::runtime_types::Type,
    natives::function::{native_gas, NativeResult},
    pop_arg,
    values::{Value, Vector, VectorRef},
};

use std::collections::VecDeque;

pub fn native_empty(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.is_empty());

    let cost = native_gas(context.cost_table(), NativeCostIndex::EMPTY, 1);
    NativeResult::map_partial_vm_result_one(cost, Vector::empty(&ty_args[0]))
}

pub fn native_length(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.len() == 1);

    let r = pop_arg!(args, VectorRef);
    let cost = native_gas(context.cost_table(), NativeCostIndex::LENGTH, 1);
    NativeResult::map_partial_vm_result_one(cost, r.len(&ty_args[0]))
}

pub fn native_push_back(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.len() == 2);

    let e = args.pop_back().unwrap();
    let r = pop_arg!(args, VectorRef);
    let cost = native_gas(
        context.cost_table(),
        NativeCostIndex::PUSH_BACK,
        e.size().get() as usize,
    );
    NativeResult::map_partial_vm_result_empty(cost, r.push_back(e, &ty_args[0]))
}

pub fn native_borrow(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.len() == 2);

    let idx = pop_arg!(args, u64) as usize;
    let r = pop_arg!(args, VectorRef);
    let cost = native_gas(context.cost_table(), NativeCostIndex::BORROW, 1);
    NativeResult::map_partial_vm_result_one(cost, r.borrow_elem(idx, &ty_args[0]))
}

pub fn native_pop(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.len() == 1);

    let r = pop_arg!(args, VectorRef);
    let cost = native_gas(context.cost_table(), NativeCostIndex::POP_BACK, 1);
    NativeResult::map_partial_vm_result_one(cost, r.pop(&ty_args[0]))
}

pub fn native_destroy_empty(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.len() == 1);

    let v = pop_arg!(args, Vector);
    let cost = native_gas(context.cost_table(), NativeCostIndex::DESTROY_EMPTY, 1);
    NativeResult::map_partial_vm_result_empty(cost, v.destroy_empty(&ty_args[0]))
}

pub fn native_swap(
    context: &mut NativeContext,
    ty_args: Vec<Type>,
    mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
    debug_assert!(ty_args.len() == 1);
    debug_assert!(args.len() == 3);

    let idx2 = pop_arg!(args, u64) as usize;
    let idx1 = pop_arg!(args, u64) as usize;
    let r = pop_arg!(args, VectorRef);
    let cost = native_gas(context.cost_table(), NativeCostIndex::SWAP, 1);
    NativeResult::map_partial_vm_result_empty(cost, r.swap(idx1, idx2, &ty_args[0]))
}