Skip to content
Snippets Groups Projects
Commit 2cb00b58 authored by Mohammad Mahhouk's avatar Mohammad Mahhouk
Browse files

cleaner code for both examples, export natives and multi-modules

parent 2f067862
No related branches found
No related tags found
No related merge requests found
......@@ -69,22 +69,15 @@ add_custom_command(
COMMAND make all && mv *.wasm build/
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
#add_custom_command(
# OUTPUT test.wasm
# COMMAND ${WASI_CLANG} --sysroot=${WASI_SYSROOT} -Wl,--no-entry -O3 -o test.wasm ${CMAKE_CURRENT_SOURCE_DIR}/test.c
# COMMENT "Compiling the test.c to test.wasm"
# WORKING_DIRECTORY ${CMAKE_CURRENT_BIN_DIR}
#)
#add_custom_target(test.wasm
# COMMAND ${WASI_CLANG} --sysroot=${WASI_SYSROOT} -Wl,--no-entry -O3 -o test.wasm ${CMAKE_CURRENT_SOURCE_DIR}/test.c
#)
#add_executable(test.wasm test.c)
#target_compile_options(test.wasm -sysroot=${WASI_SYSROOT} -Wl,--no-entry -O3)
###### There is no way to specify another compiler for a single target
set(C_SOURCE_FILES main.c)
add_executable(export_test ${C_SOURCE_FILES})
set(EXPORT_SOURCE_FILES export.c)
add_executable(export_test ${EXPORT_SOURCE_FILES})
target_include_directories(export_test PUBLIC ${WAMR_EMBEDDING_API_INCLUDE_PATH} ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(export_test PRIVATE vmlib pthread m)
\ No newline at end of file
target_link_libraries(export_test PRIVATE vmlib pthread m)
set(MULTMOD_SOURCE_FILES multimod.c)
add_executable(multimod_test ${MULTMOD_SOURCE_FILES})
target_include_directories(multimod_test PUBLIC ${WAMR_EMBEDDING_API_INCLUDE_PATH} ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(multimod_test PRIVATE vmlib pthread m)
\ No newline at end of file
C := ./wasi-sdk/bin/clang
#CFLAGS := --sysroot=/opt/wasi-sdk-12.0/share/wasi-sysroot -Wl,--no-entry,--allow-undefined,--export-all -O3
CFLAGS := --sysroot=./wasi-sdk/share/wasi-sysroot -Wl,--no-entry,--allow-undefined,--export-all -O3
CFLAGS := --sysroot=./wasi-sdk/share/wasi-sysroot -Wl,--no-entry -O3
.PHONY: test add multadd all
......
......@@ -7,5 +7,7 @@ int addFunc(int a, int b){
int main()
{
return 0;
int result = addFunc(1,1);
printf("1 + 1 is %d quick math\n", result);
return result;
}
\ No newline at end of file
export.c 0 → 100644
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "wasm_export.h"
void my_printf(wasm_exec_env_t exec_env, char *text)
{
printf("%s\n",text);
}
void my_nprintf(wasm_exec_env_t exec_env, char *text, int len)
{
printf("message: '%.*s'\n", len, text);
}
void hello_world(wasm_exec_env_t exec_env)
{
printf("simple hello world is here \n");
}
char *read_wasm_binary_to_buffer(const char *path, uint32_t *size)
{
FILE *fd = fopen(path, "rb");
if(!fd)
{
fprintf(stderr,"failed to open the %s file\n", path);
perror("fopen failed!\n");
return NULL;
}
if (fseek(fd, 0, SEEK_END) != 0)
{
perror("fseek failed\n");
return NULL;
}
long fsize = ftell(fd);
if(fseek(fd, 0, SEEK_SET) != 0) // rewind(fd); can be used here too
{
perror("fseek failed\n");
return NULL;
}
char *buffer = malloc(fsize + 1);
if (!buffer)
{
perror("malloc for file buffer failed!\n");
return NULL;
}
size_t read_bytes = fread(buffer, 1, fsize, fd);
if(read_bytes != (size_t)fsize)
{
free(buffer);
fprintf(stderr, "file read didnt read the whole file size and returned only %zu instead of %zu \n", read_bytes, fsize);
exit(EXIT_FAILURE);
}
fclose(fd);
*size = fsize; //+ 1;
//buffer[fsize] = 0;
return buffer;
}
int main(int argc, char *argv[])
{
static NativeSymbol native_symbols[] =
{
{
"my_printf", // the name of WASM function name
my_printf, // the native function pointer
"($)" // the function prototype signature
},
{
"my_nprintf", // the name of WASM function name
my_nprintf, // the native function pointer
"(*~)" // the function prototype signature
},
{
"hello_world", // the name of WASM function name
hello_world, // the native function pointer
"()" // the function prototype signature
}
};
char *buffer, error_buf[128];
wasm_module_t module;
wasm_module_inst_t module_inst;
wasm_function_inst_t func;
wasm_exec_env_t exec_env;
uint32_t size, sizeMultadd, stack_size = 8092, heap_size = 8092;
/* initialize the wasm runtime by default configurations */
if(!wasm_runtime_init())
{
fprintf(stderr,"wasm runtime initialisation failed\n");
exit(EXIT_FAILURE);
}
/* read WASM file into a memory buffer */
buffer = read_wasm_binary_to_buffer(argv[1], &size);
if(buffer == NULL)
{
fprintf(stderr, "the returned buffer for reading the wasm module is null\n");
exit(EXIT_FAILURE);
}
/* add line below if we want to export native functions to WASM app */
int n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
if (!wasm_runtime_register_natives("env", native_symbols, n_native_symbols))
{
free(buffer);
exit(EXIT_FAILURE);
}
/* parse the WASM file from buffer and create a WASM module */
module = wasm_runtime_load(buffer, size, error_buf, sizeof(error_buf));
if(module == NULL)
{
fprintf(stderr, "failed to load the module %s from the buffer with the error %s\n", argv[1], error_buf);
exit(EXIT_FAILURE);
}
/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
error_buf, sizeof(error_buf));
if(module_inst == NULL)
{
fprintf(stderr, "module instantiation failed!\n");
exit(EXIT_FAILURE);
}
/* lookup a WASM function by its name
The function signature can NULL here */
func = wasm_runtime_lookup_function(module_inst, "main", "()i");
if (func == NULL) {
printf("multadd function not found\n");
return -1;
}
/* creat an execution environment to execute the WASM functions */
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
if (exec_env == NULL) {
printf("execution environmnet creation failed\n");
return -1;
}
uint32_t args[1] = {0};
if (wasm_runtime_call_wasm(exec_env, func, 0, args) ) {
/* the return value is stored in argv[0] */
//printf("the main function returned %d \n", args[0]);
}
else {
/* exception is thrown if call fails */
printf("%s\n", wasm_runtime_get_exception(module_inst));
}
wasm_runtime_destroy();
return 0;
}
\ No newline at end of file
......@@ -9,7 +9,10 @@ int multadd(int a, int b, int c)
{
return addFunc(a,b) * c;
}
int main()
{
return 0;
int res = multadd(1,1,2);
printf("1+1 is 2 * 2 is %d quick math!! \n", res);
return res;
}
\ No newline at end of file
......@@ -82,24 +82,6 @@ module_destroyer_cb(uint8_t *buffer, uint32_t size)
int main(int argc, char *argv[])
{
static NativeSymbol native_symbols[] =
{
{
"my_printf", // the name of WASM function name
my_printf, // the native function pointer
"($)" // the function prototype signature
},
{
"my_nprintf", // the name of WASM function name
my_nprintf, // the native function pointer
"(*~)" // the function prototype signature
},
{
"hello_world", // the name of WASM function name
hello_world, // the native function pointer
"()" // the function prototype signature
}
};
char *buffer, *bufferMultadd, error_buf[128];
wasm_module_t module, moduleMultadd;
......@@ -118,85 +100,46 @@ int main(int argc, char *argv[])
wasm_runtime_set_module_reader(module_reader_cb, module_destroyer_cb);
/* read WASM file into a memory buffer */
//buffer = read_wasm_binary_to_buffer(argv[1], &size);
//if(buffer == NULL)
//{
// fprintf(stderr, "the returned buffer for reading the wasm module is null\n");
// exit(EXIT_FAILURE);
//}
//
/* read WASM file into a memory buffer */
bufferMultadd = read_wasm_binary_to_buffer(argv[2], &sizeMultadd);
if(bufferMultadd == NULL)
buffer = read_wasm_binary_to_buffer(argv[1], &size);
if(buffer == NULL)
{
fprintf(stderr, "the returned buffer for reading the wasm module is null\n");
exit(EXIT_FAILURE);
}
/* add line below if we want to export native functions to WASM app */
/*int n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
if (!wasm_runtime_register_natives("env", native_symbols, n_native_symbols))
{
free(buffer);
exit(EXIT_FAILURE);
}*/
/* parse the WASM file from buffer and create a WASM module */
//module = wasm_runtime_load(buffer, size, error_buf, sizeof(error_buf));
//if(module == NULL)
//{
// fprintf(stderr, "failed to load the module %s from the buffer\n", argv[1]);
// exit(EXIT_FAILURE);
//}
//
//if(!wasm_runtime_register_module(argv[1],module,error_buf,sizeof(error_buf)))
//{
// fprintf(stderr, "registering the module %s failed\n", argv[1]);
// exit(EXIT_FAILURE);
//}
moduleMultadd = wasm_runtime_load(bufferMultadd, sizeMultadd, error_buf, sizeof(error_buf));
if(moduleMultadd == NULL)
module = wasm_runtime_load(buffer, size, error_buf, sizeof(error_buf));
if(module == NULL)
{
fprintf(stderr, "failed to load the module %s from the buffer with the error mesage %s\n", argv[2],error_buf);
fprintf(stderr, "failed to load the module %s from the buffer with the error msg %s\n", argv[1], error_buf);
exit(EXIT_FAILURE);
}
if(!wasm_runtime_register_module(argv[2],moduleMultadd,error_buf,sizeof(error_buf)))
{
fprintf(stderr, "registering the module %s failed\n", argv[1]);
exit(EXIT_FAILURE);
}
/* create an instance of the WASM module (WASM linear memory is ready) */
//module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
// error_buf, sizeof(error_buf));
//
//if(module_inst == NULL)
//if(!wasm_runtime_register_module(argv[1],module,error_buf,sizeof(error_buf)))
//{
// fprintf(stderr, "module instantiation failed!\n");
// fprintf(stderr, "registering the module %s failed\n", argv[1]);
// exit(EXIT_FAILURE);
//}
module_inst_multadd = wasm_runtime_instantiate(moduleMultadd, stack_size, heap_size,
error_buf, sizeof(error_buf));
if(module_inst_multadd == NULL)
/* create an instance of the WASM module (WASM linear memory is ready) */
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf));
if(module_inst == NULL)
{
fprintf(stderr, "module instantiation failed!\n");
fprintf(stderr, "module instantiation failed! Error msg: %s\n", error_buf);
exit(EXIT_FAILURE);
}
/* lookup a WASM function by its name
The function signature can NULL here */
func = wasm_runtime_lookup_function(module_inst_multadd, "multadd", "(iii)i");
func = wasm_runtime_lookup_function(module_inst, "multadd", "(iii)i");
if (func == NULL) {
printf("multadd function not found\n");
return -1;
}
/* creat an execution environment to execute the WASM functions */
exec_env = wasm_runtime_create_exec_env(module_inst_multadd, stack_size);
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
if (exec_env == NULL) {
printf("execution environmnet creation failed\n");
return -1;
......
......@@ -11,9 +11,9 @@ extern void my_nprintf(char *text, int len);
import("hello_world")
extern void hello_world();
export("main_test")
export("main")
int main(){
//printf("executing wasi printf\n");
printf("executing wasi printf\n");
hello_world();
my_printf("calling now my_printf imported function");
char *hello = "hello there";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment