gobindlua

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 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
}

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

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

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 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:

  • godoc comments for structs, fields, and methods should be attached to the lua definitions.

Documentation

Index

Constants

View Source
const ARRAY_METATABLE_NAME = "gbl_array_fields"
View Source
const ARRAY_MODULES_NAME = "gbl_array"
View Source
const MAP_METATABLE_NAME = "gbl_map_fields"
View Source
const MAP_MODULES_NAME = "gbl_map"

Variables

This section is empty.

Functions

func CastArgError added in v0.0.10

func CastArgError(exp string, got any) string

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 TableElementCastError added in v0.0.10

func TableElementCastError(exp string, got any, level int) string

func UnwrapLValueToAny added in v0.0.4

func UnwrapLValueToAny(l lua.LValue) any

Types

type LuaArray

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

func (*LuaArray) LuaMetatableType

func (*LuaArray) LuaMetatableType() string

type LuaMap added in v0.0.2

type LuaMap 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 (*LuaMap) LuaMetatableType added in v0.0.2

func (*LuaMap) 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
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
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