Documentation ¶
Index ¶
- Variables
- func IsNull(v Value) bool
- func IsUndefined(v Value) bool
- type Callable
- type CompilerError
- type CompilerReferenceError
- type CompilerSyntaxError
- type ConstructorCall
- type Exception
- type FieldNameMapper
- type Flag
- type FunctionCall
- type InterruptedError
- type JsonEncodable
- type Object
- func (o *Object) DefineAccessorProperty(name string, getter, setter Value, configurable, enumerable Flag) error
- func (o *Object) DefineDataProperty(name string, value Value, writable, configurable, enumerable Flag) error
- func (o *Object) Equals(other Value) bool
- func (o *Object) Export() interface{}
- func (o *Object) ExportType() reflect.Type
- func (o *Object) Get(name string) Value
- func (o *Object) Keys() (keys []string)
- func (o *Object) MarshalJSON() ([]byte, error)
- func (o *Object) SameAs(other Value) bool
- func (o *Object) Set(name string, value interface{}) error
- func (o *Object) StrictEquals(other Value) bool
- func (o *Object) String() string
- func (o *Object) ToBoolean() bool
- func (o *Object) ToFloat() float64
- func (o *Object) ToInteger() int64
- func (o *Object) ToNumber() Value
- func (o *Object) ToObject(r *Runtime) *Object
- func (o *Object) ToString() valueString
- type ObjectLike
- type Position
- type Program
- type RandSource
- type Runtime
- func (r *Runtime) CreateObject(proto *Object) *Object
- func (r *Runtime) ExportTo(v Value, target interface{}) error
- func (r *Runtime) Get(name string) Value
- func (r *Runtime) GlobalObject() *Object
- func (r *Runtime) Interrupt(v interface{})
- func (r *Runtime) NewGoError(err error) *Object
- func (r *Runtime) NewObject() (v *Object)
- func (r *Runtime) NewTypeError(args ...interface{}) *Object
- func (r *Runtime) RunProgram(p *Program) (result Value, err error)
- func (r *Runtime) RunScript(name, src string) (Value, error)
- func (r *Runtime) RunString(str string) (Value, error)
- func (r *Runtime) Set(name string, value interface{})
- func (r *Runtime) SetFieldNameMapper(mapper FieldNameMapper)
- func (r *Runtime) SetRandSource(source RandSource)
- func (r *Runtime) ToValue(i interface{}) Value
- type SrcFile
- type Value
Constants ¶
This section is empty.
Variables ¶
var (
InvalidRuneError = errors.New("Invalid rune")
)
Functions ¶
func IsUndefined ¶
IsUndefined returns true if the supplied Value is undefined. Note, it checks against the real undefined, not against the global object's 'undefined' property.
Types ¶
type Callable ¶
Callable represents a JavaScript function that can be called from Go.
func AssertFunction ¶
AssertFunction checks if the Value is a function and returns a Callable.
type CompilerError ¶
type CompilerReferenceError ¶
type CompilerReferenceError struct {
CompilerError
}
func (*CompilerReferenceError) Error ¶
func (e *CompilerReferenceError) Error() string
type CompilerSyntaxError ¶
type CompilerSyntaxError struct {
CompilerError
}
func (*CompilerSyntaxError) Error ¶
func (e *CompilerSyntaxError) Error() string
type ConstructorCall ¶
func (ConstructorCall) Argument ¶
func (f ConstructorCall) Argument(idx int) Value
type FieldNameMapper ¶
type FieldNameMapper interface { // FieldName returns a JavaScript name for the given struct field in the given type. // If this method returns "" the field becomes hidden. FieldName(t reflect.Type, f reflect.StructField) string // FieldName returns a JavaScript name for the given method in the given type. // If this method returns "" the method becomes hidden. MethodName(t reflect.Type, m reflect.Method) string }
FieldNameMapper provides custom mapping between Go and JavaScript property names.
type FunctionCall ¶
func (FunctionCall) Argument ¶
func (f FunctionCall) Argument(idx int) Value
type InterruptedError ¶
type InterruptedError struct { Exception // contains filtered or unexported fields }
func (*InterruptedError) Error ¶
func (e *InterruptedError) Error() string
func (*InterruptedError) String ¶
func (e *InterruptedError) String() string
func (*InterruptedError) Value ¶
func (e *InterruptedError) Value() interface{}
type JsonEncodable ¶
type JsonEncodable interface {
JsonEncodable() interface{}
}
JsonEncodable allows custom JSON encoding by JSON.stringify() Note that if the returned value itself also implements JsonEncodable, it won't have any effect.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
func (*Object) DefineAccessorProperty ¶
func (o *Object) DefineAccessorProperty(name string, getter, setter Value, configurable, enumerable Flag) error
DefineAccessorProperty is a Go equivalent of Object.defineProperty(o, name, {get: getter, set: setter, configurable: configurable, enumerable: enumerable})
func (*Object) DefineDataProperty ¶
func (o *Object) DefineDataProperty(name string, value Value, writable, configurable, enumerable Flag) error
DefineDataProperty is a Go equivalent of Object.defineProperty(o, name, {value: value, writable: writable, configurable: configurable, enumerable: enumerable})
func (*Object) ExportType ¶
func (*Object) MarshalJSON ¶
MarshalJSON returns JSON representation of the Object. It is equivalent to JSON.stringify(o). Note, this implements json.Marshaler so that json.Marshal() can be used without the need to Export().
func (*Object) StrictEquals ¶
type ObjectLike ¶
type ObjectLike interface { GetObjectValue(key string) (val interface{}, exists bool) SetObjectValue(key string, val interface{}) GetObjectKeys() []string GetObjectLength() int DeleteObjectValue(key string) }
ObjectLike defines interface for js object like abstract that can be exposed to js runtime
type Program ¶
type Program struct {
// contains filtered or unexported fields
}
func Compile ¶
Compile creates an internal representation of the JavaScript code that can be later run using the Runtime.RunProgram() method. This representation is not linked to a runtime in any way and can be run in multiple runtimes (possibly at the same time).
func CompileAST ¶
CompileAST creates an internal representation of the JavaScript code that can be later run using the Runtime.RunProgram() method. This representation is not linked to a runtime in any way and can be run in multiple runtimes (possibly at the same time).
func MustCompile ¶
MustCompile is like Compile but panics if the code cannot be compiled. It simplifies safe initialization of global variables holding compiled JavaScript code.
type RandSource ¶
type RandSource func() float64
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
func New ¶
func New() *Runtime
New creates an instance of a Javascript runtime that can be used to run code. Multiple instances may be created and used simultaneously, however it is not possible to pass JS values across runtimes.
func (*Runtime) CreateObject ¶
CreateObject creates an object with given prototype. Equivalent of Object.create(proto).
func (*Runtime) ExportTo ¶
ExportTo converts a JavaScript value into the specified Go value. The second parameter must be a non-nil pointer. Returns error if conversion is not possible.
func (*Runtime) GlobalObject ¶
GlobalObject returns the global object.
func (*Runtime) Interrupt ¶
func (r *Runtime) Interrupt(v interface{})
Interrupt a running JavaScript. The corresponding Go call will return an *InterruptedError containing v. Note, it only works while in JavaScript code, it does not interrupt native Go functions (which includes all built-ins).
func (*Runtime) NewGoError ¶
func (*Runtime) NewTypeError ¶
func (*Runtime) RunProgram ¶
RunProgram executes a pre-compiled (see Compile()) code in the global context.
func (*Runtime) Set ¶
Set the specified value as a property of the global object. The value is first converted using ToValue()
func (*Runtime) SetFieldNameMapper ¶
func (r *Runtime) SetFieldNameMapper(mapper FieldNameMapper)
Sets a custom field name mapper for Go types. It can be called at any time, however the mapping for any given value is fixed at the point of creation. Setting this to nil restores the default behaviour which is all exported fields and methods are mapped to their original unchanged names.
func (*Runtime) SetRandSource ¶
func (r *Runtime) SetRandSource(source RandSource)
SetRandSource sets random source for this Runtime. If not called, the default math/rand is used.
func (*Runtime) ToValue ¶
ToValue converts a Go value into JavaScript value.
Primitive types (ints and uints, floats, string, bool) are converted to the corresponding JavaScript primitives.
func(FunctionCall) Value is treated as a native JavaScript function.
map[string]interface{} is converted into a host object that largely behaves like a JavaScript Object.
[]interface{} is converted into a host object that behaves largely like a JavaScript Array, however it's not extensible because extending it can change the pointer so it becomes detached from the original.
*[]interface{} same as above, but the array becomes extensible.
A function is wrapped within a native JavaScript function. When called the arguments are automatically converted to the appropriate Go types. If conversion is not possible, a TypeError is thrown.
A slice type is converted into a generic reflect based host object that behaves similar to an unexpandable Array.
Any other type is converted to a generic reflect based host object. Depending on the underlying type it behaves similar to a Number, String, Boolean or Object.
Note that the underlying type is not lost, calling Export() returns the original Go value. This applies to all reflect based types.
type Value ¶
type Value interface { ToInteger() int64 ToString() valueString String() string ToFloat() float64 ToNumber() Value ToBoolean() bool ToObject(*Runtime) *Object SameAs(Value) bool Equals(Value) bool StrictEquals(Value) bool Export() interface{} ExportType() reflect.Type // contains filtered or unexported methods }
Source Files ¶
- array.go
- array_sparse.go
- builtin_array.go
- builtin_boolean.go
- builtin_date.go
- builtin_error.go
- builtin_function.go
- builtin_global.go
- builtin_json.go
- builtin_math.go
- builtin_number.go
- builtin_object.go
- builtin_regexp.go
- builtin_string.go
- builtin_typedarrays.go
- compiler.go
- compiler_expr.go
- compiler_stmt.go
- date.go
- dtoa.go
- func.go
- ipow.go
- object.go
- object_args.go
- object_gomap.go
- object_gomap_reflect.go
- object_goreflect.go
- object_goslice.go
- object_goslice_reflect.go
- object_lazy.go
- object_like.go
- regexp.go
- runtime.go
- srcfile.go
- string.go
- string_ascii.go
- string_unicode.go
- value.go
- vm.go
Directories ¶
Path | Synopsis |
---|---|
Package ast declares types representing a JavaScript AST.
|
Package ast declares types representing a JavaScript AST. |
Package file encapsulates the file abstractions used by the ast & parser.
|
Package file encapsulates the file abstractions used by the ast & parser. |
Package parser implements a parser for JavaScript.
|
Package parser implements a parser for JavaScript. |
Package token defines constants representing the lexical tokens of JavaScript (ECMA5).
|
Package token defines constants representing the lexical tokens of JavaScript (ECMA5). |