2021-10-16 16:40:53 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import "github.com/yuin/gopher-lua"
|
|
|
|
|
2021-11-22 23:59:28 +00:00
|
|
|
// Document adds a documentation string to a module.
|
|
|
|
// It is accessible via the __doc metatable.
|
2021-10-16 16:40:53 +00:00
|
|
|
func Document(L *lua.LState, module lua.LValue, doc string) {
|
2021-11-22 23:59:28 +00:00
|
|
|
mt := L.GetMetatable(module)
|
|
|
|
if mt == lua.LNil {
|
|
|
|
mt = L.NewTable()
|
|
|
|
L.SetMetatable(module, mt)
|
|
|
|
}
|
2021-10-16 16:40:53 +00:00
|
|
|
L.SetField(mt, "__doc", lua.LString(doc))
|
2021-11-22 23:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetField sets a field in a table, adding docs for it.
|
|
|
|
// 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)
|
2021-11-23 00:19:36 +00:00
|
|
|
if mt == lua.LNil {
|
|
|
|
mt = L.NewTable()
|
|
|
|
docProp := L.NewTable()
|
|
|
|
L.SetField(mt, "__docProp", docProp)
|
|
|
|
|
|
|
|
L.SetMetatable(module, mt)
|
|
|
|
}
|
2021-11-22 23:59:28 +00:00
|
|
|
docProp := L.GetTable(mt, lua.LString("__docProp"))
|
2021-10-16 16:40:53 +00:00
|
|
|
|
2021-11-22 23:59:28 +00:00
|
|
|
L.SetField(docProp, field, lua.LString(doc))
|
|
|
|
L.SetField(module, field, value)
|
2021-10-16 16:40:53 +00:00
|
|
|
}
|
2021-11-23 00:19:36 +00:00
|
|
|
|