Blazing fast LuaJIT bindings with great ergonomics. Uses Go 1.14.
- Install luajit with Homebrew:
brew install luajit
- Locate the pkg-config file (it is usually in or around
/usr/local/Cellar/luajit/2.0.5/lib/pkgconfig/luajit.pc
) - Modify the LDFLAGS at the bottom of the luajit.pc file to remove -pagezero_size and -image_base arguments
go test ./...
should now work
- Download the LuaJIT source from http://luajit.org/download.html
- Follow the POSIX installation instructions at http://luajit.org/install.html
- Locate the pkg-config file
- Modify the LDFLAGS at the bottom of the luajit.pc file to remove -pagezero_size and -image_base arguments
go test ./...
should now work
- Install MinGW from http://www.mingw.org/wiki/Install_MinGW (you can skip this step if you have Git Bash installed, it can be your MinGW terminal)
- Install the Twilight Dragon GCC compiler from http://tdm-gcc.tdragon.net/download
- Download the LuaJIT source from http://luajit.org/download.html
- Run your MinGW terminal as administrator and navigate to the LuaJIT source on your hard drive
- Run
mingw32-make
, then navigate to thesrc
subdirectory and locatelua51.dll
- Copy
lua51.dll
to whatever folder you intend to use LuaJitter from go test ./...
should now work
package main
import (
"fmt"
"github.com/cannibalvox/luajitter"
)
func AddValues(args []interface{}) ([]interface{}, error) {
lValue := args[0].(float64)
rValue := args[1].(float64)
return []interface{}{lValue + rValue}, nil
}
func closeVM(vm *luajitter.LuaState) {
err := vm.Close()
if err != nil {
panic(err)
}
}
func main() {
//Create/destroy states
vm := luajitter.NewState()
defer closeVM(vm)
//Execute arbitrary code
err := vm.DoString(`
print("Hello, World")
someGlobal = {
itsANumber = 5,
itsAString = "WOW",
itsAFunction = function(l,r) return l+r end,
}
`)
if err != nil {
panic(err)
}
//Access globals with dot-separated paths
num, err := vm.GetGlobal("someGlobal.itsANumber")
if err != nil {
panic(err)
}
fmt.Println(num)
str, err := vm.GetGlobal("someGlobal.itsAString")
if err != nil {
panic(err)
}
fmt.Println(str)
//Call Lua functions from go
fObj, err := vm.GetGlobal("someGlobal.itsAFunction")
if err != nil {
panic(err)
}
f := fObj.(*luajitter.LocalLuaFunction)
sumRet, err := f.Call(6, 3)
if err != nil {
panic(err)
}
fmt.Println(sumRet[0])
//Manipulate lua globals from go
err = vm.SetGlobal("someGlobal.itsAString", "NEW STRING")
if err != nil {
panic(err)
}
str, err = vm.GetGlobal("someGlobal.itsAString")
if err != nil {
panic(err)
}
fmt.Println(str)
//Set global + initialize intervening tables
err = vm.InitGlobal("someGlobal.subGlobal.subGlobal.value", true)
if err != nil {
panic(err)
}
b, err := vm.GetGlobal("someGlobal.subGlobal.subGlobal.value")
if err != nil {
panic(err)
}
fmt.Println(b)
//Catch lua errors from go
err = vm.DoString(`
someGlobal.errorFunc = function() error("lua failed!") end
`)
if err != nil {
panic(err)
}
errFObj, err := vm.GetGlobal("someGlobal.errorFunc")
if err != nil {
panic(err)
}
errF := errFObj.(*luajitter.LocalLuaFunction)
_, err = errF.Call()
fmt.Println(err.Error())
//Call go functions from lua
err = vm.SetGlobal("addFunc", AddValues)
if err != nil {
panic(err)
}
err = vm.DoString(`
print(addFunc(1,7))
`)
if err != nil {
panic(err)
}
}