Running luajit object file from C

扶醉桌前 提交于 2019-11-27 02:57:15

问题


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.


回答1:


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



回答2:


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!


来源:https://stackoverflow.com/questions/19416981/running-luajit-object-file-from-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!