Fix function type not set issue of aot_call_indirect (#229)

Add registration of libc-wasi to 'wasi_snapshot_preview1' to support cargo-wasi
change zephyr build method from cmake to west
fix problem when preserve space for local vars
fix wasi authority problem
This commit is contained in:
Xu Jun
2020-04-07 11:04:46 +08:00
committed by GitHub
parent 374e687938
commit 5e196253f6
26 changed files with 512 additions and 143 deletions

1
samples/basic/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/out/

View File

@ -44,6 +44,7 @@ OUT_FILE=${i%.*}.wasm
-Wl,--no-threads,--strip-all,--no-entry -nostdlib \
-Wl,--export=generate_float \
-Wl,--export=float_to_string \
-Wl,--export=calculate\
-Wl,--allow-undefined \
-o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}

View File

@ -9,6 +9,7 @@
int intToStr(int x, char* str, int str_len, int digit);
int get_pow(int x, int y);
int32_t calculate_native(int32_t n, int32_t func1, int32_t func2);
void print_usage(void)
{
@ -75,6 +76,12 @@ int main(int argc, char *argv_main[])
get_pow, // the native function pointer
"(ii)i", // the function prototype signature, avoid to use i32
NULL // attachment is NULL
},
{
"calculate_native",
calculate_native,
"(iii)i",
NULL
}
};
@ -167,6 +174,23 @@ int main(int argc, char *argv_main[])
goto fail;
}
wasm_function_inst_t func3 = wasm_runtime_lookup_function(module_inst,
"calculate",
NULL);
if (!func3) {
printf("The wasm function calculate is not found.\n");
goto fail;
}
uint32_t argv3[1] = {3};
if (wasm_runtime_call_wasm(exec_env, func3, 1, argv3)) {
uint32_t result = *(uint32_t*)argv3;
printf("Native finished calling wasm function: calculate, return: %d\n", result);
} else {
printf("call wasm function calculate failed. error: %s\n", wasm_runtime_get_exception(module_inst));
goto fail;
}
fail:
if(exec_env) wasm_runtime_destroy_exec_env(exec_env);
if(module_inst) {

View File

@ -7,6 +7,11 @@
#include "wasm_export.h"
#include "math.h"
extern bool
wasm_runtime_call_indirect(wasm_exec_env_t exec_env,
uint32_t element_indices,
uint32_t argc, uint32_t argv[]);
// The first parameter is not exec_env because it is invoked by native funtions
void reverse(char * str, int len)
{
@ -63,3 +68,29 @@ 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;
}

View File

@ -6,9 +6,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int intToStr(int x, char* str, int str_len, int digit);
int get_pow(int x, int y);
int32_t calculate_native(int32_t n, int32_t func1, int32_t func2);
//
// Primitive parameters functions
@ -54,3 +56,27 @@ void float_to_string(float n, char* res, int res_size, int afterpoint)
intToStr((int)fpart, res + i + 1, sizeof(res + i + 1), afterpoint);
}
}
int32_t mul7(int32_t n)
{
printf ("calling into WASM function: %s,", __FUNCTION__);
n = n * 7;
printf (" %s return %d \n", __FUNCTION__, n);
return n;
}
int32_t mul5(int32_t n)
{
printf ("calling into WASM function: %s,", __FUNCTION__);
n = n * 5;
printf (" %s return %d \n", __FUNCTION__, n);
return n;
}
int32_t calculate(int32_t n)
{
printf ("calling into WASM function: %s\n", __FUNCTION__);
int32_t (*f1)(int32_t) = &mul5;
int32_t (*f2)(int32_t) = &mul7;
return calculate_native(n, (uint32_t)f1, (uint32_t)f2);
}