js

package
v0.1.6-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncOf

func FuncOf(fn func(this JSValue, args []JSValue) any) js.Func

FuncOf returns a wrapped function.

Invoking the JavaScript function will synchronously call the Go function fn 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.

A wrapped function triggered during a call from Go to JavaScript gets executed on the same goroutine. A wrapped function triggered by JavaScript's event loop gets executed on an extra goroutine. Blocking operations in the wrapped function will block the event loop. As a consequence, if one wrapped function blocks, other wrapped funcs will not be processed. A blocking function should therefore explicitly start a new goroutine.

Func.Release must be called to free up resources when the function will not be used any more.

func TrySet

func TrySet(_param ...string) error

FIX: tryset

Types

type JSValue

type JSValue struct {
	// contains filtered or unexported fields
}

JSValue represents a JavaScript value. On wasm architecture, it wraps the JSValue from https://golang.org/pkg/syscall/js/ package.

func Global

func Global() JSValue

Global returns the JavaScript global object, usually "window" or "global".

func TryGet

func TryGet(_param ...string) (JSValue, error)

func ValueOf

func ValueOf(x any) (_jsv JSValue)

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 (JSValue) Bool

func (_v JSValue) Bool() bool

Bool returns the value v as a bool. It panics if v is not a JavaScript boolean.

func (JSValue) Call

func (_v JSValue) Call(m string, args ...any) JSValue

Call does a JavaScript call to the method m of value v with the given arguments. It panics if v has no method m. Returns js.null if _v is undefined. The arguments get mapped to JavaScript values according to the ValueOf function.

func (JSValue) Check

func (_v JSValue) Check(propertyname string) (JSValue, error)

Check is like Get but returns an error without printing a warning in case of an error

func (JSValue) Delete

func (_v JSValue) Delete(p string)

Delete deletes the JavaScript property p of value v. It panics if v is not a JavaScript object.

func (JSValue) Equal

func (_v JSValue) Equal(w JSValue) bool

Equal reports whether v and w are equal according to JavaScript's === operator.

func (JSValue) Float

func (_v JSValue) Float() float64

Float returns the value v as a float64. It panics if v is not a JavaScript number.

func (JSValue) Get

func (_v JSValue) Get(_pname string) JSValue

Get returns the JavaScript property p of value v. Returns js.null if _v is undefined or unable to get p. Print a warning in these cases.

func (JSValue) GetBool

func (_v JSValue) GetBool(_pname string) bool

func (JSValue) GetFloat

func (_v JSValue) GetFloat(_pname string) float64

func (JSValue) GetInt

func (_v JSValue) GetInt(_pname string) int

func (JSValue) GetObject

func (_v JSValue) GetObject(_pname string) JSValue

GetObject returns the value of _v ensuring it's of type Object, otherwise returns null

func (JSValue) GetString

func (v JSValue) GetString(pname string) string

func (JSValue) Index

func (_v JSValue) Index(i int) JSValue

Index returns JavaScript index i of value v. It panics if v is not a JavaScript object. Returns js.null if _v is undefined and print a warning.

func (JSValue) InstanceOf

func (_v JSValue) InstanceOf(t JSValue) bool

func (JSValue) Int

func (_v JSValue) Int() int

Int returns the value v truncated to an int. It panics if v is not a JavaScript number.

func (JSValue) Invoke

func (_v JSValue) Invoke(args ...any) JSValue

func (JSValue) IsDefined

func (_v JSValue) IsDefined() bool

IsDefined returns true if js value is not null nor undefined

func (JSValue) IsObject

func (_v JSValue) IsObject() bool

IsObject returns true if js value is of type Object

func (JSValue) Length

func (_v JSValue) Length() int

Length returns the JavaScript property "length" of v. It panics if v is not a JavaScript object. Returns js.null if _v is undefined and print a warning.

func (JSValue) New

func (_v JSValue) New(args ...any) JSValue

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 (JSValue) Set

func (_v JSValue) Set(p string, x any)

Set sets the JavaScript property p of value v to ValueOf(x). It panics if v is not a JavaScript object. Returns js.null if _v is undefined and print a warning.

func (JSValue) String

func (_v JSValue) 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 "<T>" or "<T: V>" where T is v's type and V is a string representation of v's value.

func (JSValue) Then

func (_v JSValue) Then(f func(JSValue))

func (JSValue) Truthy

func (_v JSValue) Truthy() bool

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 (JSValue) Type

func (_v JSValue) Type() TYPE

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 (JSValue) Value

func (_v JSValue) Value() JSValue

Value returns the value itself for JSValueProvider interface

func (*JSValue) Wrap

func (_v *JSValue) Wrap(_jsvp JSValueProvider)

type JSValueProvider

type JSValueProvider interface {
	Value() JSValue
}

JSValueProvider is implemented by types that are backed by a JavaScript value.

type JSValueWrapper

type JSValueWrapper interface {
	Wrap(JSValueProvider)
}

type TYPE

type TYPE int

TYPE represents the JavaScript type of a Value.

const (
	TYPE_UNDEFINED TYPE = TYPE(js.TypeUndefined)
	TYPE_NULL      TYPE = TYPE(js.TypeNull)
	TYPE_BOOLEAN   TYPE = TYPE(js.TypeBoolean)
	TYPE_NUMBER    TYPE = TYPE(js.TypeNumber)
	TYPE_STRING    TYPE = TYPE(js.TypeString)
	TYPE_SYMBOL    TYPE = TYPE(js.TypeSymbol)
	TYPE_OBJECT    TYPE = TYPE(js.TypeObject)
	TYPE_FUNCTION  TYPE = TYPE(js.TypeFunction)
)

Constants that enumerates the JavaScript types.

func (TYPE) String

func (t TYPE) String() string

Jump to

Keyboard shortcuts

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