gobindlua

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 2 Imported by: 0

README

Generate struct bindings for GopherLua

gobindlua generates GopherLua bindings for your structs.

gobindlua is designed to be used with go:generate. For example:

//go:generate gobindlua -s SomeStruct
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 ]]

Installation

Make sure the absolute path to your $GOPATH/bin directory is in your $PATH (or wherever your Go binaries are installed). go:generate will not work with relative paths. e.g.:

export GOPATH="${HOME}/go"
export PATH="${GOPATH}/bin:${PATH}"
From https://pkg.go.dev/
go install github.com/ChrisTrenkamp/gobindlua/gobindlua@latest
From source
git clone https://github.com/ChrisTrenkamp/gobindlua
go build -o $GOPATH/bin/gobindlua gobindlua/gobindlua.go

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 build -o $GOPATH/bin/gobindlua gobindlua/gobindlua.go && go generate ./... && go test ./...

TODO

  • Add support for maps.
  • Gather user types that are used in the struct, add them as dependencies, and auto-register them in the RegisterLuaType method.
  • gobindlua should be able to forgo generating a struct, and only generate bindings for functions.
  • gobindlua should be able to exclude fields and methods.
  • See if it's possible to auto-generate documentation from the Go documentation on the struct, the struct fields, functions, and methods so it can be used with Lua LSP's (possibly with https://github.com/LuaLS/lua-language-server ?)

Documentation

Index

Constants

View Source
const MAP_METATABLE_NAME = "gobindluamap"
View Source
const SLICE_METATABLE_NAME = "gobindluaslice"

Variables

This section is empty.

Functions

func MapLuaArrayOrTableToGoMap added in v0.0.2

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

func MapLuaArrayOrTableToGoSlice

func MapLuaArrayOrTableToGoSlice[T any](p lua.LValue, 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 RegisterLuaArray

func RegisterLuaArray(L *lua.LState)

func RegisterLuaMap added in v0.0.2

func RegisterLuaMap(L *lua.LState)

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)
}

func (*LuaMap) LuaMetatableType added in v0.0.2

func (*LuaMap) LuaMetatableType() string

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.

Jump to

Keyboard shortcuts

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