當前位置: 首頁>>代碼示例>>Golang>>正文


Golang otto.UndefinedValue函數代碼示例

本文整理匯總了Golang中github.com/robertkrimen/otto.UndefinedValue函數的典型用法代碼示例。如果您正苦於以下問題:Golang UndefinedValue函數的具體用法?Golang UndefinedValue怎麽用?Golang UndefinedValue使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了UndefinedValue函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的github.com/robertkrimen/otto.UndefinedValue函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。