gobindlua

package module
v0.0.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 2 Imported by: 0

README

Generate bindings for GopherLua

gobindlua generates GopherLua bindings and LuaLS definitions. It can generate bindings and definitions for structs, interfaces, and package functions.

Example

gobindlua is designed to be used with go:generate:

// Replace *version* with a gobindlua version.

//go:generate go run github.com/ChrisTrenkamp/gobindlua/gobindlua@*version*
type Joiner interface {
    Join() string
	gobindlua.LuaUserData
}

//go:generate go run github.com/ChrisTrenkamp/gobindlua/gobindlua@*version*
type SomeStruct struct {
    SomeStrings []string
}

//gobindlua:constructor
func NewSomeStruct(strs []string) SomeStruct {
    return SomeStruct {
        SomeStrings: strs,
    }
}

//gobindlua:function
func (s SomeStruct) Join() string {
    return strings.Join(s.SomeStrings, ", ")
}

... this will generate a file called lua_SomeStruct.go. The generated bindings will work seamlessly with Lua tables:

local some_struct = require "some_struct"
local my_struct = some_struct.new({"foo", "bar", "eggs", "ham"})
print(my_struct:join()) --[[ foo, bar, eggs, ham ]]

It will also generate LuaLS definitions for the struct and interface:

---lua_SomeStruct_definitions.go
---@meta some_struct

local some_struct = {}

---@return some_struct_fields
function some_struct.new() end

---@class some_struct_fields : joiner
---@field public my_string string[]
local my_struct = {}

---@return string
function some_struct_fields:join() end

return some_struct
---lua_Joiner_definitions.go
---@meta joiner

---@class joiner
local joiner = {}

---@return string
function joiner.join() end

return joiner

Enable interface discovery

If you want to generate interface definitions, create a gobindlua-conf.txt file in the root of your Go project (next to the go.mod file), with the list of Go modules that have generated gobindlua source files (including your own project). Any interface field listed in the gobindlua-conf.txt and has a //go:generate directive will be picked up and generated as its own type in the Lua definitions. Otherwise, it will be generated as an any type.

Tutorials

See the docs for instructions on how to use gobindlua.

Hacking gobindlua

When making changes to gobindlua, you can build and test it by running:

go generate ./... && go test ./...

TODO:

  • Provide a way to rename structs/packages for the generated Lua bindings.
  • godoc comments for structs, fields, and methods should be attached to the lua definitions.

Documentation

Index

Constants

View Source
const ARRAY_METATABLE_NAME = "GblSliceTable"
View Source
const ARRAY_MODULES_NAME = "GblSlice"
View Source
const MAP_METATABLE_NAME = "GblMapTable"
View Source
const MAP_MODULES_NAME = "GblMap"

Variables

This section is empty.

Functions

func CastArgError added in v0.0.10

func CastArgError(L *lua.LState, arg int, exp string, got any)

func FuncResCastError added in v0.0.16

func FuncResCastError(L *lua.LState, res int, exp string, got any)

func LuaArrayModuleLoader added in v0.0.9

func LuaArrayModuleLoader(L *lua.LState) int

func LuaArrayRegisterGlobalMetatable added in v0.0.9

func LuaArrayRegisterGlobalMetatable(L *lua.LState)

func LuaMapModuleLoader added in v0.0.9

func LuaMapModuleLoader(L *lua.LState) int

func LuaMapRegisterGlobalMetatable added in v0.0.9

func LuaMapRegisterGlobalMetatable(L *lua.LState)

func MapLuaArrayOrTableToGoMap added in v0.0.2

func MapLuaArrayOrTableToGoMap[K comparable, V any](p lua.LValue, level int, mapper func(k, v lua.LValue) (K, V)) (map[K]V, error)

func MapLuaArrayOrTableToGoSlice

func MapLuaArrayOrTableToGoSlice[T any](p lua.LValue, level int, mapper func(val lua.LValue) T) ([]T, error)

func MapVariadicArgsToGoSlice

func MapVariadicArgsToGoSlice[T any](start int, L *lua.LState, mapper func(val lua.LValue) T) ([]T, error)

func NewUserData

func NewUserData(data LuaUserData, L *lua.LState) *lua.LUserData

func Register added in v0.0.4

func Register(L *lua.LState, toRegister ...LuaRegistrar)

func TableElemCastError added in v0.0.16

func TableElemCastError(L *lua.LState, level int, exp string, got any)

func UnwrapLValueToAny added in v0.0.4

func UnwrapLValueToAny(l lua.LValue) any

Types

type GblMap added in v0.0.19

type GblMap struct {
	Map      interface{}
	Len      func() int
	GetValue func(idx lua.LValue) lua.LValue
	SetValue func(idx lua.LValue, val lua.LValue)
	ForEach  func(f func(k, v lua.LValue))
}

func (*GblMap) LuaMetatableType added in v0.0.19

func (*GblMap) LuaMetatableType() string

type GblSlice added in v0.0.19

type GblSlice struct {
	Slice    interface{}
	Len      func() int
	Index    func(idx int) lua.LValue
	SetIndex func(idx int, val lua.LValue)
}

func (*GblSlice) LuaMetatableType added in v0.0.19

func (*GblSlice) LuaMetatableType() string

type LuaRegistrar added in v0.0.9

type LuaRegistrar interface {
	LuaModuleName() string
	LuaModuleLoader(L *lua.LState) int
	LuaRegisterGlobalMetatable(L *lua.LState)
}

type LuaUserData

type LuaUserData interface {
	LuaMetatableType() string
}

Directories

Path Synopsis
doc
01.Primitives
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
02.Slices/array
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
02.Slices/matrix
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
02.Slices/vector
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
03.Maps
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
04.Pointers
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
05.Interfaces/dog
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
05.Interfaces/human
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
05.Interfaces/lion
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
05.Interfaces/mammallist
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.
06.Functions
Code generated by gobindlua; DO NOT EDIT.
Code generated by gobindlua; DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL