wasm

package module
v0.0.0-...-5d00099 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidArgumentType = errors.New("invalid argument passed into Go function")

ErrInvalidArgumentType is returned when a generated Go function wrapper receives invalid argument types from JS.

View Source
var ErrMultipleReturnValue = errors.New("a JS function can only return one value")

ErrMultipleReturnValue is an error where a JS function is attempted to be unmarshalled into a Go function with multiple return values.

Functions

func Expose

func Expose(property string, x interface{})

Expose exposes a copy of the provided value in JS.

func FromJSValue

func FromJSValue(x js.Value, out interface{}) error

FromJSValue converts a given js.Value to the Go equivalent. The new value of 'out' is undefined if FromJSValue returns an error.

When a JS function is unmarshalled into a Go function with only one return value, the returned JS value is casted into the type of the return value. If the conversion fails, the function call panics.

When a JS function is unmarshalled into a Go function with two return values, the second one being error, the conversion error is returned instead.

func NewError

func NewError(goErr error) js.Value

NewError returns a JS Error with the provided Go error's error message.

func Ready

func Ready()

Ready notifies the JS bridge that the WASM is ready. It should be called when every value and function is exposed.

func ToJSValue

func ToJSValue(x interface{}) js.Value

ToJSValue converts a given Go value into its equivalent JS form.

One special case is that complex numbers (complex64 and complex128) are converted into objects with a real and imag property holding a number each.

A function is converted into a JS function where the function returns an error if the provided arguments do not conform to the Go equivalent but otherwise calls the Go function.

The "this" argument of a function is always passed to the Go function if its first parameter is of type js.Value. Otherwise, it is simply ignored.

If the last return value of a function is an error, it will be thrown in JS if it's non-nil. If the function returns multiple non-error values, it is converted to an array when returning to JS.

It panics when a channel or a map with keys other than string and integers are passed in.

Types

type Decoder

type Decoder interface {
	FromJSValue(js.Value) error
}

Decoder is an interface which manually decodes js.Value on its own. It overrides in FromJSValue.

type InvalidArrayError

type InvalidArrayError struct {
	Expected int
	Actual   int
}

InvalidArrayError is an error where the JS's array length do not match Go's array length.

func (InvalidArrayError) Error

func (e InvalidArrayError) Error() string

Error implements error.

type InvalidFromJSValueError

type InvalidFromJSValueError struct {
	Type reflect.Type
}

InvalidFromJSValueError is an error where an invalid argument is passed to FromJSValue. The argument to Unmarshal must be a non-nil pointer.

func (InvalidFromJSValueError) Error

func (e InvalidFromJSValueError) Error() string

Error implements error.

type InvalidTypeError

type InvalidTypeError struct {
	JSType js.Type
	GoType reflect.Type
}

InvalidTypeError is an error where the JS value cannot be unmarshalled into the provided Go type.

func (InvalidTypeError) Error

func (e InvalidTypeError) Error() string

Error implements error.

type Object

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

Object is a statically typed Object instance of js.Value. It should be instantiated with NewObject where it is checked for type instead of directly. Calling methods on a zero Object is undefined behaviour.

func Global

func Global() Object

Global returns the global object as a Object. If the global object is not an object, it panics.

func NewObject

func NewObject(raw js.Value) (Object, error)

NewObject instantiates a new Object with the provided js.Value. If the js.Value is not an Object, it returns a TypeMismatchError.

func (Object) Delete

func (o Object) Delete(p string)

Delete removes property p from the object.

func (Object) Equal

func (o Object) Equal(v js.Value) bool

Equal checks if the object is equal to another value. It is equivalent to JS's === operator.

func (Object) Expect

func (o Object) Expect(expectedType js.Type, path ...string) (js.Value, error)

Expect is a helper function that calls Get and checks the type of the final result. It returns a TypeMismatchError if a non-object is encountered while descending the path or the final type does not match with the provided expected type.

func (Object) Get

func (o Object) Get(path ...string) (js.Value, error)

Get recursively gets the Object's properties, returning a TypeMismatchError if it encounters a non-object while descending through the object.

func (Object) Index

func (o Object) Index(i int) js.Value

Index indexes into the object.

func (Object) InstanceOf

func (o Object) InstanceOf(t js.Value) bool

InstanceOf implements the instanceof operator in JavaScript. If t is not a constructor, this function returns false.

func (Object) JSValue

func (o Object) JSValue() js.Value

JSValue implements the js.Wrapper interface.

func (Object) Length

func (o Object) Length() int

Length returns the "length" property of the object.

func (Object) Set

func (o Object) Set(p string, x interface{})

Set sets the property p to the value of ToJSValue(x).

func (Object) SetIndex

func (o Object) SetIndex(i int, x interface{})

SetIndex sets the index i to the value of ToJSValue(x).

func (Object) String

func (o Object) String() string

String returns the object marshalled as a JSON string for debugging purposes.

type Promise

type Promise struct {
	Object
}

Promise is an instance of a JS promise. The zero value of this struct is not a valid Promise.

func NewPromise

func NewPromise(handler func() (interface{}, error)) Promise

NewPromise returns a promise that is fulfilled or rejected when the provided handler returns. The handler is spawned in its own goroutine.

func PromiseAll

func PromiseAll(promise ...Promise) Promise

PromiseAll creates a promise that is fulfilled when all the provided promises have been fulfilled. The promise is rejected when any of the promises provided rejects. It is implemented by calling Promise.all on JS.

func PromiseAllSettled

func PromiseAllSettled(promise ...Promise) Promise

PromiseAllSettled creates a promise that is fulfilled when all the provided promises have been fulfilled or rejected. It is implemented by calling Promise.allSettled on JS.

func PromiseAny

func PromiseAny(promise ...Promise) Promise

PromiseAny creates a promise that is fulfilled when any of the provided promises have been fulfilled. The promise is rejected when all of the provided promises gets rejected. It is implemented by calling Promise.any on JS.

func PromiseRace

func PromiseRace(promise ...Promise) Promise

PromiseRace creates a promise that is fulfilled or rejected when one of the provided promises fulfill or reject. It is implemented by calling Promise.race on JS.

func (Promise) Await

func (p Promise) Await(v interface{}) error

Await waits for the Promise. It unmarshals the resolved value to v. An error will be returned if unmarshalling is unsuccessful or the Promise rejects. It is implemented by calling then and catch on JS.

func (*Promise) FromJSValue

func (p *Promise) FromJSValue(value js.Value) error

FromJSValue turns a JS value to a Promise.

type TypeMismatchError

type TypeMismatchError struct {
	Expected js.Type
	Actual   js.Type
}

TypeMismatchError is returned when a function is called with a js.Value that has the incorrect type.

func (TypeMismatchError) Error

func (e TypeMismatchError) Error() string

type Wrapper

type Wrapper interface {
	JSValue() js.Value
}

Wrapper is an interface which manually encodes to js.Value. It overrides in ToJSValue.

Jump to

Keyboard shortcuts

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