Version: v0.1.0 Latest Latest

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

Go to latest
Published: Sep 9, 2019 License: MIT Imports: 1 Imported by: 16



Package js is a drop-in replacement for syscall/js that provides identical behavior in a WebAssembly environment, and useful non-functional behavior outside of WebAssembly.

To use it, simply import this package instead of "syscall/js" and use it in exactly the same way. Your code will compile targeting either wasm or non-wasm environments. In wasm, the functionality is exactly the same, the calls and types are delegated directly to "syscall/js". The compiler will optimize away virtually (if not literally) all of the overhead associated with this aliasing. When run outside of wasm, appropriate functionality indicates that the environment is not availble: All js.Value instances are undefined. Global(), Value.Get() always return undefined. Value.Call(), FuncOf() and other such functions will panic. Value.Truthy() will always return false. For example, Global().Truthy() can be used to determine if the js environment is functional.

Rationale: Since syscall/js is only available when the GOOS is "js", this means programs which run server-side cannot access that package and will fail to compile. Since Vugu components are inherently closely integrated with browsers and may often need to do things like declare variables of type js.Value, this is a problem for components which are rendered not only in wasm client-side but also server-side. Build tags can be used to provide multiple implementations of a components but this can become tedious.

Usually what you want is that the majority of your code which is not js-specific can be written once and execute in the browser or on the server, and the relatively small amount of functionality that is uses "js" will compile properly in both environments but just not be executed on the server. Or allow for if statements to easily disable functionality not available server-side. That's what this package provides.



View Source
const (
	TypeUndefined = Type(sjs.TypeUndefined)
	TypeNull      = Type(sjs.TypeNull)
	TypeBoolean   = Type(sjs.TypeBoolean)
	TypeNumber    = Type(sjs.TypeNumber)
	TypeString    = Type(sjs.TypeString)
	TypeSymbol    = Type(sjs.TypeSymbol)
	TypeObject    = Type(sjs.TypeObject)
	TypeFunction  = Type(sjs.TypeFunction)


This section is empty.


func CopyBytesToGo

func CopyBytesToGo(dst []byte, src Value) int

CopyBytesToGo alias to syscall/js

func CopyBytesToJS

func CopyBytesToJS(dst Value, src []byte) int

CopyBytesToJS alias to syscall/js


type Error

type Error struct {
	// Value is the underlying JavaScript error value.

Error alias to syscall/js

func (Error) Error

func (e Error) Error() string

Error alias to syscall/js

type Func

type Func struct {
	// contains filtered or unexported fields

Func alias to syscall/js

func FuncOf

func FuncOf(fn func(Value, []Value) interface{}) Func

FuncOf alias to syscall/js

func (Func) Release

func (c Func) Release()

Release alias to syscall/js

type Type

type Type int

Type alias to syscall/js

func (Type) String

func (t Type) String() string

String alias to syscall/js

type Value

type Value sjs.Value

Value alias to syscall/js

func Global

func Global() Value

Global alias to syscall/js

func Null

func Null() Value

Null alias to syscall/js

func Undefined

func Undefined() Value

Undefined alias to syscall/js

func ValueOf

func ValueOf(x interface{}) Value

ValueOf alias to syscall/js

func (Value) Bool

func (v Value) Bool() bool

func (Value) Call

func (v Value) Call(m string, args ...interface{}) Value

func (Value) Float

func (v Value) Float() float64

func (Value) Get

func (v Value) Get(p string) Value

func (Value) Index

func (v Value) Index(i int) Value

func (Value) InstanceOf

func (v Value) InstanceOf(t Value) bool

func (Value) Int

func (v Value) Int() int

func (Value) Invoke

func (v Value) Invoke(args ...interface{}) Value

func (Value) JSValue

func (v Value) JSValue() Value

JSValue alias to syscall/js

func (Value) Length

func (v Value) Length() int

func (Value) New

func (v Value) New(args ...interface{}) Value

func (Value) Set

func (v Value) Set(p string, x interface{})

func (Value) SetIndex

func (v Value) SetIndex(i int, x interface{})

func (Value) String

func (v Value) String() string

func (Value) Truthy

func (v Value) Truthy() bool

func (Value) Type

func (v Value) Type() Type

Type alias to syscall/js

type ValueError

type ValueError struct {
	Method string
	Type   Type

ValueError alias to syscall/js

func (*ValueError) Error

func (e *ValueError) Error() string

Error alias to syscall/js

type Wrapper

type Wrapper interface {
	JSValue() Value

Wrapper alias to syscall/js

Jump to

Keyboard shortcuts

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