chore: add comments to document util functions

lua5.4
TorchedSammy 2022-03-29 21:02:15 -04:00
parent 6848b59cbf
commit 245400e43d
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 6 additions and 0 deletions

View File

@ -4,12 +4,14 @@ import (
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
) )
// LuaExport represents a Go function which can be exported to Lua.
type LuaExport struct { type LuaExport struct {
Function rt.GoFunctionFunc Function rt.GoFunctionFunc
ArgNum int ArgNum int
Variadic bool Variadic bool
} }
// SetExports puts the Lua function exports in the table.
func SetExports(rtm *rt.Runtime, tbl *rt.Table, exports map[string]LuaExport) { func SetExports(rtm *rt.Runtime, tbl *rt.Table, exports map[string]LuaExport) {
for name, export := range exports { for name, export := range exports {
rtm.SetEnvGoFunc(tbl, name, export.Function, export.ArgNum, export.Variadic) rtm.SetEnvGoFunc(tbl, name, export.Function, export.ArgNum, export.Variadic)

View File

@ -39,6 +39,7 @@ func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, d
module.Set(rt.StringValue(field), value) module.Set(rt.StringValue(field), value)
} }
// DoString runs the code string in the Lua runtime.
func DoString(rtm *rt.Runtime, code string) error { func DoString(rtm *rt.Runtime, code string) error {
chunk, err := rtm.CompileAndLoadLuaChunk("", []byte(code), rt.TableValue(rtm.GlobalEnv())) chunk, err := rtm.CompileAndLoadLuaChunk("", []byte(code), rt.TableValue(rtm.GlobalEnv()))
if chunk != nil { if chunk != nil {
@ -48,6 +49,7 @@ func DoString(rtm *rt.Runtime, code string) error {
return err return err
} }
// DoString runs the contents of the file in the Lua runtime.
func DoFile(rtm *rt.Runtime, filename string) error { func DoFile(rtm *rt.Runtime, filename string) error {
data, err := os.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
@ -57,6 +59,8 @@ func DoFile(rtm *rt.Runtime, filename string) error {
return DoString(rtm, string(data)) return DoString(rtm, string(data))
} }
// HandleStrCallback handles function parameters for Go functions which take
// a string and a closure.
func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) { func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) {
if err := c.CheckNArgs(2); err != nil { if err := c.CheckNArgs(2); err != nil {
return "", nil, err return "", nil, err