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

Golang otto.UndefinedValue函数代码示例

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

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



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

示例1: printBlock

func (js *jsre) printBlock(call otto.FunctionCall) otto.Value {
	var block *types.Block
	if len(call.ArgumentList) > 0 {
		if call.Argument(0).IsNumber() {
			num, _ := call.Argument(0).ToInteger()
			block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
		} else if call.Argument(0).IsString() {
			hash, _ := call.Argument(0).ToString()
			block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
		} else {
			fmt.Println("invalid argument for dump. Either hex string or number")
		}

	} else {
		block = js.ethereum.ChainManager().CurrentBlock()
	}
	if block == nil {
		fmt.Println("block not found")
		return otto.UndefinedValue()
	}

	fmt.Println(block)

	return otto.UndefinedValue()
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:25,代码来源:admin.go


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


示例3: toValue

func toValue(vm *motto.Motto, value interface{}) otto.Value {
	var err error
	var val otto.Value
	switch n := value.(type) {
	case dict.Map, map[string]interface{}:
		bs, e := json.Marshal(n)
		if e != nil {
			err = e
			return otto.UndefinedValue()
		}
		object, oe := vm.Object("(" + string(bs) + ")")
		if oe != nil {
			err = oe
		} else {
			val = object.Value()
		}

	default:
		val, err = vm.ToValue(n)
	}

	if err != nil {
		log.Printf("Could not convert %v", err)
		return otto.UndefinedValue()
	}

	return val
}
开发者ID:kildevaeld,项目名称:scaffolt,代码行数:28,代码来源:context.go


示例4: main

func main() {
	if len(os.Args) != 2 {
		usage()
	}
	vm := motto.New()

	vm.Set("fib", func(call otto.FunctionCall) otto.Value {

		ret := fib(40)

		v, _ := otto.ToValue(ret)

		return v

	})

	vm.Set("XHR", func(call otto.FunctionCall) otto.Value {
		return otto.UndefinedValue()
	})
	vm.Set("XHR.prototype.hello", func(call otto.FunctionCall) otto.Value {
		return otto.UndefinedValue()
	})
	_, err := vm.Run(os.Args[1])
	fmt.Printf("%v", err)
}
开发者ID:kildevaeld,项目名称:motto,代码行数:25,代码来源:main.go


示例5: mkAssert

func mkAssert(t *testing.T) func(otto.FunctionCall) otto.Value {
	return func(call otto.FunctionCall) otto.Value {
		name, err := call.Argument(0).ToString()
		if err != nil {
			t.Fatalf("Error getting name of assertion: %v", err)
		}
		t.Logf("RUN %v", name)

		got, err := call.Argument(1).Export()
		if err != nil {
			t.Errorf("Eval error on %v: %v", name, err)
			return otto.UndefinedValue()
		}
		exp, err := call.Argument(2).Export()
		if err != nil {
			t.Errorf("Comparison error on %v: %v", name, err)
			return otto.UndefinedValue()
		}

		if toInt(exp) != toInt(got) {
			t.Errorf("FAIL Expected %T(%v) for %v, got %T(%v)",
				exp, exp, name, got, got)
		} else {
			t.Logf("PASS %v", name)
		}
		return otto.UndefinedValue()
	}
}
开发者ID:steveyen,项目名称:cbgb,代码行数:28,代码来源:reduction_test.go


示例6: apiServiceCall

func apiServiceCall(call otto.FunctionCall) otto.Value {
	svc := call.Argument(0).String()

	interfaceValue, err := call.Argument(1).Export()
	if err != nil {
		logrus.Errorf("Plugins: rules: javascript supplied invalid parameters")
		return otto.UndefinedValue()
	}
	params := interfaceValue.(map[string]interface{})

	future := service.CallService(svc, params)
	result := <-future.Result

	var res otto.Value

	if _, ok := result.(error); ok {
		res, err = otto.ToValue(result.(error).Error())
	} else {
		res, err = otto.ToValue(result)
	}
	if err != nil {
		logrus.Errorf("Plugins: rules: failed to convert service result to javascript")
		return otto.UndefinedValue()
	}
	return res
}
开发者ID:nethack42,项目名称:go-home,代码行数:26,代码来源:exposed.go


示例7: NewExecutor

func NewExecutor(s stream.Stream) *Executor {
	e := &Executor{
		incomingScripts: make(chan string),
		outgoingMsgs:    make(chan string),
		incomingEvents:  make(chan IncomingEvent),
		eventHandlers:   make(map[string]map[string]otto.Value),
	}
	e.xmppStream = s
	e.vm = otto.New()

	send := func(call otto.FunctionCall) otto.Value {
		str, _ := call.Argument(0).ToString()
		e.outgoingMsgs <- str
		return otto.UndefinedValue()
	}

	addHandler := func(call otto.FunctionCall) otto.Value {
		evtName, err := call.Argument(0).ToString()
		handlerName, err := call.Argument(1).ToString()
		if err != nil {
			return otto.FalseValue()
		}
		val := call.Argument(2)
		if !val.IsFunction() {
			return otto.FalseValue()
		}
		handlers, ok := e.eventHandlers[evtName]
		if !ok {
			e.eventHandlers[evtName] = map[string]otto.Value{handlerName: val}
		} else {
			handlers[handlerName] = val
		}
		return otto.TrueValue()
	}

	listHandlers := func(call otto.FunctionCall) otto.Value {
		evtName, err := call.Argument(0).ToString()
		if err != nil {
			return otto.UndefinedValue()
		}
		list := []string{}
		for handlerName := range e.eventHandlers[evtName] {
			list = append(list, handlerName)
		}
		val, err := e.vm.ToValue(list)
		if err != nil {
			return otto.UndefinedValue()
		} else {
			return val
		}
	}

	chatLibrary, _ := e.vm.Object("Chat = {};")
	chatLibrary.Set("send", send)
	chatLibrary.Set("addEventHandler", addHandler)
	chatLibrary.Set("listEventHandlers", listHandlers)
	return e
}
开发者ID:postman0,项目名称:xep,代码行数:58,代码来源:executor.go


示例8: verbosity

func (js *jsre) verbosity(call otto.FunctionCall) otto.Value {
	v, err := call.Argument(0).ToInteger()
	if err != nil {
		fmt.Println(err)
		return otto.UndefinedValue()
	}

	glog.SetV(int(v))
	return otto.UndefinedValue()
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:10,代码来源:admin.go


示例9: backtrace

func (js *jsre) backtrace(call otto.FunctionCall) otto.Value {
	tracestr, err := call.Argument(0).ToString()
	if err != nil {
		fmt.Println(err)
		return otto.UndefinedValue()
	}
	glog.GetTraceLocation().Set(tracestr)

	return otto.UndefinedValue()
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:10,代码来源:admin.go


示例10: CreateFile

func (self *context) CreateFile(call otto.FunctionCall) otto.Value {
	if len(call.ArgumentList) != 2 {
		return otto.UndefinedValue()
	}

	path, _ := call.Argument(0).ToString()
	val, _ := call.Argument(1).ToString()
	self.ctx.CreateFile(path, []byte(val))

	return otto.UndefinedValue()
}
开发者ID:kildevaeld,项目名称:scaffolt,代码行数:11,代码来源:context.go


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


示例12: OttoFromGoArray

func OttoFromGoArray(o *otto.Otto, arr []interface{}) (otto.Value, error) {
	ovarr, err := OttoFromGo(o, arr)
	if err != nil {
		return otto.UndefinedValue(),
			fmt.Errorf("could not convert arr, err: %v", err)
	}
	if ovarr.Class() != "Array" {
		return otto.UndefinedValue(),
			fmt.Errorf("expected ovarr to be array, got: %#v, class: %v, arr: %v",
				ovarr, ovarr.Class(), arr)
	}
	return ovarr, nil
}
开发者ID:steveyen,项目名称:cbgb,代码行数:13,代码来源:js.go


示例13: GetViewMapFunction

func (v *View) GetViewMapFunction() (*ViewMapFunction, error) {
	if v.preparedViewMapFunction != nil {
		return v.preparedViewMapFunction, nil
	}

	if v.Map == "" {
		return nil, fmt.Errorf("view map function missing")
	}

	o := otto.New()
	mapf, err := OttoNewFunction(o, v.Map)
	if err != nil {
		return nil, fmt.Errorf("view map function error: %v", err)
	}

	emits := []*ViewRow{}
	var emitErr error

	o.Set("emit", func(call otto.FunctionCall) otto.Value {
		if len(call.ArgumentList) <= 0 {
			emitErr = fmt.Errorf("emit() invoked with no parameters")
			return otto.UndefinedValue()
		}
		var key, value interface{}
		key, emitErr = call.ArgumentList[0].Export()
		if emitErr != nil {
			return otto.UndefinedValue()
		}
		if len(call.ArgumentList) >= 2 {
			value, emitErr = call.ArgumentList[1].Export()
			if emitErr != nil {
				return otto.UndefinedValue()
			}
		}
		emits = append(emits, &ViewRow{Key: key, Value: value})
		return otto.UndefinedValue()
	})

	v.preparedViewMapFunction = &ViewMapFunction{
		otto: o,
		mapf: mapf,
		restartEmits: func() (resEmits []*ViewRow, resEmitErr error) {
			resEmits = emits
			resEmitErr = emitErr
			emits = []*ViewRow{}
			emitErr = nil
			return resEmits, resEmitErr
		},
	}
	return v.preparedViewMapFunction, nil
}
开发者ID:scottcagno,项目名称:cbgb,代码行数:51,代码来源:ddoc.go


示例14: RegisterMethodFileOpen

// RegisterMethodFileOpen registers a method that is capable of opening files.
//
// Response object:
//
//     {
//       ok: true|false,
//       reader: "readFile" // the method to use to read from the file
//     }
//
// To read the entire file into a variable:
//
//     // in Go...
//     RegisterMethodFileOpen(js, "openFile")
//
//     // in the script...
//     var file = openFile("filename"); // open the file
//     var read = eval(file.reader); // get the reader method
//     var close = eval(file.closer); // get the closer method
//
//     var fileContents = read(-1); // read everything
//     close(); // close the file
//
func RegisterMethodFileOpen(runtime *otto.Otto, methodName string) error {

	runtime.Set(methodName, func(call otto.FunctionCall) otto.Value {

		// check the arguments
		if len(call.ArgumentList) != 1 {
			raiseError(runtime, "%s takes 1 arguments: %s(filename)", methodName, methodName)
			return otto.UndefinedValue()
		}

		// get the arguments
		var filename string
		var err error
		if filename, err = call.Argument(0).ToString(); err != nil {
			raiseError(runtime, "%s first argument must be a string containing the filename", methodName)
			return otto.UndefinedValue()
		}

		// open the file
		var file io.ReadCloser
		if file, err = os.Open(filename); err != nil {
			raiseError(runtime, "Failed to open file '%s': %s", filename, err)
			return otto.UndefinedValue()
		}

		// add the reader
		var readerMethodName string = generateMethodName("fileRead")
		NewReader(runtime, readerMethodName, file)

		// add the closer
		var closerMethodName string = generateMethodName("fileClose")
		runtime.Set(closerMethodName, func(call otto.FunctionCall) otto.Value {
			if err := file.Close(); err != nil {
				raiseError(runtime, "Failed to close file '%s': %s", filename, err)
				return otto.FalseValue()
			}
			return otto.TrueValue()
		})

		var response otto.Value
		if response, err = makeMap(runtime, map[string]interface{}{"reader": readerMethodName, "closer": closerMethodName, "ok": true}); err != nil {
			raiseError(runtime, "%s failed to make response map.", methodName)
			return otto.UndefinedValue()
		}

		// done
		return response
	})

	return nil
}
开发者ID:jmptrader,项目名称:ottox,代码行数:73,代码来源:files.go


示例15: Get

func (self *context) Get(call otto.FunctionCall) otto.Value {
	if len(call.ArgumentList) != 1 {
		return otto.UndefinedValue()
	}

	key, _ := call.Argument(0).ToString()
	val := self.ctx.Get(key)

	if val != nil {
		return toValue(self.vm, val)
	}

	return otto.UndefinedValue()
}
开发者ID:kildevaeld,项目名称:scaffolt,代码行数:14,代码来源:context.go


示例16: gopacMyIpAddress

// myIpAddress returns the IP address of the host machine.
func gopacMyIpAddress() otto.Value {
	hostname, err := os.Hostname()

	if err != nil {
		return otto.UndefinedValue()
	}

	address := gopacDnsResolve(hostname)

	if value, err := otto.ToValue(address); err == nil {
		return value
	}

	return otto.UndefinedValue()
}
开发者ID:jmptrader,项目名称:pacproxy,代码行数:16,代码来源:gopac_utils.go


示例17: setHead

func (js *jsre) setHead(call otto.FunctionCall) otto.Value {
	block, err := js.getBlock(call)
	if err != nil {
		fmt.Println(err)
		return otto.UndefinedValue()
	}

	if block == nil {
		fmt.Println("block not found")
		return otto.UndefinedValue()
	}

	js.ethereum.ChainManager().SetHead(block)
	return otto.UndefinedValue()
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:15,代码来源:admin.go


示例18: getBlockRlp

func (js *jsre) getBlockRlp(call otto.FunctionCall) otto.Value {
	block, err := js.getBlock(call)
	if err != nil {
		fmt.Println(err)
		return otto.UndefinedValue()
	}

	if block == nil {
		fmt.Println("block not found")
		return otto.UndefinedValue()
	}

	encoded, _ := rlp.EncodeToBytes(block)
	return js.re.ToVal(fmt.Sprintf("%x", encoded))
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:15,代码来源:admin.go


示例19: setExtra

func (js *jsre) setExtra(call otto.FunctionCall) otto.Value {
	extra, err := call.Argument(0).ToString()
	if err != nil {
		fmt.Println(err)
		return otto.UndefinedValue()
	}

	if len(extra) > 1024 {
		fmt.Println("error: cannot exceed 1024 bytes")
		return otto.UndefinedValue()
	}

	js.ethereum.Miner().SetExtra([]byte(extra))
	return otto.UndefinedValue()
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:15,代码来源:admin.go


示例20: stringToValue

// stringToValue makes an otto.Value containing the specified string.
func stringToValue(message string) otto.Value {
	val, err := otto.ToValue(message)
	if err != nil {
		return otto.UndefinedValue()
	}
	return val
}
开发者ID:jmptrader,项目名称:pangaea,代码行数:8,代码来源:built_in_methods.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang otto.FunctionCall类代码示例发布时间:2022-05-28
下一篇:
Golang otto.TrueValue函数代码示例发布时间: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