forked from layeh/gopher-luar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchan.go
68 lines (56 loc) · 1.16 KB
/
chan.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
package luar
import (
"reflect"
"github.com/yuin/gopher-lua"
)
func chanIndex(L *lua.LState) int {
_, mt, isPtr := check(L, 1, reflect.Chan)
key := L.CheckString(2)
if !isPtr {
if fn := mt.method(key); fn != nil {
L.Push(fn)
return 1
}
}
if fn := mt.ptrMethod(key); fn != nil {
L.Push(fn)
return 1
}
return 0
}
func chanLen(L *lua.LState) int {
ref, _, isPtr := check(L, 1, reflect.Chan)
if isPtr {
L.RaiseError("invalid operation on chan pointer")
}
L.Push(lua.LNumber(ref.Len()))
return 1
}
// chan methods
func chanSend(L *lua.LState) int {
ref, _, _ := check(L, 1, reflect.Chan)
value := L.CheckAny(2)
convertedValue := lValueToReflect(L, value, ref.Type().Elem(), nil)
if convertedValue.Type() != ref.Type().Elem() {
L.ArgError(2, "incorrect type")
}
ref.Send(convertedValue)
return 0
}
func chanReceive(L *lua.LState) int {
ref, _, _ := check(L, 1, reflect.Chan)
value, ok := ref.Recv()
if !ok {
L.Push(lua.LNil)
L.Push(lua.LBool(false))
return 2
}
L.Push(New(L, value.Interface()))
L.Push(lua.LBool(true))
return 2
}
func chanClose(L *lua.LState) int {
ref, _, _ := check(L, 1, reflect.Chan)
ref.Close()
return 0
}