WebAssembly interop between Go and JS values.

vert

GoDoc Build Status

Package vert provides WebAssembly interop between Go and JS values.

Install

GOOS=js GOARCH=wasm go get github.com/norunners/vert

Examples

Hello World!

Below is a trivial string value interop.

package main

import "github.com/norunners/vert"

func main() {
	v := vert.ValueOf("Hello World!")
	// Use v as a JS value.

	s := ""
	v.AssignTo(&s)
	// Use s as a Go value.
}

Structs & Objects

Go structs and JS objects interop seamlessly.

package main

import "github.com/norunners/vert"

type Data struct {
	Message string
}

func main() {
	v := vert.ValueOf(Data{
		Message: "Hello World!",
	})
	// e.g. {"Message": "Hello World!"}

	d := &Data{}
	v.AssignTo(d)
}

Tagged Struct Fields

Tagged struct fields allow defined JS field names.

package main

import "github.com/norunners/vert"

type Data struct {
	Message string `js:"msg"`
}

func main() {
	v := vert.ValueOf(Data{
		Message: "Hello World!",
	})
	// The defined JS tag names the field.
	// e.g. {"msg": "Hello World!"}

	d := &Data{}
	v.AssignTo(d)
}

Error Handling

AssignTo returns an error value.

package main

import "github.com/norunners/vert"

type Data struct {
	Message string
}

func main() {
	v := vert.ValueOf("Hello World!")

	d := &Data{}
	if err := v.AssignTo(d); err != nil {
		// Handle error.
	}
}

Why?

Package syscall/js, of the Go standard library, has limited interop support between Go and JS values.

  1. The function js.ValueOf will not accept struct types. The result panics with ValueOf: invalid value.
  2. The type js.Value. does not support an Interface or assignment method for non-basic Go values. However, the methods Bool, Int, Float and String support basic Go values.

Package vert leverages and extends syscall/js to accommodate these shortcomings.

License

Owner
Gopher hacker and WebAssembly fan.
null
Comments
  • Fix go 1.19 js wrapper issue

    Fix go 1.19 js wrapper issue

    This PR replaces one aspect of the #17 PR.

    with Go 1.18, the js.Wrapper is no more available, so I removed it. See https://tip.golang.org/doc/go1.18

    This will fix #16

  • Compile errors with go1.14

    Compile errors with go1.14

    Was playing around the lib to probably cover some conversion cases, but unfortunately program using it doesn't compile:

    go: finding module for package github.com/norunners/vert
    go: found github.com/norunners/vert in github.com/norunners/vert v0.0.0-20190616030331-d9c8c2aacdf1
    # github.com/norunners/vert
    ..\..\..\..\pkg\mod\github.com\norunners\[email protected]\assign.go:39:8: invalid operation: jv == js.Null() (struct containing [0]func() cannot be compared)
    ..\..\..\..\pkg\mod\github.com\norunners\[email protected]\assign.go:39:27: invalid operation: jv == js.Undefined() (struct containing [0]func() cannot be compared)
    
  • remove js.Wrapper and add time.Time convenience function

    remove js.Wrapper and add time.Time convenience function

    Hi,

    this is a great lib. While tinkering around, I found two issues, I would like to share/improve. This PR covers two aspects.

    • with Go 1.18, the js.Wrapper is no more available, so i removed it. See https://tip.golang.org/doc/go1.18
    • for type time.Time, a "new Date" object in JS is created.

    Any feedback is welcome.

  • Build failures under Go 1.18 due to js.Wrapper removal

    Build failures under Go 1.18 due to js.Wrapper removal

    js.Wrapper was removed in Go 1.18. Thus, this library fails to build under Go 1.18 due to e.g., https://github.com/norunners/vert/blob/main/value.go#L30.

    See https://github.com/golang/go/issues/50310 for some additional background; the proposal to reinstate it was declined.

  • upgrade actions/setup-go@v3 and actions/checkout@v3, because Github warnings

    upgrade actions/setup-go@v3 and actions/checkout@v3, because Github warnings

    This PR replaces one aspect of the #17 PR.

    This PR fixes warnings in the Github Action, as described: https://github.com/norunners/vert/pull/17#issuecomment-1320887148

  • Add support for array types

    Add support for array types

    This PR adds support for array types in AssignTo and ValueOf. Prior to this PR:

    • Passing an array into ValueOf would result in panic: reflect: call of reflect.Value.IsNil on array Value.
    • Passing an array into AssignTo would result in invalid assignment from JS type: object to Go kind: array.
  • add convenience method for time.Time conversion

    add convenience method for time.Time conversion

    This PR replaces one aspect of the #17 PR.

    When converting time.Time objects, a "new Date" object in JS is created. This is very convenient.

    For testing only, this PR will introduce the https://github.com/corbym/gocrest library. The library enables writing more readable code (clean code) by applying the hamcrest matcher style

Go compiler for small places. Microcontrollers, WebAssembly, and command-line tools. Based on LLVM.

TinyGo - Go compiler for small places TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (Wasm), and comma

Dec 30, 2022
A package to build progressive web apps with Go programming language and WebAssembly.
A package to build progressive web apps with Go programming language and WebAssembly.

Go-app is a package for building progressive web apps (PWA) with the Go programming language (Golang) and WebAssembly (Wasm). Shaping a UI is done by

Dec 30, 2022
A package to build progressive web apps with Go programming language and WebAssembly.
A package to build progressive web apps with Go programming language and WebAssembly.

Go-app is a package for building progressive web apps (PWA) with the Go programming language (Golang) and WebAssembly (Wasm). Shaping a UI is done by

Jan 2, 2023
WebAssembly for Proxies (Golang host implementation)

WebAssembly for Proxies (GoLang host implementation) The GoLang implementation for proxy-wasm, enabling developer to run proxy-wasm extensions in Go.

Dec 29, 2022
🐹🕸️ WebAssembly runtime for Go
🐹🕸️ WebAssembly runtime for Go

Wasmer Go Website • Docs • Slack Channel A complete and mature WebAssembly runtime for Go based on Wasmer. Features Easy to use: The wasmer API mimics

Jan 2, 2023
🐹🕸️ WebAssembly runtime for Go
🐹🕸️ WebAssembly runtime for Go

Wasmer Go Website • Docs • Slack Channel A complete and mature WebAssembly runtime for Go based on Wasmer. Features Easy to use: The wasmer API mimics

Dec 29, 2022
Vugu: A modern UI library for Go+WebAssembly (experimental)

Vugu: A modern UI library for Go+WebAssembly (experimental)

Jan 3, 2023
WebAssembly runtime for wasmer-go
WebAssembly runtime for wasmer-go

gowasmer When compiling Go to WebAssembly, the Go compiler assumes the WebAssembly is going to run in a JavaScript environment. Hence a wasm_exec.js f

Dec 28, 2022
A template project to demonstrate how to run WebAssembly functions as sidecar microservices in dapr
A template project to demonstrate how to run WebAssembly functions as sidecar microservices in dapr

Live Demo 1. Introduction DAPR is a portable, event-driven runtime that makes it easy for any developer to build resilient, stateless and stateful app

Jan 3, 2023
Tiny, blazing fast WebAssembly compute

Sat, the tiny WebAssembly compute module Sat (as in satellite) is an experiment, and isn't ready for production use. Please try it out and give feedba

Jan 5, 2023
WebAssembly Lightweight Javascript Framework in Go (AngularJS Inspired)

Tango Lightweight WASM HTML / Javascript Framework Intro WebAssembly is nice, Go on the web is nice, so I ported Tangu to Go and WebAssembly. Tangu is

Dec 20, 2022
Running a Command line tool written in Go on browser with WebAssembly

Running a command line tool written in Go on browser with WebAssembly This repo contains code/assets from the article Files: . ├── article.md

Dec 30, 2022
This library provides WebAssembly capability for goja Javascript engine

This module provides WebAssembly functions into goja javascript engine.

Jan 10, 2022
A Brainfuck to WebAssembly compiler written in Go.

brainfuck2wasm A Brainfuck to WebAssembly compiler written in Go. I am writing this compiler for a Medium article. When I complete the compiler, I'll

Jun 6, 2022
Dom - A Go API for different Web APIs for WebAssembly target

Go DOM binding (and more) for WebAssembly This library provides a Go API for dif

Jan 7, 2023
wazero: the zero dependency WebAssembly runtime for Go developers

wazero: the zero dependency WebAssembly runtime for Go developers WebAssembly is a way to safely run code compiled in other languages. Runtimes execut

Jan 2, 2023
Aes for go and java; build go fo wasm and use wasm parse java response.

aes_go_wasm_java aes for go and java; build go fo wasm and use wasm parse java response. vscode setting config settings.json { "go.toolsEnvVars":

Dec 14, 2021
DOM library for Go and WASM

Go DOM binding (and more) for WebAssembly This library provides a Go API for different Web APIs for WebAssembly target. It's in an active development,

Dec 23, 2022
Fast face detection, pupil/eyes localization and facial landmark points detection library in pure Go.
Fast face detection, pupil/eyes localization and facial landmark points detection library in pure Go.

Pigo is a pure Go face detection, pupil/eyes localization and facial landmark points detection library based on Pixel Intensity Comparison-based Objec

Dec 24, 2022