-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/runwasi_cstruct: add an example to use the output of wasm2cs…
…truct
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
include(../../cmake/LLVM.cmake) | ||
|
||
project(runwasi_cstruct LANGUAGES C) | ||
|
||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wvla -Werror") | ||
|
||
find_package(toywasm-lib-core REQUIRED) | ||
find_package(toywasm-lib-wasi REQUIRED) | ||
|
||
# REVISIT: make runwasi.[ch] a library? | ||
|
||
set(sources | ||
"../runwasi/runwasi.c" | ||
"../runwasi/runwasi_cli_args.c" | ||
"main.c" | ||
"module.c" | ||
) | ||
|
||
add_executable(runwasi_cstruct ${sources}) | ||
target_link_libraries(runwasi_cstruct toywasm-lib-core toywasm-lib-wasi m) | ||
target_include_directories(runwasi_cstruct PRIVATE "../runwasi") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#! /bin/sh | ||
|
||
set -e | ||
|
||
TOYWASM_EXTRA_CMAKE_OPTIONS="-DTOYWASM_ENABLE_WASM_THREADS=ON -DTOYWASM_ENABLE_WASM_TAILCALL=ON" \ | ||
../build-toywasm-and-app.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* an example app to run a wasi command. | ||
* | ||
* usage: | ||
* % runwasi --dir=. --env=a=b -- foo.wasm -x -y | ||
*/ | ||
|
||
#include <inttypes.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#include <toywasm/mem.h> | ||
#include <toywasm/xlog.h> | ||
|
||
#include "runwasi.h" | ||
#include "runwasi_cli_args.h" | ||
|
||
extern const struct module g_wasm_module; | ||
|
||
int | ||
main(int argc, char **argv) | ||
{ | ||
struct runwasi_cli_args a0; | ||
struct runwasi_cli_args *a = &a0; | ||
uint32_t wasi_exit_code; | ||
int ret; | ||
const int stdio_fds[3] = { | ||
STDIN_FILENO, | ||
STDOUT_FILENO, | ||
STDERR_FILENO, | ||
}; | ||
ret = runwasi_cli_args_parse(argc, argv, a); | ||
if (ret != 0) { | ||
xlog_error("failed to process cli arguments"); | ||
exit(1); | ||
} | ||
struct mem_context mctx; | ||
mem_context_init(&mctx); | ||
ret = runwasi_module(&mctx, &g_wasm_module, a->ndirs, a->dirs, a->nenvs, | ||
(const char *const *)a->envs, a->argc, | ||
(const char *const *)a->argv, stdio_fds, NULL, | ||
&wasi_exit_code); | ||
mem_context_clear(&mctx); | ||
free(a->dirs); | ||
free(a->envs); | ||
if (ret != 0) { | ||
exit(1); | ||
} | ||
exit(wasi_exit_code); | ||
} |