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
use super::VmApiImpl;
use elrond_wasm::api::{EndpointFinishApi, EndpointFinishApiImpl, Handle};

extern "C" {
    fn finish(dataOffset: *const u8, length: i32);

    // big int API
    fn bigIntFinishUnsigned(bih: i32);
    fn bigIntFinishSigned(bih: i32);

    // small int API
    fn smallIntFinishUnsigned(value: i64);
    fn smallIntFinishSigned(value: i64);

    // managed buffer API
    fn mBufferFinish(mBufferHandle: i32) -> i32;
}

impl EndpointFinishApi for VmApiImpl {
    type EndpointFinishApiImpl = VmApiImpl;

    #[inline]
    fn finish_api_impl() -> Self::EndpointFinishApiImpl {
        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 EndpointFinishApiImpl for VmApiImpl {
    #[inline]
    fn finish_slice_u8(&self, slice: &[u8]) {
        unsafe {
            finish(slice.as_ptr(), slice.len() as i32);
        }
    }

    #[inline]
    fn finish_big_int_raw(&self, handle: i32) {
        unsafe {
            bigIntFinishSigned(handle);
        }
    }

    #[inline]
    fn finish_big_uint_raw(&self, handle: i32) {
        unsafe {
            bigIntFinishUnsigned(handle);
        }
    }

    #[inline]
    fn finish_managed_buffer_raw(&self, handle: Handle) {
        unsafe {
            mBufferFinish(handle);
        }
    }

    #[inline]
    fn finish_u64(&self, value: u64) {
        unsafe {
            smallIntFinishUnsigned(value as i64);
        }
    }

    #[inline]
    fn finish_i64(&self, value: i64) {
        unsafe {
            smallIntFinishSigned(value);
        }
    }
}