import "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.
callback.go js.go typedarray.go
type Callback struct {
Value // the JavaScript function that queues the callback for execution
// contains filtered or unexported fields
}Callback is a Go function that got wrapped for use as a JavaScript 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.
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.
Release frees up resources allocated for the callback. The callback must not be invoked after calling Release.
Error wraps a JavaScript error.
Error implements the error interface.
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 represents the JavaScript type of a Value.
const (
TypeUndefined Type = iota
TypeNull
TypeBoolean
TypeNumber
TypeString
TypeSymbol
TypeObject
TypeFunction
)TypedArray represents a JavaScript typed array.
func TypedArrayOf(slice interface{}) TypedArrayTypedArrayOf 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 (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 Value struct {
// contains filtered or unexported fields
}Value represents a JavaScript value.
Global returns the JavaScript global object, usually "window" or "global".
Null returns the JavaScript value "null".
Undefined returns the JavaScript value "undefined".
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 |
Bool returns the value v as a bool. It panics if v is not a JavaScript boolean.
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.
Float returns the value v as a float64. It panics if v is not a JavaScript number.
Get returns the JavaScript property p of value v.
Index returns JavaScript index i of value v.
InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.
Int returns the value v truncated to an int. It panics if v is not a JavaScript number.
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.
Length returns the JavaScript property "length" of v.
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.
Set sets the JavaScript property p of value v to ValueOf(x).
SetIndex sets the JavaScript index i of value v to ValueOf(x).
String returns the value v converted to string according to JavaScript type conversions.
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.
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 (e *ValueError) Error() string
Package js imports 2 packages (graph) and is imported by 13 packages. Updated 2018-10-04. Refresh now. Tools for package owners.