PDA

View Full Version : Embedding C in Lua



LucasTheDuck
10-24-2009, 09:00 AM
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


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(L, 1);
lua_concat(L, 2);
return 1;
}

int l_funcion(lua_State * L) {
int n = lua_tonumber(L,1);
int ret = n + 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

http://img251.imageshack.us/img251/9671/33228183.png (http://img251.imageshack.us/i/33228183.png/)

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


CC={}
function CC.Compile(code)
TextFile.WriteFromString("F:\\cee.c", Code, false);
return File.Run("F:\\MinGW\\bin\\gcc.exe", "-O2 -shared -o f:/cee.so f:/cee.c -llua -llualib", "", SW_MINIMIZE, true);
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

Sakuya
10-24-2009, 10:07 AM
I'd better check this out, Maybe this can fix a fatal flaw in password protection with AMS.

Sakuya
10-24-2009, 10:28 AM
http://img.loldepot.com/bd44b2a77357a79c516b.png

Nevermind..

LucasTheDuck
10-24-2009, 10:47 AM
shhhh... it's secret :)

LucasTheDuck
10-24-2009, 11:15 AM
A simple callback system


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

Dermot
10-24-2009, 12:57 PM
Very interesting but when you close the app, you get the Vista crash message. 'This app has stopped working...."

LucasTheDuck
10-24-2009, 10:07 PM
I use xp but i think that you may use the clasic closing method


OnClose

Handle = Application.GetWndHandle();
Window.Close(Handle, CLOSEWND_TERMINATE);


or any dllunload method... I think that there are more methods to register funcs same as


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

int lua_msgbox(lua_State* L)
{
const char* message = luaL_checkstring(L, 1);
const char* caption = luaL_optstring(L, 2, "");
int result = MessageBox(NULL, message, caption, MB_OK);
lua_pushnumber(L, result);
return 1;
}

int libinit (lua_State* L)
{
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


const luaL_reg tab_funcs[] = {
{"Test", irExample_test},
{"Demo", irExample_demo},
{NULL, NULL}
};
luaL_openlib(L, "Example", tab_funcs, 0);

Now functions Example.Test and Example.Demo are registered and working

Dermot
10-24-2009, 10:20 PM
No crash using that method of registering. :yes

LucasTheDuck
10-25-2009, 12:56 AM
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