Lua 5.4.4 and LuaJIT bindings for the Odin programming language.
// main.odin
import lua "shared:luajit"
import "shared:luajit/luaL"
import "shared:luajit/lualib"
import "shared:luajit/luajit"
main :: proc() {
L := luaL.newstate()
defer lua.close(L)
luajit.VERSION_SYM()
lualib.openlibs(L)
luaL.dofile(L, "main.lua")
}-- main.lua
local ffi = require "ffi"
local c = ffi.C
ffi.cdef [[
int printf(const char *fmt, ...);
int MessageBoxA(void *hwnd, const char *text, const char *caption, int type);
]]
io.write "what is your name? "
local name = io.read()
c.printf("hello %s!\n", name)
c.MessageBoxA(nil, "hello " .. name .. "!", "Message", 0)Copy the lua or luajit directory to a sutable location:
git clone https://github.com/jasonliang-dev/odin-lua.git
cp -R odin-lua/lua /path/to/Odin/shared
# or
cp -R odin-lua/lua /path/to/your/projectIf you're using LuaJIT on Windows, you'll need to copy luajit/lua51.dll and
put it in the same location as your executable.
This repo has prebuilt .lib, .dll, .a, and .so files for Windows and
Linux. Follow the instructions below if you want to build these files on your
own.
Lua's GitHub mirror has a file onelua.c which includes all other source
files. Get the latest release, then:
# windows
cl /DMAKE_LIB /c /O2 onelua.c
lib onelua.obj /out:lua54.lib
# linux
clang -DMAKE_LIB -DLUA_USE_LINUX -c -O2 onelua.c
ar rcs liblua54.a onelua.oGet the source with git clone https://luajit.org/git/luajit.git, then:
# windows
cd src
msvcbuild.bat # produces lua51.lib and lua51.dll
# linux
make # produces libluajit.a and libluajit.soThis library tries to be pretty close to the Lua C API, but there are a few notable differences:
-
Functions recieve Lua state by value and not by pointer. Lua state is defined as
State :: distinct rawptr.A better name would've been
StateHandle.Stateis shorter. Done out of laziness. -
The
lua,luaopen, andluaLprefixes are trimmed off. Types and functions are namespaced by package name. -
Names from
luaconf.hkeeps their prefixes -
Since
whereis a keyword in Odin, you'll have to typeluaL.luaL_where -
newlibandnewlibtable(5.4 only, not in LuaJIT) takes a slice instead of a c style array