Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.
/ odin-lua Public archive

Odin bindings for Lua 5.4.4 and LuaJIT

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE_lua
Notifications You must be signed in to change notification settings

jasonliang-dev/odin-lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚠️ Odin now has Lua in the vendor library collection. https://github.com/odin-lang/Odin/tree/master/vendor/lua

odin-lua

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)

Install

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/project

If you're using LuaJIT on Windows, you'll need to copy luajit/lua51.dll and put it in the same location as your executable.

Building library files

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

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.o

LuaJIT

Get 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.so

Differences from C

This 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. State is shorter. Done out of laziness.

  • The lua, luaopen, and luaL prefixes are trimmed off. Types and functions are namespaced by package name.

  • Names from luaconf.h keeps their prefixes

  • Since where is a keyword in Odin, you'll have to type luaL.luaL_where

  • newlib and newlibtable (5.4 only, not in LuaJIT) takes a slice instead of a c style array

About

Odin bindings for Lua 5.4.4 and LuaJIT

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE_lua

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages