本文整理汇总了Golang中github.com/robertkrimen/otto.Value.Call方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Call方法的具体用法?Golang Value.Call怎么用?Golang Value.Call使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robertkrimen/otto.Value
的用法示例。
在下文中一共展示了Value.Call方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: runIteratorWithCallback
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()
}
示例2: runIteratorWithCallback
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)
}
}
示例3: PrettyPrint
// 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)
}
示例4: AddCallback
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,
})
}
示例5: PrettyPrint
// 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
}
示例6: runIteratorWithCallback
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()
}
示例7: callHandler
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
}
示例8: SetCommand
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,
}
}
示例9: evaluateScript
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()
}
示例10: runIteratorWithCallback
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()
}
示例11: runIteratorWithCallback
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()
}
示例12: evaluateRule
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()
}
示例13: TriggerEvent
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
}