当前位置: 首页>>代码示例>>Golang>>正文


Golang otto.Otto类代码示例

本文整理汇总了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
}
开发者ID:porty,项目名称:react-in-go,代码行数:27,代码来源:timers.go

示例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
}
开发者ID:hattya,项目名称:aster,代码行数:7,代码来源:otto_test.go

示例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)
}
开发者ID:ssonneborn22,项目名称:go-ethereum,代码行数:7,代码来源:jsre.go

示例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
}
开发者ID:jmptrader,项目名称:ottox,代码行数:47,代码来源:writer.go

示例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
}
开发者ID:deoxxa,项目名称:ottoext,代码行数:30,代码来源:fetch.go

示例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
}
开发者ID:jmptrader,项目名称:pangaea,代码行数:8,代码来源:methods.go

示例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
}
开发者ID:karalabe,项目名称:etherapis,代码行数:30,代码来源:completion.go

示例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
	})
}
开发者ID:johntdyer,项目名称:golang-devops-stuff,代码行数:25,代码来源:js.go

示例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
}
开发者ID:jmptrader,项目名称:ottox,代码行数:9,代码来源:exist.go

示例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
}
开发者ID:janne,项目名称:janitor,代码行数:8,代码来源:buffer.go

示例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)
		}
	}
}
开发者ID:mschoch,项目名称:tuq,代码行数:9,代码来源:otto_util.go

示例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()
}
开发者ID:mschoch,项目名称:tuq,代码行数:9,代码来源:otto_util.go

示例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)
}
开发者ID:NetSys,项目名称:quilt,代码行数:9,代码来源:stitch.go

示例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)
	})

}
开发者ID:johntdyer,项目名称:golang-devops-stuff,代码行数:9,代码来源:js.go

示例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
}
开发者ID:karlpilkington,项目名称:golang-devops-stuff,代码行数:9,代码来源:conversions.go


注:本文中的github.com/robertkrimen/otto.Otto类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。