-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.go
104 lines (83 loc) · 2.2 KB
/
lib.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
package luadoc
import (
"fmt"
"github.com/samber/lo"
lua "github.com/yuin/gopher-lua"
"strings"
)
type Lib struct {
Name string
Description string
Vars []*Var
Funcs []*Func
Classes []*Class
Libs []*Lib
}
func (l *Lib) Value(state *lua.LState) *lua.LTable {
var (
vars = make(map[string]lua.LValue)
funcs = make(map[string]lua.LGFunction)
)
for _, f := range l.Funcs {
if f.Value == nil {
panic(fmt.Sprintf("function %s.%s has no value", l.Name, f.Name))
}
for _, param := range f.Params {
if param.Type == "" {
panic(fmt.Sprintf("function %s.%s has a parameter %s with no type", l.Name, f.Name, param.Name))
}
}
for _, ret := range f.Returns {
if ret.Type == "" {
panic(fmt.Sprintf("function %s.%s has a return %s with no type", l.Name, f.Name, ret.Name))
}
}
funcs[f.Name] = f.Value
}
for _, v := range l.Vars {
if v.Value == nil {
panic(fmt.Sprintf("variable %s.%s has no value", l.Name, v.Name))
}
vars[v.Name] = v.Value
}
for _, l := range l.Libs {
vars[l.Name] = l.Value(state)
}
for _, c := range l.Classes {
var methods = make(map[string]lua.LGFunction)
for _, m := range c.Methods {
if m.Value == nil {
panic(fmt.Sprintf("method %s of class %s.%s has no value", m.Name, l.Name, c.Name))
}
for _, param := range m.Params {
if param.Type == "" {
panic(fmt.Sprintf("method %s of class %s.%s has a parameter %s with no type", m.Name, l.Name, c.Name, param.Name))
}
}
for _, ret := range m.Returns {
if ret.Type == "" {
panic(fmt.Sprintf("method %s of class %s.%s has a return %s with no type", m.Name, l.Name, c.Name, ret.Name))
}
}
methods[m.Name] = m.Value
}
mt := state.NewTypeMetatable(c.Name)
state.SetField(mt, "__index", state.SetFuncs(state.NewTable(), methods))
}
return newTable(state, vars, funcs)
}
func (l *Lib) LuaDoc() string {
var b strings.Builder
lo.Must0(templateLuaDocLib.Execute(&b, l))
return b.String()
}
func (l *Lib) Loader() lua.LGFunction {
return func(state *lua.LState) int {
libs := state.NewTable()
for _, lib := range l.Libs {
state.SetField(libs, lib.Name, lib.Value(state))
}
state.Push(libs)
return 1
}
}