-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathreader.go
42 lines (36 loc) · 927 Bytes
/
reader.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
package strings
import (
"github.com/vadv/gopher-lua-libs/io"
lua "github.com/yuin/gopher-lua"
"strings"
)
const (
stringsReaderType = "strings.Reader"
)
func CheckStringsReader(L *lua.LState, n int) *strings.Reader {
ud := L.CheckUserData(n)
if reader, ok := ud.Value.(*strings.Reader); ok {
return reader
}
L.ArgError(n, stringsReaderType+" expected")
return nil
}
func LVStringsReader(L *lua.LState, reader *strings.Reader) lua.LValue {
ud := L.NewUserData()
ud.Value = reader
L.SetMetatable(ud, L.GetTypeMetatable(stringsReaderType))
return ud
}
func newStringsReader(L *lua.LState) int {
s := L.CheckString(1)
L.Pop(L.GetTop())
reader := strings.NewReader(s)
L.Push(LVStringsReader(L, reader))
return 1
}
func registerStringsReader(L *lua.LState) lua.LValue {
mt := L.NewTypeMetatable(stringsReaderType)
readerTable := io.ReaderFuncTable(L)
L.SetField(mt, "__index", readerTable)
return mt
}