-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablelib.go
More file actions
135 lines (122 loc) · 2.58 KB
/
Copy pathtablelib.go
File metadata and controls
135 lines (122 loc) · 2.58 KB
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
package lua
import "sort"
var tableFuncs = map[string]LGoFunc{
"getn": tableGetN,
"concat": tableConcat,
"insert": tableInsert,
"maxn": tableMaxN,
"remove": tableRemove,
"sort": tableSort,
"create": tableCreate,
"freeze": tableMakeImmutable,
"isfrozen": tableIsImmutable,
"unpack": baseUnpack,
}
func OpenTable(L *LState) int {
mod := L.RegisterGoModule(TabLibName, tableFuncs).(*LTable)
L.Push(mod)
return 1
}
func tableSort(L *LState) int {
tbl := L.CheckTable(1)
if tbl.Immutable {
L.RaiseError("attempt to sort Immutable table")
return 0
}
sorter := lValueArraySorter{L, nil, tbl.Array}
if L.GetTop() != 1 {
sorter.Fn = L.CheckFunction(2)
}
sort.Sort(sorter)
return 0
}
func tableCreate(L *LState) int {
acap := L.CheckInt(1)
hcap := L.CheckInt(2)
tbl := CreateTable(acap, hcap)
L.Push(tbl)
return 1
}
func tableMakeImmutable(L *LState) int {
tbl := L.CheckTable(1)
tbl.Immutable = true
L.Push(tbl)
return 1
}
func tableIsImmutable(L *LState) int {
tbl := L.CheckTable(1)
L.Push(LBool(tbl.Immutable))
return 1
}
func tableGetN(L *LState) int {
L.Push(LNumber(L.CheckTable(1).Len()))
return 1
}
func tableMaxN(L *LState) int {
L.Push(LNumber(L.CheckTable(1).MaxN()))
return 1
}
func tableRemove(L *LState) int {
tbl := L.CheckTable(1)
var removed LValue
var success bool
if L.GetTop() == 1 {
removed, success = tbl.Remove(-1)
} else {
removed, success = tbl.Remove(L.CheckInt(2))
}
if !success {
L.RaiseError("attempt to remove from Immutable table")
return 0
}
L.Push(removed)
return 1
}
func tableConcat(L *LState) int {
tbl := L.CheckTable(1)
sep := LString(L.OptString(2, ""))
i := L.OptInt(3, 1)
j := L.OptInt(4, tbl.Len())
if L.GetTop() == 3 {
if i > tbl.Len() || i < 1 {
L.Push(emptyLString)
return 1
}
}
i = intMax(intMin(i, tbl.Len()), 1)
j = intMin(intMin(j, tbl.Len()), tbl.Len())
if i > j {
L.Push(emptyLString)
return 1
}
retbottom := L.GetTop()
for ; i <= j; i++ {
v := tbl.RawGetInt(i)
if !LVCanConvToString(v) {
L.RaiseError("invalid value (%s) at index %d in table for concat", v.Type().String(), i)
}
L.Push(v)
if i != j {
L.Push(sep)
}
}
L.Push(stringConcat(L, L.GetTop()-retbottom, L.reg.Top()-1))
return 1
}
func tableInsert(L *LState) int {
tbl := L.CheckTable(1)
nargs := L.GetTop()
if nargs == 1 {
L.RaiseError("wrong number of arguments")
}
var success bool
if L.GetTop() == 2 {
success = tbl.Append(L.Get(2))
} else {
success = tbl.Insert(L.CheckInt(2), L.CheckAny(3))
}
if !success {
L.RaiseError("attempt to insert into Immutable table")
}
return 0
}