-
Notifications
You must be signed in to change notification settings - Fork 19
/
prim_channel.go
170 lines (134 loc) · 4.4 KB
/
prim_channel.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2015 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file contains the go channel primitive functions.
package golisp
import (
"fmt"
"unsafe"
)
type Channel chan *Data
func RegisterChannelPrimitives() {
MakePrimitiveFunction("make-channel", "0|1", MakeChannelImpl)
MakePrimitiveFunction("channel-write", "2", ChannelWriteImpl)
MakePrimitiveFunction("channel-read", "1", ChannelReadImpl)
MakePrimitiveFunction("channel-try-write", "2", ChannelTryWriteImpl)
MakePrimitiveFunction("channel-try-read", "1", ChannelTryReadImpl)
MakePrimitiveFunction("close-channel", "1", CloseChannelImpl)
}
func MakeChannelImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
var c Channel
if Length(args) == 1 {
lengthObj := Car(args)
if !IntegerP(lengthObj) {
err = ProcessError(fmt.Sprintf("make-channel expects an Integer as its second argument but received %s.", TypeName(TypeOf(lengthObj))), env)
return
}
channelLength := IntegerValue(lengthObj)
if channelLength < 0 {
err = ProcessError(fmt.Sprintf("channel size needs to be positive; got %d.", channelLength), env)
return
}
const maxInt = int64(int(^uint(0) >> 1))
if channelLength > maxInt {
err = ProcessError(fmt.Sprintf("channel size is too big; got %d, max is %d.", channelLength, maxInt), env)
return
}
c = make(Channel, int(channelLength))
} else {
c = make(Channel)
}
return ObjectWithTypeAndValue("Channel", unsafe.Pointer(&c)), nil
}
func ChannelWriteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
channelObj := Car(args)
if !ObjectP(channelObj) || ObjectType(channelObj) != "Channel" {
err = ProcessError(fmt.Sprintf("channel<- expects an Channel object but received %s.", ObjectType(channelObj)), env)
return
}
c := *(*Channel)(ObjectValue(channelObj))
obj := Cadr(args)
func() {
defer func() {
if e := recover(); e != nil {
err = ProcessError("channel<- tried to write to a closed channel.", env)
}
}()
c <- obj
}()
if err != nil {
return
}
return
}
func ChannelReadImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
channelObj := Car(args)
if !ObjectP(channelObj) || ObjectType(channelObj) != "Channel" {
err = ProcessError(fmt.Sprintf("<-channel expects an Channel object but received %s.", ObjectType(channelObj)), env)
return
}
c := *(*Channel)(ObjectValue(channelObj))
obj, more := <-c
return ArrayToList([]*Data{obj, BooleanWithValue(more)}), nil
}
func ChannelTryWriteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
channelObj := Car(args)
if !ObjectP(channelObj) || ObjectType(channelObj) != "Channel" {
err = ProcessError(fmt.Sprintf("channel-try-write expects an Channel object but received %s.", ObjectType(channelObj)), env)
return
}
c := *(*Channel)(ObjectValue(channelObj))
obj := Cadr(args)
writeSucceeded := true
func() {
defer func() {
if e := recover(); e != nil {
err = ProcessError("channel-try-write tried to write to a closed channel.", env)
}
}()
select {
case c <- obj:
default:
writeSucceeded = false
}
}()
if err != nil {
return
}
return BooleanWithValue(writeSucceeded), nil
}
func ChannelTryReadImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
channelObj := Car(args)
if !ObjectP(channelObj) || ObjectType(channelObj) != "Channel" {
err = ProcessError(fmt.Sprintf("<-channel expects an Channel object but received %s.", ObjectType(channelObj)), env)
return
}
c := *(*Channel)(ObjectValue(channelObj))
var obj *Data
more := true
readSucceed := true
select {
case obj, more = <-c:
default:
readSucceed = false
}
return ArrayToList([]*Data{BooleanWithValue(readSucceed), obj, BooleanWithValue(more)}), nil
}
func CloseChannelImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
channelObj := Car(args)
if !ObjectP(channelObj) || ObjectType(channelObj) != "Channel" {
err = ProcessError(fmt.Sprintf("<-channel expects an Channel object but received %s.", ObjectType(channelObj)), env)
return
}
c := *(*Channel)(ObjectValue(channelObj))
func() {
defer func() {
if e := recover(); e != nil {
err = ProcessError("channel-close tried to close a channel twice.", env)
}
}()
close(c)
}()
return
}