-
Notifications
You must be signed in to change notification settings - Fork 0
/
glu_mod.go
86 lines (82 loc) · 1.66 KB
/
glu_mod.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
package glu
import (
. "github.com/yuin/gopher-lua"
"strings"
)
var (
//BaseMod the global module
BaseMod = glu(map[string]string{})
)
type (
Chunk = *FunctionProto
glu map[string]string
)
func (c glu) TopLevel() bool {
return true
}
func (c glu) CheckChunk(s *LState, n int) Chunk {
ud := s.CheckUserData(n)
if v, ok := ud.Value.(Chunk); ok {
return v
}
s.ArgError(n, "chunk expected")
return nil
}
func (c glu) PreLoad(l *LState) {
l.SetGlobal("chunk", l.NewFunction(func(s *LState) int {
chunk, err := CompileChunk(s.CheckString(1), s.CheckString(2))
if err != nil {
s.Push(LNil)
s.Push(LString(err.Error()))
return 2
}
ud := s.NewUserData()
ud.Value = chunk
s.Push(ud)
s.Push(LNil)
return 2
}))
l.SetGlobal(HelpFunc, l.NewFunction(func(s *LState) int {
if s.GetTop() < 1 {
if i, ok := c["mod"]; ok {
s.Push(LString(i))
return 1
}
sub := new(strings.Builder)
sub.WriteString(HelpHelp)
sub.WriteString("\nPreload modules:\n")
keys := make([]string, 0)
l.G.Global.
RawGetString("package").(*LTable).
RawGetString("preload").(*LTable).
ForEach(func(k LValue, _ LValue) {
sub.WriteString(k.String() + "\n")
keys = append(keys, k.String())
})
sub.WriteString("\nLoaded modules:\n")
out:
for name := range moduleNames {
for _, n := range keys {
if n == name {
continue out
}
}
sub.WriteString(name + "\n")
}
i := sub.String()
s.Push(LString(i))
c["mod"] = i
return 1
}
t := s.CheckString(1)
switch t {
case HelpKey:
s.Push(LString(HelpTopic))
case "chunk":
s.Push(LString(HelpChunk))
default:
s.Push(LNil)
}
return 1
}))
}