W3cubDocs

/Go

Package js

Overview

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

NewCallback

Package files

callback.go js.go typedarray.go

type CallbackSource

Callback is a Go function that got wrapped for use as a JavaScript callback.

type Callback struct {
        Value // the JavaScript function that queues the callback for execution
        // contains filtered or unexported fields
}

func NewCallbackSource

func NewCallback(fn func(args []Value)) Callback

NewCallback returns a wrapped callback function.

Invoking the callback in JavaScript will queue the Go function fn for execution. This execution happens asynchronously on a special goroutine that handles all callbacks and preserves the order in which the callbacks got called. As a consequence, if one callback blocks this goroutine, other callbacks will not be processed. A blocking callback should therefore explicitly start a new goroutine.

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

Example

package main

import (
	"fmt"
	"syscall/js"
)

func main() {
	var cb js.Callback
	cb = js.NewCallback(func(args []js.Value) {
		fmt.Println("button clicked")
		cb.Release() // release the callback if the button will not be clicked again
	})
	js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)
}

func NewEventCallbackSource

func NewEventCallback(flags EventCallbackFlag, fn func(event Value)) Callback

NewEventCallback returns a wrapped callback function, just like NewCallback, but the callback expects to have exactly one argument, the event. Depending on flags, it will synchronously call event.preventDefault, event.stopPropagation and/or event.stopImmediatePropagation before queuing the Go function fn for execution.

func (Callback) ReleaseSource

func (c Callback) Release()

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

type ErrorSource

Error wraps a JavaScript error.

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

func (Error) ErrorSource

func (e Error) Error() string

Error implements the error interface.

type EventCallbackFlagSource

type EventCallbackFlag int
const (
        // PreventDefault can be used with NewEventCallback to call event.preventDefault synchronously.
        PreventDefault EventCallbackFlag = 1 << iota
        // StopPropagation can be used with NewEventCallback to call event.stopPropagation synchronously.
        StopPropagation
        // StopImmediatePropagation can be used with NewEventCallback to call event.stopImmediatePropagation synchronously.
        StopImmediatePropagation
)

type TypeSource

Type represents the JavaScript type of a Value.

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

func (Type) StringSource

func (t Type) String() string

type TypedArraySource

TypedArray represents a JavaScript typed array.

type TypedArray struct {
        Value
}

func TypedArrayOfSource

func TypedArrayOf(slice interface{}) TypedArray

TypedArrayOf returns a JavaScript typed array backed by the slice's underlying array.

The supported types are []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32 and []float64. Passing an unsupported value causes a panic.

TypedArray.Release must be called to free up resources when the typed array will not be used any more.

func (TypedArray) ReleaseSource

func (a TypedArray) Release()

Release frees up resources allocated for the typed array. The typed array and its buffer must not be accessed after calling Release.

type ValueSource

Value represents a JavaScript value.

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

func GlobalSource

func Global() Value

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

func NullSource

func Null() Value

Null returns the JavaScript value "null".

func UndefinedSource

func Undefined() Value

Undefined returns the JavaScript value "undefined".

func ValueOfSource

func ValueOf(x interface{}) Value

ValueOf returns x as a JavaScript value:

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

func (Value) BoolSource

func (v Value) Bool() bool

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

func (Value) CallSource

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) FloatSource

func (v Value) Float() float64

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

func (Value) GetSource

func (v Value) Get(p string) Value

Get returns the JavaScript property p of value v.

func (Value) IndexSource

func (v Value) Index(i int) Value

Index returns JavaScript index i of value v.

func (Value) InstanceOfSource

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) IntSource

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) InvokeSource

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 function. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) LengthSource

func (v Value) Length() int

Length returns the JavaScript property "length" of v.

func (Value) NewSource

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 function. The arguments get mapped to JavaScript values according to the ValueOf function.

func (Value) SetSource

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

Set sets the JavaScript property p of value v to ValueOf(x).

func (Value) SetIndexSource

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

SetIndex sets the JavaScript index i of value v to ValueOf(x).

func (Value) StringSource

func (v Value) String() string

String returns the value v converted to string according to JavaScript type conversions.

func (Value) TypeSource

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 ValueErrorSource

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.

type ValueError struct {
        Method string
        Type   Type
}

func (*ValueError) ErrorSource

func (e *ValueError) Error() string

© Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.
https://golang.org/pkg/syscall/js/