diff options
| author | Steven <burnett@posteo.de> | 2026-01-17 21:22:52 +0000 |
|---|---|---|
| committer | Steven <burnett@posteo.de> | 2026-01-17 21:22:52 +0000 |
| commit | 68d493c5f261a99b40d90e2cc4f08b24d1975234 (patch) | |
| tree | e9a0e4438d1b7c8e5bb5e26ec2585918e85f8e80 /examples/scripting/lua | |
| parent | 7ac5be7ba8f700f69009c5e980ee7b12b0653586 (diff) | |
chore(examples): New example for LUA bindings
Diffstat (limited to 'examples/scripting/lua')
| -rw-r--r-- | examples/scripting/lua/README.md | 46 | ||||
| -rwxr-xr-x | examples/scripting/lua/deps.sh | 13 | ||||
| -rw-r--r-- | examples/scripting/lua/lua.zc | 48 | ||||
| -rw-r--r-- | examples/scripting/lua/script.lua | 17 |
4 files changed, 124 insertions, 0 deletions
diff --git a/examples/scripting/lua/README.md b/examples/scripting/lua/README.md new file mode 100644 index 0000000..dea8fa2 --- /dev/null +++ b/examples/scripting/lua/README.md @@ -0,0 +1,46 @@ +# Lua Scripting with Zen-C + +This examples demonstrates how to build your Zen-C application with Lua C bindings,<br> +and how you can call Lua code from Zen-C (.zc) context, or exposed Zen-C functions from Lua (.lua) context. + +> Lua is a powerful, efficient, lightweight, embeddable scripting language, often used in games. + +--- + +Example output: + + + +## Setup + +Get Lua 5.4 headers and lib: + +```sh +./deps.sh +``` + +This will leave the headers (.h) and lib (.a) inside a folder called ``lua``. + +## Build + +```sh +zc build lua.zc -o lua +``` + +## Run + +Either + +```sh +zc run lua.zc +``` + +or (after compilation) + +```sh +./lua +``` + +## Documentation + +- https://www.lua.org/manual/5.4/manual.html diff --git a/examples/scripting/lua/deps.sh b/examples/scripting/lua/deps.sh new file mode 100755 index 0000000..c4a9251 --- /dev/null +++ b/examples/scripting/lua/deps.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +## Downloads lua-5.4.2 headers (.h) and lib (.a) into ./lua + +wget "https://sourceforge.net/projects/luabinaries/files/5.4.2/Linux%20Libraries/lua-5.4.2_Linux54_64_lib.tar.gz" + +[[ -d "lua" ]] && rm -rf lua +mkdir -p lua + +tar -xf lua-5.4.2_Linux54_64_lib.tar.gz + +mv include/* *.a lua/ +rm -rf *.so include lua-5.4.2_Linux54_64_lib.tar.gz diff --git a/examples/scripting/lua/lua.zc b/examples/scripting/lua/lua.zc new file mode 100644 index 0000000..74403bb --- /dev/null +++ b/examples/scripting/lua/lua.zc @@ -0,0 +1,48 @@ +//> include: ./lua +//> lib: ./lua +//> link: -Wl,-Bstatic -llua54 -Wl,-Bdynamic -lm -ldl +//> cflags: -Ofast + +import "lua.h"; +import "lauxlib.h"; +import "lualib.h"; + +fn l_zenc_hello(L: lua_State*) -> int { + const name: string = (string)luaL_optstring(L, 1, "world"); + println "hello from Zen-C, {name}" + return 0; +} + +fn main() { + var L: lua_State* = luaL_newstate(); + + if (!L) return !"Could not initialize LUA."; + + // Opens standard libs + luaL_openlibs(L); + defer lua_close(L); + + // Expose a Zen-C function to Lua as global "zenc_hello" + lua_pushcfunction(L, l_zenc_hello); + lua_setglobal(L, "zenc_hello"); + + // Run some Lua from a Zen-C variable + const script = "print('hello from lua')\nzenc_hello('test')\n"; + + if (luaL_dostring(L, script) != LUA_OK) { + const err: string = (string)lua_tostring(L, -1); + !"lua error: {err}" + lua_pop(L, 1); + } + + // Run some Lua from a script file + if (luaL_loadfile(L, "script.lua") != LUA_OK) { + const err: string = (string)lua_tostring(L, -1); + !"lua load-error: {err}" + lua_pop(L, 1); + } else if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + const err: string = (string)lua_tostring(L, -1); + !"lua error: {err}" + lua_pop(L, 1); + } +} diff --git a/examples/scripting/lua/script.lua b/examples/scripting/lua/script.lua new file mode 100644 index 0000000..09ff16d --- /dev/null +++ b/examples/scripting/lua/script.lua @@ -0,0 +1,17 @@ +print('hello from lua (script file)') + +zenc_hello('test2') + +local tbl = { + ["something"] = "good", + [2] = 3 +} + +for k,v in pairs(tbl) do + print("Key: "..k.." | Val: "..v) +end + +local str = "4" +print(tonumber(str)) + +print(os.date("today is %A, in %B")) |
