Skip to content
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"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.
Expand Down Expand Up @@ -73,3 +75,31 @@
return ffcontext.NewEvaluationContextBuilder("").Build(),
fmt.Errorf("targetingKey not found in context")
}


// alloc/free implementation for wasm-unknown target from:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When not targeting wasm-wasi or gojs tinygo doesn't exports those functions.
Here we are exporting stable identifiers to be used on any target.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I didn't know about that, thanks for the addition.
I will test the full pipeline locally with your 2 PRs tonight or tomorrow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know how it goes and we continue from there 👍

// 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 {

Check warning on line 85 in cmd/wasm/main.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename function "wasm_malloc" to match the regular expression ^(_|[a-zA-Z0-9]+)$

See more on https://sonarcloud.io/project/issues?id=thomaspoignant_go-feature-flag&issues=AZq62tfe1FjsrRFXLJoa&open=AZq62tfe1FjsrRFXLJoa&pullRequest=4366
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) {

Check warning on line 96 in cmd/wasm/main.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename function "wasm_free" to match the regular expression ^(_|[a-zA-Z0-9]+)$

See more on https://sonarcloud.io/project/issues?id=thomaspoignant_go-feature-flag&issues=AZq62tfe1FjsrRFXLJob&open=AZq62tfe1FjsrRFXLJob&pullRequest=4366
if ptr == nil {
return
}
if _, ok := allocs[uintptr(ptr)]; ok {
delete(allocs, uintptr(ptr))
} else {
panic("free: invalid pointer")
}
}
Loading