LuaJIT error with Embarcadero C++Builder XE5

189 Views Asked by At

I've compiled the latest LuaJIT version using both MinGW32 and MSVC32, and then created an import lib using Embarcadero's implib tool:

implib -a lua51.lib lua51.dll

The library is linked with sample C++ program and compiled with the bcc32 compiler.

Everything that worked with vanilla Lua 5.1 works fine with LuaJIT (much faster of course), but the problem arises with FFI.

Here is a simple C function inside of my C++ code:

// header
extern "C"  {
  int test_ffi(int value);
  }
// cpp file
int test_ffi(int value) {
   int xx = value + 1;
   return xx;
}

test.lua file:

local ffi = require("ffi")
ffi.cdef[[int test_ffi(int value);]]
local a = 100
local C = ffi.C
local result = C.test_ffi(a) 

CPP execution part:

//...
lua_State *L =  luaL_newstate();
luaL_openlibs(L);
lua_pushlightuserdata(L, (void*)test_ffi);
luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
lua_pop(L, 1);
int result;
result = luaL_loadfile(L, "test.lua");
if (result == 0)
   result = lua_pcall(L, 0, 0, 0);
lua_close(L);
//...

When lua_pcall() is executed, it calls test_ffi() as expected, but the value passed to the function is not 100, but some uninitialized random value. The result from lua_pcall() is 2.

I've tried various calling conventions (__cdecl, __stdcall) but no effect.

Also, replacing the MSVC32 DLLs with MinGW32 DLLs produces the same result.

When some void function is called, lua_pcall() produces an Access Violation in lua51.dll. I know that LuaJIT does not officially support bcc32 compilers, but except for FFI, all other APIs work correctly.

Does anyone have any clue what is happening?

0

There are 0 best solutions below