C:
static int test_cfun(lua_State *L)
{
double d = lua_tonumber(L, 1);
lua_pushnumber(L, d);
return 1;
}
int main (void) {
int z; lua_State *L = lua_open();
lua_pushcfunction(L,test_cfun);
lua_setglobal(L, "test_cfun");
luaL_openlibs(L);
if (luaL_loadfile(L, "c.lua") || lua_pcall(L, 0, 0, 0))
printf("cannot run file: "); lua_tostring(L, -1);
lua_getglobal(L, "test_luafun");
lua_pushnumber(L, 2);
if (lua_pcall(L, 1, 1, 0) != 0)
printf( "error running function `f': ");
printf( "%s/n", lua_tostring(L, -1));
lua_close(L); return 0;
}
LUA:
function test_luafun(t)
return 'from lua:'..t..'|from c:'..test_cfun(33)
end
实现了lua与c的简单调用