Documentation ¶
Index ¶
- Constants
- Variables
- type Context
- type ContractCallResp
- type ContractData
- type Error
- type FunctionCall
- type Object
- func (self Object) Call(name string, argumentList ...interface{}) (Value, error)
- func (self Object) Class() string
- func (self Object) Get(name string) (Value, error)
- func (self Object) Keys() []string
- func (self Object) KeysByParent() [][]string
- func (self Object) Set(name string, value interface{}) error
- func (self Object) Value() Value
- type Otto
- func (self Otto) Call(source string, this interface{}, argumentList ...interface{}) (Value, error)
- func (self *Otto) Compile(filename string, src interface{}) (*Script, error)
- func (self *Otto) CompileWithSourceMap(filename string, src, sm interface{}) (*Script, error)
- func (self Otto) Context() Context
- func (self Otto) ContextLimit(limit int) Context
- func (self Otto) ContextSkip(limit int, skipNative bool) (ctx Context)
- func (otto *Otto) ContractCall(tx userevent.Transaction, timeout time.Duration) ([]userevent.SubTransaction, []byte, error)
- func (in *Otto) Copy() *Otto
- func (self Otto) Eval(src interface{}) (Value, error)
- func (self Otto) Get(name string) (Value, error)
- func (otto *Otto) InitContractWithTimeout(code []byte, timeout time.Duration) (*types.ContractData, error)
- func (otto *Otto) LoadContract(addr []byte) error
- func (self Otto) MakeCustomError(name, message string) Value
- func (self Otto) MakeRangeError(message string) Value
- func (self Otto) MakeSyntaxError(message string) Value
- func (self Otto) MakeTypeError(message string) Value
- func (self Otto) Object(source string) (*Object, error)
- func (self Otto) Run(src interface{}) (Value, error)
- func (self Otto) Set(name string, value interface{}) error
- func (self Otto) SetDebuggerHandler(fn func(vm *Otto))
- func (self Otto) SetRandomSource(fn func(vm *Otto) float64)
- func (self Otto) SetStackDepthLimit(limit int)
- func (self Otto) SetStackTraceLimit(limit int)
- func (self Otto) ToValue(value interface{}) (Value, error)
- func (otto *Otto) UpgradeContract(code []byte, contractData *types.ContractData, timeout time.Duration) (*types.ContractData, error)
- type Script
- type Value
- func (value Value) Call(this Value, argumentList ...interface{}) (Value, error)
- func (value Value) Class() string
- func (self Value) Export() (interface{}, error)
- func (value Value) IsBoolean() bool
- func (value Value) IsDefined() bool
- func (value Value) IsFunction() bool
- func (value Value) IsNaN() bool
- func (value Value) IsNull() bool
- func (value Value) IsNumber() bool
- func (value Value) IsObject() bool
- func (value Value) IsPrimitive() bool
- func (value Value) IsString() bool
- func (value Value) IsUndefined() bool
- func (value Value) Object() *Object
- func (value Value) String() string
- func (value Value) ToBoolean() (bool, error)
- func (value Value) ToFloat() (float64, error)
- func (value Value) ToInteger() (int64, error)
- func (value Value) ToString() (string, error)
Constants ¶
const AWM_DB_PREFIX = "_AWM_CONTRACT_DB_"
Variables ¶
var ErrVersion = errors.New("version mismatch")
var (
TIMEOUT_ERROR = errors.New("vm call error: out of time")
)
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { Filename string Line int Column int Callee string Symbols map[string]Value This Value Stacktrace []string }
Context is a structure that contains information about the current execution context.
type ContractCallResp ¶
type ContractCallResp struct {
// contains filtered or unexported fields
}
func NewContractCallResp ¶
func NewContractCallResp(txs []userevent.SubTransaction, data []byte, err error) *ContractCallResp
type ContractData ¶
type ContractData struct {
// contains filtered or unexported fields
}
func NewContractData ¶
func NewContractData(contractData *types.ContractData, err error) *ContractData
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
An Error represents a runtime error, e.g. a TypeError, a ReferenceError, etc.
type FunctionCall ¶
type FunctionCall struct { This Value ArgumentList []Value Otto *Otto // contains filtered or unexported fields }
FunctionCall is an encapsulation of a JavaScript function call.
func (FunctionCall) Argument ¶
func (self FunctionCall) Argument(index int) Value
Argument will return the value of the argument at the given index.
If no such argument exists, undefined is returned.
func (FunctionCall) CallerLocation ¶
func (self FunctionCall) CallerLocation() string
CallerLocation will return file location information (file:line:pos) where this function is being called.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object is the representation of a JavaScript object.
func (Object) Call ¶
Call a method on the object.
It is essentially equivalent to:
var method, _ := object.Get(name) method.Call(object, argumentList...)
An undefined value and an error will result if:
- There is an error during conversion of the argument list
- The property is not actually a function
- An (uncaught) exception is thrown
func (Object) Class ¶
Class will return the class string of the object.
The return value will (generally) be one of:
Object Function Array String Number Boolean Date RegExp
func (Object) Keys ¶
Keys gets the keys for the given object.
Equivalent to calling Object.keys on the object.
func (Object) KeysByParent ¶
KeysByParent gets the keys (and those of the parents) for the given object, in order of "closest" to "furthest".
type Otto ¶
type Otto struct { // Interrupt is a channel for interrupting the runtime. You can use this to halt a long running execution, for example. // See "Halting Problem" for more information. Interrupt chan func() // contains filtered or unexported fields }
Otto is the representation of the JavaScript runtime. Each instance of Otto has a self-contained namespace.
func NewVM ¶
func NewVM(chain _interface.VMChain, db db.IKVDatabase) *Otto
func (Otto) Call ¶
Call the given JavaScript with a given this and arguments.
If this is nil, then some special handling takes place to determine the proper this value, falling back to a "standard" invocation if necessary (where this is undefined).
If source begins with "new " (A lowercase new followed by a space), then Call will invoke the function constructor rather than performing a function call. In this case, the this argument has no effect.
// value is a String object value, _ := vm.Call("Object", nil, "Hello, World.") // Likewise... value, _ := vm.Call("new Object", nil, "Hello, World.") // This will perform a concat on the given array and return the result // value is [ 1, 2, 3, undefined, 4, 5, 6, 7, "abc" ] value, _ := vm.Call(`[ 1, 2, 3, undefined, 4 ].concat`, nil, 5, 6, 7, "abc")
func (*Otto) Compile ¶
Compile will parse the given source and return a Script value or nil and an error if there was a problem during compilation.
script, err := vm.Compile("", `var abc; if (!abc) abc = 0; abc += 2; abc;`) vm.Run(script)
func (*Otto) CompileWithSourceMap ¶
CompileWithSourceMap does the same thing as Compile, but with the obvious difference of applying a source map.
func (Otto) Context ¶
Context returns the current execution context of the vm, traversing up to ten stack frames, and skipping any innermost native function stack frames.
func (Otto) ContextLimit ¶
ContextLimit returns the current execution context of the vm, with a specific limit on the number of stack frames to traverse, skipping any innermost native function stack frames.
func (Otto) ContextSkip ¶
ContextSkip returns the current execution context of the vm, with a specific limit on the number of stack frames to traverse, optionally skipping any innermost native function stack frames.
func (*Otto) ContractCall ¶
func (otto *Otto) ContractCall(tx userevent.Transaction, timeout time.Duration) ([]userevent.SubTransaction, []byte, error)
func (*Otto) Copy ¶
Copy will create a copy/clone of the runtime.
Copy is useful for saving some time when creating many similar runtimes.
This method works by walking the original runtime and cloning each object, scope, stash, etc. into a new runtime.
Be on the lookout for memory leaks or inadvertent sharing of resources.
func (Otto) Eval ¶
Eval will do the same thing as Run, except without leaving the current scope.
By staying in the same scope, the code evaluated has access to everything already defined in the current stack frame. This is most useful in, for example, a debugger call.
func (Otto) Get ¶
Get the value of the top-level binding of the given name.
If there is an error (like the binding does not exist), then the value will be undefined.
func (*Otto) InitContractWithTimeout ¶
func (*Otto) LoadContract ¶
func (Otto) MakeCustomError ¶
MakeCustomError creates a new Error object with the given name and message, returning it as a Value.
func (Otto) MakeRangeError ¶
MakeRangeError creates a new RangeError object with the given message, returning it as a Value.
func (Otto) MakeSyntaxError ¶
MakeSyntaxError creates a new SyntaxError object with the given message, returning it as a Value.
func (Otto) MakeTypeError ¶
MakeTypeError creates a new TypeError object with the given message, returning it as a Value.
func (Otto) Object ¶
Object will run the given source and return the result as an object.
For example, accessing an existing object:
object, _ := vm.Object(`Number`)
Or, creating a new object:
object, _ := vm.Object(`({ xyzzy: "Nothing happens." })`)
Or, creating and assigning an object:
object, _ := vm.Object(`xyzzy = {}`) object.Set("volume", 11)
If there is an error (like the source does not result in an object), then nil and an error is returned.
func (Otto) Run ¶
Run will run the given source (parsing it first if necessary), returning the resulting value and error (if any)
src may be a string, a byte slice, a bytes.Buffer, or an io.Reader, but it MUST always be in UTF-8.
If the runtime is unable to parse source, then this function will return undefined and the parse error (nothing will be evaluated in this case).
src may also be a Script.
src may also be a Program, but if the AST has been modified, then runtime behavior is undefined.
func (Otto) Set ¶
Set the top-level binding of the given name to the given value.
Set will automatically apply ToValue to the given value in order to convert it to a JavaScript value (type Value).
If there is an error (like the binding is read-only, or the ToValue conversion fails), then an error is returned.
If the top-level binding does not exist, it will be created.
func (Otto) SetDebuggerHandler ¶
func (Otto) SetRandomSource ¶
func (Otto) SetStackDepthLimit ¶
SetStackDepthLimit sets an upper limit to the depth of the JavaScript stack. In simpler terms, this limits the number of "nested" function calls you can make in a particular interpreter instance.
Note that this doesn't take into account the Go stack depth. If your JavaScript makes a call to a Go function, otto won't keep track of what happens outside the interpreter. So if your Go function is infinitely recursive, you're still in trouble.
func (Otto) SetStackTraceLimit ¶
SetStackTraceLimit sets an upper limit to the number of stack frames that otto will use when formatting an error's stack trace. By default, the limit is 10. This is consistent with V8 and SpiderMonkey.
TODO: expose via `Error.stackTraceLimit`
func (Otto) ToValue ¶
ToValue will convert an interface{} value to a value digestible by otto/JavaScript.
func (*Otto) UpgradeContract ¶
func (otto *Otto) UpgradeContract(code []byte, contractData *types.ContractData, timeout time.Duration) (*types.ContractData, error)
type Script ¶
type Script struct {
// contains filtered or unexported fields
}
Script is a handle for some (reusable) JavaScript. Passing a Script value to a run method will evaluate the JavaScript.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is the representation of a JavaScript value.
func FalseValue ¶
func FalseValue() Value
FalseValue will return a value representing false.
It is equivalent to:
ToValue(false)
func NaNValue ¶
func NaNValue() Value
NaNValue will return a value representing NaN.
It is equivalent to:
ToValue(math.NaN())
func ToValue ¶
ToValue will convert an interface{} value to a value digestible by otto/JavaScript
This function will not work for advanced types (struct, map, slice/array, etc.) and you should use Otto.ToValue instead.
func TrueValue ¶
func TrueValue() Value
TrueValue will return a value representing true.
It is equivalent to:
ToValue(true)
func UndefinedValue ¶
func UndefinedValue() Value
UndefinedValue will return a Value representing undefined.
func (Value) Call ¶
Call the value as a function with the given this value and argument list and return the result of invocation. It is essentially equivalent to:
value.apply(thisValue, argumentList)
An undefined value and an error will result if:
- There is an error during conversion of the argument list
- The value is not actually a function
- An (uncaught) exception is thrown
func (Value) Class ¶
Class will return the class string of the value or the empty string if value is not an object.
The return value will (generally) be one of:
Object Function Array String Number Boolean Date RegExp
func (Value) Export ¶
Export will attempt to convert the value to a Go representation and return it via an interface{} kind.
Export returns an error, but it will always be nil. It is present for backwards compatibility.
If a reasonable conversion is not possible, then the original value is returned.
undefined -> nil (FIXME?: Should be Value{}) null -> nil boolean -> bool number -> A number type (int, float32, uint64, ...) string -> string Array -> []interface{} Object -> map[string]interface{}
func (Value) IsFunction ¶
IsFunction will return true if value is a function.
func (Value) IsPrimitive ¶
IsPrimitive will return true if value is a primitive (any kind of primitive).
func (Value) IsUndefined ¶
IsUndefined will return true if the value is undefined, and false otherwise.
func (Value) Object ¶
Object will return the object of the value, or nil if value is not an object.
This method will not do any implicit conversion. For example, calling this method on a string primitive value will not return a String object.
func (Value) String ¶
String will return the value as a string.
This method will make return the empty string if there is an error.
func (Value) ToBoolean ¶
ToBoolean will convert the value to a boolean (bool).
ToValue(0).ToBoolean() => false ToValue("").ToBoolean() => false ToValue(true).ToBoolean() => true ToValue(1).ToBoolean() => true ToValue("Nothing happens").ToBoolean() => true
If there is an error during the conversion process (like an uncaught exception), then the result will be false and an error.
func (Value) ToFloat ¶
ToFloat will convert the value to a number (float64).
ToValue(0).ToFloat() => 0. ToValue(1.1).ToFloat() => 1.1 ToValue("11").ToFloat() => 11.
If there is an error during the conversion process (like an uncaught exception), then the result will be 0 and an error.
func (Value) ToInteger ¶
ToInteger will convert the value to a number (int64).
ToValue(0).ToInteger() => 0 ToValue(1.1).ToInteger() => 1 ToValue("11").ToInteger() => 11
If there is an error during the conversion process (like an uncaught exception), then the result will be 0 and an error.
func (Value) ToString ¶
ToString will convert the value to a string (string).
ToValue(0).ToString() => "0" ToValue(false).ToString() => "false" ToValue(1.1).ToString() => "1.1" ToValue("11").ToString() => "11" ToValue('Nothing happens.').ToString() => "Nothing happens."
If there is an error during the conversion process (like an uncaught exception), then the result will be the empty string ("") and an error.
Source Files ¶
- builtin.go
- builtin_array.go
- builtin_awm.go
- builtin_awm_db.go
- builtin_awm_trie.go
- builtin_boolean.go
- builtin_date.go
- builtin_error.go
- builtin_function.go
- builtin_json.go
- builtin_math.go
- builtin_number.go
- builtin_object.go
- builtin_regexp.go
- builtin_string.go
- clone.go
- cmpl.go
- cmpl_evaluate.go
- cmpl_evaluate_expression.go
- cmpl_evaluate_statement.go
- cmpl_parse.go
- console.go
- dbg.go
- error.go
- evaluate.go
- global.go
- inline.go
- object.go
- object_class.go
- otto.go
- otto_.go
- property.go
- result.go
- runtime.go
- scope.go
- script.go
- stash.go
- type_arguments.go
- type_array.go
- type_boolean.go
- type_date.go
- type_error.go
- type_function.go
- type_go_array.go
- type_go_map.go
- type_go_slice.go
- type_go_struct.go
- type_number.go
- type_reference.go
- type_regexp.go
- type_string.go
- value.go
- value_boolean.go
- value_number.go
- value_primitive.go
- value_string.go
Directories ¶
Path | Synopsis |
---|---|
Package ast declares types representing a JavaScript AST.
|
Package ast declares types representing a JavaScript AST. |
Package dbg is a println/printf/log-debugging utility library.
|
Package dbg is a println/printf/log-debugging utility library. |
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 registry is an expirmental package to facillitate altering the otto runtime via import.
|
Package registry is an expirmental package to facillitate altering the otto runtime via import. |
Package terst is a terse (terst = test + terse), easy-to-use testing library for Go.
|
Package terst is a terse (terst = test + terse), easy-to-use testing library for Go. |
Package token defines constants representing the lexical tokens of JavaScript (ECMA5).
|
Package token defines constants representing the lexical tokens of JavaScript (ECMA5). |
Package underscore contains the source for the JavaScript utility-belt library.
|
Package underscore contains the source for the JavaScript utility-belt library. |