func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Value, this otto.FunctionCall, limit int) { count := 0 it, _ = it.Optimize() for { if ses.doHalt { return } _, ok := it.Next() if !ok { break } tags := make(map[string]graph.TSVal) it.TagResults(&tags) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ = callback.Call(this.This, val) count++ if limit >= 0 && count >= limit { break } for it.NextResult() == true { if ses.doHalt { return } tags := make(map[string]graph.TSVal) it.TagResults(&tags) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ = callback.Call(this.This, val) count++ if limit >= 0 && count >= limit { break } } } it.Close() }
func (wk *worker) runIteratorWithCallback(it graph.Iterator, callback otto.Value, this otto.FunctionCall, limit int) { ctx, cancel := wk.newContext() defer cancel() err := graph.Iterate(ctx, it).Paths(true).Limit(limit).TagEach(func(tags map[string]graph.Value) { val, _ := this.Otto.ToValue(wk.tagsToValueMap(tags)) val, _ = callback.Call(this.This, val) }) if err != nil { clog.Errorf("gremlin: %v", err) } }
// uses the "prettyPrint" JS function to format a value func (self *JSRE) PrettyPrint(v interface{}) (val otto.Value, err error) { var method otto.Value v, err = self.vm.ToValue(v) if err != nil { return } method, err = self.vm.Get("prettyPrint") if err != nil { return } return method.Call(method, v) }
func (p *Plugin) AddCallback(eventCode string, name string, callback otto.Value) { wrappedCallback := func(env otto.Value) { _, err := callback.Call(env) if err != nil { p.log.Printf("Callback `%s` (%#v) for event code `%s` errored: %s", name, callback, eventCode, err) } } p.callbacks[eventCode] = append(p.callbacks[eventCode], &pluginFunc{ function: wrappedCallback, help: name, }) }
// PrettyPrint writes v to standard output. func (self *JSRE) PrettyPrint(v interface{}) (val otto.Value, err error) { var method otto.Value self.do(func(vm *otto.Otto) { val, err = vm.ToValue(v) if err != nil { return } method, err = vm.Get("prettyPrint") if err != nil { return } val, err = method.Call(method, val) }) return val, err }
func (wk *worker) runIteratorWithCallback(it graph.Iterator, callback otto.Value, this otto.FunctionCall, limit int) { n := 0 it, _ = it.Optimize() if glog.V(2) { b, err := json.MarshalIndent(it.Describe(), "", " ") if err != nil { glog.V(2).Infof("failed to format description: %v", err) } else { glog.V(2).Infof("%s", b) } } for { select { case <-wk.kill: return default: } if !graph.Next(it) { break } tags := make(map[string]graph.Value) it.TagResults(tags) val, _ := this.Otto.ToValue(wk.tagsToValueMap(tags)) val, _ = callback.Call(this.This, val) n++ if limit >= 0 && n >= limit { break } for it.NextPath() { select { case <-wk.kill: return default: } tags := make(map[string]graph.Value) it.TagResults(tags) val, _ := this.Otto.ToValue(wk.tagsToValueMap(tags)) val, _ = callback.Call(this.This, val) n++ if limit >= 0 && n >= limit { break } } } it.Close() }
func (ctrl *JsController) callHandler(handler otto.Value, params ...interface{}) (interface{}, error) { if !handler.IsFunction() { return nil, fmt.Errorf("Result should be a function, got %v", handler) } out, err := handler.Call(handler, params...) if err != nil { glog.Errorf("Call resulted in failure %#v", err) return nil, err } obj, err := out.Export() if err != nil { glog.Errorf("Failed to extract response %#v", err) return nil, err } return obj, nil }
func (p *Plugin) SetCommand(name string, command otto.Value, help string) { if _, ok := p.commands[name]; ok { if p.cfg.Irc.Debug || p.cfg.Debug { p.log.Printf("Warning: Command `%s` was already defined. Overriding...", name) } } wrappedCommand := func(env otto.Value) { _, err := command.Call(env) if err != nil { p.log.Printf("Command `%s` errored: %s", name, err) } } p.commands[name] = &pluginFunc{ function: wrappedCommand, help: help, } }
func evaluateScript(src string, payload []FinalInput) (string, error) { javaScript := otto.New() var evalFunc otto.Value javaScript.Set("eval", func(call otto.FunctionCall) otto.Value { evalFunc = call.Argument(0) return otto.UndefinedValue() }) javaScript.Run(src) arg, err := javaScript.ToValue(payload) if err != nil { return "", err } ret, err := evalFunc.Call(otto.NullValue(), arg) if err != nil { return "", err } return ret.ToString() }
func (wk *worker) runIteratorWithCallback(it graph.Iterator, callback otto.Value, this otto.FunctionCall, limit int) { n := 0 it, _ = it.Optimize() glog.V(2).Infoln(it.DebugString(0)) for { select { case <-wk.kill: return default: } if !graph.Next(it) { break } tags := make(map[string]graph.Value) it.TagResults(tags) val, _ := this.Otto.ToValue(wk.tagsToValueMap(tags)) val, _ = callback.Call(this.This, val) n++ if limit >= 0 && n >= limit { break } for it.NextPath() { select { case <-wk.kill: return default: } tags := make(map[string]graph.Value) it.TagResults(tags) val, _ := this.Otto.ToValue(wk.tagsToValueMap(tags)) val, _ = callback.Call(this.This, val) n++ if limit >= 0 && n >= limit { break } } } it.Close() }
func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Value, this otto.FunctionCall, limit int) { count := 0 it, _ = it.Optimize() for { select { case <-ses.kill: return default: } if !graph.Next(it) { break } tags := make(map[string]graph.Value) it.TagResults(tags) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ = callback.Call(this.This, val) count++ if limit >= 0 && count >= limit { break } for it.NextPath() { select { case <-ses.kill: return default: } tags := make(map[string]graph.Value) it.TagResults(tags) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ = callback.Call(this.This, val) count++ if limit >= 0 && count >= limit { break } } } it.Close() }
func evaluateRule(src string, payload *Message) (bool, error) { js := otto.New() var ruleFunc otto.Value js.Set("rule", func(call otto.FunctionCall) otto.Value { ruleFunc = call.Argument(0) return otto.UndefinedValue() }) js.Run(src) arg, err := js.ToValue(payload) if err != nil { return false, err } ret, err := ruleFunc.Call(otto.NullValue(), arg) if err != nil { return false, err } return ret.ToBoolean() }
func (s *Scripting) TriggerEvent(eventName string, rawArguments ...interface{}) (err error) { var ( present bool callbacks []otto.Value callback otto.Value response otto.Value arguments []interface{} ) if callbacks, present = s.listeners[eventName]; !present { return // Not an error to have no listeners. } if arguments, err = s.convertArguments(rawArguments); err != nil { return } for _, callback = range callbacks { if response, err = callback.Call(callback, arguments...); err != nil { return } if response.IsString() { fmt.Printf("Response from callback: %s\n", response.String()) } } return }