1. Post #1
    Gold Member
    Dennab
    May 2011
    436 Posts
    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
    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)
    )

    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/s.../gm_luajit.dll

    Maybe someone can also compile this for Linux? I dislike Linux
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Optimistic x 7Winner x 6Lua King x 3Lua Helper x 3Useful x 2Informative x 1Agree x 1Zing x 1Funny x 1 (list)

  2. Post #2
    Dragon Dildoes
    Dennab
    April 2009
    4,432 Posts
    Oh.
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Dumb x 5Informative x 5Funny x 1Disagree x 1Optimistic x 1 (list)

  3. Post #3
    technicolour's Avatar
    January 2008
    108 Posts
    Is this in a separate thread?
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Optimistic Optimistic x 2 (list)

  4. Post #4
    Gold Member
    Dennab
    May 2011
    436 Posts
    Is this in a separate thread?
    No

  5. Post #5
    DylanWilson's Avatar
    January 2010
    273 Posts
    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
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 4Funny Funny x 1Informative Informative x 1 (list)

  6. Post #6
    Gold Member
    Hentie's Avatar
    May 2010
    2,129 Posts
    The execute time made me nerdgasm. Make an interface wrapper and macros for this.

  7. Post #7
    Noi
    Purrr ~
    Noi's Avatar
    February 2010
    1,256 Posts
    hot, is there ffi support?

  8. Post #8
    If somebody could assist you in making a Linux binary too, it would complete the awesomeness that is this. Good work, bud.
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Agree Agree x 1 (list)

  9. Post #9
    Gold Member
    Dennab
    May 2011
    436 Posts
    hot, is there ffi support?
    That doesn't seem wanted as you could call any API

  10. Post #10
    wizardsbane's Avatar
    January 2008
    372 Posts
    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.
    Reply With Quote Edit / Delete Reply Windows Vista United States Show Events Agree Agree x 1Optimistic Optimistic x 1 (list)

  11. Post #11
    Gold Member
    Hentie's Avatar
    May 2010
    2,129 Posts
    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.

  12. Post #12
    Gold Member
    Dennab
    May 2011
    436 Posts
    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
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Optimistic Optimistic x 8 (list)

  13. Post #13
    Casual Bananas
    E X C L's Avatar
    February 2011
    1,716 Posts
    Can you make us a mac compile?
    Reply With Quote Edit / Delete Reply Mac Netherlands Show Events Funny Funny x 1Optimistic Optimistic x 1 (list)

  14. Post #14
    Gold Member
    Dennab
    May 2011
    436 Posts
    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

  15. Post #15
    I made WAYWO a better place
    OldFusion's Avatar
    September 2011
    1,311 Posts
    If you convince garry to give me the source I will be glad to help out with that
    How about you start off with hooking lua_register and capture the functions that get pushed to the state by the engine.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Disagree Disagree x 1Funny Funny x 1 (list)

  16. Post #16
    Gold Member
    raBBish's Avatar
    March 2007
    2,667 Posts
    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.
    Reply With Quote Edit / Delete Reply Windows 7 Finland Show Events Informative Informative x 2Winner Winner x 1Optimistic Optimistic x 1 (list)

  17. Post #17
    Gold Member
    Dennab
    May 2011
    436 Posts
    How about you start off with hooking lua_register and capture the functions that get pushed to the state by the engine.
    The state is already created when the module is loaded and I'm not gonna get around modifying GMod lol
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Disagree Disagree x 1 (list)

  18. Post #18
    Gold Member
    raBBish's Avatar
    March 2007
    2,667 Posts
    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.

  19. Post #19
    Gold Member
    Hentie's Avatar
    May 2010
    2,129 Posts
    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.