utils

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

View Source
var (
	// Error is the Javascript Error type. It used to create new Javascript
	// errors.
	Error = js.Global().Get("Error")

	// JSON is the Javascript JSON type. It is used to perform JSON operations
	// on the Javascript layer.
	JSON = js.Global().Get("JSON")

	// Object is the Javascript Object type. It is used to perform Object
	// operations on the Javascript layer.
	Object = js.Global().Get("Object")

	// Promise is the Javascript Promise type. It is used to generate new
	// promises.
	Promise = js.Global().Get("Promise")

	// Uint8Array is the Javascript Uint8Array type. It is used to create new
	// Uint8Array.
	Uint8Array = js.Global().Get("Uint8Array")
)

Functions

func Await

func Await(awaitable js.Value) (result []js.Value, err []js.Value)

Await waits on a Javascript value. It blocks until the awaitable successfully resolves to the result or rejects to err.

If there is a result, err will be nil and vice versa.

func Base64ToUint8Array

func Base64ToUint8Array(_ js.Value, args []js.Value) any

Base64ToUint8Array decodes a base 64 encoded string to a Uint8Array.

Parameters:

  • args[0] - Base 64 encoded string (string).

Returns:

  • Javascript 8-bit unsigned integer array (Uint8Array).
  • Throws TypeError if decoding the string fails.

func CopyBytesToGo

func CopyBytesToGo(src js.Value) []byte

CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is a wrapper for js.CopyBytesToGo to make it more convenient.

func CopyBytesToJS

func CopyBytesToJS(src []byte) js.Value

CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is a wrapper for js.CopyBytesToJS to make it more convenient.

func CreatePromise

func CreatePromise(f PromiseFn) any

CreatePromise creates a Javascript promise to return the value of a blocking Go function to Javascript.

func JsError

func JsError(err error) js.Value

JsError converts the error to a Javascript Error.

func JsErrorToJson added in v0.3.0

func JsErrorToJson(value js.Value) string

JsErrorToJson converts the Javascript error to JSON. This should be used for all Javascript error objects instead of JsonToJS.

func JsToJson

func JsToJson(value js.Value) string

JsToJson converts the Javascript value to JSON.

func JsTrace

func JsTrace(err error) js.Value

JsTrace converts the error to a Javascript Error that includes the error's stack trace.

func JsonToJS

func JsonToJS(inputJson []byte) (js.Value, error)

JsonToJS converts a JSON bytes input to a js.Value of the object subtype.

func Throw

func Throw(exception Exception, err error)

Throw function stub to throws Javascript exceptions. The exception must be one of the defined Exception below. Any other error types will result in an error.

func Uint8ArrayEquals

func Uint8ArrayEquals(_ js.Value, args []js.Value) any

Uint8ArrayEquals returns true if the two Uint8Array are equal and false otherwise.

Parameters:

  • args[0] - Array A (Uint8Array).
  • args[1] - Array B (Uint8Array).

Returns:

  • If the two arrays are equal (boolean).

func Uint8ArrayToBase64

func Uint8ArrayToBase64(_ js.Value, args []js.Value) any

Uint8ArrayToBase64 encodes an uint8 array to a base 64 string.

Parameters:

  • args[0] - Javascript 8-bit unsigned integer array (Uint8Array).

Returns:

  • Base 64 encoded string (string).

func WrapCB

func WrapCB(parent js.Value, m string) func(args ...any) js.Value

WrapCB wraps a Javascript function in an object so that it can be called later with only the arguments and without specifying the function name.

Panics if m is not a function.

Types

type Exception

type Exception string

Exception are the possible Javascript error types that can be thrown.

const (
	// EvalError occurs when error has occurred in the eval() function.
	//
	// Deprecated: This exception is not thrown by JavaScript anymore, however
	// the EvalError object remains for compatibility.
	EvalError Exception = "EvalError"

	// RangeError occurs when a numeric variable or parameter is outside its
	// valid range.
	RangeError Exception = "RangeError"

	// ReferenceError occurs when a variable that does not exist (or hasn't yet
	// been initialized) in the current scope is referenced.
	ReferenceError Exception = "ReferenceError"

	// SyntaxError occurs when trying to interpret syntactically invalid code.
	SyntaxError Exception = "SyntaxError"

	// TypeError occurs when an operation could not be performed, typically (but
	// not exclusively) when a value is not of the expected type.
	//
	// A TypeError may be thrown when:
	//  - an operand or argument passed to a function is incompatible with the
	//    type expected by that operator or function; or
	//  - when attempting to modify a value that cannot be changed; or
	//  - when attempting to use a value in an inappropriate way.
	TypeError Exception = "TypeError"

	// URIError occurs when a global URI handling function was used in a wrong
	// way.
	URIError Exception = "URIError"
)

type PromiseFn

type PromiseFn func(resolve, reject func(args ...any) js.Value)

PromiseFn converts the Javascript Promise construct into Go.

Call resolve with the return of the function on success. Call reject with an error on failure.

Jump to

Keyboard shortcuts

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