go: github.com/golang/go/src/syscall/js Index | Examples | Files

package js

import "github.com/golang/go/src/syscall/js"

Package js gives access to the WebAssembly host environment when using the js/wasm architecture. Its API is based on JavaScript semantics.

This package is EXPERIMENTAL. Its current scope is only to allow tests to run, but not yet to provide a comprehensive API for users. It is exempt from the Go compatibility promise.

Index

Examples

Package Files

func.go js.go

func CopyBytesToGo Uses

func CopyBytesToGo(dst []byte, src Value) int

CopyBytesToGo copies bytes from the Uint8Array src to dst. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. CopyBytesToGo panics if src is not an Uint8Array.

func CopyBytesToJS Uses

func CopyBytesToJS(dst Value, src []byte) int

CopyBytesToJS copies bytes from src to the Uint8Array dst. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. CopyBytesToJS panics if dst is not an Uint8Array.

type Error Uses

type Error struct {
    // Value is the underlying JavaScript error value.
    Value
}

Error wraps a JavaScript error.

func (Error) Error Uses

func (e Error) Error() string

Error implements the error interface.

type Func Uses

type Func struct {
    Value // the JavaScript function that invokes the Go function
    // contains filtered or unexported fields
}

Func is a wrapped Go function to be called by JavaScript.

func FuncOf Uses

func FuncOf(fn func(this Value, args []Value) interface{}) Func

FuncOf returns a wrapped function.

Invoking the JavaScript function will synchronously call the Go function fn with the value of JavaScript's "this" keyword and the arguments of the invocation. The return value of the invocation is the result of the Go function mapped back to JavaScript according to ValueOf.

A wrapped function triggered during a call from Go to JavaScript gets executed on the same goroutine. A wrapped function triggered by JavaScript's event loop gets executed on an extra goroutine. Blocking operations in the wrapped function will block the event loop. As a consequence, if one wrapped function blocks, other wrapped funcs will not be processed. A blocking function should therefore explicitly start a new goroutine.

Func.Release must be called to free up resources when the function will not be used any more.

Code:

var cb js.Func
cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
    fmt.Println("button clicked")
    cb.Release() // release the function if the button will not be clicked again
    return nil
})
js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)

func (Func) Release Uses

func (c Func) Release()

Release frees up resources allocated for the function. The function must not be invoked after calling Release.

type Type Uses

type Type int

Type represents the JavaScript type of a Value.

const (
    TypeUndefined Type = iota
    TypeNull
    TypeBoolean
    TypeNumber
    TypeString
    TypeSymbol
    TypeObject
    TypeFunction
)

func (Type) String Uses

func (t Type) String() string

type Value Uses

type Value struct {
    // contains filtered or unexported fields
}

Value represents a JavaScript value. The zero value is the JavaScript value "undefined". Values can be checked for equality with the Equal method.

func Global Uses

func Global() Value

Global returns the JavaScript global object, usually "window" or "global".

func Null Uses

func Null() Value

Null returns the JavaScript value "null".

func Undefined Uses

func Undefined() Value

Undefined returns the JavaScript value "undefined".

func ValueOf Uses

func ValueOf(x interface{}) Value

ValueOf returns x as a JavaScript value:

| Go                     | JavaScript             |
| ---------------------- | ---------------------- |
| js.Value               | [its value]            |
| js.Func                | function               |
| nil                    | null                   |
| bool                   | boolean                |
| integers and floats    | number                 |
| string                 | string                 |
| []interface{}          | new array              |
| map[string]interface{} | new object             |

Panics if x is not one of the expected types.

func (Value) Bool Uses

func (v Value) Bool() bool

Bool returns the value v as a bool. It panics if v is not a JavaScript boolean.

func (Value) Call Uses

func (v Value) Call(m string, args ...interface{}) Value

Call does a JavaScript call to the method m of value v with the given arguments. It panics if v has no method m. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) Delete Uses

func (v Value) Delete(p string)

Delete deletes the JavaScript property p of value v. It panics if v is not a JavaScript object.

func (Value) Equal Uses

func (v Value) Equal(w Value) bool

Equal reports whether v and w are equal according to JavaScript's === operator.

func (Value) Float Uses

func (v Value) Float() float64

Float returns the value v as a float64. It panics if v is not a JavaScript number.

func (Value) Get Uses

func (v Value) Get(p string) Value

Get returns the JavaScript property p of value v. It panics if v is not a JavaScript object.

func (Value) Index Uses

func (v Value) Index(i int) Value

Index returns JavaScript index i of value v. It panics if v is not a JavaScript object.

func (Value) InstanceOf Uses

func (v Value) InstanceOf(t Value) bool

InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.

func (Value) Int Uses

func (v Value) Int() int

Int returns the value v truncated to an int. It panics if v is not a JavaScript number.

func (Value) Invoke Uses

func (v Value) Invoke(args ...interface{}) Value

Invoke does a JavaScript call of the value v with the given arguments. It panics if v is not a JavaScript function. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) IsNaN Uses

func (v Value) IsNaN() bool

IsNaN reports whether v is the JavaScript value "NaN".

func (Value) IsNull Uses

func (v Value) IsNull() bool

IsNull reports whether v is the JavaScript value "null".

func (Value) IsUndefined Uses

func (v Value) IsUndefined() bool

IsUndefined reports whether v is the JavaScript value "undefined".

func (Value) JSValue Uses

func (v Value) JSValue() Value

JSValue implements Wrapper interface.

func (Value) Length Uses

func (v Value) Length() int

Length returns the JavaScript property "length" of v. It panics if v is not a JavaScript object.

func (Value) New Uses

func (v Value) New(args ...interface{}) Value

New uses JavaScript's "new" operator with value v as constructor and the given arguments. It panics if v is not a JavaScript function. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) Set Uses

func (v Value) Set(p string, x interface{})

Set sets the JavaScript property p of value v to ValueOf(x). It panics if v is not a JavaScript object.

func (Value) SetIndex Uses

func (v Value) SetIndex(i int, x interface{})

SetIndex sets the JavaScript index i of value v to ValueOf(x). It panics if v is not a JavaScript object.

func (Value) String Uses

func (v Value) String() string

String returns the value v as a string. String is a special case because of Go's String method convention. Unlike the other getters, it does not panic if v's Type is not TypeString. Instead, it returns a string of the form "<T>" or "<T: V>" where T is v's type and V is a string representation of v's value.

func (Value) Truthy Uses

func (v Value) Truthy() bool

Truthy returns the JavaScript "truthiness" of the value v. In JavaScript, false, 0, "", null, undefined, and NaN are "falsy", and everything else is "truthy". See https://developer.mozilla.org/en-US/docs/Glossary/Truthy.

func (Value) Type Uses

func (v Value) Type() Type

Type returns the JavaScript type of the value v. It is similar to JavaScript's typeof operator, except that it returns TypeNull instead of TypeObject for null.

type ValueError Uses

type ValueError struct {
    Method string
    Type   Type
}

A ValueError occurs when a Value method is invoked on a Value that does not support it. Such cases are documented in the description of each method.

func (*ValueError) Error Uses

func (e *ValueError) Error() string

type Wrapper Uses

type Wrapper interface {
    // JSValue returns a JavaScript value associated with an object.
    JSValue() Value
}

Wrapper is implemented by types that are backed by a JavaScript value.

Package js imports 3 packages (graph). Updated 2019-12-07. Refresh now. Tools for package owners.