diff --git a/Makefile b/Makefile index 74c9ea01660..74438b6efde 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ build-jsonschema-generator: create-out-dir ## Build the jsonschema-generator in CGO_ENABLED=0 GO111MODULE=on $(GOWORK_ENV) $(GOCMD) build $(MODFLAG) -o out/bin/jsonschema-generator ./cmd/jsonschema-generator/ build-wasm: create-out-dir ## Build the wasm evaluation library in out/bin/ - cd cmd/wasm && $(TINYGOCMD) build -o ../../out/bin/gofeatureflag-evaluation.wasm -target wasm -opt=2 -opt=s --no-debug -scheduler=none + cd cmd/wasm && $(TINYGOCMD) build -o ../../out/bin/gofeatureflag-evaluation.wasm -target wasm-unknown -opt=2 -opt=s --no-debug -scheduler=none build-wasi: create-out-dir ## Build the wasi evaluation library in out/bin/ cd cmd/wasm && $(TINYGOCMD) build -o ../../out/bin/gofeatureflag-evaluation.wasi -target wasi -opt=2 -opt=s --no-debug -scheduler=none diff --git a/cmd/wasm/main.go b/cmd/wasm/main.go index b31f420ed72..99a06e38dbf 100644 --- a/cmd/wasm/main.go +++ b/cmd/wasm/main.go @@ -10,6 +10,8 @@ import ( "github.com/thomaspoignant/go-feature-flag/modules/core/model" "github.com/thomaspoignant/go-feature-flag/modules/core/utils" "github.com/thomaspoignant/go-feature-flag/modules/evaluation" + + "unsafe" ) // main is the entry point for the wasm module. @@ -73,3 +75,31 @@ func convertEvaluationCtx(ctx map[string]any) (ffcontext.Context, error) { return ffcontext.NewEvaluationContextBuilder("").Build(), fmt.Errorf("targetingKey not found in context") } + + +// alloc/free implementation for wasm-unknown target from: +// https://github.com/tinygo-org/tinygo/blob/2a76ceb7dd5ea5a834ec470b724882564d9681b3/src/runtime/arch_tinygowasm_malloc.go#L7 +var allocs = make(map[uintptr][]byte) + +//export wasm_malloc +func wasm_malloc(size uintptr) unsafe.Pointer { + if size == 0 { + return nil + } + buf := make([]byte, size) + ptr := unsafe.Pointer(&buf[0]) + allocs[uintptr(ptr)] = buf + return ptr +} + +//export wasm_free +func wasm_free(ptr unsafe.Pointer) { + if ptr == nil { + return + } + if _, ok := allocs[uintptr(ptr)]; ok { + delete(allocs, uintptr(ptr)) + } else { + panic("free: invalid pointer") + } +}