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

Golang otto.TrueValue函数代码示例

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

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



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

示例1: InitPlugin

func InitPlugin(createVM func() (*modules.JsVm, error)) *modules.Plugin {

	p1 := modules.Plugin{
		Name: "httplistener",
		Init: func(vm *modules.JsVm) otto.Value {
			obj, _ := vm.Object("({})")
			obj.Set("addr", ":8081")
			obj.Set("start", func(c otto.FunctionCall) otto.Value {
				addrObj, _ := obj.Get("addr")
				addr, _ := addrObj.ToString()

				go startServer(addr, createVM)
				return otto.TrueValue()
			})
			obj.Set("startAndWait", func(c otto.FunctionCall) otto.Value {
				addrObj, _ := obj.Get("addr")
				addr, _ := addrObj.ToString()

				startServer(addr, createVM)
				return otto.TrueValue()
			})
			return obj.Value()
		},
	}

	return &p1
}
开发者ID:BlackEspresso,项目名称:ServeJS,代码行数:27,代码来源:httplistener.go


示例2: loadplugin

func (i *instance) loadplugin(name string) error {
	b, err := ioutil.ReadFile(name)
	_, filename := filepath.Split(name)

	if _, ok := i.Pluginfuncs[filename]; ok {
		delete(i.Pluginfuncs, filename)
		delete(i.Plugindocs, filename)
	}

	if err != nil {
		return err
	}

	m := make(map[string]otto.Value)
	i.js.Set("Subscribe", func(call otto.FunctionCall) otto.Value {
		if len(call.ArgumentList) >= 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsFunction() {
			name := call.ArgumentList[0].String()
			f := call.ArgumentList[1]
			if strings.HasPrefix(name, "regex:") {
				_, err := regexp.Compile(name[len("regex:"):])
				if err != nil {
					i.Irc.Privmsg(i.Irccfg.Channel, "Regex error: "+err.Error()+" - Regex subscribe was not successful")
					return otto.FalseValue()
				}
			}
			m[name] = f
			return otto.TrueValue()
		} else {
			return otto.FalseValue()
		}
	})

	md := make(map[string]string)
	i.js.Set("Doc", func(call otto.FunctionCall) otto.Value {
		if len(call.ArgumentList) >= 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() {
			name := call.ArgumentList[0].String()
			doc := call.ArgumentList[1].String()
			md[name] = doc
			return otto.TrueValue()
		} else {
			return otto.FalseValue()
		}
	})

	_, err = i.js.Run(string(b))
	i.js.Set("Subscribe", nil)
	i.js.Set("Doc", nil)

	if err != nil {
		fmt.Println(err.Error())
		return err
	} else {
		i.Pluginfuncs[filename] = m
		i.Plugindocs[filename] = md
		return nil
	}

}
开发者ID:ylmbtm,项目名称:nitori-go,代码行数:58,代码来源:plugin.go


示例3: registerVM

func registerVM(vm *modules.JsVm) otto.Value {
	obj, _ := vm.Object("({})")

	obj.Set("set", func(c otto.FunctionCall) otto.Value {
		key, _ := c.Argument(0).ToString()
		val, _ := c.Argument(1).ToString()
		kvCache[key] = val
		return otto.TrueValue()
	})
	obj.Set("get", func(c otto.FunctionCall) otto.Value {
		key, _ := c.Argument(0).ToString()
		val, ok := kvCache[key]
		if !ok {
			return otto.UndefinedValue()
		}
		retV, _ := otto.ToValue(val)
		return retV
	})

	obj.Set("all", func(c otto.FunctionCall) otto.Value {
		kvObj, err := vm.Object("({})")
		for k, v := range kvCache {
			kvObj.Set(k, v)
		}
		return modules.ToResult(vm, kvObj, err)
	})

	obj.Set("load", func(c otto.FunctionCall) otto.Value {
		path, _ := c.Argument(0).ToString()
		content, err := ioutil.ReadFile(path)
		if err != nil {
			return modules.ToResult(vm, nil, err)
		}
		err = json.Unmarshal(content, &kvCache)
		if err != nil {
			return modules.ToResult(vm, nil, err)
		}

		return otto.TrueValue()
	})

	obj.Set("save", func(c otto.FunctionCall) otto.Value {
		path, _ := c.Argument(0).ToString()
		content, _ := json.Marshal(kvCache)
		ioutil.WriteFile(path, content, 0777)
		return otto.TrueValue()
	})

	obj.Set("remove", func(c otto.FunctionCall) otto.Value {
		key, _ := c.Argument(0).ToString()
		delete(kvCache, key)
		return otto.TrueValue()
	})
	return obj.Value()
}
开发者ID:BlackEspresso,项目名称:ServeJS,代码行数:55,代码来源:cache.go


示例4: ResponseWriterToJso

func ResponseWriterToJso(o *otto.Object, w http.ResponseWriter) {
	o.Set("write", func(c otto.FunctionCall) otto.Value {
		text, _ := c.Argument(0).ToString()
		w.Write([]byte(text))
		return otto.TrueValue()
	})
	o.Set("writeHeader", func(c otto.FunctionCall) otto.Value {
		statusCode, _ := c.Argument(0).ToInteger()
		w.WriteHeader(int(statusCode))
		return otto.TrueValue()
	})
}
开发者ID:BlackEspresso,项目名称:ServeJS,代码行数:12,代码来源:http.go


示例5: InjectLevelDB

func InjectLevelDB(jsEngine *JSEngine, db *leveldb.DB) {
	jsEngine.Run("var db = {};")
	dbValue, _ := jsEngine.Get("db")
	dbObj := dbValue.Object()
	dbObj.Set("put", func(call otto.FunctionCall) otto.Value {
		key, err := call.Argument(0).ToString()
		if err != nil {
			log.Println("Error:", err.Error())
			return otto.FalseValue()
		}
		value, err := call.Argument(1).ToString()
		if err != nil {
			log.Println("Error:", err.Error())
			return otto.FalseValue()
		}

		err = db.Put([]byte(key), []byte(value), nil)
		if err != nil {
			log.Println("Error:", err.Error())
			return otto.FalseValue()
		}
		return otto.TrueValue()
	})
	dbObj.Set("get", func(call otto.FunctionCall) otto.Value {
		key, err := call.Argument(0).ToString()
		if err != nil {
			log.Println("Error:", err.Error())
			return otto.FalseValue()
		}
		data, err := db.Get([]byte(key), nil)
		if err != nil {
			log.Println("Error:", err.Error())
			return otto.FalseValue()
		}
		v, _ := otto.ToValue(string(data))
		return v
	})
	dbObj.Set("remove", func(call otto.FunctionCall) otto.Value {
		key, err := call.Argument(0).ToString()
		if err != nil {
			log.Println("Error:", err.Error())
			return otto.FalseValue()
		}
		err = db.Delete([]byte(key), nil)
		if err != nil {
			log.Println("Error:", err.Error())
			return otto.FalseValue()
		}
		return otto.TrueValue()
	})
}
开发者ID:teamSUDOs,项目名称:localise-it,代码行数:51,代码来源:js.go


示例6: plurk_AddResponse

// Add Response API
func plurk_AddResponse(call otto.FunctionCall) otto.Value {

	plurkID, _ := call.Argument(0).ToInteger()
	message, _ := call.Argument(1).ToString()
	qualifier, _ := call.Argument(2).ToString()

	if plurkID == 0 {
		logger.Error("Plurk ID not specify, add response failed!")
		return otto.FalseValue()
	}

	if len(message) <= 0 || message == "undefined" {
		logger.Error("No plurk content specify, add response failed!")
		return otto.FalseValue()
	}

	if qualifier == "undefined" {
		qualifier = ":"
	}

	responses := client.GetResponses()
	res, err := responses.ResponseAdd(int(plurkID), message, qualifier)

	if err != nil {
		logger.Error("Add response failed, because %s", err.Error())
	}

	logger.Info("Add response success, content is %s", res.RawContent)

	return otto.TrueValue()
}
开发者ID:elct9620,项目名称:go-plurk-robot,代码行数:32,代码来源:mod_plurk.go


示例7: InitPlugin

func InitPlugin() *modules.Plugin {

	p1 := modules.Plugin{
		Name: "mail",
		Init: func(vm *modules.JsVm) otto.Value {
			o, _ := vm.Object("({})")

			o.Set("loadMailSettings", func(c otto.FunctionCall) otto.Value {
				loadSettings(vm)
				return otto.TrueValue()
			})
			o.Set("send", func(c otto.FunctionCall) otto.Value {
				loadSettings(vm)
				recv, _ := c.Argument(0).ToString()
				subject, _ := c.Argument(1).ToString()
				msg, _ := c.Argument(2).ToString()

				err := sendmail(recv, subject, msg, "")

				return modules.ToResult(vm, true, err)
			})
			return o.Value()
		},
	}

	return &p1
}
开发者ID:BlackEspresso,项目名称:ServeJS,代码行数:27,代码来源:mail.go


示例8: InitUtilsJSBridge

func (pm *PluginManager) InitUtilsJSBridge() {
	bridge := &pmUtilsJSBridge{
		GetPage: func(call otto.FunctionCall) otto.Value {
			var err error
			switch {
			case len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString():
				if page, err := utils.GetPage(call.Argument(0).String()); err == nil {
					if val, err := pm.js.ToValue(page); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] GetPage errored: %s\n", err)
			case len(call.ArgumentList) == 3 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() && call.ArgumentList[2].IsString():
				if page, err := utils.GetPageWithAuth(call.Argument(0).String(), call.Argument(1).String(), call.Argument(2).String()); err == nil {
					if val, err := pm.js.ToValue(page); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] GetPageWithAuth errored: %s\n", err)
			}
			return otto.FalseValue()
		},
		ExtractURL: func(call otto.FunctionCall) otto.Value {
			var err error
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				if url, err := utils.ExtractURL(call.Argument(0).String()); err == nil {
					if val, err := pm.js.ToValue(url); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] ExtractURL errored: %s\n", err)
			}
			return otto.FalseValue()
		},
		ExtractTitle: func(call otto.FunctionCall) otto.Value {
			var err error
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				if title, err := utils.ExtractTitle(call.Argument(0).String()); err == nil {
					if val, err := pm.js.ToValue(title); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] ExtractTitle errored: %s\n", err)
			}
			return otto.FalseValue()
		},
		Sleep: func(call otto.FunctionCall) otto.Value {
			var err error
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsNumber() {
				if i, err := call.Argument(0).ToInteger(); err == nil {
					utils.Sleep(i)
					return otto.TrueValue()
				}
				pm.log.Printf("[UTILS] Sleep errored: %s\n", err)
			}
			return otto.FalseValue()
		},
	}
	pm.js.Set("UTILS", bridge)
}
开发者ID:Zenithar,项目名称:aktarus,代码行数:60,代码来源:utils_js_bridge.go


示例9: registerVM

func registerVM(vm *modules.JsVm) otto.Value {
	watcher, err := fsnotify.NewWatcher()
	//defer watcher.Close()
	if err != nil {
		log.Println(pluginName + " " + err.Error())
	}
	obj, _ := vm.Object("({})")

	obj.Set("watchDir", func(c otto.FunctionCall) otto.Value {
		path, _ := c.Argument(0).ToString()
		err := watcher.Add(path)
		return modules.ToResult(vm, true, err)
	})

	obj.Set("start", func(c otto.FunctionCall) otto.Value {
		log.Println(pluginName + " start")
		go func() {
			for {
				select {
				case ev := <-watcher.Events:
					log.Println("event:", ev.String())
				case err := <-watcher.Errors:
					log.Println("error:", err)
				}
			}
		}()
		return otto.TrueValue()
	})
	return obj.Value()
}
开发者ID:BlackEspresso,项目名称:ServeJS,代码行数:30,代码来源:filewatch.go


示例10: plurk_AddPlurk

// Add Plurk API
func plurk_AddPlurk(call otto.FunctionCall) otto.Value {
	message, _ := call.Argument(0).ToString()
	qualifier, _ := call.Argument(1).ToString()
	lang, _ := call.Argument(2).ToString()

	// No message specify, return error
	if len(message) <= 0 {
		return otto.FalseValue()
	}

	// Default qualifier
	if qualifier == "undefined" {
		qualifier = ":"
	}

	if lang == "undefined" {
		lang = "en"
	}

	timeline := client.GetTimeline()
	res, err := timeline.PlurkAdd(message, qualifier, make([]int, 0), false, lang, true)

	if err != nil {
		logger.Error("Add Plurk failed, because %s", err.Error())
		return otto.FalseValue()
	}

	logger.Info("New plurk added, content is %s", res.RawContent)

	return otto.TrueValue()
}
开发者ID:elct9620,项目名称:go-plurk-robot,代码行数:32,代码来源:mod_plurk.go


示例11: unlock

func (js *jsre) unlock(call otto.FunctionCall) otto.Value {
	addr, err := call.Argument(0).ToString()
	if err != nil {
		fmt.Println(err)
		return otto.FalseValue()
	}
	seconds, err := call.Argument(2).ToInteger()
	if err != nil {
		fmt.Println(err)
		return otto.FalseValue()
	}
	arg := call.Argument(1)
	var passphrase string
	if arg.IsUndefined() {
		fmt.Println("Please enter a passphrase now.")
		passphrase, err = readPassword("Passphrase: ", true)
		if err != nil {
			utils.Fatalf("%v", err)
		}
	} else {
		passphrase, err = arg.ToString()
		if err != nil {
			fmt.Println(err)
			return otto.FalseValue()
		}
	}
	am := js.ethereum.AccountManager()
	err = am.TimedUnlock(common.FromHex(addr), passphrase, time.Duration(seconds)*time.Second)
	if err != nil {
		fmt.Printf("Unlock account failed '%v'\n", err)
		return otto.FalseValue()
	}
	return otto.TrueValue()
}
开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:34,代码来源:admin.go


示例12: Sleep

// Sleep will block the console for the specified number of seconds.
func (b *bridge) Sleep(call otto.FunctionCall) (response otto.Value) {
	if call.Argument(0).IsNumber() {
		sleep, _ := call.Argument(0).ToInteger()
		time.Sleep(time.Duration(sleep) * time.Second)
		return otto.TrueValue()
	}
	return throwJSException("usage: sleep(<number of seconds>)")
}
开发者ID:expanse-project,项目名称:go-expanse,代码行数:9,代码来源:bridge.go


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


示例14: registerVM

func registerVM(vm *modules.JsVm) otto.Value {
	obj, _ := vm.Object("({})")
	obj.Set("sleep", func(c otto.FunctionCall) otto.Value {
		sec, _ := c.Argument(0).ToFloat()
		time.Sleep(time.Duration(sec) * time.Millisecond)
		return otto.TrueValue()
	})
	return obj.Value()
}
开发者ID:BlackEspresso,项目名称:ServeJS,代码行数:9,代码来源:time.go


示例15: Sleep

// Sleep will halt the console for arg[0] seconds.
func (self *Jeth) Sleep(call otto.FunctionCall) (response otto.Value) {
	if len(call.ArgumentList) >= 1 {
		if call.Argument(0).IsNumber() {
			sleep, _ := call.Argument(0).ToInteger()
			time.Sleep(time.Duration(sleep) * time.Second)
			return otto.TrueValue()
		}
	}
	return throwJSExeception("usage: sleep(<sleep in seconds>)")
}
开发者ID:obscuren,项目名称:etherapis,代码行数:11,代码来源:jeth.go


示例16: os_rename

func os_rename(call otto.FunctionCall) otto.Value {
	if 2 <= len(call.ArgumentList) {
		src, _ := call.ArgumentList[0].ToString()
		dst, _ := call.ArgumentList[1].ToString()
		if os.Rename(src, dst) != nil {
			return otto.TrueValue()
		}
	}
	return otto.UndefinedValue()
}
开发者ID:hattya,项目名称:aster,代码行数:10,代码来源:otto.go


示例17: loadScript

/*
Executes a JS script from inside the currently executing JS code.
Should only be called from inside an RPC routine.
*/
func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
	file, err := call.Argument(0).ToString()
	if err != nil {
		return otto.FalseValue()
	}
	if err := self.execWithoutEQ(file); err != nil { // loadScript is only called from inside js
		fmt.Println("err:", err)
		return otto.FalseValue()
	}

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


示例18: InitDebugJSBridge

func (pm *PluginManager) InitDebugJSBridge() {
	bridge := &pmDebugJSBridge{
		StackTrace: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 0 {
				debug.PrintStack()
				return otto.TrueValue()
			}
			return otto.FalseValue()
		},
	}
	pm.js.Set("DEBUG", bridge)
}
开发者ID:Zenithar,项目名称:aktarus,代码行数:12,代码来源:debug_js_bridge.go


示例19: SleepBlocks

// SleepBlocks will wait for a specified number of new blocks or max for a
// given of seconds. sleepBlocks(nBlocks[, maxSleep]).
func (self *Jeth) SleepBlocks(call otto.FunctionCall) (response otto.Value) {
	nBlocks := int64(0)
	maxSleep := int64(9999999999999999) // indefinitely

	nArgs := len(call.ArgumentList)

	if nArgs == 0 {
		throwJSExeception("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
	}

	if nArgs >= 1 {
		if call.Argument(0).IsNumber() {
			nBlocks, _ = call.Argument(0).ToInteger()
		} else {
			throwJSExeception("expected number as first argument")
		}
	}

	if nArgs >= 2 {
		if call.Argument(1).IsNumber() {
			maxSleep, _ = call.Argument(1).ToInteger()
		} else {
			throwJSExeception("expected number as second argument")
		}
	}

	// go through the console, this will allow web3 to call the appropriate
	// callbacks if a delayed response or notification is received.
	currentBlockNr := func() int64 {
		result, err := call.Otto.Run("eth.blockNumber")
		if err != nil {
			throwJSExeception(err.Error())
		}
		blockNr, err := result.ToInteger()
		if err != nil {
			throwJSExeception(err.Error())
		}
		return blockNr
	}

	targetBlockNr := currentBlockNr() + nBlocks
	deadline := time.Now().Add(time.Duration(maxSleep) * time.Second)

	for time.Now().Before(deadline) {
		if currentBlockNr() >= targetBlockNr {
			return otto.TrueValue()
		}
		time.Sleep(time.Second)
	}

	return otto.FalseValue()
}
开发者ID:obscuren,项目名称:etherapis,代码行数:54,代码来源:jeth.go


示例20: jsEnv

func (p *Plugin) jsEnv(event *irc.Event) otto.Value {
	obj, _ := p.js.Object("({})")
	obj.Set("event", p.eventToValue(event))
	obj.Set("log", func(call otto.FunctionCall) otto.Value {
		if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
			p.log.Println(call.ArgumentList[0].String())
			return otto.TrueValue()
		} else {
			return otto.FalseValue()
		}
	})
	return obj.Value()
}
开发者ID:Zenithar,项目名称:aktarus,代码行数:13,代码来源:plugin.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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