diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b9c6b3..db21b8e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,13 +22,13 @@ jobs: uses: actions/setup-go@v3 with: cache: true - go-version: '1.20' + go-version: '1.21.1' - name: Install TinyGo uses: acifani/setup-tinygo@v1.1.0 with: - tinygo-version: 0.28.1 - binaryen-version: "110" + tinygo-version: 0.30.0 + binaryen-version: "116" - name: Compile example run: | diff --git a/env.go b/env.go new file mode 100644 index 0000000..b9edc53 --- /dev/null +++ b/env.go @@ -0,0 +1,76 @@ +package pdk + +type extismPointer uint64 + +//go:wasmimport env extism_input_length +func extism_input_length() uint64 + +//go:wasmimport env extism_length +func extism_length(extismPointer) uint64 + +//go:wasmimport env extism_alloc +func extism_alloc(uint64) extismPointer + +//go:wasmimport env extism_free +func extism_free(extismPointer) + +//go:wasmimport env extism_input_load_u8 +func extism_input_load_u8_(extismPointer) uint32 + +func extism_input_load_u8(p extismPointer) uint8 { + return uint8(extism_input_load_u8_(p)) +} + +//go:wasmimport env extism_input_load_u64 +func extism_input_load_u64(extismPointer) uint64 + +//go:wasmimport env extism_output_set +func extism_output_set(extismPointer, uint64) + +//go:wasmimport env extism_error_set +func extism_error_set(extismPointer) + +//go:wasmimport env extism_config_get +func extism_config_get(extismPointer) extismPointer + +//go:wasmimport env extism_var_get +func extism_var_get(extismPointer) extismPointer + +//go:wasmimport env extism_var_set +func extism_var_set(extismPointer, extismPointer) + +//go:wasmimport env extism_store_u8 +func extism_store_u8_(extismPointer, uint32) +func extism_store_u8(p extismPointer, v uint8) { + extism_store_u8_(p, uint32(v)) +} + +//go:wasmimport env extism_load_u8 +func extism_load_u8_(extismPointer) uint32 +func extism_load_u8(p extismPointer) uint8 { + return uint8(extism_load_u8_(p)) +} + +//go:wasmimport env extism_store_u64 +func extism_store_u64(extismPointer, uint64) + +//go:wasmimport env extism_load_u64 +func extism_load_u64(extismPointer) uint64 + +//go:wasmimport env extism_http_request +func extism_http_request(extismPointer, extismPointer) extismPointer + +//go:wasmimport env extism_http_status_code +func extism_http_status_code() int32 + +//go:wasmimport env extism_log_info +func extism_log_info(extismPointer) + +//go:wasmimport env extism_log_debug +func extism_log_debug(extismPointer) + +//go:wasmimport env extism_log_warn +func extism_log_warn(extismPointer) + +//go:wasmimport env extism_log_error +func extism_log_error(extismPointer) diff --git a/example/example.wasm b/example/example.wasm index f3b0261..1692906 100755 Binary files a/example/example.wasm and b/example/example.wasm differ diff --git a/example/go.mod b/example/go.mod index dbc1449..bb3e145 100644 --- a/example/go.mod +++ b/example/go.mod @@ -1,6 +1,6 @@ module github.com/extism/go-pdk/example -go 1.19 +go 1.21.0 require github.com/extism/go-pdk v0.0.0-20220910220145-f385a5f19d1d diff --git a/example/http.wasm b/example/http.wasm index a50a545..7cedefe 100755 Binary files a/example/http.wasm and b/example/http.wasm differ diff --git a/extism-pdk.h b/extism-pdk.h deleted file mode 100644 index 93cf92e..0000000 --- a/extism-pdk.h +++ /dev/null @@ -1,111 +0,0 @@ -#pragma once - -#include - -#define IMPORT(a, b) __attribute__((import_module(a), import_name(b))) - -typedef uint64_t ExtismPointer; - -IMPORT("env", "extism_input_length") extern uint64_t extism_input_length(); -IMPORT("env", "extism_length") extern uint64_t extism_length(ExtismPointer); -IMPORT("env", "extism_alloc") extern ExtismPointer extism_alloc(uint64_t); -IMPORT("env", "extism_free") extern void extism_free(ExtismPointer); - -IMPORT("env", "extism_input_load_u8") -extern uint8_t extism_input_load_u8(ExtismPointer); - -IMPORT("env", "extism_input_load_u64") -extern uint64_t extism_input_load_u64(ExtismPointer); - -IMPORT("env", "extism_output_set") -extern void extism_output_set(ExtismPointer, uint64_t); - -IMPORT("env", "extism_error_set") -extern void extism_error_set(ExtismPointer); - -IMPORT("env", "extism_config_get") -extern ExtismPointer extism_config_get(ExtismPointer); - -IMPORT("env", "extism_var_get") -extern ExtismPointer extism_var_get(ExtismPointer); - -IMPORT("env", "extism_var_set") -extern void extism_var_set(ExtismPointer, ExtismPointer); - -IMPORT("env", "extism_store_u8") -extern void extism_store_u8(ExtismPointer, uint8_t); - -IMPORT("env", "extism_load_u8") -extern uint8_t extism_load_u8(ExtismPointer); - -IMPORT("env", "extism_store_u64") -extern void extism_store_u64(ExtismPointer, uint64_t); - -IMPORT("env", "extism_load_u64") -extern uint64_t extism_load_u64(ExtismPointer); - -IMPORT("env", "extism_http_request") -extern ExtismPointer extism_http_request(ExtismPointer, ExtismPointer); - -IMPORT("env", "extism_http_status_code") -extern int32_t extism_http_status_code(); - -IMPORT("env", "extism_log_info") -extern void extism_log_info(ExtismPointer); -IMPORT("env", "extism_log_debug") -extern void extism_log_debug(ExtismPointer); -IMPORT("env", "extism_log_warn") -extern void extism_log_warn(ExtismPointer); -IMPORT("env", "extism_log_error") -extern void extism_log_error(ExtismPointer); - -static void extism_load(ExtismPointer offs, 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_load_u8(offs + i); - continue; - } - - n = extism_load_u64(offs + i); - *((uint64_t *)buffer + (i / 8)) = n; - i += 7; - } -} - -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(ExtismPointer offs, const uint8_t *buffer, - uint64_t length) { - uint64_t n; - uint64_t left = 0; - for (uint64_t i = 0; i < length; i++) { - left = length - i; - if (left < 8) { - extism_store_u8(offs + i, buffer[i]); - continue; - } - - n = *((uint64_t *)buffer + (i / 8)); - extism_store_u64(offs + i, n); - i += 7; - } -} diff --git a/extism_pdk.go b/extism_pdk.go index 4bfbe60..4b5d7c9 100644 --- a/extism_pdk.go +++ b/extism_pdk.go @@ -7,13 +7,8 @@ import ( "github.com/valyala/fastjson" ) -/* -#include "extism-pdk.h" -*/ -import "C" - type Memory struct { - offset uint64 + offset extismPointer length uint64 } @@ -26,49 +21,49 @@ const ( LogError ) -func load(offset uint64, buf []byte) { +func load(offset extismPointer, buf []byte) { length := len(buf) for i := 0; i < length; i++ { if length-i >= 8 { - x := C.extism_load_u64(offset + uint64(i)) + x := extism_load_u64(offset + extismPointer(i)) binary.LittleEndian.PutUint64(buf[i:i+8], x) i += 7 continue } - buf[i] = byte(C.extism_load_u8(offset + uint64(i))) + buf[i] = extism_load_u8(offset + extismPointer(i)) } } func loadInput() []byte { - length := int(C.extism_input_length()) + length := int(extism_input_length()) buf := make([]byte, length) for i := 0; i < length; i++ { if length-i >= 8 { - x := C.extism_input_load_u64(uint64(i)) + x := extism_input_load_u64(extismPointer(i)) binary.LittleEndian.PutUint64(buf[i:i+8], x) i += 7 continue } - buf[i] = byte(C.extism_input_load_u8(uint64(i))) + buf[i] = extism_input_load_u8(extismPointer(i)) } return buf } -func store(offset uint64, buf []byte) { +func store(offset extismPointer, buf []byte) { length := len(buf) for i := 0; i < length; i++ { if length-i >= 8 { x := binary.LittleEndian.Uint64(buf[i : i+8]) - C.extism_store_u64(offset+uint64(i), C.uint64_t(x)) + extism_store_u64(offset+extismPointer(i), x) i += 7 continue } - C.extism_store_u8(offset+uint64(i), C.uint8_t(buf[i])) + extism_store_u8(offset+extismPointer(i), buf[i]) } } @@ -77,24 +72,24 @@ func Input() []byte { } func Allocate(length int) Memory { - clength := C.uint64_t(length) - offset := C.extism_alloc(clength) + clength := uint64(length) + offset := extism_alloc(clength) return Memory{ - offset: uint64(offset), - length: uint64(clength), + offset: offset, + length: clength, } } func AllocateBytes(data []byte) Memory { - clength := C.uint64_t(len(data)) - offset := C.extism_alloc(clength) + clength := uint64(len(data)) + offset := extism_alloc(clength) store(offset, data) return Memory{ - offset: uint64(offset), - length: uint64(clength), + offset: offset, + length: clength, } } @@ -108,15 +103,15 @@ func InputString() string { } func OutputMemory(mem Memory) { - C.extism_output_set(mem.offset, mem.length) + extism_output_set(mem.offset, mem.length) } func Output(data []byte) { - clength := C.uint64_t(len(data)) - offset := C.extism_alloc(clength) + clength := uint64(len(data)) + offset := extism_alloc(clength) store(offset, data) - C.extism_output_set(offset, clength) + extism_output_set(offset, clength) } func OutputString(s string) { @@ -127,13 +122,13 @@ func GetConfig(key string) (string, bool) { mem := AllocateBytes([]byte(key)) defer mem.Free() - offset := C.extism_config_get(C.uint64_t(mem.offset)) - clength := C.extism_length(offset) + offset := extism_config_get(mem.offset) + clength := extism_length(offset) if offset == 0 || clength == 0 { return "", false } - value := make([]byte, uint64(clength)) + value := make([]byte, clength) load(offset, value) return string(value), true @@ -142,13 +137,13 @@ func GetConfig(key string) (string, bool) { func LogMemory(level LogLevel, memory Memory) { switch level { case LogInfo: - C.extism_log_info(memory.offset) + extism_log_info(memory.offset) case LogDebug: - C.extism_log_debug(memory.offset) + extism_log_debug(memory.offset) case LogWarn: - C.extism_log_warn(memory.offset) + extism_log_warn(memory.offset) case LogError: - C.extism_log_error(memory.offset) + extism_log_error(memory.offset) } } @@ -162,13 +157,13 @@ func Log(level LogLevel, s string) { func GetVar(key string) []byte { mem := AllocateBytes([]byte(key)) - offset := C.extism_var_get(C.uint64_t(mem.offset)) - clength := C.extism_length(offset) + offset := extism_var_get(mem.offset) + clength := extism_length(offset) if offset == 0 || clength == 0 { return nil } - value := make([]byte, uint64(clength)) + value := make([]byte, clength) load(offset, value) return value @@ -181,18 +176,12 @@ func SetVar(key string, value []byte) { valMem := AllocateBytes(value) defer valMem.Free() - C.extism_var_set( - C.uint64_t(keyMem.offset), - C.uint64_t(valMem.offset), - ) + extism_var_set(keyMem.offset, valMem.offset) } func RemoveVar(key string) { mem := AllocateBytes([]byte(key)) - C.extism_var_set( - C.uint64_t(mem.offset), - 0, - ) + extism_var_set(mem.offset, 0) } type HTTPRequest struct { @@ -260,9 +249,9 @@ func (r *HTTPRequest) Send() HTTPResponse { data := AllocateBytes(r.body) defer data.Free() - offset := C.extism_http_request(C.uint64_t(req.offset), data.offset) - length := uint64(C.extism_length(offset)) - status := uint16(C.extism_http_status_code()) + offset := extism_http_request(req.offset, data.offset) + length := extism_length(offset) + status := uint16(extism_http_status_code()) memory := Memory{offset, length} @@ -281,7 +270,7 @@ func (m *Memory) Store(data []byte) { } func (m *Memory) Free() { - C.extism_free(m.offset) + extism_free(m.offset) } func (m *Memory) Length() uint64 { @@ -289,10 +278,10 @@ func (m *Memory) Length() uint64 { } func (m *Memory) Offset() uint64 { - return m.offset + return uint64(m.offset) } func FindMemory(offset uint64) Memory { - length := uint64(C.extism_length(offset)) - return Memory{offset, length} + length := extism_length(extismPointer(offset)) + return Memory{extismPointer(offset), length} } diff --git a/go.mod b/go.mod index 61b66f8..de4c74e 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/extism/go-pdk -go 1.20 +go 1.21.0 require github.com/valyala/fastjson v1.6.3 diff --git a/go.work b/go.work index f6c1540..3efabc8 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.19 +go 1.21.0 use .