lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


For me it looks like you're searching for this handy little tool: https://github.com/pocomane/glua


Am 22.07.2021 22:10 schrieb Peter Hickman <peterhickman386@googlemail.com>:
I'm wanting to make a small lua application completely self contained, no installation dependencies. So my plan is to use physfs so that all the lua scripts would be in one file. Then I will use a little C script like this:

/*
 * From https://lucasklassmann.com/blog/2019-02-02-how-to-embeddeding-lua-in-c/
 *
 * cc -o boot boot.c -I/usr/local/Cellar/lua/5.4.3/include/lua -llua
 */

#include <stdio.h>
#include <stdlib.h>

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

int main(int argc, char **argv) {
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);

  char *code = "local physfs = require 'physfs' \
local function require(name) \
local fullname = name .. '.lua' \
local x = physfs.openRead(fullname) \
assert(x, 'File not found: ' .. fullname) \
local code = load(x:read()) \
return code() \
end \
physfs.mount('data.zip') \
require('game')";

  if (luaL_loadstring(L, code) == LUA_OK) {
    if (lua_pcall(L, 0, 0, 0) == LUA_OK) {
      lua_pop(L, lua_gettop(L));
    }
  }

  lua_close(L);
  return 0;
}

This works great, I have a little shell script to build the zip file and I can just run it. There is one issue and that is that physfs is itself a dependency

My plan is to incorporate physfs into the C script and then expose the three methods I use (openRead, mount and read) with something like "const struct luaL_Reg ..."

I'm pretty sure that it will work just fine. But there is a question, physfs is a nice and small library and I only need three methods so it's not a big deal. But if I was to use SDL2? Exposing the whole of SDL2 (sdl2, sdl2_gfx, sdl2_image, sdl2_mixer, sdl2_ttf) as C functions is going to be a nightmare. Along with the issue of getting SDL2 to use physfs when it loads images, fonts and sound files

Is there a better way of doing this?

Or is it "welcome to the club" :(