otto
--
Package otto is a JavaScript parser and interpreter written natively in Go.
http://godoc.org/github.com/robertkrimen/otto
Run something in the VM
Get a value out of the VM
Set a number
Set a string
Get the value of an expression
An error happens
Set a Go function
Set a Go function that returns something useful
Use the functions in JavaScript
Parser
A separate parser is available in the parser package if you're just interested in building an AST.
http://godoc.org/github.com/robertkrimen/otto/parser
Parse and return an AST
otto
You can run (Go) JavaScript from the commandline with: http://github.com/robertkrimen/otto/tree/master/otto
Run JavaScript by entering some source on stdin or by giving otto a filename:
underscore
Optionally include the JavaScript utility-belt library, underscore, with this import:
For more information: http://github.com/robertkrimen/otto/tree/master/underscore
Caveat Emptor
The following are some limitations with otto:
"use strict" will parse, but does nothing.
The regular expression engine (re2/regexp) is not fully compatible with the ECMA5 specification.
Otto targets ES5. ES6 features (eg: Typed Arrays) are not supported.
Regular Expression Incompatibility
Go translates JavaScript-style regular expressions into something that is "regexp" compatible via parser.TransformRegExp
. Unfortunately, RegExp requires backtracking for some patterns, and backtracking is not supported by the standard Go engine: https://code.google.com/p/re2/wiki/Syntax
Therefore, the following syntax is incompatible:
A brief discussion of these limitations: "Regexp (?!re)" https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/7qgSDWPIh_E
More information about re2: https://code.google.com/p/re2/
In addition to the above, re2 (Go) has a different definition for \s: [\t\n\f\r ]. The JavaScript definition, on the other hand, also includes \v, Unicode "Separator, Space", etc.
Halting Problem
If you want to stop long running executions (like third-party code), you can use the interrupt channel to do this:
Where is setTimeout/setInterval?
These timing functions are not actually part of the ECMA-262 specification. Typically, they belong to the window
object (in the browser). It would not be difficult to provide something like these via Go, but you probably want to wrap otto in an event loop in that case.
For an example of how this could be done in Go with otto, see natto:
http://github.com/robertkrimen/natto
Here is some more discussion of the issue:
Usage
type Error
An Error represents a runtime error, e.g. a TypeError, a ReferenceError, etc.
func (Error) Error
Error returns a description of the error
func (Error) String
String returns a description of the error and a trace of where the error occurred.
type FunctionCall
FunctionCall is an encapsulation of a JavaScript function call.
func (FunctionCall) Argument
Argument will return the value of the argument at the given index.
If no such argument exists, undefined is returned.
type Object
Object is the representation of a JavaScript object.
func (Object) Call
Call a method on the object.
It is essentially equivalent to:
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:
func (Object) Get
Get the value of the property with the given name.
func (Object) Keys
Get the keys for the object
Equivalent to calling Object.keys on the object
func (Object) Set
Set the property of the given name to the given value.
An error will result if the setting the property triggers an exception (i.e. read-only), or there is an error during conversion of the given value.
func (Object) Value
Value will return self as a value.
type Otto
Otto is the representation of the JavaScript runtime. Each instance of Otto has a self-contained namespace.
func New
New will allocate a new JavaScript runtime
func Run
Run will allocate a new JavaScript runtime, run the given source on the allocated runtime, and return the runtime, 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.
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) 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.
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.
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) 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) Object
Object will run the given source and return the result as an object.
For example, accessing an existing object:
Or, creating a new object:
Or, creating and assigning an object:
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) ToValue
ToValue will convert an interface{} value to a value digestible by otto/JavaScript.
type Script
Script is a handle for some (reusable) JavaScript. Passing a Script value to a run method will evaluate the JavaScript.
func (*Script) String
type Value
Value is the representation of a JavaScript value.
func FalseValue
FalseValue will return a value representing false.
It is equivalent to:
func NaNValue
NaNValue will return a value representing NaN.
It is equivalent to:
func NullValue
NullValue will return a Value representing null.
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
TrueValue will return a value representing true.
It is equivalent to:
func UndefinedValue
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:
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:
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.
func (Value) IsBoolean
IsBoolean will return true if value is a boolean (primitive).
func (Value) IsDefined
IsDefined will return false if the value is undefined, and true otherwise.
func (Value) IsFunction
IsFunction will return true if value is a function.
func (Value) IsNaN
IsNaN will return true if value is NaN (or would convert to NaN).
func (Value) IsNull
IsNull will return true if the value is null, and false otherwise.
func (Value) IsNumber
IsNumber will return true if value is a number (primitive).
func (Value) IsObject
IsObject will return true if value is an object.
func (Value) IsPrimitive
IsPrimitive will return true if value is a primitive (any kind of primitive).
func (Value) IsString
IsString will return true if value is a string (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).
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).
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).
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).
If there is an error during the conversion process (like an uncaught exception), then the result will be the empty string ("") and an error.
-- godocdown http://github.com/robertkrimen/godocdown
Last updated