• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang otto.Otto类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/robertkrimen/otto.Otto的典型用法代码示例。如果您正苦于以下问题:Golang Otto类的具体用法?Golang Otto怎么用?Golang Otto使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Otto类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


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


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


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


示例19: getConstant

func getConstant(vm *otto.Otto, name string) string {
	val, err := vm.Get(name)
	if err != nil {
		return ""
	}
	str, err := val.ToString()
	if err != nil {
		return ""
	}
	return str
}
开发者ID:5Sigma,项目名称:Conduit,代码行数:11,代码来源:engine.go


示例20: requireFromOtto

func requireFromOtto(moduleName string, vm *otto.Otto) (otto.Value, error) {
	rawModule, errRequire := RequireModule(moduleName)
	if errRequire != nil {
		return otto.UndefinedValue(), errRequire
	}

	module, errConvert := vm.ToValue(rawModule)
	if errConvert != nil {
		return otto.UndefinedValue(), errConvert
	}

	return module, nil
}
开发者ID:cloudwan,项目名称:gohan,代码行数:13,代码来源:gohan_core.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang otto.Value类代码示例发布时间:2022-05-28
下一篇:
Golang otto.Object类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap