-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtype.go
59 lines (49 loc) · 1.1 KB
/
type.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
package luar
import (
"reflect"
"github.com/yuin/gopher-lua"
)
func checkType(L *lua.LState, idx int) reflect.Type {
ud := L.CheckUserData(idx)
return ud.Value.(reflect.Type)
}
func typeCall(L *lua.LState) int {
ref := checkType(L, 1)
var value reflect.Value
switch ref.Kind() {
case reflect.Chan:
buffer := L.OptInt(2, 0)
if buffer < 0 {
L.ArgError(2, "negative buffer size")
}
if ref.ChanDir() != reflect.BothDir {
L.RaiseError("unidirectional channel type")
}
value = reflect.MakeChan(ref, buffer)
case reflect.Map:
value = reflect.MakeMap(ref)
case reflect.Slice:
length := L.OptInt(2, 0)
capacity := L.OptInt(3, length)
if length < 0 {
L.ArgError(2, "negative length")
}
if capacity < 0 {
L.ArgError(3, "negative capacity")
}
if length > capacity {
L.RaiseError("length > capacity")
}
value = reflect.MakeSlice(ref, length, capacity)
default:
value = reflect.New(ref)
}
L.Push(New(L, value.Interface()))
return 1
}
func typeEq(L *lua.LState) int {
type1 := checkType(L, 1)
type2 := checkType(L, 2)
L.Push(lua.LBool(type1 == type2))
return 1
}