int main(int argc,char* argv[]) { lua_State *L; int ok; int res; char str[80]; printf("[C] Welcome to the simple embedded Lua example v2\n"); printf("[C] We are in C\n"); printf("[C] opening a Lua state & loading the libraries\n"); L=lua_open(); luaopen_base(L); luaopen_string(L); luaopen_math(L); printf("[C] now loading the SWIG wrappered library\n"); luaopen_example(L); printf("[C] all looks ok\n"); printf("\n"); printf("[C] lets load the file 'runme.lua'\n"); printf("[C] any lua code in this file will be executed\n"); if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) { printf("[C] ERROR: cannot run lua file: %s",lua_tostring(L, -1)); exit(3); } printf("[C] We are now back in C, all looks ok\n"); printf("\n"); printf("[C] lets call the Lua function 'add(1,1)'\n"); printf("[C] using the C function 'call_add'\n"); ok=call_add(L,1,1,&res); printf("[C] the function returned %d with value %d\n",ok,res); printf("\n"); printf("[C] lets do this rather easier\n"); printf("[C] we will call the same Lua function using a generic C function 'call_va'\n"); ok=call_va(L,"add","ii>i",1,1,&res); printf("[C] the function returned %d with value %d\n",ok,res); printf("\n"); printf("[C] we will now use the same generic C function to call 'append(\"cat\",\"dog\")'\n"); ok=call_va(L,"append","ss>s","cat","dog",str); printf("[C] the function returned %d with value %s\n",ok,str); printf("\n"); printf("[C] we can also make some bad calls to ensure the code doesn't fail\n"); printf("[C] calling adds(1,2)\n"); ok=call_va(L,"adds","ii>i",1,2,&res); printf("[C] the function returned %d with value %d\n",ok,res); printf("[C] calling add(1,'fred')\n"); ok=call_va(L,"add","is>i",1,"fred",&res); printf("[C] the function returned %d with value %d\n",ok,res); printf("\n"); printf("[C] Note: no protection if you mess up the va-args, this is C\n"); printf("\n"); printf("[C] Finally we will call the wrappered gcd function gdc(6,9):\n"); printf("[C] This will pass the values to Lua, then call the wrappered function\n"); printf(" Which will get the values from Lua, call the C code \n"); printf(" and return the value to Lua and eventually back to C\n"); printf("[C] Certainly not the best way to do it :-)\n"); ok=call_va(L,"gcd","ii>i",6,9,&res); printf("[C] the function returned %d with value %d\n",ok,res); printf("\n"); printf("[C] all finished, closing the lua state\n"); lua_close(L); return 0; }
int main(void) { char *fname = "TestConfig.lua"; lua_State *L = luaL_newstate(); load(L,fname); /*load confile file*/ luaL_openlibs(L); double x = 0.5, y = 0.5; double result; call_va(L,"f","dd>d",x,y,&result); printf("result=%g\n",result); /*result=0.239713*/ return 0; }
int main(void) { lua_State *L = luaL_newstate(); /* opens Lua */ luaL_openlibs(L); /* opens the standard libraries */ if (luaL_loadfile(L, "f.lua") || lua_pcall(L, 0, 0, 0)) error(L, "cannot run config. file: %s", lua_tostring(L, -1)); double x = -1.0; double y = 2.0; double z = 0.0; call_va(L, "f", "dd>d", x, y, &z); printf("z %f\n", z); return 0; }
void call_worker_message(char *id, char *msg){ config_t *cfg; cfg = get_config(); call_va(cfg->master_lua_worker,"ss>",id,msg); }
double newf(lua_State *L, double x, double y) { double z = 0; call_va(L, "f", "dd>d", x, y, &z); return z; }