Move commn iwasm code related to WASI to a shared file (#2477)
This commit is contained in:
@ -10,6 +10,10 @@
|
||||
#include "bh_read_file.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
#include "../common/libc_wasi.c"
|
||||
#endif
|
||||
|
||||
static int app_argc;
|
||||
static char **app_argv;
|
||||
|
||||
@ -48,12 +52,7 @@ print_help()
|
||||
printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
|
||||
" that runs commands in the form of `FUNC ARG...`\n");
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
printf(" --env=<env> Pass wasi environment variables with \"key=value\"\n");
|
||||
printf(" to the program, for example:\n");
|
||||
printf(" --env=\"key1=value1\" --env=\"key2=value2\"\n");
|
||||
printf(" --dir=<dir> Grant wasi access to the given host directories\n");
|
||||
printf(" to the program, for example:\n");
|
||||
printf(" --dir=<dir1> --dir=<dir2>\n");
|
||||
libc_wasi_print_help();
|
||||
#endif
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
printf(" --module-path=<path> Indicate a module search path. default is current\n"
|
||||
@ -172,25 +171,6 @@ app_instance_repl(wasm_module_inst_t module_inst)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
static bool
|
||||
validate_env_str(char *env)
|
||||
{
|
||||
char *p = env;
|
||||
int key_len = 0;
|
||||
|
||||
while (*p != '\0' && *p != '=') {
|
||||
key_len++;
|
||||
p++;
|
||||
}
|
||||
|
||||
if (*p != '=' || key_len == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
|
||||
static char global_heap_buf[WASM_GLOBAL_HEAP_SIZE] = { 0 };
|
||||
#endif
|
||||
@ -265,16 +245,17 @@ main(int argc, char *argv[])
|
||||
bool is_repl_mode = false;
|
||||
bool is_xip_file = false;
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
const char *dir_list[8] = { NULL };
|
||||
uint32 dir_list_size = 0;
|
||||
const char *env_list[8] = { NULL };
|
||||
uint32 env_list_size = 0;
|
||||
libc_wasi_parse_context_t wasi_parse_ctx;
|
||||
#endif
|
||||
#if WASM_ENABLE_DEBUG_INTERP != 0
|
||||
char *ip_addr = NULL;
|
||||
int instance_port = 0;
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
|
||||
#endif
|
||||
|
||||
/* Process options. */
|
||||
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
|
||||
if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--function")) {
|
||||
@ -356,38 +337,6 @@ main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
else if (!strncmp(argv[0], "--dir=", 6)) {
|
||||
if (argv[0][6] == '\0')
|
||||
return print_help();
|
||||
if (dir_list_size >= sizeof(dir_list) / sizeof(char *)) {
|
||||
printf("Only allow max dir number %d\n",
|
||||
(int)(sizeof(dir_list) / sizeof(char *)));
|
||||
return 1;
|
||||
}
|
||||
dir_list[dir_list_size++] = argv[0] + 6;
|
||||
}
|
||||
else if (!strncmp(argv[0], "--env=", 6)) {
|
||||
char *tmp_env;
|
||||
|
||||
if (argv[0][6] == '\0')
|
||||
return print_help();
|
||||
if (env_list_size >= sizeof(env_list) / sizeof(char *)) {
|
||||
printf("Only allow max env number %d\n",
|
||||
(int)(sizeof(env_list) / sizeof(char *)));
|
||||
return 1;
|
||||
}
|
||||
tmp_env = argv[0] + 6;
|
||||
if (validate_env_str(tmp_env))
|
||||
env_list[env_list_size++] = tmp_env;
|
||||
else {
|
||||
printf("Wasm parse env string failed: expect \"key=value\", "
|
||||
"got \"%s\"\n",
|
||||
tmp_env);
|
||||
return print_help();
|
||||
}
|
||||
}
|
||||
#endif /* WASM_ENABLE_LIBC_WASI */
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
else if (!strncmp(argv[0], MODULE_PATH, strlen(MODULE_PATH))) {
|
||||
module_search_path = handle_module_path(argv[0]);
|
||||
@ -423,8 +372,22 @@ main(int argc, char *argv[])
|
||||
patch);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
else {
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
libc_wasi_parse_result_t result =
|
||||
libc_wasi_parse(argv[0], &wasi_parse_ctx);
|
||||
switch (result) {
|
||||
case LIBC_WASI_PARSE_RESULT_OK:
|
||||
continue;
|
||||
case LIBC_WASI_PARSE_RESULT_NEED_HELP:
|
||||
return print_help();
|
||||
case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
return print_help();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (argc == 0)
|
||||
@ -507,8 +470,7 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
wasm_runtime_set_wasi_args(wasm_module, dir_list, dir_list_size, NULL, 0,
|
||||
env_list, env_list_size, argv, argc);
|
||||
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
|
||||
#endif
|
||||
|
||||
/* instantiate the module */
|
||||
|
||||
Reference in New Issue
Block a user