codec

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HtmlElements = map[string]string{}/* 141 elements not displayed */

Functions

func CallbackErrorf added in v0.8.0

func CallbackErrorf[T any](s js.Scope[T], format string, a ...any) error

func DecodeAs added in v0.8.0

func DecodeAs[T, U any](s js.Scope[U], v js.Value[U]) (res T, err error)

func DecodeBoolean

func DecodeBoolean[T any](_ js.Scope[T], val js.Value[T]) (bool, error)

func DecodeFunction

func DecodeFunction[T any](s js.Scope[T], v js.Value[T]) (js.Function[T], error)

func DecodeHTMLElement

func DecodeHTMLElement[T any](s js.Scope[T], v js.Value[T]) (html.HTMLElement, error)

func DecodeInnerObject added in v0.9.2

func DecodeInnerObject[T, U any](s js.Scope[T], v js.Value[T]) (res U, err error)

func DecodeInt

func DecodeInt[T any](_ js.Scope[T], v js.Value[T]) (int, error)

func DecodeNode

func DecodeNode[T any](s js.Scope[T], v js.Value[T]) (dom.Node, error)

func DecodeOptions added in v0.8.0

func DecodeOptions[T, U any](
	scope js.Scope[T], val js.Value[T], specs Options[T, U],
) (opts []U, err error)

func DecodeRequestBody added in v0.9.3

func DecodeRequestBody[T any](s js.Scope[T], val js.Value[T]) (io.Reader, error)

DecodeRequestBody constructs an io.Reader used for HTTP request bodies for both XMLHttpRequest.send and fetch's RequestInit.body, which supports almost identical values.

Note: Not all valid body types aren't yet supported

func DecodeString

func DecodeString[T any](_ js.Scope[T], v js.Value[T]) (string, error)

func EncodeBoolean

func EncodeBoolean[T any](s js.CallbackScope[T], b bool) (js.Value[T], error)

func EncodeCallbackErrorf added in v0.8.0

func EncodeCallbackErrorf[T any](
	ctx js.CallbackContext[T],
	format string,
	a ...any,
) (js.Value[T], error)

EncodeCallbackErrorf create callback return values with a specific error message, and logs the error to the logger associated with the script context.

While _uncaught_ JavaScript errors would be logged by default, if the error is caught by JavaScript, the error message will not be logged.

This is particularly valuable for not-implemented methods, as JavaScript code will fail when perfectly valid assumptions about the function are violated.

func EncodeConstrucedValue

func EncodeConstrucedValue[T any](s js.CallbackScope[T], val any) (js.Value[T], error)

EncodeConstrucedValue is a simple helper for JS constructor callbacks to store the constructed Go value in the JavaScript object, and possibly cache it with the script context.

func EncodeEntity

func EncodeEntity[T any](s js.Scope[T], e entity.ObjectIder) (js.Value[T], error)

getJSInstance gets the JavaScript object that wraps a specific Go object. If a wrapper already has been created, that wrapper is returned; otherwise a new object is created with the correct prototype configured.

func EncodeEntityScoped

func EncodeEntityScoped[T any](scope js.Scope[T], e entity.ObjectIder) (js.Value[T], error)

TODO: Embed scope in CallbackScope, so only one function is necessary

func EncodeError added in v0.8.0

func EncodeError[T any](scope js.Scope[T], err error) (js.Value[T], error)

func EncodeInt

func EncodeInt[T any](s js.CallbackScope[T], i int) (js.Value[T], error)

func EncodeNillableString

func EncodeNillableString[T any](
	scope js.CallbackScope[T],
	s string,
	hasValue bool,
) (js.Value[T], error)

func EncodeNull

func EncodeNull[T any](s js.CallbackScope[T]) (js.Value[T], error)

func EncodeNullableString

func EncodeNullableString[T any](scope js.CallbackScope[T], s *string) (js.Value[T], error)

func EncodePromise added in v0.8.0

func EncodePromise[T, U any](
	scope js.Scope[T],
	prom promise.Promise[U],
	encoder Encoder[T, U],
) (js.Value[T], error)

EncodePromise converts a promise.Promise value to a JavaScript Promise value, using encoder to convert the native fulfilled value to a JavaScript value.

The returned Promise will not settile immediately after a value is received from prom, but will be deferred to run on the "main loop" that the embedder controls.

func EncodePromiseFunc added in v0.8.0

func EncodePromiseFunc[T any](
	c js.Scope[T],
	f func() (js.Value[T], error),
) (js.Value[T], error)

EncodePromiseFunc returnes a JavaScript Promise that will settle with the result of running function f. Function f must be safe to run concurrently, as it will execute in a separate goroutine.

The promise will not settile immediately after f finishes, but will be deferred to run on the "main loop" that the embedder controls.

func EncodeString

func EncodeString[T any](scope js.Scope[T], s string) (js.Value[T], error)

func EncodeStringScoped

func EncodeStringScoped[T any](scope js.Scope[T], s string) (js.Value[T], error)

TODO: Embed scope in CallbackScope, so only one function is necessary

func LookupJSPrototype

func LookupJSPrototype(entity entity.ObjectIder) string

func NewOptionsDecoder added in v0.8.0

func NewOptionsDecoder[T, U any](
	opts Options[T, U],
) func(js.CallbackContext[T], js.Value[T]) ([]U, error)

func UnsupportedOptionErrorf added in v0.8.0

func UnsupportedOptionErrorf[T any](
	s js.Scope[T],
	value js.Value[T],
	webAPI string,
	key string,
) error

UnsupportedOptionErrorf is used to detect when script code uses options or arguments that are not yet supported by Gost-DOM.

The primary usage is to identify when a failing test case is failing because of missing functionality in Gost-DOM, rather than an error in the system under test.

The value argument represents a JavaScript function argument keyed option value. If it is not null or undefined, an error is logged and returned. The name of the api is specified in the webAPI, and the unsupported method or option is specified in key.

func ZeroValue

func ZeroValue[T any]() (res T)

Types

type Encoder added in v0.8.0

type Encoder[T, U any] = func(js.Scope[T], U) (js.Value[T], error)

type EventInit

type EventInit struct {
	Bubbles    bool
	Cancelable bool
	Init       any
}

func DecodeEventInit

func DecodeEventInit[T any](
	_ js.Scope[T],
	val js.Value[T],
) (EventInit, error)

type OptionDecoder added in v0.8.0

type OptionDecoder[T, U any] = func(js.Scope[T], js.Value[T]) (U, error)

func OptDecoder added in v0.8.0

func OptDecoder[T, U, V any](
	decode func(scope js.Scope[T], val js.Value[T]) (U, error),
	f func(U) V,
) OptionDecoder[T, V]

type Options added in v0.8.0

type Options[T, U any] map[string]OptionDecoder[T, U]

Jump to

Keyboard shortcuts

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