本文整理汇总了Golang中github.com/robertkrimen/otto.TrueValue函数的典型用法代码示例。如果您正苦于以下问题:Golang TrueValue函数的具体用法?Golang TrueValue怎么用?Golang TrueValue使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TrueValue函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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
}
}
示例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()
}
示例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()
})
}
示例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()
})
}
示例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()
}
示例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
}
示例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)
}
示例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()
}
示例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()
}
示例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()
}
示例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>)")
}
示例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
}
示例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()
}
示例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>)")
}