Skip to content

Commit

Permalink
examples/runwasi_cstruct: add an example to use the output of wasm2cs…
Browse files Browse the repository at this point in the history
…truct
  • Loading branch information
yamt committed Aug 3, 2024
1 parent 7f25623 commit 5c82f40
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/runwasi_cstruct/CMakeLists.txt
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")
6 changes: 6 additions & 0 deletions examples/runwasi_cstruct/build.sh
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
51 changes: 51 additions & 0 deletions examples/runwasi_cstruct/main.c
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);
}

0 comments on commit 5c82f40

Please sign in to comment.