PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/lua/embed2/embed2.c

#
C | 233 lines | 173 code | 24 blank | 36 comment | 19 complexity | e4b0c793e26ab99e7c01c2a0bb27e4e4 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* embed2.c some more test for an embeded interpreter
  2. This will go a bit further as it will pass values to and from the lua code.
  3. It uses less of the SWIG code, and more of the raw lua API's
  4. What it will do is load the wrappered lib, load runme.lua and then call some functions.
  5. To make life easier, all the printf's have either [C] or [Lua] at the start
  6. so you can see where they are coming from.
  7. We will be using the luaL_dostring()/lua_dostring() function to call into lua
  8. */
  9. /* Deal with Microsoft's attempt at deprecating C standard runtime functions */
  10. #if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  11. # define _CRT_SECURE_NO_DEPRECATE
  12. #endif
  13. /* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
  14. #if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
  15. # define _SCL_SECURE_NO_DEPRECATE
  16. #endif
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <lua.h>
  20. #include <lauxlib.h>
  21. #include <lualib.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. /* the SWIG wrappered library */
  25. extern int luaopen_example(lua_State*L);
  26. /* This is an example of how to call the Lua function
  27. int add(int,int)
  28. its very tedious, but gives you an idea of the issues involded.
  29. (look below for a better idea)
  30. */
  31. int call_add(lua_State *L,int a,int b,int* res) {
  32. int top;
  33. /* ok, here we go:
  34. push a, push b, call 'add' check & return res
  35. */
  36. top=lua_gettop(L); /* for later */
  37. lua_pushstring(L, "add"); /* function name */
  38. lua_gettable(L, LUA_GLOBALSINDEX); /* function to be called */
  39. if (!lua_isfunction(L,-1)) {
  40. printf("[C] error: cannot find function 'add'\n");
  41. lua_settop(L,top); // reset
  42. return 0;
  43. }
  44. lua_pushnumber(L,a);
  45. lua_pushnumber(L,b);
  46. if (lua_pcall(L, 2, 1, 0) != 0) /* call function with 2 arguments and 1 result */
  47. {
  48. printf("[C] error running function `add': %s\n",lua_tostring(L, -1));
  49. lua_settop(L,top); // reset
  50. return 0;
  51. }
  52. // check results
  53. if (!lua_isnumber(L,-1)) {
  54. printf("[C] error: returned value is not a number\n");
  55. lua_settop(L,top); // reset
  56. return 0;
  57. }
  58. *res=(int)lua_tonumber(L,-1);
  59. lua_settop(L,top); /* reset stack */
  60. return 1; // ok
  61. }
  62. /* This is a variargs call function for calling from C into Lua.
  63. Original Code from Programming in Lua (PIL) by Roberto Ierusalimschy
  64. ISBN 85-903798-1-7
  65. http://www.lua.org/pil/25.3.html
  66. This has been modified slightly to make it compile, and its still a bit rough.
  67. But it gives the idea of how to make it work.
  68. */
  69. int call_va (lua_State *L,const char *func, const char *sig, ...) {
  70. va_list vl;
  71. int narg, nres; /* number of arguments and results */
  72. int top;
  73. top=lua_gettop(L); /* for later */
  74. va_start(vl, sig);
  75. lua_getglobal(L, func); /* get function */
  76. /* push arguments */
  77. narg = 0;
  78. while (*sig) { /* push arguments */
  79. switch (*sig++) {
  80. case 'd': /* double argument */
  81. lua_pushnumber(L, va_arg(vl, double));
  82. break;
  83. case 'i': /* int argument */
  84. lua_pushnumber(L, va_arg(vl, int));
  85. break;
  86. case 's': /* string argument */
  87. lua_pushstring(L, va_arg(vl, char *));
  88. break;
  89. case '>':
  90. goto endwhile;
  91. default:
  92. printf("invalid option (%c)\n", *(sig - 1));
  93. goto fail;
  94. }
  95. narg++;
  96. /* do we need this?*/
  97. /* luaL_checkstack(L, 1, "too many arguments"); */
  98. }
  99. endwhile:
  100. /* do the call */
  101. nres = (int)strlen(sig); /* number of expected results */
  102. if (lua_pcall(L, narg, nres, 0) != 0) /* do the call */
  103. {
  104. printf("error running function `%s': %s\n",func, lua_tostring(L, -1));
  105. goto fail;
  106. }
  107. /* retrieve results */
  108. nres = -nres; /* stack index of first result */
  109. while (*sig) { /* get results */
  110. switch (*sig++) {
  111. case 'd': /* double result */
  112. if (!lua_isnumber(L, nres)) {
  113. printf("wrong result type\n");
  114. goto fail;
  115. }
  116. *va_arg(vl, double *) = lua_tonumber(L, nres);
  117. break;
  118. case 'i': /* int result */
  119. if (!lua_isnumber(L, nres)) {
  120. printf("wrong result type\n");
  121. goto fail;
  122. }
  123. *va_arg(vl, int *) = (int)lua_tonumber(L, nres);
  124. break;
  125. case 's': /* string result */
  126. if (!lua_isstring(L, nres)) {
  127. printf("wrong result type\n");
  128. goto fail;
  129. }
  130. strcpy(va_arg(vl, char *),lua_tostring(L, nres));/* WARNING possible buffer overflow */
  131. break;
  132. default: {
  133. printf("invalid option (%c)", *(sig - 1));
  134. goto fail;
  135. }
  136. }
  137. nres++;
  138. }
  139. va_end(vl);
  140. lua_settop(L,top); /* reset stack */
  141. return 1; /* ok */
  142. fail:
  143. lua_settop(L,top); /* reset stack */
  144. return 0; /* error */
  145. }
  146. int main(int argc,char* argv[]) {
  147. lua_State *L;
  148. int ok;
  149. int res;
  150. char str[80];
  151. printf("[C] Welcome to the simple embedded Lua example v2\n");
  152. printf("[C] We are in C\n");
  153. printf("[C] opening a Lua state & loading the libraries\n");
  154. L=lua_open();
  155. luaopen_base(L);
  156. luaopen_string(L);
  157. luaopen_math(L);
  158. printf("[C] now loading the SWIG wrappered library\n");
  159. luaopen_example(L);
  160. printf("[C] all looks ok\n");
  161. printf("\n");
  162. printf("[C] lets load the file 'runme.lua'\n");
  163. printf("[C] any lua code in this file will be executed\n");
  164. if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) {
  165. printf("[C] ERROR: cannot run lua file: %s",lua_tostring(L, -1));
  166. exit(3);
  167. }
  168. printf("[C] We are now back in C, all looks ok\n");
  169. printf("\n");
  170. printf("[C] lets call the Lua function 'add(1,1)'\n");
  171. printf("[C] using the C function 'call_add'\n");
  172. ok=call_add(L,1,1,&res);
  173. printf("[C] the function returned %d with value %d\n",ok,res);
  174. printf("\n");
  175. printf("[C] lets do this rather easier\n");
  176. printf("[C] we will call the same Lua function using a generic C function 'call_va'\n");
  177. ok=call_va(L,"add","ii>i",1,1,&res);
  178. printf("[C] the function returned %d with value %d\n",ok,res);
  179. printf("\n");
  180. printf("[C] we will now use the same generic C function to call 'append(\"cat\",\"dog\")'\n");
  181. ok=call_va(L,"append","ss>s","cat","dog",str);
  182. printf("[C] the function returned %d with value %s\n",ok,str);
  183. printf("\n");
  184. printf("[C] we can also make some bad calls to ensure the code doesn't fail\n");
  185. printf("[C] calling adds(1,2)\n");
  186. ok=call_va(L,"adds","ii>i",1,2,&res);
  187. printf("[C] the function returned %d with value %d\n",ok,res);
  188. printf("[C] calling add(1,'fred')\n");
  189. ok=call_va(L,"add","is>i",1,"fred",&res);
  190. printf("[C] the function returned %d with value %d\n",ok,res);
  191. printf("\n");
  192. printf("[C] Note: no protection if you mess up the va-args, this is C\n");
  193. printf("\n");
  194. printf("[C] Finally we will call the wrappered gcd function gdc(6,9):\n");
  195. printf("[C] This will pass the values to Lua, then call the wrappered function\n");
  196. printf(" Which will get the values from Lua, call the C code \n");
  197. printf(" and return the value to Lua and eventually back to C\n");
  198. printf("[C] Certainly not the best way to do it :-)\n");
  199. ok=call_va(L,"gcd","ii>i",6,9,&res);
  200. printf("[C] the function returned %d with value %d\n",ok,res);
  201. printf("\n");
  202. printf("[C] all finished, closing the lua state\n");
  203. lua_close(L);
  204. return 0;
  205. }