aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/olebedev/go-duktape.v3/timers.go
blob: e12ee1f2ebbf70250a32c1d96e2450b31ea5dc60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package duktape

import (
    "errors"
    "fmt"
    "time"
)

// DefineTimers defines `setTimeout`, `clearTimeout`, `setInterval`,
// `clearInterval` into global context.
func (d *Context) PushTimers() error {
    d.PushGlobalStash()
    // check if timers already exists
    if !d.HasPropString(-1, "timers") {
        d.PushObject()
        d.PutPropString(-2, "timers") // stash -> [ timers:{} ]
        d.Pop()

        d.PushGlobalGoFunction("setTimeout", setTimeout)
        d.PushGlobalGoFunction("setInterval", setInterval)
        d.PushGlobalGoFunction("clearTimeout", clearTimeout)
        d.PushGlobalGoFunction("clearInterval", clearTimeout)
        return nil
    } else {
        d.Pop()
        return errors.New("Timers are already defined")
    }
}

func (d *Context) FlushTimers() {
    d.PushGlobalStash()
    d.PushObject()
    d.PutPropString(-2, "timers") // stash -> [ timers:{} ]
    d.Pop()
}

func setTimeout(c *Context) int {
    id := c.pushTimer(0)
    timeout := c.ToNumber(1)
    if timeout < 1 {
        timeout = 1
    }
    go func(id float64) {
        <-time.After(time.Duration(timeout) * time.Millisecond)
        c.Lock()
        defer c.Unlock()
        if c.duk_context == nil {
            fmt.Println("[duktape] Warning!\nsetTimeout invokes callback after the context was destroyed.")
            return
        }

        // check if timer still exists
        c.putTimer(id)
        if c.GetType(-1).IsObject() {
            c.Pcall(0 /* nargs */)
        }
        c.dropTimer(id)
    }(id)
    c.PushNumber(id)
    return 1
}

func clearTimeout(c *Context) int {
    if c.GetType(0).IsNumber() {
        c.dropTimer(c.GetNumber(0))
        c.Pop()
    }
    return 0
}

func setInterval(c *Context) int {
    id := c.pushTimer(0)
    timeout := c.ToNumber(1)
    if timeout < 1 {
        timeout = 1
    }
    go func(id float64) {
        ticker := time.NewTicker(time.Duration(timeout) * time.Millisecond)
        for _ = range ticker.C {
            c.Lock()
            // check if duktape context exists
            if c.duk_context == nil {
                c.dropTimer(id)
                c.Pop()
                ticker.Stop()
                fmt.Println("[duktape] Warning!\nsetInterval invokes callback after the context was destroyed.")
                c.Unlock()
                continue
            }

            // check if timer still exists
            c.putTimer(id)
            if c.GetType(-1).IsObject() {
                c.Pcall(0 /* nargs */)
                c.Pop()
            } else {
                c.dropTimer(id)
                c.Pop()
                ticker.Stop()
            }
            c.Unlock()
        }
    }(id)
    c.PushNumber(id)
    return 1
}

func (d *Context) pushTimer(index int) float64 {
    id := d.timerIndex.get()

    d.PushGlobalStash()
    d.GetPropString(-1, "timers")
    d.PushNumber(id)
    d.Dup(index)
    d.PutProp(-3)
    d.Pop2()

    return id
}

func (d *Context) dropTimer(id float64) {
    d.PushGlobalStash()
    d.GetPropString(-1, "timers")
    d.PushNumber(id)
    d.DelProp(-2)
    d.Pop2()
}

func (d *Context) putTimer(id float64) {
    d.PushGlobalStash()           // stash -> [ ..., timers: { <id>: { func: true } } ]
    d.GetPropString(-1, "timers") // stash -> [ ..., timers: { <id>: { func: true } } }, { <id>: { func: true } ]
    d.PushNumber(id)
    d.GetProp(-2) // stash -> [ ..., timers: { <id>: { func: true } } }, { <id>: { func: true }, { func: true } ]
    d.Replace(-3)
    d.Pop()
}