Skip to content

Instantly share code, notes, and snippets.

@lagagain
Last active August 15, 2022 06:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagagain/00212c5e51d76a19716d268ab89fae7c to your computer and use it in GitHub Desktop.
Save lagagain/00212c5e51d76a19716d268ab89fae7c to your computer and use it in GitHub Desktop.
[practice] Lua interact with C
#include "lua.h"
#include "lauxlib.h"
#include<stdio.h>
#include<stdlib.h>
int l_bar0(lua_State *L){
printf("----------- in the bar0 ---------------\n");
printf("----------- out the bar0 ---------------\n\n");
return 0;
}
int l_bar1_int(lua_State *L){
printf("----------- in the bar1 ---------------\n");
printf("gettop : %d\n",lua_gettop(L));
int input_from_lua = lua_tointeger(L,-1);
printf("input is %d\n",input_from_lua);
lua_pop(L,1);
printf("gettop : %d\n",lua_gettop(L));
printf("----------- out the bar1 ---------------\n\n");
return 0;
}
int l_bar1_str(lua_State *L){
printf("----------- in the bar1_str ---------------\n");
printf("gettop : %d\n",lua_gettop(L));
char* input_from_lua = lua_tostring(L,-1);
printf("input is %s\n",input_from_lua);
printf("gettop : %d\n",lua_gettop(L));
lua_pop(L,1);
printf("----------- out the bar1_str ---------------\n\n");
return 0;
}
int l_bar2(lua_State *L){
printf("----------- in the bar2 ---------------\n");
printf("gettop : %d\n",lua_gettop(L));
char* input_from_lua = lua_tostring(L,-1);
lua_pop(L,1);
int input_int = lua_tonumber(L,-1);
lua_pop(L,1);
sprintf(input_from_lua,"%s - %d",input_from_lua,input_int);
printf("result is %s\n",input_from_lua);
printf("gettop : %d\n",lua_gettop(L));
printf("----------- out the bar2 ---------------\n\n");
return 0;
}
int main(int argc, char *argv[]){
// new a lua VM
lua_State *L = luaL_newstate();
// open all libraries
luaL_openlibs(L);
lua_register(L,"bar0",l_bar0);
lua_register(L,"bar1",l_bar1_int);
lua_register(L,"bar1_str",l_bar1_str);
lua_register(L,"bar2",l_bar2);
// do file
luaL_dofile(L,"test.lua");
// call foo
lua_getglobal(L,"foo");
lua_pushnumber(L,19);
lua_pcall(L,1,1,0);
// show res
double res = lua_tonumber(L,-1);
lua_pop(L,1);
printf("result = %f\n", res);
// call hello
lua_getglobal(L,"hello");
lua_pushstring(L,"Me");
lua_pcall(L,1,0,0);
// call charAt
int input_int = 0;
printf("Need index of \"Hello World\" : ");
scanf("%d",&input_int);
lua_getglobal(L,"charAt");
lua_pushinteger(L,input_int);
lua_pushstring(L,"Hello World");
lua_pcall(L,2,1,0);
char *ch = lua_tostring(L,-1);
// show result
if(ch){
printf("%d of \"Hello World\" is %s\n",input_int, ch);
}else{
printf("Happend error!!\n");
}
lua_pop(L,1);
// get t1.a
lua_getglobal (L,"t1");
lua_pushstring(L,"a");
lua_gettable (L,-2);
int t_v = lua_tointeger(L,-1);
lua_pop(L,1);
printf("t1.a = %d\n", t_v);
// get t1.b
lua_getglobal (L,"t1");
lua_pushstring(L,"b");
lua_gettable (L,-2);
int t_vb = lua_tointeger(L,-1);
lua_pop(L,1);
printf("t1.b = %d\n", t_vb);
lua_pop(L,1);
//call showTable
lua_getglobal (L,"showTable");
lua_newtable (L);
lua_pushstring(L,"key1");
lua_pushstring(L,"value1");
lua_settable (L,-3);
lua_pushstring(L,"key2");
lua_pushstring(L,"value2");
lua_settable (L,-3);
lua_pushstring(L,"key3");
lua_pushstring(L,"value3");
lua_settable(L,-3);
lua_pcall(L,1,0,0);
lua_pop(L,1); // pop table
printf("getstatic : %d\n",lua_getstack(L,0,NULL));
printf("gettop : %d\n",lua_gettop(L));
printf("-------------------------------\n");
return 0;
}
all:
gcc main.c -o main -I/usr/include/lua5.3/ -L/usr/lib/x86_64-linux-gnu/ -llua5.3
clean:
rm main
var = 1000
t1 = {
a = 100,
b = 2,
}
function foo(y)
print "................... in function foo ..............."
print "................... out function foo ..............."
print ()
return y * y
end
function hello(name)
print "................... in function hello ..............."
print ("Hello " .. name)
print "................... out function hello ..............."
print ()
end
function charAt(index,str)
print "................... in function charAt ..............."
print ("result is "..str:sub(index,index+1))
print "................... out function charAt ..............."
print ()
return str:sub(index,index)
end
function showTable(t)
print "................... in function showTable ..............."
for k,v in pairs(t) do
print(k .. "-----" .. v)
end
print "................... out function showTable ..............."
print ()
end
bar0()
bar1(12354)
bar1_str("Lua")
bar2(var,"Hello World in Lua")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment