-
Notifications
You must be signed in to change notification settings - Fork 9
/
os.go
146 lines (135 loc) · 3.56 KB
/
os.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
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
136
137
138
139
140
141
142
143
144
145
146
package library
import (
"os"
"path/filepath"
"strings"
lua "github.com/anaminus/gopher-lua"
"github.com/anaminus/rbxmk"
"github.com/anaminus/rbxmk/dump"
"github.com/anaminus/rbxmk/dump/dt"
)
func init() { register(OS, 10) }
var OS = rbxmk.Library{
Name: "os",
ImportedAs: "os",
Open: openOS,
Dump: dumpOS,
}
func openOS(s rbxmk.State) *lua.LTable {
lib := s.L.CreateTable(0, 4)
lib.RawSetString("expand", s.WrapFunc(osExpand))
lib.RawSetString("getenv", s.WrapFunc(osGetenv))
lib.RawSetString("join", s.WrapFunc(osJoin))
lib.RawSetString("split", s.WrapFunc(osSplit))
return lib
}
func osExpand(s rbxmk.State) int {
path := s.CheckString(1)
expanded := s.World.Expand(path)
s.L.Push(lua.LString(expanded))
return 1
}
func osGetenv(s rbxmk.State) int {
switch lv := s.L.Get(1).(type) {
case *lua.LNilType:
vars := os.Environ()
table := s.L.CreateTable(0, len(vars))
for _, v := range vars {
if i := strings.IndexByte(v, '='); i >= 0 {
table.RawSetString(v[:i], lua.LString(v[i+1:]))
continue
}
// Shouldn't happen, but just in case, set the whole variable to an
// empty string.
table.RawSetString(v, lua.LString(""))
}
s.L.Push(table)
return 1
case lua.LString:
if value, ok := os.LookupEnv(s.CheckString(1)); ok {
s.L.Push(lua.LString(value))
return 1
}
s.L.Push(lua.LNil)
return 1
default:
return s.TypeError(1, "string or nil", lv.Type().String())
}
}
func osJoin(s rbxmk.State) int {
j := make([]string, s.Count())
for i := 1; i <= s.Count(); i++ {
j[i-1] = s.CheckString(i)
}
filename := filepath.Join(j...)
s.L.Push(lua.LString(filename))
return 1
}
func osSplit(s rbxmk.State) int {
path := s.CheckString(1)
n := s.Count()
components := make([]string, n-1)
for i := 2; i <= n; i++ {
components[i-2] = s.CheckString(i)
}
components, err := s.World.Split(path, components...)
if err != nil {
return s.RaiseError("%s", err)
}
for _, comp := range components {
s.L.Push(lua.LString(comp))
}
return n - 1
}
func dumpOS(s rbxmk.State) dump.Library {
return dump.Library{
Struct: dump.Struct{
Fields: dump.Fields{
"expand": dump.Function{
Parameters: dump.Parameters{
{Name: "path", Type: dt.Prim("string")},
},
Returns: dump.Parameters{
{Type: dt.Prim("string")},
},
Summary: "Libraries/os:Fields/path/Summary",
Description: "Libraries/os:Fields/path/Description",
},
"getenv": dump.Function{
Parameters: dump.Parameters{
{Name: "name", Type: dt.Optional{T: dt.Prim("string")}},
},
Returns: dump.Parameters{
{Type: dt.Or{dt.Optional{T: dt.Prim("string")}, dt.Dictionary{V: dt.Prim("string")}}},
},
Summary: "Libraries/os:Fields/getenv/Summary",
Description: "Libraries/os:Fields/getenv/Description",
},
"join": dump.Function{
Parameters: dump.Parameters{
{Name: "...", Type: dt.Prim("string")},
},
Returns: dump.Parameters{
{Type: dt.Prim("string")},
},
Summary: "Libraries/os:Fields/join/Summary",
Description: "Libraries/os:Fields/join/Description",
},
"split": dump.Function{
Parameters: dump.Parameters{
{Name: "path", Type: dt.Prim("string")},
{Name: "...", Type: dt.Prim("string")},
},
Returns: dump.Parameters{
{Name: "...", Type: dt.Prim("string")},
},
CanError: true,
Summary: "Libraries/os:Fields/split/Summary",
Description: "Libraries/os:Fields/split/Description",
},
},
Summary: "Libraries/os:Summary",
Description: "Libraries/os:Description",
},
}
}