Documentation
¶
Overview ¶
Package js provides ffi support for JavaScript.
Index ¶
- func Free[T ~uint32](refs ...T)
- func Must[T any](x T, ok bool) T
- func Pointer[T any](p *T) unsafe.Pointer
- func SizeI[T num.Integer | num.Pointer](v T) (x int32)
- func SizeU[T num.Integer | num.Pointer](v T) (x uint32)
- func SliceData[T any](s []T) unsafe.Pointer
- func StringData(s string) unsafe.Pointer
- func ThrowCallbackValueNotReturned()
- func ThrowInvalidCallbackInvocation()
- type Any
- func (x Any) AsArray() Array[Any]
- func (x Any) AsFunc() Func[Any]
- func (x Any) AsNumber() Number[float64]
- func (x Any) AsObject() Object
- func (x Any) AsString() String
- func (x Any) Free()
- func (x Any) FromRef(ref Ref) Any
- func (x Any) InstanceOf(o Ref) bool
- func (x Any) Once() Any
- func (x Any) Ref() Ref
- type Array
- func (a Array[T]) Copy(start int, outBuf []Ref) int
- func (a Array[T]) Fill(start int, take bool, elems ...Ref) int
- func (a Array[T]) Free()
- func (a Array[T]) FromRef(ref Ref) Array[T]
- func (a Array[T]) Length() int
- func (a Array[T]) MustFill(start int, take bool, elems ...Ref) Array[T]
- func (a Array[T]) Nth(i int) (Ref, bool)
- func (a Array[T]) NthBool(i int) (bool, bool)
- func (a Array[T]) NthNum(i int) (float64, bool)
- func (a Array[T]) Once() Array[T]
- func (a Array[T]) Ref() Ref
- func (a Array[T]) SetNth(i int, take bool, val Ref) bool
- func (a Array[T]) SetNthBool(i int, val bool) bool
- func (a Array[T]) SetNthNum(i int, val float64) bool
- func (a Array[T]) SetNthString(i int, s string) bool
- func (a Array[T]) Slice(start, end int) Array[T]
- func (a Array[T]) SliceFrom(start int) Array[T]
- type ArrayBuffer
- type ArrayBufferView
- type BigInt
- type BigInt64Array
- type BigUint64Array
- type Boolean
- type CallbackContext
- type CallbackDispatcher
- type DataView
- type Executor
- type ExecutorFunc
- type Float32Array
- type Float64Array
- type FrozenArray
- type Func
- func (fn Func[T]) Call(this Ref, take bool, args ...Ref) Ref
- func (fn Func[T]) CallBool(this Ref, take bool, args ...Ref) bool
- func (fn Func[T]) CallNum(this Ref, take bool, args ...Ref) float64
- func (fn Func[T]) CallVoid(this Ref, take bool, args ...Ref)
- func (fn Func[T]) Free()
- func (fn Func[T]) FromRef(ref Ref) Func[T]
- func (fn Func[T]) Once() Func[T]
- func (fn Func[T]) Ref() Ref
- func (fn Func[T]) Try(this Ref, take bool, catch Func[func(err Any, args []Ref) Ref], ...) (Ref, bool)
- type Int8Array
- type Int16Array
- type Int32Array
- type Number
- type Object
- func (o Object) BoolProp(name string) bool
- func (o Object) BoolPropByString(name String) bool
- func (o Object) DefineProp(name string, flags PropDesc, getter Func[func() Any], ...) bool
- func (o Object) DeleteProp(name string) bool
- func (o Object) Free()
- func (o Object) FromRef(ref Ref) Object
- func (o Object) NumProp(name string) float64
- func (o Object) NumPropByString(name String) float64
- func (o Object) Once() Object
- func (o Object) Prop(name string) Any
- func (o Object) PropByString(name String) Any
- func (o Object) PropDesc(name string) PropDesc
- func (o Object) Prototype() (proto Object, hasPrototype bool)
- func (o Object) Ref() Ref
- func (o Object) SetBoolProp(name string, val bool) bool
- func (o Object) SetBoolPropByString(s String, val bool) bool
- func (o Object) SetNumProp(name string, val float64) bool
- func (o Object) SetNumPropByString(prop String, val float64) bool
- func (o Object) SetProp(name string, free bool, val Ref) bool
- func (o Object) SetPropByString(prop String, free bool, val Ref) bool
- func (o Object) SetPrototype(free bool, proto Ref) bool
- func (o Object) SetStringProp(name, val string) bool
- func (o Object) SetStringPropByString(name String, free bool, val String) bool
- func (o Object) StringProp(name string) String
- func (o Object) StringPropByString(name String) String
- type ObservableArray
- type Promise
- func (p Promise[T]) Await(take bool) (value T, err Any, fulfilled bool)
- func (p Promise[T]) Free()
- func (p Promise[T]) FromRef(ref Ref) Promise[T]
- func (p Promise[T]) Once() Promise[T]
- func (p Promise[T]) Ref() Ref
- func (p Promise[T]) Then(onfulfilled, onrejected Func[func(Ref)], onfinally Func[func()]) Promise[T]
- type PropDesc
- type Record
- type Ref
- type SharedArrayBuffer
- type String
- func (s String) Append(x string) String
- func (s String) Equals(other string) bool
- func (s String) Free()
- func (s String) FromRef(ref Ref) String
- func (s String) Len() int
- func (s String) Once() String
- func (s String) Prepend(x string) String
- func (s String) Read(buf []byte) int
- func (s String) Ref() Ref
- type TypedArray
- func (a TypedArray[T]) Buffer(take bool) Ref
- func (a TypedArray[T]) ByteLength() int
- func (a TypedArray[T]) ByteOffset() int
- func (a TypedArray[T]) Copy(start int, outBuf []T) int
- func (a TypedArray[T]) Fill(start int, elems ...T) int
- func (a TypedArray[T]) Free()
- func (a TypedArray[T]) FromArrayBuffer(take bool, buf ArrayBuffer) TypedArray[T]
- func (a TypedArray[T]) FromRef(ref Ref) TypedArray[T]
- func (a TypedArray[T]) FromSharedArrayBuffer(take bool, buf SharedArrayBuffer) TypedArray[T]
- func (a TypedArray[T]) Length() int
- func (a TypedArray[T]) MustFill(start int, elems ...T) TypedArray[T]
- func (a TypedArray[T]) Nth(i int) (T, bool)
- func (a TypedArray[T]) Once() TypedArray[T]
- func (a TypedArray[T]) Ref() Ref
- func (a TypedArray[T]) Set(other TypedArray[T]) TypedArray[T]
- func (a TypedArray[T]) SetNth(i int, val T) bool
- func (a TypedArray[T]) Slice(start, end int) TypedArray[T]
- func (a TypedArray[T]) SliceFrom(start int) TypedArray[T]
- type Uint8Array
- type Uint8ClampedArray
- type Uint16Array
- type Uint32Array
- type UntypedCallback
- type Void
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StringData ¶
func ThrowCallbackValueNotReturned ¶
func ThrowCallbackValueNotReturned()
func ThrowInvalidCallbackInvocation ¶
func ThrowInvalidCallbackInvocation()
Types ¶
type Any ¶
type Any struct {
// contains filtered or unexported fields
}
func TryAwait ¶
func TryAwait[
T interface{ FromRef(Ref) T },
](
p Promise[T], exception Any, ok bool,
) (
value T, err Any, fulfilled bool,
)
TryAwait is a helper function to consume Promises returned from TryXxx functions.
It calls p.Await(true) when ok is true.
func (Any) InstanceOf ¶
type ArrayBuffer ¶
type ArrayBuffer struct {
// contains filtered or unexported fields
}
TODO
func (ArrayBuffer) Free ¶
func (buf ArrayBuffer) Free()
func (ArrayBuffer) FromRef ¶
func (buf ArrayBuffer) FromRef(ref Ref) ArrayBuffer
func (ArrayBuffer) Once ¶
func (buf ArrayBuffer) Once() ArrayBuffer
func (ArrayBuffer) Ref ¶
func (buf ArrayBuffer) Ref() Ref
type ArrayBufferView ¶
type ArrayBufferView struct {
// contains filtered or unexported fields
}
TODO
func (ArrayBufferView) Free ¶
func (v ArrayBufferView) Free()
func (ArrayBufferView) FromRef ¶
func (v ArrayBufferView) FromRef(ref Ref) ArrayBufferView
func (ArrayBufferView) Once ¶
func (v ArrayBufferView) Once() ArrayBufferView
func (ArrayBufferView) Ref ¶
func (v ArrayBufferView) Ref() Ref
type BigInt64Array ¶
type BigInt64Array = TypedArray[int64]
type BigUint64Array ¶
type BigUint64Array = TypedArray[uint64]
type CallbackContext ¶
type CallbackContext struct {
// contains filtered or unexported fields
}
CallbackContext is the context of a function call from JS to Go.
Limitation: At most 16 args (including `this`) are allowed.
The call can be made synchronously (go -> js -> go) and asynchronously (js -> go), the library supports both in one form.
It is user's responsibility to ensure the callback does not call await-like functions such as js.Promise.Await() and yield.Thread() when the js side expects returning result synchronously, and if not so, the js side will get a Promise instead of immediate value.
To return value, call one of .ReturnXxx functions. If none of the return functions was called, it returns js value "undefined". If more than one return functions were called, it returns the value passed in the last .ReturnXxx function.
After returning from the target Go function, All heap references available in the slice returned by .Args() are freed, so users MUST NOT retain any of these references, and if you do want to use after the callback, call Ref.Clone() to make a copy.
func (*CallbackContext) Args ¶
func (ctx *CallbackContext) Args() []Ref
func (*CallbackContext) Return ¶
func (ctx *CallbackContext) Return(ref Ref) bool
func (*CallbackContext) ReturnBool ¶
func (ctx *CallbackContext) ReturnBool(b bool) bool
func (*CallbackContext) ReturnNum ¶
func (ctx *CallbackContext) ReturnNum(n float64) bool
func (*CallbackContext) ReturnString ¶
func (ctx *CallbackContext) ReturnString(s string) bool
type CallbackDispatcher ¶
type CallbackDispatcher interface {
ffi.CallbackDispatcher[CallbackContext]
}
CallbackDispatcher defines the interface of a js callback dispatcher.
type Executor ¶
type Executor[T any] struct { // contains filtered or unexported fields }
Executor is a helper for constructing new Promises in js.
TODO: support handling multiple promises simultaneously.
func (*Executor[T]) DispatchCallback ¶
func (p *Executor[T]) DispatchCallback(targetPC uintptr, ctx *CallbackContext)
DispatchCallback implements CallbackDispatcher.
func (*Executor[T]) Reset ¶
func (p *Executor[T]) Reset(arg T, fn ExecutorFunc[T]) Ref
Reset prepares a new promise for use in js.
type ExecutorFunc ¶
ExecutorFunc defines the go function signature of a promise executor.
type Float32Array ¶
type Float32Array = TypedArray[float32]
type Float64Array ¶
type Float64Array = TypedArray[float64]
type FrozenArray ¶
type FrozenArray[T any] struct { // contains filtered or unexported fields }
TODO
func (FrozenArray[T]) FromRef ¶
func (a FrozenArray[T]) FromRef(ref Ref) FrozenArray[T]
func (FrozenArray[T]) Ref ¶
func (a FrozenArray[T]) Ref() Ref
type Func ¶
type Func[T any] struct { // contains filtered or unexported fields }
func RegisterCallback ¶
func RegisterCallback[F any, D CallbackDispatcher](dispatcher D, targetPC uintptr) Func[F]
RegisterCallback creates a js function to be called from js.
See CallbackContext for more details.
NOTE: type param D MUST be a pointer type, that is, it either
- comes with star sign (e.g. *Foo)
- or being a function (e.g. PromiseCallbackHandleFunc).
func (Func[T]) Call ¶
Call calls js function fn and returns the heap reference of the js return value.
When free is ture, all args after it are freed after the call.
func (Func[T]) CallBool ¶
CallBool calls js function fn and treate the return value as a js boolean.
When free is ture, all args after it are freed after the call.
func (Func[T]) CallNum ¶
CallNum calls js function fn and treate the return value as a js number.
When free is ture, all args after it are freed after the call.
func (Func[T]) CallVoid ¶
CallVoid calls js function fn and discard any return value.
When free is ture, all args after it are freed after the call.
func (Func[T]) Try ¶
func (fn Func[T]) Try( this Ref, take bool, catch Func[func(err Any, args []Ref) Ref], finally Func[func(args []Ref)], args ...Ref, ) (Ref, bool)
Try runs the fn in a trycatch block, return heap reference of the result of either fn or catch.
When free is true, all args after it (catch, finally, args...) are freed after the call.
The bool return value indicates whether the try was successful (catch was not called)
type Int8Array ¶
type Int8Array = TypedArray[int8]
type Int16Array ¶
type Int16Array = TypedArray[int16]
type Int32Array ¶
type Int32Array = TypedArray[int32]
type Number ¶
type Number[T numTypes] struct {
// contains filtered or unexported fields
}
Number represents a js number living in the heap.
NOTE: This type is defined for code not working directly with imported js functions (using go:wasmimport) and should be avoided whenever possible.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
An Object is a reference to a js object.
func (Object) BoolPropByString ¶
func (Object) DefineProp ¶
func (Object) DeleteProp ¶
func (Object) NumPropByString ¶
func (Object) PropByString ¶
func (Object) SetNumPropByString ¶
func (Object) SetPropByString ¶
func (Object) SetPrototype ¶
SetPrototype sets o's prototype to proto
When free is ture, proto is freed after the call.
func (Object) SetStringProp ¶
func (Object) SetStringPropByString ¶
func (Object) StringProp ¶
func (Object) StringPropByString ¶
type ObservableArray ¶
type ObservableArray[T any] struct { // contains filtered or unexported fields }
TODO
func (ObservableArray[T]) FromRef ¶
func (a ObservableArray[T]) FromRef(ref Ref) ObservableArray[T]
func (ObservableArray[T]) Ref ¶
func (a ObservableArray[T]) Ref() Ref
type Promise ¶
type Promise[T interface{ FromRef(Ref) T }] struct {
// contains filtered or unexported fields
}
func NewRejectedPromise ¶
func NewResolvedPromise ¶
func (Promise[T]) Await ¶
Await works like `await`
When take is true, it frees the Promise p from the application heap.
func (Promise[T]) Then ¶
func (p Promise[T]) Then(onfulfilled, onrejected Func[func(Ref)], onfinally Func[func()]) Promise[T]
Then calls Promise.then(onfulfilled, onrejected) and if onfinally is not undefined/null value, call Promise.finally(onfinally).
NOTE: It is generally discouraged to use this function for callbacks to Go code.
type Ref ¶
type SharedArrayBuffer ¶
type SharedArrayBuffer struct {
// contains filtered or unexported fields
}
TODO
func (SharedArrayBuffer) Free ¶
func (buf SharedArrayBuffer) Free()
func (SharedArrayBuffer) FromRef ¶
func (buf SharedArrayBuffer) FromRef(ref Ref) SharedArrayBuffer
func (SharedArrayBuffer) Once ¶
func (buf SharedArrayBuffer) Once() SharedArrayBuffer
func (SharedArrayBuffer) Ref ¶
func (buf SharedArrayBuffer) Ref() Ref
type String ¶
type String struct {
// contains filtered or unexported fields
}
A String is a reference to a javascript string.
func NewString ¶
NewString creates a js string for the string, return the reference to the created string.
type TypedArray ¶
type TypedArray[T elem] struct {
// contains filtered or unexported fields
}
func NewTypedArray ¶
func NewTypedArray[T elem](sz int) TypedArray[T]
func NewTypedArrayOf ¶
func NewTypedArrayOf[T elem](elems ...T) TypedArray[T]
func (TypedArray[T]) Buffer ¶
func (a TypedArray[T]) Buffer(take bool) Ref
func (TypedArray[T]) ByteLength ¶
func (a TypedArray[T]) ByteLength() int
func (TypedArray[T]) ByteOffset ¶
func (a TypedArray[T]) ByteOffset() int
func (TypedArray[T]) Copy ¶
func (a TypedArray[T]) Copy(start int, outBuf []T) int
func (TypedArray[T]) Fill ¶
func (a TypedArray[T]) Fill(start int, elems ...T) int
func (TypedArray[T]) Free ¶
func (a TypedArray[T]) Free()
func (TypedArray[T]) FromArrayBuffer ¶
func (a TypedArray[T]) FromArrayBuffer(take bool, buf ArrayBuffer) TypedArray[T]
func (TypedArray[T]) FromRef ¶
func (a TypedArray[T]) FromRef(ref Ref) TypedArray[T]
func (TypedArray[T]) FromSharedArrayBuffer ¶
func (a TypedArray[T]) FromSharedArrayBuffer(take bool, buf SharedArrayBuffer) TypedArray[T]
func (TypedArray[T]) Length ¶
func (a TypedArray[T]) Length() int
func (TypedArray[T]) MustFill ¶
func (a TypedArray[T]) MustFill(start int, elems ...T) TypedArray[T]
func (TypedArray[T]) Nth ¶
func (a TypedArray[T]) Nth(i int) (T, bool)
func (TypedArray[T]) Once ¶
func (a TypedArray[T]) Once() TypedArray[T]
func (TypedArray[T]) Ref ¶
func (a TypedArray[T]) Ref() Ref
func (TypedArray[T]) Set ¶
func (a TypedArray[T]) Set(other TypedArray[T]) TypedArray[T]
func (TypedArray[T]) SetNth ¶
func (a TypedArray[T]) SetNth(i int, val T) bool
func (TypedArray[T]) Slice ¶
func (a TypedArray[T]) Slice(start, end int) TypedArray[T]
func (TypedArray[T]) SliceFrom ¶
func (a TypedArray[T]) SliceFrom(start int) TypedArray[T]
type Uint8Array ¶
type Uint8Array = TypedArray[uint8]
type Uint8ClampedArray ¶
type Uint8ClampedArray = TypedArray[uint8]
type Uint16Array ¶
type Uint16Array = TypedArray[uint16]
type Uint32Array ¶
type Uint32Array = TypedArray[uint32]
type UntypedCallback ¶
type UntypedCallback func(args []Ref)
UntypedCallback dispatches js callbacks without resolving argument types.
By convension, args[0] refers to the js `this` context.
func (UntypedCallback) DispatchCallback ¶
func (fn UntypedCallback) DispatchCallback(_ uintptr, ctx *CallbackContext)
DispatchCallback implements CallbackDispatcher.
func (UntypedCallback) Register ¶
func (fn UntypedCallback) Register() Func[func([]Ref)]
Register registers this function to js and return the reference to that js function.