Skip to content

speedata/go-lua

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

274 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-lua

A Lua 5.3 VM in pure Go — no CGo, no dependencies.

This is a fork of Shopify/go-lua, upgraded from Lua 5.2 to Lua 5.3.

What's new compared to Shopify/go-lua?

  • Native 64-bit integers (int64) alongside floats (float64)
  • Bitwise operators: &, |, ~, <<, >> and unary ~
  • Integer division: //
  • Coroutines: coroutine.create, resume, yield, wrap, status, running
  • UTF-8 library: utf8.char, utf8.codes, utf8.codepoint, utf8.len, utf8.offset
  • String packing: string.pack, string.unpack, string.packsize
  • Math extensions: math.tointeger, math.type, math.ult, math.maxinteger, math.mininteger
  • Table move: table.move(a1, f, e, t [,a2])
  • Table metamethods: table.insert, table.remove, table.sort respect __index/__newindex
  • Hex float format: string.format supports %a/%A

Getting started

go get github.com/speedata/go-lua

A minimal example:

package main

import "github.com/speedata/go-lua"

func main() {
    l := lua.NewState()
    lua.OpenLibraries(l)
    if err := lua.DoFile(l, "hello.lua"); err != nil {
        panic(err)
    }
}

Calling Lua from Go

l := lua.NewState()
lua.OpenLibraries(l)

lua.DoString(l, `
    function greet(name)
        return "Hello, " .. name .. "!"
    end
`)

l.Global("greet")
l.PushString("World")
l.Call(1, 1)
result, _ := l.ToString(-1)
fmt.Println(result) // Hello, World!

Registering Go functions in Lua

l := lua.NewState()
lua.OpenLibraries(l)

l.Register("add", func(l *lua.State) int {
    a := lua.CheckNumber(l, 1)
    b := lua.CheckNumber(l, 2)
    l.PushNumber(a + b)
    return 1
})

lua.DoString(l, `print(add(2, 3))`) // 5

Test suite status

We run the official Lua 5.3 test suites. Currently 19 out of 24 pass:

Test Status Notes
bitwise Pass
calls Pass
closure Pass
code Pass
constructs Pass
coroutine Pass
errors Pass
events Pass
files Pass
goto Pass
literals Pass
locals Pass
math Pass
pm (pattern matching) Pass
sort Pass
strings Pass
tpack (string.pack) Pass
utf8 Pass
vararg Pass
attrib Needs debug.getinfo, weak refs
db Needs debug.getlocal for coroutines
gc Go's GC, not controllable like Lua's
big Tables with >2^18 elements
main Requires standalone Lua binary

Known limitations

  • No weak references__mode on metatables is not supported (Go's GC doesn't offer that hook)
  • No string.dump — serializing functions to bytecode is not implemented
  • Partial debug librarydebug.getlocal and debug.upvalueid are not yet implemented
  • No C API — pure Go, so C Lua libraries won't work (that's kind of the point though)

Development

git clone https://github.com/speedata/go-lua.git
go build ./...
go test ./...

Some tests optionally use luac 5.3 for compiling Lua source to bytecode. If it's not in your PATH, those tests get skipped automatically.

License

MIT — see LICENSE.md.

Originally forked from Shopify/go-lua.

About

A Lua VM in Go

Resources

License

Stars

Watchers

Forks

Languages

  • Go 54.8%
  • Lua 39.7%
  • C 5.4%
  • Makefile 0.1%