js

package
v0.0.0-...-a048d37 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ValueUndefined is the ref of Undefined
	ValueUndefined = 0
)

Variables

View Source
var (
	// ExceptionNotfound wraps the EEXIS errno
	ExceptionNotfound = NewException("EEXIS", "not found")
	// ExceptionNoSys wraps the ENOSYS errno
	ExceptionNoSys = NewException("ENOSYS", "not implemention")
	// ExceptionInvalidArgument wraps the EINVAL errno
	ExceptionInvalidArgument = NewException("EINVAL", "invalid argument")
	// ExceptionUndefined wraps the EINVAL errno
	ExceptionUndefined = NewException("EINVAL", "undefined")
)

Functions

func Array

func Array(args []interface{}) interface{}

Array simulates Array function

func Date

func Date(argument []interface{}) interface{}

Date simulates Date function

func RegisterBuiltins

func RegisterBuiltins(g *Global)

RegisterBuiltins register js builtins to Global object

func Throw

func Throw(fmtstr string, args ...interface{})

Throw uses a fmt like style to throw an exception

func ThrowException

func ThrowException(e *Exception)

ThrowException throw an exception

func Uint8Array

func Uint8Array(args []interface{}) interface{}

Uint8Array simulates Uint8Array function

Types

type Exception

type Exception struct {
	Code    string
	Message string
}

Exception simulates js Exception

func ExceptionRefNotFound

func ExceptionRefNotFound(ref Ref) *Exception

ExceptionRefNotFound is the Exception throwed when Ref is not found by VM

func NewException

func NewException(code, msg string) *Exception

NewException instances a Exception

func (*Exception) Error

func (e *Exception) Error() string

Error returns the error message of Exception

type Global

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

Global simulates js'global object

func NewGlobal

func NewGlobal() *Global

NewGlobal instances a global object

func (*Global) GetProperty

func (g *Global) GetProperty(name string) (interface{}, bool)

GetProperty implements js.PropertyGetter interface

func (*Global) Register

func (g *Global) Register(name string, prop interface{})

Register property to global object

type Memory

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

Memory simulates the Memory object in wasm_exec.js

func NewMemory

func NewMemory(f func() []byte) *Memory

NewMemory instance a Memory

func (*Memory) GetProperty

func (m *Memory) GetProperty(name string) (interface{}, bool)

GetProperty implements the PropertyGetter interface

type PropertyGetter

type PropertyGetter interface {
	GetProperty(property string) (interface{}, bool)
}

A PropertyGetter can get property from GetProperty method

type Ref

type Ref int64

Ref represents the unique id of a js object

const (
	// ValueNaN is the ref of Nan
	ValueNaN Ref = nanHead<<32 | iota
	// ValueZero is the ref of number 0
	ValueZero
	// ValueNull is the ref of Null
	ValueNull
	// ValueTrue is the ref of True
	ValueTrue
	// ValueFalse is the ref of False
	ValueFalse
	// ValueGlobal is the ref of global
	ValueGlobal
	// ValueMemory is the ref of wasm Memory object
	ValueMemory
	// ValueGo is the ref of Go object
	ValueGo
)

func (Ref) ID

func (r Ref) ID() int64

ID return the id of ref

func (Ref) Number

func (r Ref) Number() (int64, bool)

Number return ref as a number, if ref not a number, false will be returned

func (Ref) String

func (r Ref) String() string

String return the debug string of ref

type VM

type VM struct {
	Log *log.Logger
	// contains filtered or unexported fields
}

VM simulates the js runtime

func NewVM

func NewVM(config *VMConfig) *VM

NewVM instance a VM object

func (*VM) Call

func (vm *VM) Call(ref Ref, method string, args []Ref) Ref

Call call ref's method using method as method name

func (*VM) CatchException

func (vm *VM) CatchException(e *Ref)

CatchException will recover panic and store the value to e only if the recovered type is *Exception otherwise panic will go on

func (*VM) DebugStr

func (vm *VM) DebugStr(ref Ref) string

DebugStr return the debug string of ref

func (*VM) Exception

func (vm *VM) Exception(e *Exception) Ref

Exception wraps an *Exception to Ref

func (*VM) Invoke

func (vm *VM) Invoke(ref Ref, args []Ref) Ref

Invoke call ref as a js function

func (*VM) New

func (vm *VM) New(ref Ref, args []Ref) Ref

New treat ref as the construct function, args as arguments to instance a js object

func (*VM) Property

func (vm *VM) Property(ref Ref, name string) Ref

Property return the property of a js object if not found, undefined will be returned

func (*VM) Store

func (vm *VM) Store(x interface{}) Ref

Store wrap a Go object to Ref

func (*VM) Value

func (vm *VM) Value(ref Ref) *Value

Value return the stored value from ref

type VMConfig

type VMConfig struct {
	// the wasm Memory
	Memory *Memory

	Global *Global
}

VMConfig is the config of VM object

type Value

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

Value is the internal representing of a js object

func (*Value) Bytes

func (v *Value) Bytes() ([]byte, error)

Bytes return ref as []byte, error will return if type of ref is not Uint8Array

func (*Value) Name

func (v *Value) Name() string

Name is used for debugging

func (*Value) String

func (v *Value) String() string

String return the string representing of a value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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