From 087380cac2bb1843ed095ee7879a82d0e840721d Mon Sep 17 00:00:00 2001 From: zach Date: Sat, 10 Sep 2022 09:55:25 -0700 Subject: [PATCH 1/7] chore: update for pdk changes --- extism-pdk.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/extism-pdk.h b/extism-pdk.h index fb8674d..ebb4d27 100644 --- a/extism-pdk.h +++ b/extism-pdk.h @@ -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); @@ -62,6 +68,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; From f29634b490c4fed162696ce081e85d2872f98670 Mon Sep 17 00:00:00 2001 From: zach Date: Sat, 10 Sep 2022 15:01:28 -0700 Subject: [PATCH 2/7] feat: add logging --- extism-pdk.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/extism-pdk.h b/extism-pdk.h index ebb4d27..74b8b28 100644 --- a/extism-pdk.h +++ b/extism-pdk.h @@ -51,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; From 2b85f9d00497a8fbec6a3dfbb1e67a3e011e9d42 Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 13 Sep 2022 11:43:55 -0700 Subject: [PATCH 3/7] feat: add example code from extism repo --- .gitignore | 1 + Makefile | 4 ++++ example/count_vowels.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 example/count_vowels.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19e1bce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.wasm diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..01d361c --- /dev/null +++ b/Makefile @@ -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 + diff --git a/example/count_vowels.c b/example/count_vowels.c new file mode 100644 index 0000000..bb6d7b0 --- /dev/null +++ b/example/count_vowels.c @@ -0,0 +1,30 @@ +#include "../extism-pdk.h" + +#include + +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; +} From 0e49da7649a40db01d4b2f0dd005955e92c4aec2 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 14 Sep 2022 12:33:06 -0600 Subject: [PATCH 4/7] ci: test example c plugin in workflow --- .github/workflows/ci.yml | 65 ++++++++++++++++++++++++++++++++++++++++ example/count_vowels.c | 14 +++++---- 2 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5790738 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,65 @@ +name: CI +on: [push, pull_request] + +jobs: + test-example: + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - stable + steps: + - uses: actions/checkout@v3 + + - name: Checkout emscripten-core/emsdk + uses: actions/checkout@v3 + with: + repository: emscripten-core/emsdk + path: emsdk + + - name: Install emcc compiler + run: | + pushd emsdk + ./emsdk install latest + ./emsdk activate latest + source ./emsdk_env.sh + + emsdk install emscripten-main-64bit + emsdk activate emscripten-main-64bit + + emcc -v + popd + + - 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' diff --git a/example/count_vowels.c b/example/count_vowels.c index bb6d7b0..3a5fb7c 100644 --- a/example/count_vowels.c +++ b/example/count_vowels.c @@ -1,20 +1,22 @@ #include "../extism-pdk.h" -#include - -int32_t count_vowels() { +int32_t count_vowels() +{ uint64_t length = extism_input_length(); - if (length == 0) { + if (length == 0) + { return 0; } int64_t count = 0; char ch = 0; - for (int64_t i = 0; i < length; i++) { + 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') { + ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') + { count += 1; } } From 6174e35c2222a841bdec6ddc5bfbc105c48988e2 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 14 Sep 2022 12:38:50 -0600 Subject: [PATCH 5/7] ci: use emscripten installer from action --- .github/workflows/ci.yml | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5790738..1381b27 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,17 +18,13 @@ jobs: path: emsdk - name: Install emcc compiler - run: | - pushd emsdk - ./emsdk install latest - ./emsdk activate latest - source ./emsdk_env.sh - - emsdk install emscripten-main-64bit - emsdk activate emscripten-main-64bit + uses: mymindstorm/setup-emsdk@v11 + with: + version: latest + actions-cache-folder: "emsdk-cache" - emcc -v - popd + - name: Verify emcc install + run: emcc -v - name: Setup Python env uses: actions/setup-python@v4 From 5e213c9bc0265ed3c3d0edd5834e8836018d0843 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 14 Sep 2022 12:42:16 -0600 Subject: [PATCH 6/7] ci: add c header back to example code --- example/count_vowels.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example/count_vowels.c b/example/count_vowels.c index 3a5fb7c..a680988 100644 --- a/example/count_vowels.c +++ b/example/count_vowels.c @@ -1,5 +1,7 @@ #include "../extism-pdk.h" +#include + int32_t count_vowels() { uint64_t length = extism_input_length(); From 592eac42044307ef5da51cf62410f41561029f01 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Wed, 14 Sep 2022 12:44:00 -0600 Subject: [PATCH 7/7] ci: remove needless checkout from workflow --- .github/workflows/ci.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1381b27..fabafb4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,12 +11,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Checkout emscripten-core/emsdk - uses: actions/checkout@v3 - with: - repository: emscripten-core/emsdk - path: emsdk - - name: Install emcc compiler uses: mymindstorm/setup-emsdk@v11 with: