feat: module name as first argument

master
Sean McBride 3 years ago
parent 255461198d
commit d05a58af75

@ -18,9 +18,8 @@
thread_local struct sandbox *worker_thread_current_sandbox = NULL; thread_local struct sandbox *worker_thread_current_sandbox = NULL;
// TODO: Propagate arguments from *.json spec file const int dummy_argc = 3;
const int dummy_argc = 4; const char *dummy_argv[] = { "foo", "bar", "baz" };
const char *dummy_argv[] = { "/test/test/test", "foo", "bar", "baz" };
/** /**
* @brief Switches from an executing sandbox to the worker thread base context * @brief Switches from an executing sandbox to the worker thread base context
@ -141,6 +140,7 @@ current_sandbox_init()
int rc = 0; int rc = 0;
char *error_message = NULL; char *error_message = NULL;
char *args[dummy_argc + 1];
sandbox_open_http(sandbox); sandbox_open_http(sandbox);
@ -164,8 +164,12 @@ current_sandbox_init()
/* Initialize WASI */ /* Initialize WASI */
wasi_options_t options; wasi_options_t options;
wasi_options_init(&options); wasi_options_init(&options);
options.argc = dummy_argc;
options.argv = dummy_argv; args[0] = sandbox->module->name;
for (int i = 0; i < dummy_argc; i++) args[i + 1] = (char *)dummy_argv[i];
options.argc = dummy_argc + 1;
options.argv = &args;
sandbox->wasi_context = wasi_context_init(&options); sandbox->wasi_context = wasi_context_init(&options);
sledge_abi__current_wasm_module_instance.wasi_context = sandbox->wasi_context; sledge_abi__current_wasm_module_instance.wasi_context = sandbox->wasi_context;
assert(sandbox->wasi_context != NULL); assert(sandbox->wasi_context != NULL);

@ -36,27 +36,12 @@ wasi_context_init(wasi_options_t *options)
if (options->argc > 0) { if (options->argc > 0) {
assert(options->argv != NULL); assert(options->argv != NULL);
/* Strip path from first arg, calculating offset and length */
size_t first_arg_offset = 0;
size_t first_arg_len = 0;
for (first_arg_offset = strlen(options->argv[0]); first_arg_offset > 0; first_arg_offset--) {
if (options->argv[0][first_arg_offset] == '/') {
first_arg_offset++;
break;
}
}
first_arg_len = strlen(options->argv[0]) - first_arg_offset;
/* Calculate argument buffer size */ /* Calculate argument buffer size */
__wasi_size_t argv_buf_size = 0; __wasi_size_t argv_buf_size = 0;
__wasi_size_t argv_buffer_offsets[options->argc + 1]; __wasi_size_t argv_buffer_offsets[options->argc + 1];
for (int i = 0; i < options->argc; i++) { for (int i = 0; i < options->argc; i++) {
argv_buffer_offsets[i] = argv_buf_size; argv_buffer_offsets[i] = argv_buf_size;
if (i == 0) { argv_buf_size += strlen(options->argv[i]) + 1;
argv_buf_size += first_arg_len + 1;
} else {
argv_buf_size += strlen(options->argv[i]) + 1;
}
} }
argv_buffer_offsets[options->argc] = argv_buf_size; argv_buffer_offsets[options->argc] = argv_buf_size;
@ -76,10 +61,7 @@ wasi_context_init(wasi_options_t *options)
} }
/* Copy the binary name minux the path as the first arg */ /* Copy the binary name minux the path as the first arg */
strncpy(wasi_context->argv_buf, &options->argv[0][first_arg_offset], first_arg_len); for (int i = 0; i < options->argc; i++) {
/* Copy the binary name minux the path as the first arg */
for (int i = 1; i < options->argc; i++) {
strncpy(&wasi_context->argv_buf[argv_buffer_offsets[i]], options->argv[i], strncpy(&wasi_context->argv_buf[argv_buffer_offsets[i]], options->argv[i],
argv_buffer_offsets[i + 1] - argv_buffer_offsets[i]); argv_buffer_offsets[i + 1] - argv_buffer_offsets[i]);
} }

Loading…
Cancel
Save