Skip to content

feat(wasm): add CreateWasmFuncWithReturn for value-returning JS callbacks#28

Merged
felipegenef merged 1 commit into
mainfrom
feat/create-wasm-func-with-return-callback
Jun 2, 2026
Merged

feat(wasm): add CreateWasmFuncWithReturn for value-returning JS callbacks#28
felipegenef merged 1 commit into
mainfrom
feat/create-wasm-func-with-return-callback

Conversation

@felipegenef

Copy link
Copy Markdown
Owner

What's new

CreateWasmFuncWithReturn — Value-Returning JS Callback Constructor

Gothic WASM code can now hand a value-returning Go closure to JavaScript without
dropping down to ExecJS. CreateWasmFuncWithReturn wraps syscall/js.FuncOf, registers the
closure as a named global, and returns a JSValue holding the underlying js.Func
so it can be assigned directly to a JS object property.

This closes the last gap in the Go→JS callback family. Until now we had:

  • CreateWasmFunc / CreateWasmStringFunc / CreateWasmBoolFunc — fire-and-forget
    callbacks invoked from HTML attributes, all returning void.

Third-party JS libraries (uPlot, Chart.js, Leaflet, ECharts, …) routinely accept
option callbacks that must return a value synchronously — axis tick formatters,
tooltip renderers, point-style resolvers. Those could not be expressed with the
void-returning helpers, forcing an ExecJS string-eval workaround that lost type
safety and Go-side state. CreateWasmFuncWithReturn makes them first-class.

The js.Func is retained for the lifetime of the page (added to the runtime keep
slice and never released), matching the persistence semantics of the existing
AddEventListener* and CreateWasm* helpers — so it is safe to install once during
ClientSideState setup.

Signature

func CreateWasmFuncWithReturn(name string, fn func(this JSValue, args []JSValue) any) JSValue

The returned any is marshalled back to JS via the same conversion used by
JSValue.Set/Call, so returning a JSValue, a string, a number, or a bool all work
as expected.

Example

import . "github.com/felipegenef/gothicframework/v2/pkg/wasm"

func ClientSideState() {
    // Register a callback and pass it as a property on a JS library config object.
    cb := CreateWasmFuncWithReturn("myFormatter", func(this JSValue, args []JSValue) any {
        return args[0].String() + "_suffix"
    })

    config := JS().Get("Object").New()
    config.Set("formatter", cb) // JS calls this and reads the return value
}

Bug fixes / improvements

None.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@felipegenef felipegenef merged commit 353975d into main Jun 2, 2026
1 check passed
@felipegenef felipegenef deleted the feat/create-wasm-func-with-return-callback branch June 2, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants