Apply clang format for samples files (#833)
Apply clang format for c source files under samples folder
This commit is contained in:
@ -8,23 +8,27 @@
|
||||
#include "bh_read_file.h"
|
||||
#include "bh_getopt.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);
|
||||
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)
|
||||
void
|
||||
print_usage(void)
|
||||
{
|
||||
fprintf(stdout, "Options:\r\n");
|
||||
fprintf(stdout, " -f [path of wasm file] \n");
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv_main[])
|
||||
int
|
||||
main(int argc, char *argv_main[])
|
||||
{
|
||||
static char global_heap_buf[512 * 1024];
|
||||
char *buffer, error_buf[128];
|
||||
int opt;
|
||||
char * wasm_path = NULL;
|
||||
char *wasm_path = NULL;
|
||||
|
||||
wasm_module_t module = NULL;
|
||||
wasm_module_inst_t module_inst = NULL;
|
||||
@ -32,25 +36,23 @@ int main(int argc, char *argv_main[])
|
||||
uint32 buf_size, stack_size = 8092, heap_size = 8092;
|
||||
wasm_function_inst_t func = NULL;
|
||||
wasm_function_inst_t func2 = NULL;
|
||||
char * native_buffer = NULL;
|
||||
char *native_buffer = NULL;
|
||||
uint32_t wasm_buffer = 0;
|
||||
|
||||
RuntimeInitArgs init_args;
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
while ((opt = getopt(argc, argv_main, "hf:")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'f':
|
||||
wasm_path = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
print_usage();
|
||||
return 0;
|
||||
case '?':
|
||||
print_usage();
|
||||
return 0;
|
||||
while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
wasm_path = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
print_usage();
|
||||
return 0;
|
||||
case '?':
|
||||
print_usage();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (optind == 1) {
|
||||
@ -64,26 +66,20 @@ int main(int argc, char *argv_main[])
|
||||
// For the function signature specifications, goto the link:
|
||||
// https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md
|
||||
|
||||
static NativeSymbol native_symbols[] =
|
||||
{
|
||||
static NativeSymbol native_symbols[] = {
|
||||
{
|
||||
"intToStr", // the name of WASM function name
|
||||
intToStr, // the native function pointer
|
||||
"(i*~i)i", // the function prototype signature, avoid to use i32
|
||||
NULL // attachment is NULL
|
||||
"intToStr", // the name of WASM function name
|
||||
intToStr, // the native function pointer
|
||||
"(i*~i)i", // the function prototype signature, avoid to use i32
|
||||
NULL // attachment is NULL
|
||||
},
|
||||
{
|
||||
"get_pow", // the name of WASM function name
|
||||
get_pow, // the native function pointer
|
||||
"(ii)i", // the function prototype signature, avoid to use i32
|
||||
NULL // attachment is NULL
|
||||
"get_pow", // the name of WASM function name
|
||||
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
|
||||
}
|
||||
{ "calculate_native", calculate_native, "(iii)i", NULL }
|
||||
};
|
||||
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
@ -102,30 +98,27 @@ int main(int argc, char *argv_main[])
|
||||
|
||||
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
|
||||
|
||||
if(!buffer) {
|
||||
if (!buffer) {
|
||||
printf("Open wasm app file [%s] failed.\n", wasm_path);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
module = wasm_runtime_load(buffer, buf_size, error_buf, sizeof(error_buf));
|
||||
if(!module) {
|
||||
if (!module) {
|
||||
printf("Load wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
module_inst = wasm_runtime_instantiate(module,
|
||||
stack_size,
|
||||
heap_size,
|
||||
error_buf,
|
||||
sizeof(error_buf));
|
||||
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
|
||||
error_buf, sizeof(error_buf));
|
||||
|
||||
if(!module_inst) {
|
||||
if (!module_inst) {
|
||||
printf("Instantiate wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
|
||||
if(!exec_env) {
|
||||
if (!exec_env) {
|
||||
printf("Create wasm execution environment failed.\n");
|
||||
goto fail;
|
||||
}
|
||||
@ -135,72 +128,90 @@ int main(int argc, char *argv_main[])
|
||||
argv[0] = 10;
|
||||
// the second arg will occupy two array elements
|
||||
memcpy(&argv[1], &arg_d, sizeof(arg_d));
|
||||
*(float*)(argv+3) = 300.002;
|
||||
*(float *)(argv + 3) = 300.002;
|
||||
|
||||
if(!(func = wasm_runtime_lookup_function(module_inst, "generate_float", NULL))){
|
||||
if (!(func = wasm_runtime_lookup_function(module_inst, "generate_float",
|
||||
NULL))) {
|
||||
printf("The generate_float wasm function is not found.\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
// pass 4 elements for function arguments
|
||||
if (!wasm_runtime_call_wasm(exec_env, func, 4, argv) ) {
|
||||
printf("call wasm function generate_float failed. %s\n", wasm_runtime_get_exception(module_inst));
|
||||
if (!wasm_runtime_call_wasm(exec_env, func, 4, argv)) {
|
||||
printf("call wasm function generate_float failed. %s\n",
|
||||
wasm_runtime_get_exception(module_inst));
|
||||
goto fail;
|
||||
}
|
||||
|
||||
float ret_val;
|
||||
memcpy(&ret_val, argv, sizeof(float));
|
||||
printf("Native finished calling wasm function generate_float(), returned a float value: %ff\n", ret_val );
|
||||
printf("Native finished calling wasm function generate_float(), returned a "
|
||||
"float value: %ff\n",
|
||||
ret_val);
|
||||
|
||||
// Next we will pass a buffer to the WASM function
|
||||
uint32 argv2[4];
|
||||
|
||||
// must allocate buffer from wasm instance memory space (never use pointer from host runtime)
|
||||
wasm_buffer = wasm_runtime_module_malloc(module_inst, 100, (void**)&native_buffer);
|
||||
// must allocate buffer from wasm instance memory space (never use pointer
|
||||
// from host runtime)
|
||||
wasm_buffer =
|
||||
wasm_runtime_module_malloc(module_inst, 100, (void **)&native_buffer);
|
||||
|
||||
memcpy(argv2, &ret_val, sizeof(float)); // the first argument
|
||||
argv2[1] = wasm_buffer; // the second argument is the wasm buffer address
|
||||
argv2[2] = 100; // the third argument is the wasm buffer size
|
||||
argv2[3] = 3; // the last argument is the digits after decimal point for converting float to string
|
||||
argv2[1] = wasm_buffer; // the second argument is the wasm buffer address
|
||||
argv2[2] = 100; // the third argument is the wasm buffer size
|
||||
argv2[3] = 3; // the last argument is the digits after decimal point for
|
||||
// converting float to string
|
||||
|
||||
if(!(func2 = wasm_runtime_lookup_function(module_inst, "float_to_string", NULL))){
|
||||
printf("The wasm function float_to_string wasm function is not found.\n");
|
||||
if (!(func2 = wasm_runtime_lookup_function(module_inst, "float_to_string",
|
||||
NULL))) {
|
||||
printf(
|
||||
"The wasm function float_to_string wasm function is not found.\n");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (wasm_runtime_call_wasm(exec_env, func2, 4, argv2) ) {
|
||||
printf("Native finished calling wasm function: float_to_string, returned a formatted string: %s\n", native_buffer);
|
||||
if (wasm_runtime_call_wasm(exec_env, func2, 4, argv2)) {
|
||||
printf("Native finished calling wasm function: float_to_string, "
|
||||
"returned a formatted string: %s\n",
|
||||
native_buffer);
|
||||
}
|
||||
else {
|
||||
printf("call wasm function float_to_string failed. error: %s\n", wasm_runtime_get_exception(module_inst));
|
||||
printf("call wasm function float_to_string failed. error: %s\n",
|
||||
wasm_runtime_get_exception(module_inst));
|
||||
goto fail;
|
||||
}
|
||||
|
||||
wasm_function_inst_t func3 = wasm_runtime_lookup_function(module_inst,
|
||||
"calculate",
|
||||
NULL);
|
||||
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};
|
||||
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));
|
||||
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) {
|
||||
if(wasm_buffer) wasm_runtime_module_free(module_inst, wasm_buffer);
|
||||
if (exec_env)
|
||||
wasm_runtime_destroy_exec_env(exec_env);
|
||||
if (module_inst) {
|
||||
if (wasm_buffer)
|
||||
wasm_runtime_module_free(module_inst, wasm_buffer);
|
||||
wasm_runtime_deinstantiate(module_inst);
|
||||
}
|
||||
if(module) wasm_runtime_unload(module);
|
||||
if(buffer) BH_FREE(buffer);
|
||||
if (module)
|
||||
wasm_runtime_unload(module);
|
||||
if (buffer)
|
||||
BH_FREE(buffer);
|
||||
wasm_runtime_destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -8,12 +8,12 @@
|
||||
#include "math.h"
|
||||
|
||||
extern bool
|
||||
wasm_runtime_call_indirect(wasm_exec_env_t exec_env,
|
||||
uint32_t element_indices,
|
||||
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)
|
||||
void
|
||||
reverse(char *str, int len)
|
||||
{
|
||||
int i = 0, j = len - 1, temp;
|
||||
while (i < j) {
|
||||
@ -32,11 +32,12 @@ void reverse(char * str, int len)
|
||||
// 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
|
||||
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__);
|
||||
printf("calling into native function: %s\n", __FUNCTION__);
|
||||
|
||||
while (x) {
|
||||
// native is responsible for checking the str_len overflow
|
||||
@ -64,8 +65,10 @@ int intToStr(wasm_exec_env_t exec_env, int x, char* str, int str_len, int digit)
|
||||
return i;
|
||||
}
|
||||
|
||||
int get_pow(wasm_exec_env_t exec_env, int x, int y) {
|
||||
printf ("calling into native function: %s\n", __FUNCTION__);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -8,21 +8,25 @@
|
||||
#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);
|
||||
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
|
||||
//
|
||||
float generate_float(int iteration, double seed1, float seed2)
|
||||
float
|
||||
generate_float(int iteration, double seed1, float seed2)
|
||||
{
|
||||
float ret;
|
||||
|
||||
printf ("calling into WASM function: %s\n", __FUNCTION__);
|
||||
printf("calling into WASM function: %s\n", __FUNCTION__);
|
||||
|
||||
for (int i=0; i<iteration; i++){
|
||||
ret += 1.0f/seed1 + seed2;
|
||||
for (int i = 0; i < iteration; i++) {
|
||||
ret += 1.0f / seed1 + seed2;
|
||||
}
|
||||
|
||||
return ret;
|
||||
@ -30,10 +34,11 @@ float generate_float(int iteration, double seed1, float seed2)
|
||||
|
||||
// Converts a floating-point/double number to a string.
|
||||
// intToStr() is implemented outside wasm app
|
||||
void float_to_string(float n, char* res, int res_size, int afterpoint)
|
||||
void
|
||||
float_to_string(float n, char *res, int res_size, int afterpoint)
|
||||
{
|
||||
|
||||
printf ("calling into WASM function: %s\n", __FUNCTION__);
|
||||
printf("calling into WASM function: %s\n", __FUNCTION__);
|
||||
|
||||
// Extract integer part
|
||||
int ipart = (int)n;
|
||||
@ -57,25 +62,28 @@ void float_to_string(float n, char* res, int res_size, int afterpoint)
|
||||
}
|
||||
}
|
||||
|
||||
int32_t mul7(int32_t n)
|
||||
int32_t
|
||||
mul7(int32_t n)
|
||||
{
|
||||
printf ("calling into WASM function: %s,", __FUNCTION__);
|
||||
printf("calling into WASM function: %s,", __FUNCTION__);
|
||||
n = n * 7;
|
||||
printf (" %s return %d \n", __FUNCTION__, n);
|
||||
printf(" %s return %d \n", __FUNCTION__, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
int32_t mul5(int32_t n)
|
||||
int32_t
|
||||
mul5(int32_t n)
|
||||
{
|
||||
printf ("calling into WASM function: %s,", __FUNCTION__);
|
||||
printf("calling into WASM function: %s,", __FUNCTION__);
|
||||
n = n * 5;
|
||||
printf (" %s return %d \n", __FUNCTION__, n);
|
||||
printf(" %s return %d \n", __FUNCTION__, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
int32_t calculate(int32_t n)
|
||||
int32_t
|
||||
calculate(int32_t n)
|
||||
{
|
||||
printf ("calling into WASM function: %s\n", __FUNCTION__);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user