lua-users home
lua-l archive

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


Greetings --- I'm attempting to convert an embedded lua project i have from Lua5.1 to use LuaJIT.  I'm attempting to do a full embedding on Win32 - so there are no external .lua files needed.  I've followed the instructions but have hit a dead end. Hopefully somebody who's been down this path before will see where i've gone wrong.... :/

I have done:

1-original project running fine with Lua5.1
2-built LuaJIT (no config changes at all) Verified the command line luajit.exe works.
3-changed my project to link with the LuaJIT .dll (but don't enable JIT yet) - works.

4-attempt to embed the JIT .lua files:
    a- run 'opt.lua' and 'opt_inline.lua' through the default 5.1 luac.c compiler and produce bytecode
    b- run bytecode files through bin2c to generate compilable binary data.
    c- install the bytecode into the package table according to instructions(see ::InstallJITCode() below)
    d- turn on the jit compiler with "require("jit.opt").start()"

Everything works until i try step d). Then I get no results or C callbacks out of Lua when i try to run any Lua functions. No Lua errors are generated on any of the steps. I've been over the steps on the install instructions several times and can't see anything i'm missing. Any pointers appreciated!

cheers - j


int LuaSystem::InstallJITCode()
{
    static const char packageTable[] = "package";
    lua_getglobal(L,packageTable);

    if ( lua_istable(L,-1) )
    {
        static const char preloadTable[] = "preload";
        lua_pushstring(L,preloadTable);
        lua_gettable(L,-2);
        if ( lua_istable(L,-1) )
        {
            //push in the jit code
            static const char jitCode[] = "jit.opt";
            lua_pushstring(L,jitCode);

            int error = luaL_loadbuffer(L,(const char*)_LuaJIT_opt,sizeof(_LuaJIT_opt),jitCode);
            if ( error )
            {
                ReportError(error);
                lua_pop(L,2);
                return error;
            }
            lua_settable(L,-3);


            //push in the jit inline code
            static const char jitInlineCode[] = " jit.opt_inline";
            lua_pushstring(L,jitInlineCode);

            error = luaL_loadbuffer(L,(const char*)_LuaJIT_opt_inline,sizeof(_LuaJIT_opt_inline),jitInlineCode);
            if ( error )
            {
                ReportError(error);
                lua_pop(L,2);
                return error;
            }
            lua_settable(L,-3);

            //cleanup the stack - two tables remaining
            lua_pop(L,2);

            return 0;
        }
        else
        {
            lua_pop(L,2);
            lastError = "Error - no 'package.preload' table";
            return 1;
        }
    }
    else
    {
        //no package global table
        lua_pop(L,1);
        lastError = "Error - no global 'package' table";
        return 1;
    }

}