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


Golang Otto.ToValue方法代码示例

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


在下文中一共展示了Otto.ToValue方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: 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

示例2: 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

示例3: 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

示例4: InitEnv

func InitEnv(vm *otto.Otto) {
	vm.Set("G", new(Env))

	vm.Set("toJS", func(call otto.FunctionCall) otto.Value {
		result, err := vm.ToValue(call.Argument(0))
		if err != nil {
			panic(err)
		}
		return result
	})
}
开发者ID:jsmorph,项目名称:tinygraph,代码行数:11,代码来源:repl.go

示例5: 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

示例6: funcToOtto

func funcToOtto(context *otto.Otto, fn reflect.Value) interface{} {
	return func(call otto.FunctionCall) otto.Value {
		convertedArgs := make([]reflect.Value, 0)
		for _, v := range call.ArgumentList {
			exported, _ := v.Export()
			convertedArgs = append(convertedArgs, reflect.ValueOf(exported))
		}
		ret := fn.Call(convertedArgs)
		if len(ret) > 0 {
			val, _ := context.ToValue(ret[0].Interface())
			return val
		} else {
			return otto.UndefinedValue()
		}
	}
}
开发者ID:rcoelho,项目名称:go-plugins,代码行数:16,代码来源:ottojs.go

示例7: makeFilterWithVM

func makeFilterWithVM(vm *otto.Otto, script string) (func(input string) (string, error), error) {
	var (
		res  *http.Response
		body []byte
		err  error
	)

	res, err = http.Get(REQUIREJS_URL)
	if err != nil {
		log.Fatal(err)
	}
	body, err = ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatal(err)
	}
	res.Body.Close()
	rjs := string(body[:])

	return func(input string) (string, error) {
		logrus.Debug("Adding RequireJS")
		vm.Run(rjs)

		logrus.Debug("Executing script: " + script)
		_, err := vm.Run(`
                        var module = { "exports": function() { } }
                `)
		if err != nil {
			return "", err
		}
		_, err = vm.Run(script)
		if err != nil {
			return "", err
		}
		event := make(chan otto.Value)

		oval, err := vm.ToValue(input)
		if err != nil {
			return "", err
		}
		jscontext := createContext(vm, event)

		go vm.Call("module.exports", nil, oval, jscontext)
		value := <-event
		return value.ToString()
	}, nil
}
开发者ID:ewindisch,项目名称:iopipe,代码行数:46,代码来源:filter.go

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

示例9: addGetter

func (ctrl *JsController) addGetter(o *otto.Otto) {
	o.Set("get", func(call otto.FunctionCall) otto.Value {
		if len(call.ArgumentList) <= 0 {
			glog.Errorf("GET: Missing arguments")
			return newError(o, fmt.Errorf("GET: missing arguments"))
		}

		// Convert first argument, expect either string with url or list of strings
		upstreamsI, err := call.Argument(0).Export()
		if err != nil {
			glog.Errorf("GET: Failed to export first argument: %s", err)
			return newError(o, err)
		}
		upstreams, err := toStringArray(upstreamsI)
		if err != nil {
			glog.Errorf("GET: Failed to convert upstreams: %s", err)
			return newError(o, err)
		}

		// Second argument may be absent
		var query client.MultiDict
		if len(call.ArgumentList) > 1 {
			queryI, err := call.Argument(1).Export()
			if err != nil {
				glog.Errorf("GET: Failed to export first argument: %s", err)
				return newError(o, err)
			}
			dict, err := toMultiDict(queryI)
			if err != nil {
				glog.Errorf("GET: Failed: %s", err)
				return newError(o, err)
			}
			query = dict
		}

		// Third argument is optional username/password object
		var auth *netutils.BasicAuth
		if len(call.ArgumentList) > 2 {
			queryI, err := call.Argument(2).Export()
			if err != nil {
				glog.Errorf("GET: Failed: %s", err)
				return newError(o, err)
			}
			creds, err := toBasicAuth(queryI)
			if err != nil {
				glog.Errorf("GET: Failed: %s", err)
				return newError(o, err)
			}
			auth = creds
		}
		writer := NewResponseWriter()
		err = ctrl.Client.Get(writer, upstreams, query, auth)
		if err != nil {
			glog.Errorf("GET: Failed: %s", err)
			return newError(o, err)
		}
		reply := writer.ToReply()
		converted, err := o.ToValue(reply)
		if err != nil {
			glog.Errorf("GET: Failed: %s", err)
			return newError(o, err)
		}
		return converted
	})
}
开发者ID:johntdyer,项目名称:golang-devops-stuff,代码行数:65,代码来源:js.go

示例10: toOttoValueOrPanic

// Returns t as otto.Value or panics
func toOttoValueOrPanic(vm *otto.Otto, t interface{}) otto.Value {
	obj, err := vm.ToValue(t)
	utils.UnlessNilThenPanic(err)

	return obj
}
开发者ID:rizqi101,项目名称:conquest,代码行数:7,代码来源:js.go


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