WebAssembly interop between Go and JS values.

Overview

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

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

    opened by nitram509 9
  • 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)
    
    opened by inliquid 6
  • 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.

    opened by nitram509 4
  • 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.

    opened by ralimi 2
  • upgrade actions/setup-go@v3 and actions/checkout@v3, because Github warnings

    upgrade actions/[email protected] and actions/[email protected], 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

    opened by nitram509 1
  • 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.
    opened by albrow 1
  • 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

    opened by nitram509 1
Owner
Gopher hacker and WebAssembly fan.
null
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

TinyGo 12.1k Dec 30, 2022
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

Maxence Charriere 6.7k Dec 30, 2022
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

Maxence Charriere 6.7k 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.

MOSN 38 Dec 29, 2022
🐹🕸️ 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

Wasmer 2.3k Jan 2, 2023
🐹🕸️ 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

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

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

Vugu 4.5k Jan 3, 2023
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

mattn 99 Dec 28, 2022
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

Second State 185 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

Suborbital 320 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

enimatek 8 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

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

This module provides WebAssembly functions into goja javascript engine.

YC-L 1 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

Luke I. Wilson 2 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

Denys Smirnov 472 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

Tetrate Labs 2.3k 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":

忆年 0 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,

Denys Smirnov 469 Dec 23, 2022
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

Endre Simo 3.9k Dec 24, 2022