lua_open returns null using luaJIT
Asked Answered
R

1

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.)

Renfroe answered 31/12, 2013 at 14:42 Comment(11)
Which version of LuaJIT are you using?Demijohn
@RyanStein Current stable version 2.0.2.Renfroe
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.Interlaminate
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.Selie
Is what you've posted your entire test case for this problem?Demijohn
@RyanStein Yes, I added an SSCCE that yields the result for me.Renfroe
I'm not able to reproduce the result. Can you provide more information about your environment? OS, compiler, etc.Demijohn
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.Spoofery
@Spoofery I added the details to the question.Renfroe
The only time luaL_newstate would return NULL is due to a memory allocation error. Have you tried using lua_newstate with a custom allocator yet?Demijohn
@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.Rendezvous
R
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
Rendezvous answered 2/1, 2014 at 2:8 Comment(3)
Do you also know how these arguments affect the linking step?Renfroe
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.)Rendezvous
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...Stronghold

© 2022 - 2024 — McMap. All rights reserved.