Documentation ¶
Index ¶
- Constants
- func ErrorCode(err error) string
- func RegisterPackagePusher(pckgName string, f PackagePusher)
- type Context
- func (ctx *Context) ConsumeLastGoError() error
- func (ctx *Context) GetValue(index int, value interface{}) error
- func (ctx *Context) LastGoError() error
- func (ctx *Context) PushGlobalGoFunction(name string, f interface{}) (int, error)
- func (ctx *Context) PushGlobalInterface(name string, v interface{}) error
- func (ctx *Context) PushGlobalPackage(pckgName, alias string) error
- func (ctx *Context) PushGlobalProxy(name string, v interface{}) int
- func (ctx *Context) PushGlobalStruct(name string, s interface{}) (int, error)
- func (ctx *Context) PushGlobalType(name string, s interface{}) int
- func (ctx *Context) PushGoFunction(f interface{}) int
- func (ctx *Context) PushInterface(v interface{}) error
- func (ctx *Context) PushProxy(v interface{}) int
- func (ctx *Context) PushStruct(s interface{}) (int, error)
- func (ctx *Context) PushType(s interface{}) int
- func (ctx *Context) SetErrorFactory(f ErrorFactoryFunc)
- func (ctx *Context) SetRequireFunction(f interface{}) int
- type Error
- type ErrorFactoryFunc
- type PackagePusher
- type Proxy
Constants ¶
const ( // ErrorCodeUndefinedProperty is returned when accessing an invalid property. ErrorCodeUndefinedProperty = "candyjs:undefinedproperty" // ErrorCodePackageNotFound is returned when a package cannot be found, usually this // happend when a PackagePusher function was not registered using // RegisterPackagePusher. ErrorCodePackageNotFound = "candyjs:packagenotfound" )
Variables ¶
This section is empty.
Functions ¶
func ErrorCode ¶
ErrorCode returns the candy error code for this error. Returns "" if the error is not a candyjs error.
func RegisterPackagePusher ¶
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.
Types ¶
type Context ¶
type Context struct { *duktape.Context // contains filtered or unexported fields }
Context represents a Duktape thread and its call and value stacks.
func (*Context) ConsumeLastGoError ¶
ConsumeLastGoError returns the last error returned by a GO function and clears it.
func (*Context) GetValue ¶
GetValue gets and marshals the value at the specified stack index. Takes special care for proxies.
func (*Context) LastGoError ¶
LastGoError returns the last error returned by a GO function.
func (*Context) PushGlobalGoFunction ¶
PushGlobalGoFunction like PushGoFunction but pushed to the global object
func (*Context) PushGlobalInterface ¶
PushGlobalInterface like PushInterface but pushed to the global object
func (*Context) PushGlobalPackage ¶
PushGlobalPackage all the functions and types from the given package using the pre-registered PackagePusher function.
func (*Context) PushGlobalProxy ¶
PushGlobalProxy like PushProxy but pushed to the global object
func (*Context) PushGlobalStruct ¶
PushGlobalStruct like PushStruct but pushed to the global object
func (*Context) PushGlobalType ¶
PushGlobalType like PushType but pushed to the global object
func (*Context) PushGoFunction ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 type, instead of require pass a `reflect.Type`.
func (*Context) SetErrorFactory ¶
func (ctx *Context) SetErrorFactory(f ErrorFactoryFunc)
SetErrorFactory sets the function used to create go errors from Javascript execptions. By default `errors.New(execption.toString())` will be used.
func (*Context) SetRequireFunction ¶
SetRequireFunction sets the modSearch function into the Duktape JS object http://duktape.org/guide.html#builtin-duktape-modsearch-modloade
type ErrorFactoryFunc ¶
ErrorFactoryFunc ...
type PackagePusher ¶
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
type Proxy ¶
type Proxy interface { Has(t interface{}, k string) bool Get(t interface{}, k string, recv interface{}) (interface{}, error) Set(t interface{}, k string, v, recv interface{}) (bool, error) Enumerate(t interface{}) (interface{}, error) }
Proxy defines the GO interface for ECMASCRIPTs proxy objects.