-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathbuilder.go
48 lines (41 loc) · 1.1 KB
/
builder.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
package strings
import (
lio "github.com/vadv/gopher-lua-libs/io"
lua "github.com/yuin/gopher-lua"
"strings"
)
const (
stringsBuilderType = "strings.Builder"
)
func CheckStringsBuilder(L *lua.LState, n int) *strings.Builder {
ud := L.CheckUserData(n)
if builder, ok := ud.Value.(*strings.Builder); ok {
return builder
}
L.ArgError(n, stringsBuilderType+" expected")
return nil
}
func LVStringsBuilder(L *lua.LState, builder *strings.Builder) lua.LValue {
ud := L.NewUserData()
ud.Value = builder
L.SetMetatable(ud, L.GetTypeMetatable(stringsBuilderType))
return ud
}
func stringsBuilderString(L *lua.LState) int {
builder := CheckStringsBuilder(L, 1)
s := builder.String()
L.Push(lua.LString(s))
return 1
}
func newStringsBuilder(L *lua.LState) int {
builder := &strings.Builder{}
L.Push(LVStringsBuilder(L, builder))
return 1
}
func registerStringsBuilder(L *lua.LState) lua.LValue {
mt := L.NewTypeMetatable(stringsBuilderType)
writerTable := lio.WriterFuncTable(L)
L.SetField(writerTable, "string", L.NewFunction(stringsBuilderString))
L.SetField(mt, "__index", writerTable)
return mt
}