Skip to content

Instantly share code, notes, and snippets.

@benbjohnson
Last active February 11, 2019 12:10
Show Gist options
  • Save benbjohnson/5622779 to your computer and use it in GitHub Desktop.
Save benbjohnson/5622779 to your computer and use it in GitHub Desktop.
LuaJIT + Go Integration Test

Create an empty directory, download the code and run it:

$ mkdir /tmp/lj
$ cd /tmp/lj
$ wget https://gist.github.com/benbjohnson/5622779/raw/e53d227ebdbea8d513b62ad076feb3f6ac1c1594/luajit.go
$ go run luajit.go

And you should see:

Hello from the C preamble!
Hello from Lua!
Hello from the C preamble!
package main
/*
#cgo LDFLAGS: -lluajit-5.1
#include <stdlib.h>
#include <stdio.h>
#include <luajit-2.0/lua.h>
#include <luajit-2.0/lualib.h>
#include <luajit-2.0/lauxlib.h>
int my_func() {
printf("Hello from the C preamble!\n");
return 0;
}
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)
const src = `
local ffi = require('ffi')
ffi.cdef([[
int my_func();
]])
print("Hello from Lua!")
ffi.C.my_func()
`
func main() {
C.my_func()
// Initialize state.
state := C.luaL_newstate()
if state == nil {
fmt.Println("Unable to initialize Lua context.")
os.Exit(1)
}
C.luaL_openlibs(state)
// Compile the script.
csrc := C.CString(src)
defer C.free(unsafe.Pointer(csrc))
if C.luaL_loadstring(state, csrc) != 0 {
errstring := C.GoString(C.lua_tolstring(state, -1, nil))
fmt.Printf("Lua error: %v\n", errstring)
os.Exit(1)
}
// Run script.
if C.lua_pcall(state, 0, 0, 0) != 0 {
errstring := C.GoString(C.lua_tolstring(state, -1, nil))
fmt.Printf("Lua execution error: %v\n", errstring)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment