本文整理汇总了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()
}
示例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
}
示例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
}
示例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)
}
示例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()
}
}
示例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
}
示例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
}
示例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()
}
示例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()
}
示例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()
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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()
}