Documentation ¶
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 ¶
- type Error
- type Func
- type Type
- type TypedArray
- type Value
- func (v Value) Bool() bool
- func (v Value) Call(m string, args ...interface{}) Value
- func (v Value) Float() float64
- func (v Value) Get(p string) Value
- func (v Value) Index(i int) Value
- func (v Value) InstanceOf(t Value) bool
- func (v Value) Int() int
- func (v Value) Invoke(args ...interface{}) Value
- func (v Value) JSValue() Value
- func (v Value) Length() int
- func (v Value) New(args ...interface{}) Value
- func (v Value) Set(p string, x interface{})
- func (v Value) SetIndex(i int, x interface{})
- func (v Value) String() string
- func (v Value) Truthy() bool
- func (v Value) Type() Type
- type ValueError
- type Wrapper
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct { // Value is the underlying JavaScript error value. Value }
Error wraps a JavaScript error.
type Func ¶
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 ¶
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.
Example ¶
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)
Output:
type TypedArray ¶
type TypedArray struct {
Value
}
TypedArray represents a JavaScript typed array.
func TypedArrayOf ¶
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) Release ¶
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 ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents a JavaScript value. The zero value is the JavaScript value "undefined".
func Global ¶
func Global() Value
Global returns the JavaScript global object, usually "window" or "global".
func ValueOf ¶
func ValueOf(x interface{}) Value
ValueOf returns x as a JavaScript value:
| Go | JavaScript | | ---------------------- | ---------------------- | | js.Value | [its value] | | js.TypedArray | typed array | | 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) Call ¶
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) Float ¶
Float returns the value v as a float64. It panics if v is not a JavaScript number.
func (Value) InstanceOf ¶
InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.
func (Value) Int ¶
Int returns the value v truncated to an int. It panics if v is not a JavaScript number.
func (Value) Invoke ¶
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) New ¶
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) String ¶
String returns the value v converted to string according to JavaScript type conversions.
func (Value) Truthy ¶
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.
type ValueError ¶
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 ¶
func (e *ValueError) Error() string