Running luajit object file from C
Asked Answered
H

2

5

From the documentation: http://luajit.org/running.html

luajit -b test.lua test.obj                 # Generate object file
# Link test.obj with your application and load it with require("test")

But doesn't explain how to do these things. I guess they're assuming anyone using Lua is also a C programmer, not the case with me! Can I get some help? GCC as an example.

I would also like to do the same thing except from the C byte array header. I can't find documentation on this either.

luajit -bt h -n test test.lua test.h

This creates the header file but I don't know how to run it from C. Thanks.

Haskins answered 17/10, 2013 at 1:54 Comment(1)
You should be able to use luaL_loadstring to load that C array. Once you get the function back on the stack you can execute it with something like call or pcall or you can save it to the package.preload table if you want to require it later -- depending on what you want to do.Rustic
J
17

main.lua

print("Hello from main.lua")

app.c

#include <stdio.h>

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main(int argc, char **argv)
{
  int status;
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);
  lua_getglobal(L, "require");
  lua_pushliteral(L, "main");
  status = lua_pcall(L, 1, 0, 0);
  if (status) {
    fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
    return 1;
  }
  return 0;
}

Shell commands:

luajit -b main.lua main.o
gcc -O2 -Wall -Wl,-E -o app app.c main.o -Ixx -Lxx -lluajit-5.1 -lm -ldl

Replace -Ixx and -Lxx by the LuaJIT include and library directories. If you've installed it in /usr/local (the default), then most GCC installations will find it without these two options.

The first command compiles the Lua source code to bytecode and embeds it into the object file main.o.

The second command compiles and links the minimal C application code. Note that it links in the embedded bytecode, too. The -Wl,-E is mandatory (on Linux) to export all symbols from the executable.

Now move the original main.lua away (to ensure it's really running the embedded bytecode and not the Lua source code file) and then run your app:

mv main.lua main.lua.orig
./app
# Output: Hello from main.lua
Janejanean answered 17/10, 2013 at 13:24 Comment(3)
Awesome! Thanks! Can luac generate a .o file as well?Haskins
Nope, only luajit -b can do that in one step. There are 3rd party tools like bin2c that help with that for plain Lua. Look at the other answer for the general approach.Janejanean
Is there any way to do something like assert(loadfile("main")) instead of require("main")? loadfile doesn't seem to find the linked module like require does and instead just runs off to search for a file. I am trying to pass arguments (from app.c using argv) to the linked module, and can't use require because it doesn't return the module as a function that I can call like loadfile does.Cereus
R
8

The basic usage is as follows:

  • Generate the header file using luajit
  • #include that header in the source file(s) that's going to be referencing its symbols
  • Compile the source into a runnable executable or shared binary module for lua depending on your use-case.

Here's a minimal example to illustrate:

test.lua

return
{
  fooprint = function (s) return print("from foo: "..s) end,
  barprint = function (s) return print("from bar: "..s) end
}

test.h

// luajit -b test.lua test.h
#define luaJIT_BC_test_SIZE 155
static const char luaJIT_BC_test[] = {
27,76,74,1,2,44,0,1,4,0,2,0,5,52,1,0,0,37,2,1,0,16,3,0,0,36,2,3,2,64,1,2,0,15,
102,114,111,109,32,102,111,111,58,32,10,112,114,105,110,116,44,0,1,4,0,2,0,5,
52,1,0,0,37,2,1,0,16,3,0,0,36,2,3,2,64,1,2,0,15,102,114,111,109,32,98,97,114,
58,32,10,112,114,105,110,116,58,3,0,2,0,5,0,7,51,0,1,0,49,1,0,0,58,1,2,0,49,1,
3,0,58,1,4,0,48,0,0,128,72,0,2,0,13,98,97,114,112,114,105,110,116,0,13,102,
111,111,112,114,105,110,116,1,0,0,0,0
};

runtest.cpp

// g++ -Wall -pedantic -g runtest.cpp -o runtest.exe -llua51
#include <stdio.h>
#include <assert.h>

#include "lua.hpp"
#include "test.h"

static const char *runtest = 
"test = require 'test'\n"
"test.fooprint('it works!')\n"
"test.barprint('it works!')\n";


int main()
{
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);

  lua_getglobal(L, "package");
  lua_getfield(L, -1, "preload");
  // package, preload, luaJIT_BC_test
  bool err = luaL_loadbuffer(L, luaJIT_BC_test, luaJIT_BC_test_SIZE, NULL);
  assert(!err);

  // package.preload.test = luaJIT_BC_test
  lua_setfield(L, -2, "test");

  // check that 'test' lib is now available; run the embedded test script 
  lua_settop(L, 0);
  err = luaL_dostring(L, runtest);
  assert(!err);

  lua_close(L);
}

This is pretty straight-forward. This example takes the byte-code and places it into the package.preload table for this program's lua environment. Other lua scripts can then use this by doing require 'test'. The embedded lua source in runtest does exactly this and outputs:

from foo: it works!
from bar: it works!
Rustic answered 17/10, 2013 at 12:26 Comment(1)
Awesome, thanks! This is what I needed to know, luaL_loadbuffer. Can you tell me if the same thing is possible using regular luac?Haskins

© 2022 - 2024 — McMap. All rights reserved.