Search
⌃K

C

Prerequisites

  • wasi-libc
$ cd wasi-libc
$ make install INSTALL_DIR=../libc
  • wasi-sdk
Install and extract static library from here: https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-14
$ mv path/to/libcland_rt.builtins.wasm32.a /path/to/llvm/lib/clang/VERSION/lib/wasi/
  • cJSON

Code

function.c
#include "three_em.h"
cJSON *
handler (cJSON * state, cJSON * action)
{
cJSON *result = NULL;
cJSON *counter = NULL;
counter = cJSON_GetObjectItemCaseSensitive (state, "counter");
result = cJSON_CreateObject ();
cJSON_AddNumberToObject (result, "counter", counter->valueint + 1);
return result;
}
MAKE_CONTRACT (handler)

Compiling

$ sudo apt install llvm clang
$ clang --target=wasm32-unknown-wasi -O3 -flto -Wl,--lto-O3 -Wl,--export=_alloc -Wl,--export=get_len -Wl,--export=handle --sysroot ./libc -nostartfiles -Wl,--no-entry -v -o function.wasm function.c cJSON/cJSON.c

Deployment

$ exm function:deploy --src function.wasm --init-state "{"counter": 0}" --token MY_EXM_TOKEN

Interaction

$ exm function:write [functionId] --input '{}' --token MY_EXM_TOKEN
// { state: { counter: 1 } }
Last modified 8mo ago