本文整理汇总了Golang中github.com/robertkrimen/otto.Otto.Run方法的典型用法代码示例。如果您正苦于以下问题:Golang Otto.Run方法的具体用法?Golang Otto.Run怎么用?Golang Otto.Run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robertkrimen/otto.Otto
的用法示例。
在下文中一共展示了Otto.Run方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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)
}
示例3: 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)
}
}
}
示例4: 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)
}
示例5: 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()
}
示例6: testString
func testString(vm *otto.Otto, src string) (string, error) {
v, err := vm.Run(src)
if err == nil {
if !v.IsString() {
err = fmt.Errorf("expected String, got %v", v)
} else {
return v.ToString()
}
}
return "", err
}
示例7: Execute
// Execute runs the EvalTask's otto.Script in the vm provided, pushing the
// resultant return value and error (or nil) into the associated channels.
// If the execution results in an error, it will return that error.
func (e EvalTask) Execute(vm *otto.Otto, l *loop.Loop) error {
v, err := vm.Run(e.Script)
e.Value <- v
e.Error <- err
if e.SoftError {
return nil
}
return err
}
示例8: testBoolean
func testBoolean(vm *otto.Otto, src string) (bool, error) {
v, err := vm.Run(src)
if err == nil {
if !v.IsBoolean() {
err = fmt.Errorf("expected Boolean, got %v", v)
} else {
return v.ToBoolean()
}
}
return false, err
}
示例9: reverseGremlinChainTo
func reverseGremlinChainTo(env *otto.Otto, prevObj *otto.Object, tag string) (*otto.Object, *otto.Object) {
env.Run("var _base_object = {}")
base, err := env.Object("_base_object")
if err != nil {
glog.Error(err)
return otto.NullValue().Object(), otto.NullValue().Object()
}
if isVertexChain(prevObj) {
base.Set("_gremlin_type", "vertex")
} else {
base.Set("_gremlin_type", "morphism")
}
return reverseGremlinChainHelper(env, prevObj, base, tag)
}
示例10: evaluateExpressionInEnvironmentAsInteger
func evaluateExpressionInEnvironmentAsInteger(o *otto.Otto, expr parser.Expression) int64 {
result, err := o.Run(fmt.Sprintf("%v", expr))
if err != nil {
log.Printf("Error running otto %v", err)
} else {
result, err := result.ToInteger()
if err != nil {
log.Printf("Error converting otto result to integer %v", err)
} else {
return result
}
}
return -1
}
示例11: putDocumentIntoEnvironment
// serialize all the top level elements of doc to JSON and
// put them in the environment
// FIXME there has to be a better way than this
func putDocumentIntoEnvironment(o *otto.Otto, doc Document) {
for k, v := range doc {
v_json, err := json.Marshal(v)
if err != nil {
log.Printf("JSON serialization failed: %v", err)
} else {
_, err := o.Run(k + "=" + string(v_json))
if err != nil {
log.Printf("Error running otto put: %v", err)
}
}
}
}
示例12: evaluateExpressionStringInEnvironmentAsBoolean
func evaluateExpressionStringInEnvironmentAsBoolean(o *otto.Otto, expression string) bool {
result, err := o.Run(expression)
if err != nil {
log.Printf("Error running otto %v, %v", expression, err)
} else {
//log.Printf("result was %v", result)
result, err := result.ToBoolean()
if err != nil {
log.Printf("Error converting otto result to boolean %v", err)
} else if result {
return true
}
}
return false
}
示例13: parseContext
func parseContext(vm *otto.Otto) (stc Stitch, err error) {
vmCtx, err := vm.Run("deployment.toQuiltRepresentation()")
if err != nil {
return stc, err
}
// Export() always returns `nil` as the error (it's only present for
// backwards compatibility), so we can safely ignore it.
exp, _ := vmCtx.Export()
ctxStr, err := json.Marshal(exp)
if err != nil {
return stc, err
}
err = json.Unmarshal(ctxStr, &stc)
return stc, err
}
示例14: createContext
func createContext(vm *otto.Otto, event chan otto.Value) otto.Value {
var cbfunc = func(invar ...interface{}) {
var lovar otto.Value
if len(invar) > 0 {
lovar, _ = vm.ToValue(invar[0])
} else {
lovar = otto.NullValue()
}
event <- lovar
close(event)
}
vm.Set("_iopipe_cb", cbfunc)
vm.Run(`_iopipe_context = { "done": _iopipe_cb,
"success": _iopipe_cb,
"fail": _iopipe_cb }`)
jscontext, _ := vm.Get("_iopipe_context")
return jscontext
}
示例15: reverseGremlinChainHelper
func reverseGremlinChainHelper(env *otto.Otto, chain *otto.Object, newBase *otto.Object, tag string) (*otto.Object, *otto.Object) {
kindVal, _ := chain.Get("_gremlin_type")
kind, _ := kindVal.ToString()
if tag != "" {
if kind == "tag" {
tags := getStringArgs(chain)
for _, t := range tags {
if t == tag {
return newBase, chain
}
}
}
}
if kind == "morphism" || kind == "vertex" {
return newBase, chain
}
var newKind string
switch kind {
case "in":
newKind = "out"
case "out":
newKind = "in"
default:
newKind = kind
}
prev, _ := chain.Get("_gremlin_prev")
env.Run("var out = {}")
out, _ := env.Object("out")
out.Set("_gremlin_type", newKind)
values, _ := chain.Get("_gremlin_values")
out.Set("_gremlin_values", values)
back, _ := chain.Get("_gremlin_back_chain")
out.Set("_gremlin_back_chain", back)
out.Set("_gremlin_prev", newBase)
strings, _ := chain.Get("string_args")
out.Set("string_args", strings)
return reverseGremlinChainHelper(env, prev.Object(), out, tag)
}