本文整理汇总了Golang中github.com/robertkrimen/otto.Otto类的典型用法代码示例。如果您正苦于以下问题:Golang Otto类的具体用法?Golang Otto怎么用?Golang Otto使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Otto类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Execute
func (t *timerTask) Execute(vm *otto.Otto, l *loop.Loop) error {
var arguments []interface{}
if len(t.call.ArgumentList) > 2 {
tmp := t.call.ArgumentList[2:]
arguments = make([]interface{}, 2+len(tmp))
for i, value := range tmp {
arguments[i+2] = value
}
} else {
arguments = make([]interface{}, 1)
}
arguments[0] = t.call.ArgumentList[0]
if _, err := vm.Call(`Function.call.call`, nil, arguments...); err != nil {
return err
}
if t.interval && !t.stopped {
t.timer.Reset(t.duration)
l.Add(t)
}
return nil
}
示例2: testUndefined
func testUndefined(vm *otto.Otto, src string) error {
v, err := vm.Run(src)
if err == nil && !v.IsUndefined() {
err = fmt.Errorf("expected undefined, got %v", v)
}
return err
}
示例3: compileAndRun
func compileAndRun(vm *otto.Otto, filename string, src interface{}) (otto.Value, error) {
script, err := vm.Compile(filename, src)
if err != nil {
return otto.Value{}, err
}
return vm.Run(script)
}
示例4: NewWriter
// NewWriter adds a write method to the specified runtime that allows
// client code to write to the specified io.Writer.
//
// The client function created has the following syntax:
//
// var response = writeMethodName(contentToWrite)
//
// Response object:
//
// {
// len: bytes_written,
// error: error|undefined
// }
func NewWriter(runtime *otto.Otto, methodName string, writer io.Writer) error {
runtime.Set(methodName, func(call otto.FunctionCall) otto.Value {
var data string
var count int
var err error
var val otto.Value
if data, err = call.Argument(0).ToString(); err == nil {
if count, err = writer.Write([]byte(data)); err == nil {
if val, err = makeMap(runtime, map[string]interface{}{"len": count}); err != nil {
raiseError(runtime, "Failed to create output object: %s", err)
} else {
return val
}
}
}
if err != nil {
if val, err := makeMap(runtime, map[string]interface{}{"len": 0, "error": err.Error()}); err != nil {
raiseError(runtime, "Failed to create output object: %s", err)
return otto.UndefinedValue()
} else {
return val
}
}
return otto.UndefinedValue()
})
return nil
}
示例5: Execute
func (t *fetchTask) Execute(vm *otto.Otto, l *loop.Loop) error {
var arguments []interface{}
if t.err != nil {
e, err := vm.Call(`new Error`, nil, t.err.Error())
if err != nil {
return err
}
arguments = append(arguments, e)
}
t.jsRes.Set("status", t.status)
t.jsRes.Set("statusText", t.statusText)
h := mustValue(t.jsRes.Get("headers")).Object()
for k, vs := range t.headers {
for _, v := range vs {
if _, err := h.Call("append", k, v); err != nil {
return err
}
}
}
t.jsRes.Set("_body", string(t.body))
if _, err := t.cb.Call(otto.NullValue(), arguments...); err != nil {
return err
}
return nil
}
示例6: Bind
// Bind binds the method to the specified runtime.
func (m *Method) Bind(runtime *otto.Otto) error {
castFunc := (func(call otto.FunctionCall) otto.Value)(m.Func)
if err := runtime.Set("$$"+m.Name, castFunc); err != nil {
return err
}
return nil
}
示例7: getCompletions
func getCompletions(vm *otto.Otto, line string) (results []string) {
parts := strings.Split(line, ".")
objRef := "this"
prefix := line
if len(parts) > 1 {
objRef = strings.Join(parts[0:len(parts)-1], ".")
prefix = parts[len(parts)-1]
}
obj, _ := vm.Object(objRef)
if obj == nil {
return nil
}
iterOwnAndConstructorKeys(vm, obj, func(k string) {
if strings.HasPrefix(k, prefix) {
if objRef == "this" {
results = append(results, k)
} else {
results = append(results, strings.Join(parts[:len(parts)-1], ".")+"."+k)
}
}
})
// e.g. web3<tab><tab> append dot since its an object
if obj, _ = vm.Object(line); obj != nil {
results = append(results, line+".")
}
sort.Strings(results)
return results
}
示例8: addDiscoveryService
func (ctrl *JsController) addDiscoveryService(o *otto.Otto) {
o.Set("discover", func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) == 0 {
glog.Errorf("DISCOVER: Missing arguments")
return otto.NullValue()
}
url, _ := call.Argument(0).ToString()
upstreams, err := ctrl.DiscoveryService.Get(url)
if err != nil {
glog.Errorf("Failed to discover upstreams: %v", err)
return otto.NullValue()
}
glog.Infof("Discovered upstreams: %v", upstreams)
result, err := o.ToValue(upstreams)
if err != nil {
glog.Errorf("Failed to convert: %v", err)
return otto.NullValue()
}
return result
})
}
示例9: Exist
// Exist gets whether a variable exists in the runtime or not.
func Exist(runtime *otto.Otto, name string) bool {
if val, err := runtime.Get(name); err == nil {
return val != otto.UndefinedValue()
}
return false
}
示例10: Sync
func (b Buffers) Sync(o *otto.Otto) error {
if v, err := o.ToValue(b); err != nil {
return err
} else {
o.Set("buffers", v)
}
return nil
}
示例11: cleanupDocumentFromEnvironment
// now we be sure to cleanup the environment as best we can
func cleanupDocumentFromEnvironment(o *otto.Otto, doc Document) {
for k, _ := range doc {
_, err := o.Run(k + "=undefined")
if err != nil {
log.Printf("Error running otto cleanup: %v", err)
}
}
}
示例12: evaluateExpressionInEnvironment
func evaluateExpressionInEnvironment(o *otto.Otto, expr parser.Expression) otto.Value {
result, err := o.Run(fmt.Sprintf("ignore = %v", expr))
if err != nil {
log.Printf("Error running otto eval %v, %v", expr, err)
} else {
return result
}
return otto.UndefinedValue()
}
示例13: run
func run(vm *otto.Otto, filename string, code string) (otto.Value, error) {
// Compile before running so that stacktraces have filenames.
script, err := vm.Compile(filename, code)
if err != nil {
return otto.Value{}, err
}
return vm.Run(script)
}
示例14: addLoggers
func (ctrl *JsController) addLoggers(o *otto.Otto) {
o.Set("info", func(call otto.FunctionCall) otto.Value {
return log("info", call)
})
o.Set("error", func(call otto.FunctionCall) otto.Value {
return log("error", call)
})
}
示例15: newError
func newError(o *otto.Otto, inError error) otto.Value {
obj := errorToJs(inError)
jsObj, err := o.ToValue(obj)
if err != nil {
glog.Errorf("Error: %s", err)
return otto.NullValue()
}
return jsObj
}