Debian Codegraph @

internal/lua/lua.go

raw
1 package lua
2
3 import (
4 "archive/zip"
5 "bytes"
6 "errors"
7 "fmt"
8 "io"
9 "io/ioutil"
10 "math"
11 "math/rand"
12 "net"
13 "net/http"
14 "os"
15 "path"
16 "path/filepath"
17 "regexp"
18 "runtime"
19 "strings"
20 "time"
21 "unicode/utf8"
22
23 humanize "github.com/dustin/go-humanize"
24 lua "github.com/yuin/gopher-lua"
25 luar "layeh.com/gopher-luar"
26 )
27
28 var L *lua.LState
29
30 // LoadFile loads a lua file
31 func LoadFile(module string, file string, data []byte) error {
32 pluginDef := []byte("module(\"" + module + "\", package.seeall)")
33
34 if fn, err := L.Load(bytes.NewReader(append(pluginDef, data...)), file); err != nil {
35 return err
36 } else {
37 L.Push(fn)
38 return L.PCall(0, lua.MultRet, nil)
39 }
40 }
41
42 // Import allows a lua plugin to import a package
43 func Import(pkg string) *lua.LTable {
44 switch pkg {
45 case "fmt":
46 return importFmt()
47 case "io":
48 return importIo()
49 case "io/ioutil", "ioutil":
50 return importIoUtil()
51 case "net":
52 return importNet()
53 case "math":
54 return importMath()
55 case "math/rand":
56 return importMathRand()
57 case "os":
58 return importOs()
59 case "runtime":
60 return importRuntime()
61 case "path":
62 return importPath()
63 case "path/filepath", "filepath":
64 return importFilePath()
65 case "strings":
66 return importStrings()
67 case "regexp":
68 return importRegexp()
69 case "errors":
70 return importErrors()
71 case "time":
72 return importTime()
73 case "unicode/utf8", "utf8":
74 return importUtf8()
75 case "humanize":
76 return importHumanize()
77 case "net/http", "http":
78 return importHTTP()
79 case "archive/zip":
80 return importArchiveZip()
81 default:
82 return nil
83 }
84 }
85
86 func importFmt() *lua.LTable {
87 pkg := L.NewTable()
88
89 L.SetField(pkg, "Errorf", luar.New(L, fmt.Errorf))
90 L.SetField(pkg, "Fprint", luar.New(L, fmt.Fprint))
91 L.SetField(pkg, "Fprintf", luar.New(L, fmt.Fprintf))
92 L.SetField(pkg, "Fprintln", luar.New(L, fmt.Fprintln))
93 L.SetField(pkg, "Fscan", luar.New(L, fmt.Fscan))
94 L.SetField(pkg, "Fscanf", luar.New(L, fmt.Fscanf))
95 L.SetField(pkg, "Fscanln", luar.New(L, fmt.Fscanln))
96 L.SetField(pkg, "Print", luar.New(L, fmt.Print))
97 L.SetField(pkg, "Printf", luar.New(L, fmt.Printf))
98 L.SetField(pkg, "Println", luar.New(L, fmt.Println))
99 L.SetField(pkg, "Scan", luar.New(L, fmt.Scan))
100 L.SetField(pkg, "Scanf", luar.New(L, fmt.Scanf))
101 L.SetField(pkg, "Scanln", luar.New(L, fmt.Scanln))
102 L.SetField(pkg, "Sprint", luar.New(L, fmt.Sprint))
103 L.SetField(pkg, "Sprintf", luar.New(L, fmt.Sprintf))
104 L.SetField(pkg, "Sprintln", luar.New(L, fmt.Sprintln))
105 L.SetField(pkg, "Sscan", luar.New(L, fmt.Sscan))
106 L.SetField(pkg, "Sscanf", luar.New(L, fmt.Sscanf))
107 L.SetField(pkg, "Sscanln", luar.New(L, fmt.Sscanln))
108
109 return pkg
110 }
111
112 func importIo() *lua.LTable {
113 pkg := L.NewTable()
114
115 L.SetField(pkg, "Copy", luar.New(L, io.Copy))
116 L.SetField(pkg, "CopyN", luar.New(L, io.CopyN))
117 L.SetField(pkg, "EOF", luar.New(L, io.EOF))
118 L.SetField(pkg, "ErrClosedPipe", luar.New(L, io.ErrClosedPipe))
119 L.SetField(pkg, "ErrNoProgress", luar.New(L, io.ErrNoProgress))
120 L.SetField(pkg, "ErrShortBuffer", luar.New(L, io.ErrShortBuffer))
121 L.SetField(pkg, "ErrShortWrite", luar.New(L, io.ErrShortWrite))
122 L.SetField(pkg, "ErrUnexpectedEOF", luar.New(L, io.ErrUnexpectedEOF))
123 L.SetField(pkg, "LimitReader", luar.New(L, io.LimitReader))
124 L.SetField(pkg, "MultiReader", luar.New(L, io.MultiReader))
125 L.SetField(pkg, "MultiWriter", luar.New(L, io.MultiWriter))
126 L.SetField(pkg, "NewSectionReader", luar.New(L, io.NewSectionReader))
127 L.SetField(pkg, "Pipe", luar.New(L, io.Pipe))
128 L.SetField(pkg, "ReadAll", luar.New(L, io.ReadAll))
129 L.SetField(pkg, "ReadAtLeast", luar.New(L, io.ReadAtLeast))
130 L.SetField(pkg, "ReadFull", luar.New(L, io.ReadFull))
131 L.SetField(pkg, "TeeReader", luar.New(L, io.TeeReader))
132 L.SetField(pkg, "WriteString", luar.New(L, io.WriteString))
133
134 return pkg
135 }
136
137 func importIoUtil() *lua.LTable {
138 pkg := L.NewTable()
139
140 L.SetField(pkg, "ReadAll", luar.New(L, ioutil.ReadAll))
141 L.SetField(pkg, "ReadDir", luar.New(L, ioutil.ReadDir))
142 L.SetField(pkg, "ReadFile", luar.New(L, ioutil.ReadFile))
143 L.SetField(pkg, "WriteFile", luar.New(L, ioutil.WriteFile))
144
145 return pkg
146 }
147
148 func importNet() *lua.LTable {
149 pkg := L.NewTable()
150
151 L.SetField(pkg, "CIDRMask", luar.New(L, net.CIDRMask))
152 L.SetField(pkg, "Dial", luar.New(L, net.Dial))
153 L.SetField(pkg, "DialIP", luar.New(L, net.DialIP))
154 L.SetField(pkg, "DialTCP", luar.New(L, net.DialTCP))
155 L.SetField(pkg, "DialTimeout", luar.New(L, net.DialTimeout))
156 L.SetField(pkg, "DialUDP", luar.New(L, net.DialUDP))
157 L.SetField(pkg, "DialUnix", luar.New(L, net.DialUnix))
158 L.SetField(pkg, "ErrWriteToConnected", luar.New(L, net.ErrWriteToConnected))
159 L.SetField(pkg, "FileConn", luar.New(L, net.FileConn))
160 L.SetField(pkg, "FileListener", luar.New(L, net.FileListener))
161 L.SetField(pkg, "FilePacketConn", luar.New(L, net.FilePacketConn))
162 L.SetField(pkg, "FlagBroadcast", luar.New(L, net.FlagBroadcast))
163 L.SetField(pkg, "FlagLoopback", luar.New(L, net.FlagLoopback))
164 L.SetField(pkg, "FlagMulticast", luar.New(L, net.FlagMulticast))
165 L.SetField(pkg, "FlagPointToPoint", luar.New(L, net.FlagPointToPoint))
166 L.SetField(pkg, "FlagUp", luar.New(L, net.FlagUp))
167 L.SetField(pkg, "IPv4", luar.New(L, net.IPv4))
168 L.SetField(pkg, "IPv4Mask", luar.New(L, net.IPv4Mask))
169 L.SetField(pkg, "IPv4allrouter", luar.New(L, net.IPv4allrouter))
170 L.SetField(pkg, "IPv4allsys", luar.New(L, net.IPv4allsys))
171 L.SetField(pkg, "IPv4bcast", luar.New(L, net.IPv4bcast))
172 L.SetField(pkg, "IPv4len", luar.New(L, net.IPv4len))
173 L.SetField(pkg, "IPv4zero", luar.New(L, net.IPv4zero))
174 L.SetField(pkg, "IPv6interfacelocalallnodes", luar.New(L, net.IPv6interfacelocalallnodes))
175 L.SetField(pkg, "IPv6len", luar.New(L, net.IPv6len))
176 L.SetField(pkg, "IPv6linklocalallnodes", luar.New(L, net.IPv6linklocalallnodes))
177 L.SetField(pkg, "IPv6linklocalallrouters", luar.New(L, net.IPv6linklocalallrouters))
178 L.SetField(pkg, "IPv6loopback", luar.New(L, net.IPv6loopback))
179 L.SetField(pkg, "IPv6unspecified", luar.New(L, net.IPv6unspecified))
180 L.SetField(pkg, "IPv6zero", luar.New(L, net.IPv6zero))
181 L.SetField(pkg, "InterfaceAddrs", luar.New(L, net.InterfaceAddrs))
182 L.SetField(pkg, "InterfaceByIndex", luar.New(L, net.InterfaceByIndex))
183 L.SetField(pkg, "InterfaceByName", luar.New(L, net.InterfaceByName))
184 L.SetField(pkg, "Interfaces", luar.New(L, net.Interfaces))
185 L.SetField(pkg, "JoinHostPort", luar.New(L, net.JoinHostPort))
186 L.SetField(pkg, "Listen", luar.New(L, net.Listen))
187 L.SetField(pkg, "ListenIP", luar.New(L, net.ListenIP))
188 L.SetField(pkg, "ListenMulticastUDP", luar.New(L, net.ListenMulticastUDP))
189 L.SetField(pkg, "ListenPacket", luar.New(L, net.ListenPacket))
190 L.SetField(pkg, "ListenTCP", luar.New(L, net.ListenTCP))
191 L.SetField(pkg, "ListenUDP", luar.New(L, net.ListenUDP))
192 L.SetField(pkg, "ListenUnix", luar.New(L, net.ListenUnix))
193 L.SetField(pkg, "ListenUnixgram", luar.New(L, net.ListenUnixgram))
194 L.SetField(pkg, "LookupAddr", luar.New(L, net.LookupAddr))
195 L.SetField(pkg, "LookupCNAME", luar.New(L, net.LookupCNAME))
196 L.SetField(pkg, "LookupHost", luar.New(L, net.LookupHost))
197 L.SetField(pkg, "LookupIP", luar.New(L, net.LookupIP))
198 L.SetField(pkg, "LookupMX", luar.New(L, net.LookupMX))
199 L.SetField(pkg, "LookupNS", luar.New(L, net.LookupNS))
200 L.SetField(pkg, "LookupPort", luar.New(L, net.LookupPort))
201 L.SetField(pkg, "LookupSRV", luar.New(L, net.LookupSRV))
202 L.SetField(pkg, "LookupTXT", luar.New(L, net.LookupTXT))
203 L.SetField(pkg, "ParseCIDR", luar.New(L, net.ParseCIDR))
204 L.SetField(pkg, "ParseIP", luar.New(L, net.ParseIP))
205 L.SetField(pkg, "ParseMAC", luar.New(L, net.ParseMAC))
206 L.SetField(pkg, "Pipe", luar.New(L, net.Pipe))
207 L.SetField(pkg, "ResolveIPAddr", luar.New(L, net.ResolveIPAddr))
208 L.SetField(pkg, "ResolveTCPAddr", luar.New(L, net.ResolveTCPAddr))
209 L.SetField(pkg, "ResolveUDPAddr", luar.New(L, net.ResolveUDPAddr))
210 L.SetField(pkg, "ResolveUnixAddr", luar.New(L, net.ResolveUnixAddr))
211 L.SetField(pkg, "SplitHostPort", luar.New(L, net.SplitHostPort))
212
213 return pkg
214 }
215
216 func importMath() *lua.LTable {
217 pkg := L.NewTable()
218
219 L.SetField(pkg, "Abs", luar.New(L, math.Abs))
220 L.SetField(pkg, "Acos", luar.New(L, math.Acos))
221 L.SetField(pkg, "Acosh", luar.New(L, math.Acosh))
222 L.SetField(pkg, "Asin", luar.New(L, math.Asin))
223 L.SetField(pkg, "Asinh", luar.New(L, math.Asinh))
224 L.SetField(pkg, "Atan", luar.New(L, math.Atan))
225 L.SetField(pkg, "Atan2", luar.New(L, math.Atan2))
226 L.SetField(pkg, "Atanh", luar.New(L, math.Atanh))
227 L.SetField(pkg, "Cbrt", luar.New(L, math.Cbrt))
228 L.SetField(pkg, "Ceil", luar.New(L, math.Ceil))
229 L.SetField(pkg, "Copysign", luar.New(L, math.Copysign))
230 L.SetField(pkg, "Cos", luar.New(L, math.Cos))
231 L.SetField(pkg, "Cosh", luar.New(L, math.Cosh))
232 L.SetField(pkg, "Dim", luar.New(L, math.Dim))
233 L.SetField(pkg, "Erf", luar.New(L, math.Erf))
234 L.SetField(pkg, "Erfc", luar.New(L, math.Erfc))
235 L.SetField(pkg, "Exp", luar.New(L, math.Exp))
236 L.SetField(pkg, "Exp2", luar.New(L, math.Exp2))
237 L.SetField(pkg, "Expm1", luar.New(L, math.Expm1))
238 L.SetField(pkg, "Float32bits", luar.New(L, math.Float32bits))
239 L.SetField(pkg, "Float32frombits", luar.New(L, math.Float32frombits))
240 L.SetField(pkg, "Float64bits", luar.New(L, math.Float64bits))
241 L.SetField(pkg, "Float64frombits", luar.New(L, math.Float64frombits))
242 L.SetField(pkg, "Floor", luar.New(L, math.Floor))
243 L.SetField(pkg, "Frexp", luar.New(L, math.Frexp))
244 L.SetField(pkg, "Gamma", luar.New(L, math.Gamma))
245 L.SetField(pkg, "Hypot", luar.New(L, math.Hypot))
246 L.SetField(pkg, "Ilogb", luar.New(L, math.Ilogb))
247 L.SetField(pkg, "Inf", luar.New(L, math.Inf))
248 L.SetField(pkg, "IsInf", luar.New(L, math.IsInf))
249 L.SetField(pkg, "IsNaN", luar.New(L, math.IsNaN))
250 L.SetField(pkg, "J0", luar.New(L, math.J0))
251 L.SetField(pkg, "J1", luar.New(L, math.J1))
252 L.SetField(pkg, "Jn", luar.New(L, math.Jn))
253 L.SetField(pkg, "Ldexp", luar.New(L, math.Ldexp))
254 L.SetField(pkg, "Lgamma", luar.New(L, math.Lgamma))
255 L.SetField(pkg, "Log", luar.New(L, math.Log))
256 L.SetField(pkg, "Log10", luar.New(L, math.Log10))
257 L.SetField(pkg, "Log1p", luar.New(L, math.Log1p))
258 L.SetField(pkg, "Log2", luar.New(L, math.Log2))
259 L.SetField(pkg, "Logb", luar.New(L, math.Logb))
260 L.SetField(pkg, "Max", luar.New(L, math.Max))
261 L.SetField(pkg, "Min", luar.New(L, math.Min))
262 L.SetField(pkg, "Mod", luar.New(L, math.Mod))
263 L.SetField(pkg, "Modf", luar.New(L, math.Modf))
264 L.SetField(pkg, "NaN", luar.New(L, math.NaN))
265 L.SetField(pkg, "Nextafter", luar.New(L, math.Nextafter))
266 L.SetField(pkg, "Pow", luar.New(L, math.Pow))
267 L.SetField(pkg, "Pow10", luar.New(L, math.Pow10))
268 L.SetField(pkg, "Remainder", luar.New(L, math.Remainder))
269 L.SetField(pkg, "Signbit", luar.New(L, math.Signbit))
270 L.SetField(pkg, "Sin", luar.New(L, math.Sin))
271 L.SetField(pkg, "Sincos", luar.New(L, math.Sincos))
272 L.SetField(pkg, "Sinh", luar.New(L, math.Sinh))
273 L.SetField(pkg, "Sqrt", luar.New(L, math.Sqrt))
274 L.SetField(pkg, "Tan", luar.New(L, math.Tan))
275 L.SetField(pkg, "Tanh", luar.New(L, math.Tanh))
276 L.SetField(pkg, "Trunc", luar.New(L, math.Trunc))
277 L.SetField(pkg, "Y0", luar.New(L, math.Y0))
278 L.SetField(pkg, "Y1", luar.New(L, math.Y1))
279 L.SetField(pkg, "Yn", luar.New(L, math.Yn))
280
281 return pkg
282 }
283
284 func importMathRand() *lua.LTable {
285 pkg := L.NewTable()
286
287 L.SetField(pkg, "ExpFloat64", luar.New(L, rand.ExpFloat64))
288 L.SetField(pkg, "Float32", luar.New(L, rand.Float32))
289 L.SetField(pkg, "Float64", luar.New(L, rand.Float64))
290 L.SetField(pkg, "Int", luar.New(L, rand.Int))
291 L.SetField(pkg, "Int31", luar.New(L, rand.Int31))
292 L.SetField(pkg, "Int31n", luar.New(L, rand.Int31n))
293 L.SetField(pkg, "Int63", luar.New(L, rand.Int63))
294 L.SetField(pkg, "Int63n", luar.New(L, rand.Int63n))
295 L.SetField(pkg, "Intn", luar.New(L, rand.Intn))
296 L.SetField(pkg, "NormFloat64", luar.New(L, rand.NormFloat64))
297 L.SetField(pkg, "Perm", luar.New(L, rand.Perm))
298 L.SetField(pkg, "Seed", luar.New(L, rand.Seed))
299 L.SetField(pkg, "Uint32", luar.New(L, rand.Uint32))
300
301 return pkg
302 }
303
304 func importOs() *lua.LTable {
305 pkg := L.NewTable()
306
307 L.SetField(pkg, "Args", luar.New(L, os.Args))
308 L.SetField(pkg, "Chdir", luar.New(L, os.Chdir))
309 L.SetField(pkg, "Chmod", luar.New(L, os.Chmod))
310 L.SetField(pkg, "Chown", luar.New(L, os.Chown))
311 L.SetField(pkg, "Chtimes", luar.New(L, os.Chtimes))
312 L.SetField(pkg, "Clearenv", luar.New(L, os.Clearenv))
313 L.SetField(pkg, "Create", luar.New(L, os.Create))
314 L.SetField(pkg, "DevNull", luar.New(L, os.DevNull))
315 L.SetField(pkg, "Environ", luar.New(L, os.Environ))
316 L.SetField(pkg, "ErrExist", luar.New(L, os.ErrExist))
317 L.SetField(pkg, "ErrInvalid", luar.New(L, os.ErrInvalid))
318 L.SetField(pkg, "ErrNotExist", luar.New(L, os.ErrNotExist))
319 L.SetField(pkg, "ErrPermission", luar.New(L, os.ErrPermission))
320 L.SetField(pkg, "Exit", luar.New(L, os.Exit))
321 L.SetField(pkg, "Expand", luar.New(L, os.Expand))
322 L.SetField(pkg, "ExpandEnv", luar.New(L, os.ExpandEnv))
323 L.SetField(pkg, "FindProcess", luar.New(L, os.FindProcess))
324 L.SetField(pkg, "Getegid", luar.New(L, os.Getegid))
325 L.SetField(pkg, "Getenv", luar.New(L, os.Getenv))
326 L.SetField(pkg, "Geteuid", luar.New(L, os.Geteuid))
327 L.SetField(pkg, "Getgid", luar.New(L, os.Getgid))
328 L.SetField(pkg, "Getgroups", luar.New(L, os.Getgroups))
329 L.SetField(pkg, "Getpagesize", luar.New(L, os.Getpagesize))
330 L.SetField(pkg, "Getpid", luar.New(L, os.Getpid))
331 L.SetField(pkg, "Getuid", luar.New(L, os.Getuid))
332 L.SetField(pkg, "Getwd", luar.New(L, os.Getwd))
333 L.SetField(pkg, "Hostname", luar.New(L, os.Hostname))
334 L.SetField(pkg, "Interrupt", luar.New(L, os.Interrupt))
335 L.SetField(pkg, "IsExist", luar.New(L, os.IsExist))
336 L.SetField(pkg, "IsNotExist", luar.New(L, os.IsNotExist))
337 L.SetField(pkg, "IsPathSeparator", luar.New(L, os.IsPathSeparator))
338 L.SetField(pkg, "IsPermission", luar.New(L, os.IsPermission))
339 L.SetField(pkg, "Kill", luar.New(L, os.Kill))
340 L.SetField(pkg, "Lchown", luar.New(L, os.Lchown))
341 L.SetField(pkg, "Link", luar.New(L, os.Link))
342 L.SetField(pkg, "Lstat", luar.New(L, os.Lstat))
343 L.SetField(pkg, "Mkdir", luar.New(L, os.Mkdir))
344 L.SetField(pkg, "MkdirAll", luar.New(L, os.MkdirAll))
345 L.SetField(pkg, "ModeAppend", luar.New(L, os.ModeAppend))
346 L.SetField(pkg, "ModeCharDevice", luar.New(L, os.ModeCharDevice))
347 L.SetField(pkg, "ModeDevice", luar.New(L, os.ModeDevice))
348 L.SetField(pkg, "ModeDir", luar.New(L, os.ModeDir))
349 L.SetField(pkg, "ModeExclusive", luar.New(L, os.ModeExclusive))
350 L.SetField(pkg, "ModeNamedPipe", luar.New(L, os.ModeNamedPipe))
351 L.SetField(pkg, "ModePerm", luar.New(L, os.ModePerm))
352 L.SetField(pkg, "ModeSetgid", luar.New(L, os.ModeSetgid))
353 L.SetField(pkg, "ModeSetuid", luar.New(L, os.ModeSetuid))
354 L.SetField(pkg, "ModeSocket", luar.New(L, os.ModeSocket))
355 L.SetField(pkg, "ModeSticky", luar.New(L, os.ModeSticky))
356 L.SetField(pkg, "ModeSymlink", luar.New(L, os.ModeSymlink))
357 L.SetField(pkg, "ModeTemporary", luar.New(L, os.ModeTemporary))
358 L.SetField(pkg, "ModeType", luar.New(L, os.ModeType))
359 L.SetField(pkg, "NewFile", luar.New(L, os.NewFile))
360 L.SetField(pkg, "NewSyscallError", luar.New(L, os.NewSyscallError))
361 L.SetField(pkg, "O_APPEND", luar.New(L, os.O_APPEND))
362 L.SetField(pkg, "O_CREATE", luar.New(L, os.O_CREATE))
363 L.SetField(pkg, "O_EXCL", luar.New(L, os.O_EXCL))
364 L.SetField(pkg, "O_RDONLY", luar.New(L, os.O_RDONLY))
365 L.SetField(pkg, "O_RDWR", luar.New(L, os.O_RDWR))
366 L.SetField(pkg, "O_SYNC", luar.New(L, os.O_SYNC))
367 L.SetField(pkg, "O_TRUNC", luar.New(L, os.O_TRUNC))
368 L.SetField(pkg, "O_WRONLY", luar.New(L, os.O_WRONLY))
369 L.SetField(pkg, "Open", luar.New(L, os.Open))
370 L.SetField(pkg, "OpenFile", luar.New(L, os.OpenFile))
371 L.SetField(pkg, "PathListSeparator", luar.New(L, os.PathListSeparator))
372 L.SetField(pkg, "PathSeparator", luar.New(L, os.PathSeparator))
373 L.SetField(pkg, "Pipe", luar.New(L, os.Pipe))
374 L.SetField(pkg, "ReadDir", luar.New(L, os.ReadDir))
375 L.SetField(pkg, "ReadFile", luar.New(L, os.ReadFile))
376 L.SetField(pkg, "Readlink", luar.New(L, os.Readlink))
377 L.SetField(pkg, "Remove", luar.New(L, os.Remove))
378 L.SetField(pkg, "RemoveAll", luar.New(L, os.RemoveAll))
379 L.SetField(pkg, "Rename", luar.New(L, os.Rename))
380 L.SetField(pkg, "SEEK_CUR", luar.New(L, io.SeekCurrent))
381 L.SetField(pkg, "SEEK_END", luar.New(L, io.SeekEnd))
382 L.SetField(pkg, "SEEK_SET", luar.New(L, io.SeekStart))
383 L.SetField(pkg, "SameFile", luar.New(L, os.SameFile))
384 L.SetField(pkg, "Setenv", luar.New(L, os.Setenv))
385 L.SetField(pkg, "StartProcess", luar.New(L, os.StartProcess))
386 L.SetField(pkg, "Stat", luar.New(L, os.Stat))
387 L.SetField(pkg, "Stderr", luar.New(L, os.Stderr))
388 L.SetField(pkg, "Stdin", luar.New(L, os.Stdin))
389 L.SetField(pkg, "Stdout", luar.New(L, os.Stdout))
390 L.SetField(pkg, "Symlink", luar.New(L, os.Symlink))
391 L.SetField(pkg, "TempDir", luar.New(L, os.TempDir))
392 L.SetField(pkg, "Truncate", luar.New(L, os.Truncate))
393 L.SetField(pkg, "UserHomeDir", luar.New(L, os.UserHomeDir))
394 L.SetField(pkg, "WriteFile", luar.New(L, os.WriteFile))
395
396 return pkg
397 }
398
399 func importRuntime() *lua.LTable {
400 pkg := L.NewTable()
401
402 L.SetField(pkg, "GC", luar.New(L, runtime.GC))
403 L.SetField(pkg, "GOARCH", luar.New(L, runtime.GOARCH))
404 L.SetField(pkg, "GOMAXPROCS", luar.New(L, runtime.GOMAXPROCS))
405 L.SetField(pkg, "GOOS", luar.New(L, runtime.GOOS))
406 L.SetField(pkg, "GOROOT", luar.New(L, runtime.GOROOT))
407
408 return pkg
409 }
410
411 func importPath() *lua.LTable {
412 pkg := L.NewTable()
413
414 L.SetField(pkg, "Base", luar.New(L, path.Base))
415 L.SetField(pkg, "Clean", luar.New(L, path.Clean))
416 L.SetField(pkg, "Dir", luar.New(L, path.Dir))
417 L.SetField(pkg, "ErrBadPattern", luar.New(L, path.ErrBadPattern))
418 L.SetField(pkg, "Ext", luar.New(L, path.Ext))
419 L.SetField(pkg, "IsAbs", luar.New(L, path.IsAbs))
420 L.SetField(pkg, "Join", luar.New(L, path.Join))
421 L.SetField(pkg, "Match", luar.New(L, path.Match))
422 L.SetField(pkg, "Split", luar.New(L, path.Split))
423
424 return pkg
425 }
426
427 func importFilePath() *lua.LTable {
428 pkg := L.NewTable()
429
430 L.SetField(pkg, "Abs", luar.New(L, filepath.Abs))
431 L.SetField(pkg, "Base", luar.New(L, filepath.Base))
432 L.SetField(pkg, "Clean", luar.New(L, filepath.Clean))
433 L.SetField(pkg, "Dir", luar.New(L, filepath.Dir))
434 L.SetField(pkg, "EvalSymlinks", luar.New(L, filepath.EvalSymlinks))
435 L.SetField(pkg, "Ext", luar.New(L, filepath.Ext))
436 L.SetField(pkg, "FromSlash", luar.New(L, filepath.FromSlash))
437 L.SetField(pkg, "Glob", luar.New(L, filepath.Glob))
438 L.SetField(pkg, "HasPrefix", luar.New(L, filepath.HasPrefix))
439 L.SetField(pkg, "IsAbs", luar.New(L, filepath.IsAbs))
440 L.SetField(pkg, "Join", luar.New(L, filepath.Join))
441 L.SetField(pkg, "Match", luar.New(L, filepath.Match))
442 L.SetField(pkg, "Rel", luar.New(L, filepath.Rel))
443 L.SetField(pkg, "Split", luar.New(L, filepath.Split))
444 L.SetField(pkg, "SplitList", luar.New(L, filepath.SplitList))
445 L.SetField(pkg, "ToSlash", luar.New(L, filepath.ToSlash))
446 L.SetField(pkg, "VolumeName", luar.New(L, filepath.VolumeName))
447
448 return pkg
449 }
450
451 func importStrings() *lua.LTable {
452 pkg := L.NewTable()
453
454 L.SetField(pkg, "Contains", luar.New(L, strings.Contains))
455 L.SetField(pkg, "ContainsAny", luar.New(L, strings.ContainsAny))
456 L.SetField(pkg, "ContainsRune", luar.New(L, strings.ContainsRune))
457 L.SetField(pkg, "Count", luar.New(L, strings.Count))
458 L.SetField(pkg, "EqualFold", luar.New(L, strings.EqualFold))
459 L.SetField(pkg, "Fields", luar.New(L, strings.Fields))
460 L.SetField(pkg, "FieldsFunc", luar.New(L, strings.FieldsFunc))
461 L.SetField(pkg, "HasPrefix", luar.New(L, strings.HasPrefix))
462 L.SetField(pkg, "HasSuffix", luar.New(L, strings.HasSuffix))
463 L.SetField(pkg, "Index", luar.New(L, strings.Index))
464 L.SetField(pkg, "IndexAny", luar.New(L, strings.IndexAny))
465 L.SetField(pkg, "IndexByte", luar.New(L, strings.IndexByte))
466 L.SetField(pkg, "IndexFunc", luar.New(L, strings.IndexFunc))
467 L.SetField(pkg, "IndexRune", luar.New(L, strings.IndexRune))
468 L.SetField(pkg, "Join", luar.New(L, strings.Join))
469 L.SetField(pkg, "LastIndex", luar.New(L, strings.LastIndex))
470 L.SetField(pkg, "LastIndexAny", luar.New(L, strings.LastIndexAny))
471 L.SetField(pkg, "LastIndexFunc", luar.New(L, strings.LastIndexFunc))
472 L.SetField(pkg, "Map", luar.New(L, strings.Map))
473 L.SetField(pkg, "NewReader", luar.New(L, strings.NewReader))
474 L.SetField(pkg, "NewReplacer", luar.New(L, strings.NewReplacer))
475 L.SetField(pkg, "Repeat", luar.New(L, strings.Repeat))
476 L.SetField(pkg, "Replace", luar.New(L, strings.Replace))
477 L.SetField(pkg, "Split", luar.New(L, strings.Split))
478 L.SetField(pkg, "SplitAfter", luar.New(L, strings.SplitAfter))
479 L.SetField(pkg, "SplitAfterN", luar.New(L, strings.SplitAfterN))
480 L.SetField(pkg, "SplitN", luar.New(L, strings.SplitN))
481 L.SetField(pkg, "Title", luar.New(L, strings.Title))
482 L.SetField(pkg, "ToLower", luar.New(L, strings.ToLower))
483 L.SetField(pkg, "ToLowerSpecial", luar.New(L, strings.ToLowerSpecial))
484 L.SetField(pkg, "ToTitle", luar.New(L, strings.ToTitle))
485 L.SetField(pkg, "ToTitleSpecial", luar.New(L, strings.ToTitleSpecial))
486 L.SetField(pkg, "ToUpper", luar.New(L, strings.ToUpper))
487 L.SetField(pkg, "ToUpperSpecial", luar.New(L, strings.ToUpperSpecial))
488 L.SetField(pkg, "Trim", luar.New(L, strings.Trim))
489 L.SetField(pkg, "TrimFunc", luar.New(L, strings.TrimFunc))
490 L.SetField(pkg, "TrimLeft", luar.New(L, strings.TrimLeft))
491 L.SetField(pkg, "TrimLeftFunc", luar.New(L, strings.TrimLeftFunc))
492 L.SetField(pkg, "TrimPrefix", luar.New(L, strings.TrimPrefix))
493 L.SetField(pkg, "TrimRight", luar.New(L, strings.TrimRight))
494 L.SetField(pkg, "TrimRightFunc", luar.New(L, strings.TrimRightFunc))
495 L.SetField(pkg, "TrimSpace", luar.New(L, strings.TrimSpace))
496 L.SetField(pkg, "TrimSuffix", luar.New(L, strings.TrimSuffix))
497
498 return pkg
499 }
500
501 func importRegexp() *lua.LTable {
502 pkg := L.NewTable()
503
504 L.SetField(pkg, "Match", luar.New(L, regexp.Match))
505 L.SetField(pkg, "MatchReader", luar.New(L, regexp.MatchReader))
506 L.SetField(pkg, "MatchString", luar.New(L, regexp.MatchString))
507 L.SetField(pkg, "QuoteMeta", luar.New(L, regexp.QuoteMeta))
508 L.SetField(pkg, "Compile", luar.New(L, regexp.Compile))
509 L.SetField(pkg, "CompilePOSIX", luar.New(L, regexp.CompilePOSIX))
510 L.SetField(pkg, "MustCompile", luar.New(L, regexp.MustCompile))
511 L.SetField(pkg, "MustCompilePOSIX", luar.New(L, regexp.MustCompilePOSIX))
512
513 return pkg
514 }
515
516 func importErrors() *lua.LTable {
517 pkg := L.NewTable()
518
519 L.SetField(pkg, "New", luar.New(L, errors.New))
520
521 return pkg
522 }
523
524 func importTime() *lua.LTable {
525 pkg := L.NewTable()
526
527 L.SetField(pkg, "Sleep", luar.New(L, time.Sleep))
528 L.SetField(pkg, "Since", luar.New(L, time.Since))
529 L.SetField(pkg, "FixedZone", luar.New(L, time.FixedZone))
530 L.SetField(pkg, "LoadLocation", luar.New(L, time.LoadLocation))
531 L.SetField(pkg, "Date", luar.New(L, time.Date))
532 L.SetField(pkg, "Now", luar.New(L, time.Now))
533 L.SetField(pkg, "Parse", luar.New(L, time.Parse))
534 L.SetField(pkg, "ParseDuration", luar.New(L, time.ParseDuration))
535 L.SetField(pkg, "ParseInLocation", luar.New(L, time.ParseInLocation))
536 L.SetField(pkg, "Unix", luar.New(L, time.Unix))
537 L.SetField(pkg, "Nanosecond", luar.New(L, time.Nanosecond))
538 L.SetField(pkg, "Microsecond", luar.New(L, time.Microsecond))
539 L.SetField(pkg, "Millisecond", luar.New(L, time.Millisecond))
540 L.SetField(pkg, "Second", luar.New(L, time.Second))
541 L.SetField(pkg, "Minute", luar.New(L, time.Minute))
542 L.SetField(pkg, "Hour", luar.New(L, time.Hour))
543
544 // TODO: these raw Go timer APIs don't provide any synchronization
545 // with micro. Stop exposing them to lua once plugins switch to using
546 // the safer micro.After() interface instead. See issue #3209
547 L.SetField(pkg, "After", luar.New(L, time.After))
548 L.SetField(pkg, "AfterFunc", luar.New(L, time.AfterFunc))
549 L.SetField(pkg, "NewTicker", luar.New(L, time.NewTicker))
550 L.SetField(pkg, "NewTimer", luar.New(L, time.NewTimer))
551 L.SetField(pkg, "Tick", luar.New(L, time.Tick))
552
553 return pkg
554 }
555
556 func importUtf8() *lua.LTable {
557 pkg := L.NewTable()
558
559 L.SetField(pkg, "DecodeLastRune", luar.New(L, utf8.DecodeLastRune))
560 L.SetField(pkg, "DecodeLastRuneInString", luar.New(L, utf8.DecodeLastRuneInString))
561 L.SetField(pkg, "DecodeRune", luar.New(L, utf8.DecodeRune))
562 L.SetField(pkg, "DecodeRuneInString", luar.New(L, utf8.DecodeRuneInString))
563 L.SetField(pkg, "EncodeRune", luar.New(L, utf8.EncodeRune))
564 L.SetField(pkg, "FullRune", luar.New(L, utf8.FullRune))
565 L.SetField(pkg, "FullRuneInString", luar.New(L, utf8.FullRuneInString))
566 L.SetField(pkg, "RuneCount", luar.New(L, utf8.RuneCount))
567 L.SetField(pkg, "RuneCountInString", luar.New(L, utf8.RuneCountInString))
568 L.SetField(pkg, "RuneLen", luar.New(L, utf8.RuneLen))
569 L.SetField(pkg, "RuneStart", luar.New(L, utf8.RuneStart))
570 L.SetField(pkg, "Valid", luar.New(L, utf8.Valid))
571 L.SetField(pkg, "ValidRune", luar.New(L, utf8.ValidRune))
572 L.SetField(pkg, "ValidString", luar.New(L, utf8.ValidString))
573
574 return pkg
575 }
576
577 func importHumanize() *lua.LTable {
578 pkg := L.NewTable()
579
580 L.SetField(pkg, "Bytes", luar.New(L, humanize.Bytes))
581 L.SetField(pkg, "Ordinal", luar.New(L, humanize.Ordinal))
582
583 return pkg
584 }
585
586 func importHTTP() *lua.LTable {
587 pkg := L.NewTable()
588
589 L.SetField(pkg, "Get", luar.New(L, http.Get))
590 L.SetField(pkg, "Post", luar.New(L, http.Post))
591
592 return pkg
593 }
594
595 func importArchiveZip() *lua.LTable {
596 pkg := L.NewTable()
597
598 L.SetField(pkg, "OpenReader", luar.New(L, zip.OpenReader))
599 L.SetField(pkg, "NewReader", luar.New(L, zip.NewReader))
600 L.SetField(pkg, "NewWriter", luar.New(L, zip.NewWriter))
601
602 return pkg
603 }