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
use crate::VmApiImpl;
use elrond_wasm::{
    api::{EndpointArgumentApi, EndpointArgumentApiImpl, Handle},
    types::heap::BoxedBytes,
};

extern "C" {
    fn getNumArguments() -> i32;
    fn getArgumentLength(id: i32) -> i32;
    fn getArgument(id: i32, dstOffset: *mut u8) -> i32;

    // managed buffer API (currently the main one)
    fn mBufferGetArgument(argId: i32, mBufferHandle: i32) -> i32;

    // big int API
    fn bigIntGetUnsignedArgument(arg_index: i32, dest: i32);
    fn bigIntGetSignedArgument(arg_index: i32, dest: i32);

    // small int API
    fn smallIntGetUnsignedArgument(id: i32) -> i64;
    fn smallIntGetSignedArgument(id: i32) -> i64;
}

impl EndpointArgumentApi for VmApiImpl {
    type EndpointArgumentApiImpl = VmApiImpl;

    #[inline]
    fn argument_api_impl() -> Self::EndpointArgumentApiImpl {
        VmApiImpl {}
    }
}

/// Interface to only be used by code generated by the macros.
/// The smart contract code doesn't have access to these methods directly.
impl EndpointArgumentApiImpl for VmApiImpl {
    #[inline]
    fn get_num_arguments(&self) -> i32 {
        unsafe { getNumArguments() }
    }

    #[inline]
    fn get_argument_len(&self, arg_index: i32) -> usize {
        unsafe { getArgumentLength(arg_index) as usize }
    }

    #[inline]
    fn load_argument_managed_buffer(&self, arg_index: i32, dest: Handle) {
        unsafe {
            mBufferGetArgument(arg_index, dest);
        }
    }

    fn get_argument_boxed_bytes(&self, arg_index: i32) -> BoxedBytes {
        let len = self.get_argument_len(arg_index);
        unsafe {
            let mut res = BoxedBytes::allocate(len);
            if len > 0 {
                getArgument(arg_index, res.as_mut_ptr());
            }
            res
        }
    }

    #[inline]
    fn load_argument_big_int_unsigned(&self, arg_index: i32, dest: Handle) {
        unsafe {
            bigIntGetUnsignedArgument(arg_index, dest);
        }
    }

    #[inline]
    fn load_argument_big_int_signed(&self, arg_index: i32, dest: Handle) {
        unsafe {
            bigIntGetSignedArgument(arg_index, dest);
        }
    }

    #[inline]
    fn get_argument_u64(&self, arg_index: i32) -> u64 {
        unsafe { smallIntGetUnsignedArgument(arg_index) as u64 }
    }

    #[inline]
    fn get_argument_i64(&self, arg_index: i32) -> i64 {
        unsafe { smallIntGetSignedArgument(arg_index) }
    }
}