Indigo Rose Software

Professional Software Development Tools

 
+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2009
    Posts
    115

    Embedding C in Lua

    Hello, As a contribution, I present this method to create libraries for AMS without compiling anything, using c code.

    The only drawback for that work, dependency of MinGW compiler that must be installed and the LUA libraries created, but, you can create a cache of compiled or simply copy the return .so file and run them in any application

    This sample may be help the explanation

    PHP Code:
    Code=[[
    #include <lua.h>
    #include <lauxlib.h>
    #include <stdio.h>
    #include <stdlib.h>


    int start(lua_State L) {
        
    luaL_checkstring(L,1);
        
    lua_pushstring(L"hello ");
        
    lua_pushvalue(L1);
        
    lua_concat(L2);
        return 
    1;
      }
      
    int l_funcion(lua_State L) {
        
    int n lua_tonumber(L,1);
        
    int ret 38;
        
    lua_pushnumber(L,ret);
        return 
    1;
    }

    int l_do(lua_State L) {
        
    lua_dostring(L,"Dialog.Message(l_funcion(5), start(\"world\"))");
        return 
    1;
    }

    ]]

    bOk          =CC.Compile(Code)
    start        =CC.Register("start")
    l_funcion    =CC.Register("l_funcion")
    RunLib       =CC.Register("l_do")


    RunLib() 
    This returns this



    The posibilities for this are big, you can compile with some libraries such as mysql, soket, winapi...

    You can use any compiler, i used MinGW but I tested the compiler of VisualStudio2005 and work perfect

    I think that this functions are self explicative

    PHP Code:
    CC={}
    function 
    CC.Compile(code)
    TextFile.WriteFromString("F:\\cee.c"Codefalse);
    return 
    File.Run("F:\\MinGW\\bin\\gcc.exe""-O2 -shared -o f:/cee.so f:/cee.c -llua -llualib"""SW_MINIMIZEtrue);
    end

    function CC.Register(cfunc)
    return 
    loadlib("f://cee.so",cfunc)
    end 
    This is all you need.

    PS. Remember to put lua.h lualib.h luaxlib.h in /includes and liblua.a liblualib.a in /lib, i put the files for testing
    Attached Files

  2. #2
    Join Date
    Oct 2009
    Location
    Merton, United Kingdom
    Posts
    684
    I'd better check this out, Maybe this can fix a fatal flaw in password protection with AMS.

  3. #3
    Join Date
    Oct 2009
    Location
    Merton, United Kingdom
    Posts
    684


    Nevermind..

  4. #4
    Join Date
    Oct 2009
    Posts
    115
    shhhh... it's secret

  5. #5
    Join Date
    Oct 2009
    Posts
    115
    A simple callback system

    PHP Code:
    Code=[[
    #include <lua.h>
    #include <lauxlib.h>
    #include <stdio.h>
    #include <stdlib.h>

    int l_callback(lua_State L) {
        
    lua_getglobal(L,"LuaFunction");
        
    lua_pushstring(L,"hello");
        
    lua_pushstring(L,"world");
        if(
    lua_pcall(L,2,1,0) != 0){
            
    //ERROR
        
    }
        return 
    1;
    }


    ]]
    function 
    LuaFunction (arg1,arg2)
    Dialog.Message(arg1,arg2)
    end
    bOk          
    =CC.Compile(Code)
    l_callback   =CC.Register("l_callback")

    l_callback() 
    The lua C api is cool

  6. #6
    Join Date
    Apr 2004
    Location
    Vancouver, Canada
    Posts
    1,790
    Very interesting but when you close the app, you get the Vista crash message. 'This app has stopped working...."
    Dermot

    I am so out of here

  7. #7
    Join Date
    Oct 2009
    Posts
    115
    I use xp but i think that you may use the clasic closing method

    PHP Code:
    OnClose

    Handle 
    Application.GetWndHandle();
    Window.Close(HandleCLOSEWND_TERMINATE); 
    or any dllunload method... I think that there are more methods to register funcs same as

    PHP Code:
     Code=[[
      
    #include <windows.h>
      #include <lua.h>
      #include <lauxlib.h>
      #include <stdio.h>
      #include <stdlib.h>

      
    int lua_msgbox(lua_StateL)
      {
        const 
    charmessage luaL_checkstring(L1);
        const 
    charcaption luaL_optstring(L2"");
        
    int result MessageBox(NULLmessagecaptionMB_OK);
        
    lua_pushnumber(Lresult);
        return 
    1;
      }

      
    int libinit (lua_StateL)
      {
       
    lua_register(L"msgbox",  lua_msgbox);
       return 
    0;
      }
    ]]
    bOk         =CC.Compile(Code)
    LoadLib     =CC.Register("libinit")
    LoadLib()
    msgbox('mytext'
    Automaticly lua_msgbox is registered in L, you can aso use a Lual_reg to register more funcs

    PHP Code:
    const luaL_reg tab_funcs[] = {
            {
    "Test"irExample_test},
            {
    "Demo"irExample_demo},
            {
    NULLNULL}
        };
    luaL_openlib(L"Example"tab_funcs0); 
    Now functions Example.Test and Example.Demo are registered and working
    Last edited by LucasTheDuck; 10-24-2009 at 09:15 PM.

  8. #8
    Join Date
    Apr 2004
    Location
    Vancouver, Canada
    Posts
    1,790
    No crash using that method of registering.
    Dermot

    I am so out of here

  9. #9
    Join Date
    Oct 2009
    Posts
    115
    Right! This is the proper method to do...

    if anyone can test on deferent OS and architecture could be a great testing, i will test with w7 x32 but i don't have any x64

    I'm looking for do this embeddable in an actions plugin and a cache system based on hash of code, i'm triying to use CINT but he needs precompiled lua and other libs... for this raeson it's very difficult and ineficient, i prefer use mingw that have very much optons and libraries
    Last edited by LucasTheDuck; 10-24-2009 at 11:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts