lua-users home
lua-l archive

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


Hi, list!

We have an application, dynamically linked with Lua. Lua is compiled
as C++ code to allow us to write our bindings in C++.

As you know, compiling Lua as C++ basically changes longjmp() in
LUAI_THROW to throw; and setjmp() in LUAI_TRY to try {} catch() {}.
This change is crucial as longjmp/setjmp basically do not call any
destructors in C++ objects.

Now we're considering a switch to LuaJIT. Vanilla LuaJIT does not
compile as C++.

To compile it successfully under GCC, we had to do two things:

1. Fix goto jump across variable initialization in jit_coroutine_resume():

diff --git a/gamed/libs/luajit/src/ljit_x86.h b/gamed/libs/luajit/src/ljit_x86.h
index 329b3f0..d0d0160 100644
--- a/gamed/libs/luajit/src/ljit_x86.h
+++ b/gamed/libs/luajit/src/ljit_x86.h
@@ -607,6 +607,10 @@ static void jit_inline_base(jit_State *J,
jit_InlineInfo *ii)
 /* Helper function for inlined coroutine.resume(). */
 static StkId jit_coroutine_resume(lua_State *L, StkId base, int nresults)
 {
+  unsigned int ndelta = 0;
+  int nargs = 0;
+
   lua_State *co = thvalue(base-1);
   /* Check for proper usage. Merge of lua_resume() and auxresume() checks. */
   if (co->status != LUA_YIELD) {
@@ -623,8 +627,8 @@ errdead:
     }
   }
   {
-    unsigned int ndelta = (char *)L->top - (char *)base;
-    int nargs = ndelta/sizeof(TValue);  /* Compute nargs. */
+    /*unsigned int */ndelta = (char *)L->top - (char *)base;
+    /*int */nargs = ndelta/sizeof(TValue);  /* Compute nargs. */
     int status;
     if ((char *)co->stack_last-(char *)co->top <= ndelta) {
       co->ci->top = (StkId)(((char *)co->top) + ndelta);  /* Ok before grow. */

2. Replace define LUAI_USER_ALIGNMENT_T with typedef (as C++ does not
support creating new types inside sizeof()).

diff --git a/gamed/libs/luajit/src/luaconf.h b/gamed/libs/luajit/src/luaconf.h
index 0d4d338..09cff85 100644
--- a/gamed/libs/luajit/src/luaconf.h
+++ b/gamed/libs/luajit/src/luaconf.h
@@ -615,7 +615,7 @@ union luai_Cast { double l_d; long l_l; };
 ** aligned in 16-byte boundaries, then you should add long double in the
 ** union.) Probably you do not need to change this.
 */
-#define LUAI_USER_ALIGNMENT_T  union { double u; void *s; long l; }
+typedef union tagLUAI_USER_ALIGNMENT_T { double u; void *s; long l; }
LUAI_USER_ALIGNMENT_T;

With these changes applied code LuaJIT compiles successfully as C++ code.

However, compiled LuaJIT crushes on errors (same when embedded in our app):

$ ./luajit
Lua 5.1.3  Copyright (C) 1994-2008 Lua.org, PUC-Rio
LuaJIT 1.1.4  Copyright (C) 2005-2008 Mike Pall, http://luajit.org/
> error("boo")
terminate called after throwing an instance of 'lua_longjmp*'
Aborted (core dumped)

Stacktrace:

#0  0xffffe410 in __kernel_vsyscall ()
#1  0xb7c4c875 in raise () from /lib/tls/i686/cmov/libc.so.6
#2  0xb7c4e201 in abort () from /lib/tls/i686/cmov/libc.so.6
#3  0xb7e576e0 in __gnu_cxx::__verbose_terminate_handler () from
/usr/lib/libstdc++.so.6
#4  0xb7e54f65 in ?? () from /usr/lib/libstdc++.so.6
#5  0xb7e54fa2 in std::terminate () from /usr/lib/libstdc++.so.6
#6  0xb7e550ca in __cxa_throw () from /usr/lib/libstdc++.so.6
#7  0xb7ea7715 in ?? () from ./libtmluajit.so
#8  0xb7e9af79 in ?? () from ./libtmluajit.so
#9  0xb7e986e1 in lua_error () from ./libtmluajit.so
#10 0xb7eab3e8 in ?? () from ./libtmluajit.so
#11 0xb7bbc12a in ?? ()
#12 0x0804c010 in ?? ()
#13 0x08053b98 in ?? ()
#14 0x08053c48 in ?? ()
#15 0xb7bbc7b7 in ?? ()
#16 0x08053b98 in ?? ()
#17 0x08053c98 in ?? ()
#18 0x08053bc0 in ?? ()
#19 0xb7bbc044 in ?? ()
#20 0x0804c010 in ?? ()
#21 0xb7ec7ad8 in ?? () from ./libtmluajit.so
#22 0xbfde9988 in ?? ()
#23 0xb7eb2beb in ?? () from ./libtmluajit.so
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Unfortunately, we currently do not have any resources to debug this
issue. We also currently have no resources on adopting our existing
bindings to plain C Lua. So this is a showstopper

Do anyone successfully use LuaJIT compiled as C++ code? Is it possible at all?

We need LuaJIT to work successfully on following platforms:

1. Production-grade
   Linux x64 (compiled as 32-bit code). BTW, would it work at all as such?

2. Development-grade
   Linux x32
   Windows x32
   Intel OS X

Thanks in advance,
Alexander.