fix: only add __docProp metatable from SetField function

dev
TorchedSammy 2021-11-22 19:19:36 -05:00
parent 43ddab699f
commit 179947cd98
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 8 additions and 5 deletions

View File

@ -1,7 +1,6 @@
package util
import "github.com/yuin/gopher-lua"
import "fmt"
// Document adds a documentation string to a module.
// It is accessible via the __doc metatable.
@ -9,9 +8,6 @@ func Document(L *lua.LState, module lua.LValue, doc string) {
mt := L.GetMetatable(module)
if mt == lua.LNil {
mt = L.NewTable()
docProp := L.NewTable()
L.SetField(mt, "__docProp", docProp)
L.SetMetatable(module, mt)
}
L.SetField(mt, "__doc", lua.LString(doc))
@ -21,9 +17,16 @@ func Document(L *lua.LState, module lua.LValue, doc string) {
// It is accessible via the __docProp metatable. It is a table of the names of the fields.
func SetField(L *lua.LState, module lua.LValue, field string, value lua.LValue, doc string) {
mt := L.GetMetatable(module)
if mt == lua.LNil {
mt = L.NewTable()
docProp := L.NewTable()
L.SetField(mt, "__docProp", docProp)
L.SetMetatable(module, mt)
}
docProp := L.GetTable(mt, lua.LString("__docProp"))
fmt.Println("docProp", docProp)
L.SetField(docProp, field, lua.LString(doc))
L.SetField(module, field, value)
}