Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new input interface, add logging functions #1

Merged
merged 7 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI
on: [push, pull_request]

jobs:
test-example:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
steps:
- uses: actions/checkout@v3

- name: Install emcc compiler
uses: mymindstorm/setup-emsdk@v11
with:
version: latest
actions-cache-folder: "emsdk-cache"

- name: Verify emcc install
run: emcc -v

- name: Setup Python env
uses: actions/setup-python@v4
with:
python-version: "3.9"
check-latest: true

- name: Install Poetry
uses: snok/install-poetry@v1

- name: Checkout extism/cli
uses: actions/checkout@v3
with:
repository: extism/cli
path: cli

- name: Install Extism & CLI
run: |
pushd cli
pip3 install cffi
pip3 install .
popd

extism install latest

- name: Compile example
run: |
make example

- name: Test example
run: |
# --wasi is needed since stdio's snprintf() is called in the example/count_vowels.c source
TEST=$(extism call example.wasm --input "this is a test" --wasi count_vowels)
echo $TEST | grep '"count": 4'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: example
example:
emcc -o example.wasm example/count_vowels.c --no-entry -Wl,--export-all -sERROR_ON_UNDEFINED_SYMBOLS=0

34 changes: 34 additions & 0 deletions example/count_vowels.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "../extism-pdk.h"

#include <stdio.h>

int32_t count_vowels()
{
uint64_t length = extism_input_length();

if (length == 0)
{
return 0;
}

int64_t count = 0;
char ch = 0;
for (int64_t i = 0; i < length; i++)
{
ch = extism_input_load_u8(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
count += 1;
}
}

char out[128];
int n = snprintf(out, 128, "{\"count\": %lld}", count);

uint64_t offs_ = extism_alloc(n);
extism_store(offs_, (const uint8_t *)out, n);
extism_output_set(offs_, n);

return 0;
}
34 changes: 33 additions & 1 deletion extism-pdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))

IMPORT("env", "extism_input_offset") extern uint64_t extism_input_offset();
IMPORT("env", "extism_input_length") extern uint64_t extism_input_length();
IMPORT("env", "extism_length") extern uint64_t extism_length(uint64_t);
IMPORT("env", "extism_alloc") extern uint64_t extism_alloc(uint64_t);
IMPORT("env", "extism_free") extern void extism_free(uint64_t);

IMPORT("env", "extism_input_load_u8")
extern uint8_t extism_input_load_u8(uint64_t);

IMPORT("env", "extism_input_load_u64")
extern uint64_t extism_input_load_u64(uint64_t);

IMPORT("env", "extism_output_set")
extern void extism_output_set(uint64_t, uint64_t);

Expand Down Expand Up @@ -45,6 +51,15 @@ extern uint64_t extism_load_u64(uint64_t);
IMPORT("env", "extism_http_request")
extern uint64_t extism_http_request(uint64_t, uint64_t);

IMPORT("env", "extism_log_info")
extern void extism_log_info(uint64_t);
IMPORT("env", "extism_log_debug")
extern void extism_log_debug(uint64_t);
IMPORT("env", "extism_log_warn")
extern void extism_log_warn(uint64_t);
IMPORT("env", "extism_log_error")
extern void extism_log_error(uint64_t);

static void extism_load(uint64_t offs, uint8_t *buffer, uint64_t length) {
uint64_t n;
uint64_t left = 0;
Expand All @@ -62,6 +77,23 @@ static void extism_load(uint64_t offs, uint8_t *buffer, uint64_t length) {
}
}

static void extism_load_input(uint8_t *buffer, uint64_t length) {
uint64_t n;
uint64_t left = 0;

for (uint64_t i = 0; i < length; i += 1) {
left = length - i;
if (left < 8) {
buffer[i] = extism_input_load_u8(i);
continue;
}

n = extism_input_load_u64(i);
*((uint64_t *)buffer + (i / 8)) = n;
i += 7;
}
}

static void extism_store(uint64_t offs, const uint8_t *buffer,
uint64_t length) {
uint64_t n;
Expand Down