Page 1 of 1

[Bug/Fix] LUA Jit compatibility

Posted: Wed Jul 23, 2014 18:34
by scriptslol
Hello,

I've looked high and low for any other posts related to CEGUI and LUAJit compatibility and have found nothing. I hope this helps anyone that might of run into the same issues.

I've been integrating CEGUI 0.8.4 into an Engine that uses LUAJit 2.0.3 (5.1) as its lua virtual machine and I really wanted to be able to share the lua_state between the two. Since luajit uses the same api it should be a simple task of having CEGUILuaScriptModule link with lua51.lib & lua51.dll instead of the provided lua.lib & lua.dll.

However, when CEGUI is creating the lua bindings a stack overflow happens when attempting to execute - lua_CEGUI.cpp:68563

I figured this was because the generated byte code being executed was incompatible with luajit (since tolua++ uses regular lua in all of its madness). But, digging into the *.pkg files in "cegui-0.8.4\cegui\src\ScriptModules\Lua\package" I noticed that it had lua written in between [$ ... $] sections.

After taking all of the string lua sections and replacing it's corresponding byte code section in lua_CEGUI.cpp (they appear in the same order in which the packages were included in CEGUI.pkg) I was able to isolate the issue. Oh, the issue was reproducible in both x86/x64 with msvc110(VS 2012) run times.

Example byte code replacement:

Code: Select all

 
{ /* begin embedded lua code */
   int top = lua_gettop(tolua_S);
//static unsigned char B[] = {
//      10, 67, 69, 71, 85, 73, 46, 87, 105, 110, 100, 111, 119, 70, 97,
//      99, 116, 111, 114, 121, 73, 116, 101, 114, 97, 116, 111, 114, 46, 105,
//      116, 101, 114, 97, 116, 111, 114, 32, 61, 32, 67, 69, 71, 85, 73,
//      46, 105, 116, 101, 114, 97, 116, 111, 114, 95, 105, 109, 112, 108, 59,
//      32
//};
//tolua_dobuffer(tolua_S, (char*)B, sizeof(B), "tolua: embedded Lua code 5");*/
   luaL_dostring(tolua_S, "CEGUI.WindowFactoryIterator.iterator = CEGUI.iterator_impl;\n");
   lua_settop(tolua_S, top);
  } /* end of embedded lua code */


The code that is tripping stack overflows is located in/at "cegui-0.8.4\cegui\src\ScriptModules\Lua\package\HelperFunctions.pkg : 76 & 230 - 271"

For some reason assigning any of those functions from the 'CEGUI' module to the 'CEGUI.Window' module will always trip the overflow. Doing luaL_dostring() without those assignments will allow CEGUI to link and use the oh so delicious luajit.

In summary; Fix / remove the mentioned lines from the package files and CEGUI will be out-of-the-box luajit compatible.

I hope this was helpful,

Re: [Bug/Fix] LUA Jit compatibility

Posted: Tue Jul 29, 2014 19:43
by Ident
In order to get this into CEGUI core code we would have to be sure that this has no negative side-effects.
Can you explain to me (as i dont use Lua) why or if this has side-effects?

If this should go into CEGUI code, a pull request would also be very helpful for us so we can work with it better

Re: [Bug/Fix] LUA Jit compatibility

Posted: Fri Aug 01, 2014 20:26
by scriptslol
Hello Ident, thanks for looking at this.

lua_CEGUI.cpp is really just running lua functions. The package files have a bit of lua in them to help reassign functionality to other modules for convenience purposes. I went ahead and kept most of the byte code sections. All I did was remove the lua window casting helper functions and everything works as intended.

The only side effect is instead of doing:

Code: Select all

function someWindowEvent(e)
    local combo = e:toCombobox()
end


you will have to do:

Code: Select all

function someWindowEvent(e)
    local combo = CEGUI.toCombobox(e)
end


A list of the affected window casting functions can be found on the wiki:
http://cegui.org.uk/wiki/Event_reference#LUA_Helpers_Windows

As far as implications of this change are concerned it won't have side-effects on CEGUI since this is completely isolated in the lua vm. I actually tried to execute "CEGUI.Window.toCombobox = CEGUI.toCombobox" in one of the gameplay scripts and the stack overflow happen. So, it's limited to LUAjit and a bug that triggers circular meta-table look up. But, since this causes a run time crash in a cegui file it could be seen as a bug in CEGUI, when it is not.

Re: [Bug/Fix] LUA Jit compatibility

Posted: Mon Aug 04, 2014 08:30
by Ident
I would like to integrate your changes into default branch ( it can't go into the v0 or v0-8 branches)

I am not sure I understand the instructions for changing the code in your first post correctly and I cannot test any changes. Would you be so kind to make a pull request for default branch, including your changes? I will merge the changes in.