From bf4b7f77d1781bbf93be8ea0ff6e62d40d69ad5d Mon Sep 17 00:00:00 2001 From: felipegenef Date: Tue, 2 Jun 2026 17:15:26 -0300 Subject: [PATCH] feat(wasm): add CreateWasmFuncWithReturn for value-returning JS callbacks --- cmd/version.go | 2 +- pkg/wasm/stubs.go | 16 ++++++++++++++++ pkg/wasm/wasm-runtime/runtime/events.go | 18 ++++++++++++++++++ pkg/wasm/wasm-runtime/runtime/events_stub.go | 2 ++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/version.go b/cmd/version.go index 21a6296..63347bc 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -var CURRENT_VERSION string = "v2.17.3" +var CURRENT_VERSION string = "v2.17.4" // versionCmd represents the version command var versionCmd = &cobra.Command{ diff --git a/pkg/wasm/stubs.go b/pkg/wasm/stubs.go index 2e3e715..5786b0a 100644 --- a/pkg/wasm/stubs.go +++ b/pkg/wasm/stubs.go @@ -204,6 +204,22 @@ func CreateWasmFunc(name string, fn func()) {} func CreateWasmStringFunc(name string, fn func(string)) {} func CreateWasmBoolFunc(name string, fn func(bool)) {} +// CreateWasmFuncWithReturn registers a named global JS function that can return a value back to JS. +// Wraps syscall/js.FuncOf. Use when a JS library expects a callback that returns a value +// synchronously (e.g. option objects, formatters, renderers). The function persists for the +// lifetime of the page. Returns a JSValue so it can be passed directly to JS object properties. +// +// Example: +// +// // Register a callback and pass it as a property on a JS config object: +// cb := CreateWasmFuncWithReturn("myCallback", func(this JSValue, args []JSValue) any { +// return args[0].String() + "_suffix" +// }) +// config.Set("formatter", cb) +func CreateWasmFuncWithReturn(name string, fn func(this JSValue, args []JSValue) any) JSValue { + return JSValue{} +} + // ── Topic infrastructure (generated code only — not part of the user API) ──── // TopicKey is a typed key used by the auto-generated topic system. diff --git a/pkg/wasm/wasm-runtime/runtime/events.go b/pkg/wasm/wasm-runtime/runtime/events.go index 7d75a77..6c3ddae 100644 --- a/pkg/wasm/wasm-runtime/runtime/events.go +++ b/pkg/wasm/wasm-runtime/runtime/events.go @@ -205,3 +205,21 @@ func CreateWasmBoolFunc(name string, fn func(bool)) { }) }) } + +// CreateWasmFuncWithReturn registers a named global JS function that can return a value back to JS. +// Wraps syscall/js.FuncOf. The returned JSValue holds the js.Func so it can be passed +// directly to JS object properties (chart configs, map renderers, etc.) that require a +// synchronous return value. The js.Func is retained in keep and never released — it must +// stay alive for the lifetime of the page; releasing it would panic on the next invocation. +func CreateWasmFuncWithReturn(name string, fn func(this JSValue, args []JSValue) any) JSValue { + f := js.FuncOf(func(this js.Value, args []js.Value) interface{} { + jsArgs := make([]JSValue, len(args)) + for i, a := range args { + jsArgs[i] = JSValue{a} + } + return toJSVal(fn(JSValue{this}, jsArgs)) + }) + keep = append(keep, f) + js.Global().Set(name, f) + return JSValue{f.Value} +} diff --git a/pkg/wasm/wasm-runtime/runtime/events_stub.go b/pkg/wasm/wasm-runtime/runtime/events_stub.go index a22db3f..73f538b 100644 --- a/pkg/wasm/wasm-runtime/runtime/events_stub.go +++ b/pkg/wasm/wasm-runtime/runtime/events_stub.go @@ -5,3 +5,5 @@ package runtime func CreateWasmFunc(name string, fn func()) {} func CreateWasmStringFunc(name string, fn func(string)) {} func CreateWasmBoolFunc(name string, fn func(bool)) {} + +func CreateWasmFuncWithReturn(name string, fn func(this JSValue, args []JSValue) any) JSValue { return JSValue{} }