jsvalue

package module
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2026 License: MIT Imports: 3 Imported by: 0

README

jsvalue

Efficient conversions between JavaScript and Go for a WebAssembly environment with support for TinyGo.

Usage

Importing
import "github.com/tinywasm/jsvalue"
ToJS

Converts Go values to syscall/js.Value.

// Basic types
val := jsvalue.ToJS(123)
val := jsvalue.ToJS("hello")

// Complex types (slices, maps, structs)
data := map[string]any{"foo": 1, "bar": "baz"}
jsVal := jsvalue.ToJS(data)
ToGo

Converts syscall/js.Value to Go values, populating a pointer destination. this allows for zero-allocation conversions when reusing existing data structures.

// Integers (Zero allocation)
var n int
err := jsvalue.ToGo(jsVal, &n)

// Reuse map to minimize allocations
// (Allocates ~56B vs ~408B when creating new map)
data := make(map[string]any)
err := jsvalue.ToGo(jsObj, &data)

// Structs
type User struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}
var user User
err := jsvalue.ToGo(jsObj, &user)

Performance Results

Last updated: 2025-12-11 14:18:12

goos: js
goarch: wasm
pkg: github.com/tinywasm/jsvalue
BenchmarkToJS_Int       	325238552	        18.44 ns/op	       0 B/op	       0 allocs/op
BenchmarkToJS_String    	 3052036	      2032 ns/op	       8 B/op	       1 allocs/op
BenchmarkToJS_Struct    	  816326	      6634 ns/op	      56 B/op	       7 allocs/op
BenchmarkToGo_Int       	378214886	        15.89 ns/op	       0 B/op	       0 allocs/op
BenchmarkToGo_Struct    	  881052	      6696 ns/op	      56 B/op	       7 allocs/op
BenchmarkToGo_Any_Int   	35842251	       159.6 ns/op	      24 B/op	       2 allocs/op
BenchmarkToGo_Any_Map   	  425230	     15862 ns/op	     408 B/op	       9 allocs/op
BenchmarkToGo_Map_Reuse 	  542494	     13395 ns/op	      56 B/op	       6 allocs/op
PASS

Last updated: 2025-12-11 13:57:12

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

View Source
var Uint8ArrayClass = js.Global().Get("Uint8Array")

Uint8ArrayClass is the JS Uint8Array constructor.

Functions

func AwaitPromise added in v0.0.11

func AwaitPromise(p js.Value) (js.Value, error)

AwaitPromise blocks the current goroutine until the JS Promise p resolves or rejects. Safe in WASM: channel receive yields the goroutine; the JS event loop keeps running.

func AwaitRequest added in v0.0.11

func AwaitRequest(req js.Value) (js.Value, error)

AwaitRequest blocks the current goroutine until the IndexedDB request req fires its "success" or "error" event. Ported from tinywasm/indexdb processRequest — proven pattern.

func ScanValue added in v0.0.12

func ScanValue(v js.Value, dest any) error

ScanValue copies the JS value v into the Go pointer dest. Supports *string, *int, *int64, *int32, *float64, *bool, *[]byte, *any. []byte is read from a Uint8Array via js.CopyBytesToGo.

func ToAny added in v0.0.12

func ToAny(v js.Value) any

ToAny converts a JS value to a Go any. Integer numbers are returned as int64; non-integer numbers as float64.

func ToGo

func ToGo(jsVal js.Value, v any) error

ToGo converts JavaScript values to Go values.

func ToJS

func ToJS(data any) js.Value

ToJS converts Go values to JavaScript values recursively.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL