Documentation ¶
Overview ¶
Package safejs provides guardrails around the syscall/js package, like turning thrown exceptions into errors.
Since syscall/js is experimental, this package may have breaking changes to stay aligned with the latest versions of Go.
Index ¶
- Constants
- func CopyBytesToGo(dst []byte, src Value) (int, error)
- func CopyBytesToJS(dst Value, src []byte) (int, error)
- func Unsafe(value Value) js.Value
- type Error
- type Func
- type Type
- type Value
- func (v Value) Bool() (bool, error)
- func (v Value) Call(m string, args ...any) (Value, error)
- func (v Value) Delete(p string) error
- func (v Value) Equal(w Value) bool
- func (v Value) Float() (float64, error)
- func (v Value) Get(p string) (Value, error)
- func (v Value) Index(i int) (Value, error)
- func (v Value) InstanceOf(t Value) (bool, error)
- func (v Value) Int() (int, error)
- func (v Value) Invoke(args ...any) (Value, error)
- func (v Value) IsNaN() bool
- func (v Value) IsNull() bool
- func (v Value) IsUndefined() bool
- func (v Value) Length() (int, error)
- func (v Value) New(args ...any) (Value, error)
- func (v Value) Set(p string, x any) error
- func (v Value) SetIndex(i int, x any) error
- func (v Value) String() (string, error)
- func (v Value) Truthy() (bool, error)
- func (v Value) Type() Type
Constants ¶
const ( TypeUndefined = Type(js.TypeUndefined) TypeNull = Type(js.TypeNull) TypeBoolean = Type(js.TypeBoolean) TypeNumber = Type(js.TypeNumber) TypeString = Type(js.TypeString) TypeSymbol = Type(js.TypeSymbol) TypeObject = Type(js.TypeObject) TypeFunction = Type(js.TypeFunction) )
Available JavaScript types
Variables ¶
This section is empty.
Functions ¶
func CopyBytesToGo ¶
CopyBytesToGo copies bytes from src to dst. Returns the number of bytes copied, which is the minimum of the lengths of src and dst. Returns an error if src is not an Uint8Array or Uint8ClampedArray.
func CopyBytesToJS ¶
CopyBytesToJS copies bytes from src to dst. Returns the number of bytes copied, which is the minimum of the lengths of src and dst. Returns an error if dst is not an Uint8Array or Uint8ClampedArray.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error wraps a JavaScript error.
type Func ¶
type Func struct {
// contains filtered or unexported fields
}
Func is a wrapped Go function to be called by JavaScript.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a safer version of js.Value. Any panic returns an error instead.
func Global ¶
func Global() Value
Global returns the JavaScript global object, usually "window" or "global".
func MustGetGlobal ¶ added in v0.1.1
MustGetGlobal fetches the given global, then verifies it is truthy. Panics on error or falsy values. This is intended for simple global variable initialization, like preparing classes for later instantiation.
For example:
var jsUint8Array = safejs.MustGetGlobal("Uint8Array")
func Safe ¶
Safe wraps a js.Value into a safejs.Value. Ideal for use in libraries where exposed types must match the standard library.
func ValueOf ¶
ValueOf returns value as a JavaScript value. See js.ValueOf for details.
func (Value) Call ¶
Call does a JavaScript call to the method m of value v with the given arguments. The arguments are mapped to JavaScript values according to the ValueOf function. Returns an error if v has no method m, the arguments failed to map to JavaScript values, or the function throws an error.
func (Value) Delete ¶
Delete deletes the JavaScript property p of value v. Returns an error if v is not a JavaScript object.
func (Value) Equal ¶
Equal reports whether v and w are equal according to JavaScript's === operator.
func (Value) Float ¶
Float returns the value v as a float64. Returns an error if v is not a JavaScript number.
func (Value) Get ¶
Get returns the JavaScript property p of value v. Returns an error if v is not a JavaScript object.
func (Value) Index ¶
Index returns JavaScript index i of value v. Returns an error if v is not a JavaScript object.
func (Value) InstanceOf ¶
InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator. Returns an error if v is not a constructable type.
func (Value) Int ¶
Int returns the value v truncated to an int. Returns an error if v is not a JavaScript number.
func (Value) Invoke ¶
Invoke does a JavaScript call of the value v with the given arguments. The arguments get mapped to JavaScript values according to the ValueOf function. Returns an error if v is not a JavaScript function, the arguments failed to map to JavaScript values, or the function throws an error.
func (Value) IsUndefined ¶
IsUndefined reports whether v is the JavaScript value "undefined".
func (Value) Length ¶
Length returns the JavaScript property "length" of v. Returns an error if v is not a JavaScript object.
func (Value) New ¶
New uses JavaScript's "new" operator with value v as constructor and the given arguments. The arguments get mapped to JavaScript values according to the ValueOf function. Returns an error if v is not a JavaScript function, the arguments failed to map to JavaScript values, or the constructor throws an error.
func (Value) Set ¶
Set sets the JavaScript property p of value v to ValueOf(x). Returns an error if v is not a JavaScript object or x failed to map to a JavaScript value.
func (Value) SetIndex ¶
SetIndex sets the JavaScript index i of value v to ValueOf(x). Returns an error if if v is not a JavaScript object or x failed to map to a JavaScript value.
func (Value) String ¶
String returns the value v as a string. Unlike the other getters, String() does not return an error if v's Type is not TypeString. Instead, it returns a string of the form "<T>" or "<T: V>" where T is v's type and V is a string representation of v's value.
Returns an error if v is an invalid type or the string failed to load from the JavaScript runtime.
NOTE: syscall/js takes the stance that String is a special case due to Go's String method convention and avoids panicking. However, js.String() can still fail in other ways so an error is returned anyway.
func (Value) Truthy ¶
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.
Returns an error if v's type is invalid or if the value fails to load from the JavaScript runtime.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
assert
Package assert contains small assertion test functions to assist in writing clean tests.
|
Package assert contains small assertion test functions to assist in writing clean tests. |
catch
Package catch runs functions and returns panic values as errors instead.
|
Package catch runs functions and returns panic values as errors instead. |
stackerr
Package stackerr adds stack traces to verbose error messages.
|
Package stackerr adds stack traces to verbose error messages. |
Package jsguard analyzes unsafe calls to Go's syscall/js package
|
Package jsguard analyzes unsafe calls to Go's syscall/js package |
cmd/jsguard
Command jsguard reports unsafe calls to Go's syscall/js package
|
Command jsguard reports unsafe calls to Go's syscall/js package |