Implement StreamFunction in C
🚧
This feature is currently in alpha and subject to change.
Install CLI
$ curl -fsSL "https://get.yomo.run" | sh
Write a StreamFunction in C
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
__attribute__((import_module("env"),
import_name("yomo_observe_datatag"))) extern void
observe(uint32_t tag);
__attribute__((import_module("env"),
import_name("yomo_context_tag"))) extern uint32_t
get_tag();
__attribute__((import_module("env"),
import_name("yomo_context_data_size"))) extern size_t
get_input_size();
__attribute__((import_module("env"),
import_name("yomo_context_data"))) extern size_t
load_input(char *pointer, size_t length);
__attribute__((import_module("env"), import_name("yomo_write"))) extern int32_t
dump_output(uint32_t tag, const char *pointer, size_t length);
uint32_t yomo_init() {
printf("wasm c sfn init\n");
return 0;
}
void yomo_observe_datatags() { observe(0x33); }
void yomo_handler() {
// load input tag & data
uint32_t tag = get_tag();
size_t length = get_input_size();
char *input = malloc(length);
load_input(input, length);
printf("wasm c sfn received %zu bytes with tag[%#x]\n", length, tag);
// process app data
char *output = malloc(length);
for (size_t i = 0; i < length; i++) {
output[i] = toupper(input[i]);
}
// dump output data
dump_output(0x34, output, length);
free(input);
free(output);
}
Compile to WASI (opens in a new tab)
# download wasi-sdk from https://github.com/WebAssembly/wasi-sdk/releases,
# and specify wasi-sdk version and path
$ export WASI_VERSION_FULL=20.0
$ export WASI_SDK_PATH=~/Downloads/wasi-sdk-$WASI_VERSION_FULL
$ $WASI_SDK_PATH/bin/clang --target=wasm32-wasi \
--sysroot=$WASI_SDK_PATH/share/wasi-sysroot \
-nostartfiles -fvisibility=hidden -O3 \
-Wl,--no-entry,--export=yomo_init,--export=yomo_handler,--export=yomo_observe_datatags \
-o sfn.wasm sfn.c
Run Streaming Serverless Function
yomo run /path/to/sfn.wasm