7

Using the recent luaJIT lua_open returns null. This does not happen with the regular lua library.

lua_State *L = lua_open();
std::cout << L << std::endl;

Output: 0x0

How can I get luaJIT to work?

SSCCE:

#include <iostream>
#include <luajit-2.0/lua.hpp>
//linked library: libluajit-5.1.a

int main(int argc, const char * argv[])
{
    lua_State *L = luaL_newstate(); // lua_open();
    std::cout << L << std::endl; // 0x0
}

Additional information: Built on OSX 10.9 from source (tried both 2.0.2 and from git) with make and make install. Using compiler:

$ cc --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

(Using the luajit command line application luajit works fine, a test script gets executed without errors.)

Appleshell
  • 7,088
  • 6
  • 47
  • 96
  • Which version of LuaJIT are you using? – Ryan Stein Dec 31 '13 at 16:01
  • @RyanStein Current stable version 2.0.2. – Appleshell Dec 31 '13 at 16:12
  • Wasn't lua_open replaced by lua_newstate? Unless you're using your own mem manager, you should be able to use luaL_newstate which is lua_newstate with default mem manager. – Oliver Dec 31 '13 at 16:32
  • With luajit you need to use luaL_newstate instead of lua_newstate anyway but as RyanStein indicates in his comment below lua_open is a define for luaL_newstate. – Etan Reisner Dec 31 '13 at 16:47
  • Is what you've posted your entire test case for this problem? – Ryan Stein Dec 31 '13 at 17:28
  • @RyanStein Yes, I added an SSCCE that yields the result for me. – Appleshell Dec 31 '13 at 17:41
  • I'm not able to reproduce the result. Can you provide more information about your environment? OS, compiler, etc. – Ryan Stein Dec 31 '13 at 18:17
  • agree with @RyanStein. I cannot reproduce this problem on my Win7 mingw-gcc setup either. How do you have luajit setup on your system? Did you build it directly from luajit's git repo? Was it from a distro package manager? etc. – greatwolf Jan 01 '14 at 03:27
  • @greatwolf I added the details to the question. – Appleshell Jan 01 '14 at 07:21
  • The only time `luaL_newstate` would return `NULL` is due to a memory allocation error. Have you tried using [`lua_newstate`](http://www.lua.org/manual/5.1/manual.html#lua_newstate) with a [custom allocator](http://www.lua.org/manual/5.1/manual.html#lua_Alloc) yet? – Ryan Stein Jan 01 '14 at 21:53
  • @Ryan LuaJIT doesn't support custom allocators when built for 64 bit processors, and probably won't support it in 32 bit processors when the new GC rolls out. – Colonel Thirty Two Jan 02 '14 at 23:01

1 Answers1

14

Apparently, x64 Mac applications need special handling; see http://luajit.org/install.html.

If you're building a 64 bit application on OSX which links directly or indirectly against LuaJIT, you need to link your main executable with these flags:

-pagezero_size 10000 -image_base 100000000
Community
  • 1
  • 1
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Do you also know how these arguments affect the linking step? – Appleshell Jan 03 '14 at 15:06
  • I know that LuaJIT on x64 needs to allocate memory in the lower 2GB range. I think these flags set up the process to let LuaJIT be able to use that range, but I'm not sure what exactly they do (a search for 'pagezero_size' returns more LuaJIT-related results than what it actually does.) – Colonel Thirty Two Jan 03 '14 at 15:58
  • In case that I have no control over my main executable (e.g. because I need to use a main exec binary which is provided by my system and I cannot change that), is there any work around or other way? Preferably a dynamic way? Can I somehow make LuaJIT still work? Or maybe automatically switch to the standard Lua in that case... – Albert Mar 13 '16 at 21:32