js

package standard library
go1.11beta1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2018 License: BSD-3-Clause Imports: 2 Imported by: 2,512

Documentation

Rendered for js/wasm

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

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

Callback is a Go function that got wrapped for use as a JavaScript callback. A Callback can be passed to functions of this package that accept interface{}, for example Value.Set and Value.Call.

func NewCallback

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

NewCallback returns a wrapped callback function. It can be passed to functions of this package that accept interface{}, for example Value.Set and Value.Call.

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.Close must be called to free up resources when the callback will not be used any more.

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

func NewEventCallback

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

func (c Callback) Close()

type Error

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

Error wraps a JavaScript error.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

type EventCallbackFlag

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 Value

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

Value represents a JavaScript value.

func Global

func Global() Value

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

func Null

func Null() Value

Null returns the JavaScript value "null".

func Undefined

func Undefined() Value

Undefined returns the JavaScript value "undefined".

func ValueOf

func ValueOf(x interface{}) Value

ValueOf returns x as a JavaScript value.

func (Value) Bool

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

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.

func (Value) Float

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

func (v Value) Get(p string) Value

Get returns the JavaScript property p of value v.

func (Value) Index

func (v Value) Index(i int) Value

Index returns JavaScript index i of value v.

func (Value) InstanceOf

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

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

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.

func (Value) Length

func (v Value) Length() int

Length returns the JavaScript property "length" of v.

func (Value) New

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.

func (Value) Set

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

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

func (Value) SetIndex

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

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

func (Value) String

func (v Value) String() string

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

Jump to

Keyboard shortcuts

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