js

js

https://pkg.go.dev/syscall/js@go1.20.1

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.

常量

This section is empty.

变量

This section is empty.

函数

func CopyBytesToGo <- go1.13

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

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

func CopyBytesToJS <- go1.13

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

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

类型

type Error

1
2
3
4
type Error struct {
	// Value is the underlying JavaScript error value.
	Value
}

Error wraps a JavaScript error.

(Error) Error

1
func (e Error) Error() string

Error implements the error interface.

type Func <- go1.12

1
2
3
4
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 <- go1.12

1
func FuncOf(fn func(this Value, args []Value) any) Func

FuncOf returns a function to be used by JavaScript.

The Go function fn is called 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.

Invoking the wrapped Go function from JavaScript will pause the event loop and spawn a new goroutine. Other wrapped functions which are triggered during a call from Go to JavaScript get executed on the same goroutine.

As a consequence, if one wrapped function blocks, JavaScript’s event loop is blocked until that function returns. Hence, calling any async JavaScript API, which requires the event loop, like fetch (http.Client), will cause an immediate deadlock. Therefore a blocking function should explicitly start a new goroutine.

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

Example

(Func) Release <- go1.12

1
func (c Func) Release()

Release frees up resources allocated for the function. The function must not be invoked after calling Release. It is allowed to call Release while the function is still running.

type Type

1
type Type int

Type represents the JavaScript type of a Value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
const (
	TypeUndefined Type = iota
	TypeNull
	TypeBoolean
	TypeNumber
	TypeString
	TypeSymbol
	TypeObject
	TypeFunction
)

(Type) String

1
func (t Type) String() string

type Value

1
2
3
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

1
func Global() Value

Global returns the JavaScript global object, usually “window” or “global”.

func Null

1
func Null() Value

Null returns the JavaScript value “null”.

func Undefined

1
func Undefined() Value

Undefined returns the JavaScript value “undefined”.

func ValueOf

1
func ValueOf(x any) 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.

(Value) Bool

1
func (v Value) Bool() bool

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

(Value) Call

1
func (v Value) Call(m string, args ...any) 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.

(Value) Delete <- go1.14

1
func (v Value) Delete(p string)

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

(Value) Equal <- go1.14

1
func (v Value) Equal(w Value) bool

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

(Value) Float

1
func (v Value) Float() float64

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

(Value) Get

1
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.

(Value) Index

1
func (v Value) Index(i int) Value

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

(Value) InstanceOf

1
func (v Value) InstanceOf(t Value) bool

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

(Value) Int

1
func (v Value) Int() int

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

(Value) Invoke

1
func (v Value) Invoke(args ...any) 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.

(Value) IsNaN <- go1.14

1
func (v Value) IsNaN() bool

IsNaN reports whether v is the JavaScript value “NaN”.

(Value) IsNull <- go1.14

1
func (v Value) IsNull() bool

IsNull reports whether v is the JavaScript value “null”.

(Value) IsUndefined <- go1.14

1
func (v Value) IsUndefined() bool

IsUndefined reports whether v is the JavaScript value “undefined”.

(Value) Length

1
func (v Value) Length() int

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

(Value) New

1
func (v Value) New(args ...any) 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.

(Value) Set

1
func (v Value) Set(p string, x any)

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

(Value) SetIndex

1
func (v Value) SetIndex(i int, x any)

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

(Value) String

1
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 “” or “<T: V>” where T is v’s type and V is a string representation of v’s value.

(Value) Truthy <- go1.12

1
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.

(Value) Type

1
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

1
2
3
4
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.

(*ValueError) Error

1
func (e *ValueError) Error() string