-
Notifications
You must be signed in to change notification settings - Fork 22
/
spin.go
126 lines (102 loc) · 3.34 KB
/
spin.go
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
package macros
import (
"log"
"sync"
. "github.com/kocircuit/kocircuit/lang/circuit/eval"
. "github.com/kocircuit/kocircuit/lang/circuit/model"
. "github.com/kocircuit/kocircuit/lang/go/eval"
. "github.com/kocircuit/kocircuit/lang/go/eval/symbol"
. "github.com/kocircuit/kocircuit/lang/go/kit/util"
)
func init() {
RegisterEvalMacro("Spin", new(EvalSpinMacro))
}
const spinDoc = `
The builtin Spin function executes an argument function in a new co-routine.
Spin expects a single unnamed variety (functional value) argument.
It creates a new co-routine and executes its argument function there.
Once execution commences Spin returns a "handle" structure.
The handle structure contains a single field, called Wait, which holds a functional value.
Calling Wait will block until the underlying spawned execution completes.
When it does, Wait returns the value returned by the spawned function.
If the spawned execution panics, Wait will reproduce that same panic (and its value).`
type EvalSpinMacro struct{}
func (m EvalSpinMacro) MacroID() string { return m.Help() }
func (m EvalSpinMacro) Label() string { return "spin" }
func (m EvalSpinMacro) MacroSheathString() *string { return PtrString("Spin") }
func (m EvalSpinMacro) Help() string { return "Spin" }
func (m EvalSpinMacro) Doc() string { return spinDoc }
func (EvalSpinMacro) Invoke(span *Span, arg Arg) (returns Return, effect Effect, err error) {
if vty, ok := arg.(*StructSymbol).LinkField("value", true).(*VarietySymbol); !ok {
return nil, nil, span.Errorf(nil, "spin expects a variety, got %v", arg)
} else {
done := make(chan *waitResult, 1)
go func() {
defer func() {
if r := recover(); r != nil {
evalPanic := r.(*EvalPanic)
log.Println(
evalPanic.Origin.Errorf(nil, "panic inside spin: %v", evalPanic.Panic),
)
done <- &waitResult{Panic: evalPanic, Returned: EmptySymbol{}}
close(done)
}
}()
if returned, _, invErr := vty.Invoke(span); invErr != nil {
log.Printf("spin error (%v)", invErr)
done <- &waitResult{Error: invErr, Returned: EmptySymbol{}}
} else {
done <- &waitResult{Returned: returned.(Symbol)}
}
close(done)
}()
return MakeStructSymbol(
FieldSymbols{
{
Name: "Wait",
Value: MakeVarietySymbol(
&evalWaitMacro{waiter: &waiter{done: done}},
nil,
),
},
},
), nil, nil
}
}
type waitResult struct {
Panic *EvalPanic `ko:"name=panic"`
Error error `ko:"name=error"`
Returned Symbol `ko:"name=returned"`
}
type waiter struct {
sync.Mutex
done chan *waitResult
result *waitResult
}
func (w *waiter) Wait() *waitResult {
w.Lock()
defer w.Unlock()
if wr, ok := <-w.done; ok {
w.result = wr
}
return w.result
}
type evalWaitMacro struct {
waiter *waiter
}
func (m *evalWaitMacro) MacroID() string { return m.Help() }
func (m *evalWaitMacro) Label() string { return "wait" }
func (m *evalWaitMacro) MacroSheathString() *string { return PtrString("Wait") }
func (m *evalWaitMacro) Help() string { return "Wait" }
func (m *evalWaitMacro) Doc() string { return spinDoc }
func (m *evalWaitMacro) Invoke(span *Span, arg Arg) (returns Return, effect Effect, err error) {
wr := m.waiter.Wait()
switch {
case wr.Error != nil:
return nil, nil, span.Errorf(err, "spinned function error")
case wr.Panic != nil:
panic(wr.Panic)
default:
return wr.Returned, nil, nil
}
}