Imports #
"internal/synctest"
"sync"
"runtime"
"unsafe"
"internal/synctest"
"sync"
"runtime"
"unsafe"
const TypeBoolean
const TypeFunction
const TypeNull
const TypeNumber
const TypeObject
const TypeString
const TypeSymbol
const TypeUndefined Type = iota
var arrayConstructor = *ast.CallExpr
var funcs = *ast.CallExpr
var funcsMu sync.Mutex
var jsGo = *ast.CallExpr
nanHead are the upper 32 bits of a ref which are set if the value is not encoded as an IEEE 754 number (see above).
const nanHead = 0x7FF80000
var nextFuncID uint32 = 1
var objectConstructor = *ast.CallExpr
const typeFlagFunction
the type flags need to be in sync with wasm_exec.js
const typeFlagNone = iota
const typeFlagObject
const typeFlagString
const typeFlagSymbol
var valueFalse = *ast.CallExpr
var valueGlobal = *ast.CallExpr
var valueNaN = *ast.CallExpr
var valueNull = *ast.CallExpr
var valueTrue = *ast.CallExpr
var valueUndefined = Value{...}
var valueZero = *ast.CallExpr
Type represents the JavaScript type of a Value.
type Type int
ref is used to identify a JavaScript value, since the value itself can not be passed to WebAssembly. The JavaScript value "undefined" is represented by the value 0. A JavaScript number (64-bit float, except 0 and NaN) is represented by its IEEE 754 binary representation. All other values are represented as an IEEE 754 binary representation of NaN with bits 0-31 used as an ID and bits 32-34 used to differentiate between string, symbol, function and object.
type ref uint64
Error wraps a JavaScript error.
type Error struct {
Value
}
Func is a wrapped Go function to be called by JavaScript.
type Func struct {
Value
bubble *synctest.Bubble
id uint32
}
Value represents a JavaScript value. The zero value is the JavaScript value "undefined". Values can be checked for equality with the Equal method.
type Value struct {
_ [0]func()
ref ref
gcPtr *ref
}
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.
type ValueError struct {
Method string
Type Type
}
Bool returns the value v as a bool. It panics if v is not a JavaScript boolean.
func (v Value) Bool() bool
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.
func (v Value) Call(m string, args ...any) Value
CopyBytesToGo copies bytes from src to dst. It panics if src is not a Uint8Array or Uint8ClampedArray. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
func CopyBytesToGo(dst []byte, src Value) int
CopyBytesToJS copies bytes from src to dst. It panics if dst is not a Uint8Array or Uint8ClampedArray. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
func CopyBytesToJS(dst Value, src []byte) int
Delete deletes the JavaScript property p of value v. It panics if v is not a JavaScript object.
func (v Value) Delete(p string)
Equal reports whether v and w are equal according to JavaScript's === operator.
func (v Value) Equal(w Value) bool
Error implements the error interface.
func (e Error) Error() string
func (e *ValueError) Error() string
Float returns the value v as a float64. It panics if v is not a JavaScript number.
func (v Value) Float() float64
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.
func FuncOf(fn func(this Value, args []Value) any) Func
Get returns the JavaScript property p of value v. It panics if v is not a JavaScript object.
func (v Value) Get(p string) Value
Global returns the JavaScript global object, usually "window" or "global".
func Global() Value
Index returns JavaScript index i of value v. It panics if v is not a JavaScript object.
func (v Value) Index(i int) Value
InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator.
func (v Value) InstanceOf(t Value) bool
Int returns the value v truncated to an int. It panics if v is not a JavaScript number.
func (v Value) Int() int
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.
func (v Value) Invoke(args ...any) Value
IsNaN reports whether v is the JavaScript value "NaN".
func (v Value) IsNaN() bool
IsNull reports whether v is the JavaScript value "null".
func (v Value) IsNull() bool
IsUndefined reports whether v is the JavaScript value "undefined".
func (v Value) IsUndefined() bool
Length returns the JavaScript property "length" of v. It panics if v is not a JavaScript object.
func (v Value) Length() int
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.
func (v Value) New(args ...any) Value
Null returns the JavaScript value "null".
func Null() Value
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.
func (c Func) Release()
Set sets the JavaScript property p of value v to ValueOf(x). It panics if v is not a JavaScript object.
func (v Value) Set(p string, x any)
SetIndex sets the JavaScript index i of value v to ValueOf(x). It panics if v is not a JavaScript object.
func (v Value) SetIndex(i int, x any)
func (t Type) 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 "
func (v Value) String() string
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.
func (v Value) Truthy() bool
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.
func (v Value) Type() Type
Undefined returns the JavaScript value "undefined".
func Undefined() 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.
func ValueOf(x any) Value
copyBytesToGo copies bytes from src to dst. Using go:noescape is safe because the dst byte slice is only used as a dst copy buffer and no references to it are maintained. go:wasmimport gojs syscall/js.copyBytesToGo go:noescape
func copyBytesToGo(dst []byte, src ref) (int, bool)
copyBytesToJS copies bytes from src to dst. Using go:noescape is safe because the src byte slice is only used as a src copy buffer and no references to it are maintained. go:wasmimport gojs syscall/js.copyBytesToJS go:noescape
func copyBytesToJS(dst ref, src []byte) (int, bool)
go:wasmimport gojs syscall/js.finalizeRef
func finalizeRef(r ref)
func (v Value) float(method string) float64
func floatValue(f float64) Value
handleEvent retrieves the pending event (window._pendingEvent) and calls the js.Func on it. It returns true if an event was handled.
func handleEvent() bool
func init()
func (v Value) isNumber() bool
func (t Type) isObject() bool
func jsString(v Value) string
makeArgSlices makes two slices to hold JavaScript arg data. It can be paired with storeArgs to make-and-store JavaScript arg slices. However, the two functions are separated to ensure makeArgSlices is inlined which will prevent the slices from being heap allocated for small (<=16) numbers of args.
func makeArgSlices(size int) (argVals []Value, argRefs []ref)
func makeValue(r ref) Value
func predefValue(id uint32, typeFlag byte) Value
setEventHandler is defined in the runtime package.
func setEventHandler(fn func() bool)
storeArgs maps input args onto respective Value and ref slices. It can be paired with makeArgSlices to make-and-store JavaScript arg slices.
func storeArgs(args []any, argValsDst []Value, argRefsDst []ref)
stringVal copies string x to Javascript and returns a ref. Using go:noescape is safe because no references are maintained to the Go string x after the syscall returns. go:wasmimport gojs syscall/js.stringVal go:noescape
func stringVal(x string) ref
valueCall does a JavaScript call to the method name m of ref v with the given arguments. Using go:noescape is safe because no references are maintained to the Go string m after the syscall returns. Additionally, the args slice is only used temporarily to collect the JavaScript objects for the JavaScript method invocation. go:wasmimport gojs syscall/js.valueCall go:nosplit go:noescape
func valueCall(v ref, m string, args []ref) (ref, bool)
valueDelete deletes the JavaScript property p of ref v. Using go:noescape is safe because no references are maintained to the Go string p after the syscall returns. go:wasmimport gojs syscall/js.valueDelete go:noescape
func valueDelete(v ref, p string)
valueGet returns a ref to JavaScript property p of ref v. Using go:noescape is safe because no references are maintained to the Go string p after the syscall returns. go:wasmimport gojs syscall/js.valueGet go:noescape
func valueGet(v ref, p string) ref
go:wasmimport gojs syscall/js.valueIndex
func valueIndex(v ref, i int) ref
go:wasmimport gojs syscall/js.valueInstanceOf
func valueInstanceOf(v ref, t ref) bool
valueInvoke does a JavaScript call to value v with the given arguments. Using go:noescape is safe because the args slice is only used temporarily to collect the JavaScript objects for the JavaScript method invocation. go:wasmimport gojs syscall/js.valueInvoke go:noescape
func valueInvoke(v ref, args []ref) (ref, bool)
go:wasmimport gojs syscall/js.valueLength
func valueLength(v ref) int
valueLoadString loads string data located at ref v into byte slice b. Using go:noescape is safe because the byte slice is only used as a destination for storing the string data and references to it are not maintained. go:wasmimport gojs syscall/js.valueLoadString go:noescape
func valueLoadString(v ref, b []byte)
valueNew uses JavaScript's "new" operator with value v as a constructor and the given arguments. Using go:noescape is safe because the args slice is only used temporarily to collect the JavaScript objects for the constructor execution. go:wasmimport gojs syscall/js.valueNew go:noescape
func valueNew(v ref, args []ref) (ref, bool)
go:wasmimport gojs syscall/js.valuePrepareString
func valuePrepareString(v ref) (ref, int)
valueSet sets property p of ref v to ref x. Using go:noescape is safe because no references are maintained to the Go string p after the syscall returns. go:wasmimport gojs syscall/js.valueSet go:noescape
func valueSet(v ref, p string, x ref)
go:wasmimport gojs syscall/js.valueSetIndex
func valueSetIndex(v ref, i int, x ref)
Generated with Arrow