go-candyjs: github.com/mcuadros/go-candyjs Index | Files

package candyjs

import "github.com/mcuadros/go-candyjs"

Index

Package Files

base.go misc.go package.go proxy.go storage.go

Variables

var ErrPackageNotFound = errors.New("Unable to find the requested package")

PackageNotFound error is throw when a package cannot be found, usually this happend when a PackagePusher function was not registered using RegisterPackagePusher.

var ErrUndefinedProperty = errors.New("undefined property")

ErrUndefinedProperty is throw when a property for a given proxied object on javascript cannot be found, basically a valid method or field cannot found.

func RegisterPackagePusher Uses

func RegisterPackagePusher(pckgName string, f PackagePusher)

RegisterPackagePusher registers a PackagePusher into the global storage, this storage is a private map defined on the candyjs package. The pushers are launch by the function PushGlobalPackage.

type Context Uses

type Context struct {
    *duktape.Context
    // contains filtered or unexported fields
}

Context represents a Duktape thread and its call and value stacks.

func NewContext Uses

func NewContext() *Context

NewContext returns a new Context

func (*Context) PushGlobalGoFunction Uses

func (ctx *Context) PushGlobalGoFunction(name string, f interface{}) (int, error)

PushGlobalGoFunction like PushGoFunction but pushed to the global object

func (*Context) PushGlobalInterface Uses

func (ctx *Context) PushGlobalInterface(name string, v interface{}) error

PushGlobalInterface like PushInterface but pushed to the global object

func (*Context) PushGlobalPackage Uses

func (ctx *Context) PushGlobalPackage(pckgName, alias string) error

PushGlobalPackage all the functions and types from the given package using the pre-registered PackagePusher function.

func (*Context) PushGlobalProxy Uses

func (ctx *Context) PushGlobalProxy(name string, v interface{}) int

PushGlobalProxy like PushProxy but pushed to the global object

func (*Context) PushGlobalStruct Uses

func (ctx *Context) PushGlobalStruct(name string, s interface{}) (int, error)

PushGlobalStruct like PushStruct but pushed to the global object

func (*Context) PushGlobalType Uses

func (ctx *Context) PushGlobalType(name string, s interface{}) int

PushGlobalType like PushType but pushed to the global object

func (*Context) PushGoFunction Uses

func (ctx *Context) PushGoFunction(f interface{}) int

PushGoFunction push a native Go function of any signature to the stack. A pointer to the function is stored in the internals of the context and collected by the duktape GC removing any reference in Go also.

The most common types are supported as input arguments, also the variadic functions can be used.

You can use JS functions as arguments but you should wrapper it with the helper `CandyJS.proxy`. Example:

ctx.PushGlobalGoFunction("test", func(fn func(int, int) int) {
	...
})

ctx.PevalString(`test(CandyJS.proxy(function(a, b) { return a * b; }));`)

The structs can be delivered to the functions in three ways:

- In-line representation as plain JS objects: `{'int':42}`
- Using a previous pushed type using `PushGlobalType`: `new MyModel`
- Using a previous pushed instance using `PushGlobalProxy`

All other types are loaded into Go using `json.Unmarshal` internally

The following types are not supported chans, complex64 or complex128, and the types rune, byte and arrays are not tested.

The returns are handled in the following ways:

- The result of functions with a single return value like `func() int` is
  pushed directly to the stack.
- Functions with a n return values like `func() (int, int)` are pushed as
  an array. The errors are removed from this array.
- Returns of functions with a trailling error like `func() (string, err)`:
  if err is not nil an error is throw in the context, and the other values
  are discarded. IF err is nil, the values are pushed to the stack, following
  the previuos rules.

All the non erros returning values are pushed following the same rules of `PushInterface` method

func (*Context) PushInterface Uses

func (ctx *Context) PushInterface(v interface{}) error

PushInterface push any type of value to the stack, the following types are supported:

- Bool
- Int, Int8, Int16, Int32, Uint, Uint8, Uint16, Uint32 and Uint64
- Float32 and Float64
- Strings and []byte
- Structs
- Functions with any signature

Please read carefully the following notes:

- The pointers are resolved and the value is pushed
- Structs are pushed ussing PushProxy, if you want to make a copy use PushStruct
- Int64 and UInt64 are supported but before push it to the stack are casted
  to float64
- Any unsuported value is pushed as a null

func (*Context) PushProxy Uses

func (ctx *Context) PushProxy(v interface{}) int

PushProxy push a proxified pointer of the given value to the stack, this refence will be stored on an internal storage. The pushed objects has the exact same methods and properties from the original value. http://duktape.org/guide.html#virtualization-proxy-object

func (*Context) PushStruct Uses

func (ctx *Context) PushStruct(s interface{}) (int, error)

PushStruct push a object to the stack with the same methods and properties the pushed object is a copy, any change made on JS is not reflected on the Go instance.

func (*Context) PushType Uses

func (ctx *Context) PushType(s interface{}) int

PushType push a constructor for the type of the given value, this constructor returns an empty instance of the type. The value passed is discarded, only is used for retrieve the time, instead of require pass a `reflect.Type`.

func (*Context) SetRequireFunction Uses

func (ctx *Context) SetRequireFunction(f interface{}) int

SetRequireFunction sets the modSearch function into the Duktape JS object http://duktape.org/guide.html#builtin-duktape-modsearch-modloade

type PackagePusher Uses

type PackagePusher func(ctx *Context)

PackagePusher should be a function capable of register all functions and types contained on a golang packages. This functions are generated by the go generate tool `candyjs` a example of this header is:

//go:generate candyjs import time

Package candyjs imports 8 packages (graph) and is imported by 2 packages. Updated 2017-12-14. Refresh now. Tools for package owners.