本文整理汇总了Golang中github.com/robertkrimen/otto.Otto.Get方法的典型用法代码示例。如果您正苦于以下问题:Golang Otto.Get方法的具体用法?Golang Otto.Get怎么用?Golang Otto.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robertkrimen/otto.Otto
的用法示例。
在下文中一共展示了Otto.Get方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Exist
// Exist gets whether a variable exists in the runtime or not.
func Exist(runtime *otto.Otto, name string) bool {
if val, err := runtime.Get(name); err == nil {
return val != otto.UndefinedValue()
}
return false
}
示例2: getConstant
func getConstant(vm *otto.Otto, name string) string {
val, err := vm.Get(name)
if err != nil {
return ""
}
str, err := val.ToString()
if err != nil {
return ""
}
return str
}
示例3: createContext
func createContext(vm *otto.Otto, event chan otto.Value) otto.Value {
var cbfunc = func(invar ...interface{}) {
var lovar otto.Value
if len(invar) > 0 {
lovar, _ = vm.ToValue(invar[0])
} else {
lovar = otto.NullValue()
}
event <- lovar
close(event)
}
vm.Set("_iopipe_cb", cbfunc)
vm.Run(`_iopipe_context = { "done": _iopipe_cb,
"success": _iopipe_cb,
"fail": _iopipe_cb }`)
jscontext, _ := vm.Get("_iopipe_context")
return jscontext
}
示例4: Define
func Define(vm *otto.Otto, argv []string) error {
if v, err := vm.Get("process"); err != nil {
return err
} else if !v.IsUndefined() {
return nil
}
env := make(map[string]string)
for _, e := range os.Environ() {
a := strings.SplitN(e, "=", 2)
env[a[0]] = a[1]
}
return vm.Set("process", map[string]interface{}{
"env": env,
"argv": argv,
})
}
示例5: Define
func Define(vm *otto.Otto, l *loop.Loop) error {
if v, err := vm.Get("Promise"); err != nil {
return err
} else if !v.IsUndefined() {
return nil
}
if err := timers.Define(vm, l); err != nil {
return err
}
s, err := vm.Compile("promise-bundle.js", src)
if err != nil {
return err
}
if _, err := vm.Run(s); err != nil {
return err
}
return nil
}
示例6: Define
func Define(vm *otto.Otto, l *loop.Loop) error {
if v, err := vm.Get("Promise"); err != nil {
return err
} else if !v.IsUndefined() {
return nil
}
if err := timers.Define(vm, l); err != nil {
return err
}
s, err := vm.Compile("bundle.js", rice.MustFindBox("dist-promise").MustString("bundle.js"))
if err != nil {
return err
}
if _, err := vm.Run(s); err != nil {
return err
}
return nil
}
示例7: Define
func Define(vm *otto.Otto, l *loop.Loop) error {
if v, err := vm.Get("setTimeout"); err != nil {
return err
} else if !v.IsUndefined() {
return nil
}
newTimer := func(interval bool) func(call otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
delay, _ := call.Argument(1).ToInteger()
if delay < minDelay[interval] {
delay = minDelay[interval]
}
t := &timerTask{
duration: time.Duration(delay) * time.Millisecond,
call: call,
interval: interval,
}
l.Add(t)
t.timer = time.AfterFunc(t.duration, func() {
l.Ready(t)
})
value, err := call.Otto.ToValue(t)
if err != nil {
panic(err)
}
return value
}
}
vm.Set("setTimeout", newTimer(false))
vm.Set("setInterval", newTimer(true))
vm.Set("setImmediate", func(call otto.FunctionCall) otto.Value {
t := &timerTask{
duration: time.Millisecond,
call: call,
}
l.Add(t)
t.timer = time.AfterFunc(t.duration, func() {
l.Ready(t)
})
value, err := call.Otto.ToValue(t)
if err != nil {
panic(err)
}
return value
})
clearTimeout := func(call otto.FunctionCall) otto.Value {
v, _ := call.Argument(0).Export()
if t, ok := v.(*timerTask); ok {
t.stopped = true
t.timer.Stop()
l.Remove(t)
}
return otto.UndefinedValue()
}
vm.Set("clearTimeout", clearTimeout)
vm.Set("clearInterval", clearTimeout)
vm.Set("clearImmediate", clearTimeout)
return nil
}