lua call C function
C file : test.c
============================
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
static int square(lua_State *L){ //用來取的lua參數,參數是以stack的狀態存在
float value = lua_tonumber(L, -1); //轉型成C的型態
lua_pushnumber(L, value*value); //回傳
return 1;
}
static int merge(lua_State *L){
const char *str1 = lua_tostring(L, -2); //注意取的參數的順序
const char *str2 = lua_tostring(L, -1);
char str[80];
strcpy(str, str1);
strcat(str, str2);
}
int luaopen_gg(lua_State *L){ // luaopen_*,載入時的主要進入點必需與.so和lua的lib同名
lua_register(L, "square", square); //跟lua註冊function
lua_register(L, "cube", cube);
lua_register(L, "merge", merge);
return 0;
}
notes : 預先載入的lib : string, package, _G, os, table, math, coroutine, debug, io 要避免同名。
============================
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <string.h>
static int square(lua_State *L){ //用來取的lua參數,參數是以stack的狀態存在
float value = lua_tonumber(L, -1); //轉型成C的型態
lua_pushnumber(L, value*value); //回傳
return 1;
}
static int merge(lua_State *L){
const char *str1 = lua_tostring(L, -2); //注意取的參數的順序
const char *str2 = lua_tostring(L, -1);
char str[80];
strcpy(str, str1);
strcat(str, str2);
}
int luaopen_gg(lua_State *L){ // luaopen_*,載入時的主要進入點必需與.so和lua的lib同名
lua_register(L, "square", square); //跟lua註冊function
lua_register(L, "cube", cube);
lua_register(L, "merge", merge);
return 0;
}
compile : gcc -Wall -shared -fPIC -o gg.so -I/usr/include/lua5.1 test.c
lua file : test.lua
=======================
package.cpath = package.cpath .. ";./lib/gg.so" 載入不同路徑的C lib
package.cpath = package.cpath .. ";./lib/gg.so" 載入不同路徑的C lib
require("gg") //load gg lib
print(square(14))
print(cube(25))
print(merge("hello", " world!"))
function square_d(n)
ans = square(n)
print("lua value "..ans)
return(ans)
end
function square_d(n)
ans = square(n)
print("lua value "..ans)
return(ans)
end
c file : start.c
======================
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
lua_State* ls;
void squ(int n)
{
luaL_loadfile(ls, "func.lua"); \\load func.lua
printf("scan lua file start\n");
lua_pcall(ls, 0, 0, 0); \\run func.lua to know function
printf("scan lua file end\n");
lua_getglobal(ls, "square_d"); \\execute by function name
lua_pushnumber(ls, (double)n); \\set parameter
lua_call(ls, 1,1); \\run function ,1 parameter , 1 return value
int result = (int)lua_tonumber(ls, -1); \\get result
printf("c value %d\n", result);
lua_pop(ls, 1); \\popup return value and clear stack
}
int main( int argc, char** arvg)
{
ls = luaL_newstate();
luaL_openlibs(ls); \\load lua other libraries
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
lua_State* ls;
void squ(int n)
{
luaL_loadfile(ls, "func.lua"); \\load func.lua
printf("scan lua file start\n");
lua_pcall(ls, 0, 0, 0); \\run func.lua to know function
printf("scan lua file end\n");
lua_getglobal(ls, "square_d"); \\execute by function name
lua_pushnumber(ls, (double)n); \\set parameter
lua_call(ls, 1,1); \\run function ,1 parameter , 1 return value
int result = (int)lua_tonumber(ls, -1); \\get result
printf("c value %d\n", result);
lua_pop(ls, 1); \\popup return value and clear stack
}
int main( int argc, char** arvg)
{
ls = luaL_newstate();
luaL_openlibs(ls); \\load lua other libraries
squ(27);
lua_close(ls);
printf("PAUSE\n");
return 0;
}
lua_close(ls);
printf("PAUSE\n");
return 0;
}
compile : gcc -o start start.c -I/usr/include/lua5.1 -llua5.1
notes : 預先載入的lib : string, package, _G, os, table, math, coroutine, debug, io 要避免同名。
留言
張貼留言