summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/basic/src/native_impl.c
blob: 1374c8dd89fc88dcd9264174e5d13ec853fa4033 (plain)
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
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "bh_platform.h"
#include "wasm_export.h"
#include "math.h"

// The first parameter is not exec_env because it is invoked by native funtions
void
reverse(char *str, int len)
{
    int i = 0, j = len - 1, temp;
    while (i < j) {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++;
        j--;
    }
}

// The first parameter exec_env must be defined using type wasm_exec_env_t
// which is the calling convention for exporting native API by WAMR.
//
// Converts a given integer x to string str[].
// digit is the number of digits required in the output.
// If digit is more than the number of digits in x,
// then 0s are added at the beginning.
int
intToStr(wasm_exec_env_t exec_env, int x, char *str, int str_len, int digit)
{
    int i = 0;

    printf("calling into native function: %s\n", __FUNCTION__);

    while (x) {
        // native is responsible for checking the str_len overflow
        if (i >= str_len) {
            return -1;
        }
        str[i++] = (x % 10) + '0';
        x = x / 10;
    }

    // If number of digits required is more, then
    // add 0s at the beginning
    while (i < digit) {
        if (i >= str_len) {
            return -1;
        }
        str[i++] = '0';
    }

    reverse(str, i);

    if (i >= str_len)
        return -1;
    str[i] = '\0';
    return i;
}

int
get_pow(wasm_exec_env_t exec_env, int x, int y)
{
    printf("calling into native function: %s\n", __FUNCTION__);
    return (int)pow(x, y);
}

int32_t
calculate_native(wasm_exec_env_t exec_env, int32_t n, int32_t func1,
                 int32_t func2)
{
    printf("calling into native function: %s, n=%d, func1=%d, func2=%d\n",
           __FUNCTION__, n, func1, func2);

    uint32_t argv[] = { n };
    if (!wasm_runtime_call_indirect(exec_env, func1, 1, argv)) {
        printf("call func1 failed\n");
        return 0xDEAD;
    }

    uint32_t n1 = argv[0];
    printf("call func1 and return n1=%d\n", n1);

    if (!wasm_runtime_call_indirect(exec_env, func2, 1, argv)) {
        printf("call func2 failed\n");
        return 0xDEAD;
    }

    uint32_t n2 = argv[0];
    printf("call func2 and return n2=%d\n", n2);
    return n1 + n2;
}