luajitter

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 6 Imported by: 0

README

LuaJitter

Blazing fast LuaJIT bindings with great ergonomics. Uses Go 1.14.

Installing

MacOS
  1. Install luajit with Homebrew: brew install luajit
  2. Locate the pkg-config file (it is usually in or around /usr/local/Cellar/luajit/2.0.5/lib/pkgconfig/luajit.pc)
  3. Modify the LDFLAGS at the bottom of the luajit.pc file to remove -pagezero_size and -image_base arguments
  4. go test ./... should now work
Linux
  1. Download the LuaJIT source from http://luajit.org/download.html
  2. Follow the POSIX installation instructions at http://luajit.org/install.html
  3. Locate the pkg-config file
  4. Modify the LDFLAGS at the bottom of the luajit.pc file to remove -pagezero_size and -image_base arguments
  5. go test ./... should now work
Windows
  1. 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)
  2. Install the Twilight Dragon GCC compiler from http://tdm-gcc.tdragon.net/download
  3. Download the LuaJIT source from http://luajit.org/download.html
  4. Run your MinGW terminal as administrator and navigate to the LuaJIT source on your hard drive
  5. Run mingw32-make, then navigate to the src subdirectory and locate lua51.dll
  6. Copy lua51.dll to whatever folder you intend to use LuaJitter from
  7. go test ./... should now work

Usage

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoErrorToLua

func GoErrorToLua(err error) *C.lua_err

func LuaErrorToGo

func LuaErrorToGo(err *C.lua_err) error

Types

type LocalData

type LocalData interface {
	LuaValue() *C.struct_lua_value
	HomeVM() *LuaState
	Close() error
}

type LocalLuaData

type LocalLuaData struct {
	// contains filtered or unexported fields
}

func (*LocalLuaData) Close

func (d *LocalLuaData) Close() error

func (*LocalLuaData) HomeVM

func (d *LocalLuaData) HomeVM() *LuaState

func (*LocalLuaData) LuaValue

func (d *LocalLuaData) LuaValue() *C.struct_lua_value

type LocalLuaFunction

type LocalLuaFunction struct {
	LocalLuaData
}

func (*LocalLuaFunction) Call

func (f *LocalLuaFunction) Call(args ...interface{}) ([]interface{}, error)

type LocalLuaTable

type LocalLuaTable struct {
	LocalLuaData
}

func (*LocalLuaTable) Unroll

func (table *LocalLuaTable) Unroll() (map[interface{}]interface{}, error)

type LuaState

type LuaState struct {
	// contains filtered or unexported fields
}

func NewState

func NewState() *LuaState

func (*LuaState) Close

func (s *LuaState) Close() error

func (*LuaState) DoString

func (s *LuaState) DoString(doString string) error

func (*LuaState) GetGlobal

func (s *LuaState) GetGlobal(path string) (interface{}, error)

func (*LuaState) InitGlobal

func (s *LuaState) InitGlobal(path string, value interface{}) error

func (*LuaState) SetGlobal

func (s *LuaState) SetGlobal(path string, value interface{}) error

Directories

Path Synopsis
lib

Jump to

Keyboard shortcuts

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