Skip to content

Commit

Permalink
strings: register type in package and not globally (#47)
Browse files Browse the repository at this point in the history
Co-authored-by: vadv <[email protected]>
  • Loading branch information
waschik and vadv authored Nov 2, 2022
1 parent cf36c91 commit d4e40cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions strings/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func newStringsBuilder(L *lua.LState) int {
return 1
}

func registerStringsBuilder(L *lua.LState) {
func registerStringsBuilder(L *lua.LState) lua.LValue {
mt := L.NewTypeMetatable(stringsBuilderType)
L.SetGlobal(stringsBuilderType, mt)
writerTable := lio.WriterFuncTable(L)
L.SetField(writerTable, "string", L.NewFunction(stringsBuilderString))
L.SetField(mt, "__index", writerTable)
return mt
}
6 changes: 4 additions & 2 deletions strings/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ func Preload(L *lua.LState) {

// Loader is the module loader function.
func Loader(L *lua.LState) int {
registerStringsReader(L)
registerStringsBuilder(L)
readerMt := registerStringsReader(L)
builderMt := registerStringsBuilder(L)

t := L.NewTable()
t.RawSetString("Reader", readerMt)
t.RawSetString("Builder", builderMt)
L.SetFuncs(t, api)
L.Push(t)
return 1
Expand Down
4 changes: 2 additions & 2 deletions strings/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func newStringsReader(L *lua.LState) int {
return 1
}

func registerStringsReader(L *lua.LState) {
func registerStringsReader(L *lua.LState) lua.LValue {
mt := L.NewTypeMetatable(stringsReaderType)
L.SetGlobal(stringsReaderType, mt)
readerTable := io.ReaderFuncTable(L)
L.SetField(mt, "__index", readerTable)
return mt
}
16 changes: 15 additions & 1 deletion strings/test/test_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,27 @@ function TestReader(t)
assert(got == s, string.format("'%s' ~= '%s'", got, s))
end

function TestReaderMetatable(t)
local reader = strings.new_reader("")
local got = getmetatable(reader)
local expected = strings.Reader
assert(got == expected, string.format("'%s' ~= '%s'", got, expected))
end

function TestBuilder(t)
local builder = strings.new_builder()
builder:write("foo", "bar", 123)
local got = builder:string()
assert(got == "foobar123", string.format("'%s' ~= '%s'", got, "foobar123"))
end

function TestBuilderMetatable(t)
local builder = strings.new_builder()
local got = getmetatable(builder)
local expected = strings.Builder
assert(got == expected, string.format("'%s' ~= '%s'", got, expected))
end

function TestTrimSpace(t)
tests = {
{
Expand Down Expand Up @@ -114,4 +128,4 @@ function TestReadLine(t)
line = reader:read("*l")
assert(not line, line)
end)
end
end

0 comments on commit d4e40cb

Please sign in to comment.