// 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 }
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 }
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 }
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, }) }
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 }
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 }
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 }