gm_luajit - You wanted it

What this is
I made this module as everyone was bitching about how slow it is and how much better GMod would be with it. So here it is, LuaJIT within Garry’s Mod.

What can I do?
For example you can run a loop that needs to be done fast within LuaJIT and return the result back to the GLua code, you can also call GLua functions from inside of the LuaJIT code which is however still pretty much experimental.

Example
[lua]require’luajit’

local ply = LocalPlayer()

function testfunc(…)
ply:Kill()
end

LuaJIT.Run(
– Code
[[
– All parameter are stored within _A
print(_A)

    -- Calls the SetPos function
    GLuaCall(unpack(_A))
    
    -- Calls testfunc within GLua
    GLuaCall("testfunc", unpack(_A))
    
    print("Print from LuaJIT")
]],

-- Parameters
_R.Player.SetPos, ply, Vector(0, 0, 0)

)[/lua]

Screenshots
http://dl.dropbox.com/u/29407915/luajit.png
http://dl.dropbox.com/u/29407915/luajit2.PNG
http://dl.dropbox.com/u/29407915/luajit3.png

Enough Chit-Chat, where is it
I would like to call this module more or less alpha state as there’s much planned and more to do.

Code: https://mattmodules.googlecode.com/svn/trunk/gm_luajit
Windows Binary: https://mattmodules.googlecode.com/svn/trunk/gm_luajit/Release/gm_luajit.dll

Maybe someone can also compile this for Linux? I dislike Linux

Oh.

Is this in a separate thread?

No

Thank you my dear sir, now if you’ll excuse me I have a few hundred lines of Lua to make awesomer
also, food for thought, Wiremod + this

The execute time made me nerdgasm. Make an interface wrapper and macros for this.

hot, is there ffi support?

If somebody could assist you in making a Linux binary too, it would complete the awesomeness that is this. Good work, bud.

That doesn’t seem wanted as you could call any API

I fucking came. Thanks for doing this, it’s already been of great use. The feel of running a sorting algorithm at compiled speed is exhilarating. So many possibilities.

Would calling glua functions in the luajit interface be slower? Man, if there was a way to grab the C function of each glua function and run that instead.

If you convince garry to give me the source I will be glad to help out with that

Can you make us a mac compile?

When I have finished what I planned for it I will do releases for all the platforms (I think). I will add new things this weekend such as adding metatables for Vector/Angle/Color to use at least some of them in LuaJIT directly

How about you start off with hooking lua_register and capture the functions that get pushed to the state by the engine.

Garry doesn’t use lua_register (and it doesn’t exist in lua_shared), he uses his own CLuaLibrary class thing to register stuff. Globals have their own CLuaGlobalLibrary. The former uses ILuaInterface::Push to push the function, then SetMember to add it to the library table. Latter uses just SetGlobal(name, function). SetMember uses lua_pushvalue and lua_settable, SetGlobal uses lua_setfield with the global table index.

The state is already created when the module is loaded and I’m not gonna get around modifying GMod lol

No need for that. The Lua libraries and classes have to be initialized somewhere, of course. And as it happens, garry has conveniently placed them into two global arrays (well, actually CUtlVectors probably), which are then iterated in functions InitLuaLibraries and InitLuaClasses. Each CLuaLibrary and CLuaClass has an array of <string, function> structs, containing the method names and C functions.
With this, you could for example sigscan InitLuaLibraries and InitLuaClasses, add offsets to the library and class lists, then iterate over those and save the C functions somewhere and use them.

The functions could also be sigscanned. You can find them if you dig around client.dylib with IDA. Library functions’ names are <library><function>, input____IsKeyDown for example. Class methods are called LC<class>_<method>, LC_Angle____Forward for example. But since these are the Mac binaries, you can’t use these signatures in Windows. You’ll have to find them from the .dll with other methods, since the .dll doesn’t contain debug info, like function names.

I’ve opened .so files in IDA before and retrieved some of the functions back from linux with names too.
I’m not tech savvy so I don’t know what I can do with a signature. I learned how to sigscan over at SourceMM, though.