Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. /* hellofunc.c (C) 2011 by Steve Litt
  2. * gcc -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 -llua5.1 hellofunc.c
  3. * Note the word "power" matches the string after the underscore in
  4. * function luaopen_power(). This is a must.
  5. * The -shared arg lets it compile to .so format.
  6. * The -fPIC is for certain situations and harmless in others.
  7. * On your computer, the -I and -l args will probably be different.
  8. */
  9.  
  10. #include <lua.h> /* Always include this */
  11. #include <lauxlib.h> /* Always include this */
  12. #include <lualib.h> /* Always include this */
  13.  
  14. static int isquare(lua_State *L){ /* Internal name of func */
  15. float rtrn = lua_tonumber(L, -1); /* Get the single number arg */
  16. printf("Top of square(), nbr=%f\n",rtrn);
  17. lua_pushnumber(L,rtrn*rtrn); /* Push the return */
  18. return 1; /* One return value */
  19. }
  20. static int icube(lua_State *L){ /* Internal name of func */
  21. float rtrn = lua_tonumber(L, -1); /* Get the single number arg */
  22. printf("Top of cube(), number=%f\n",rtrn);
  23. lua_pushnumber(L,rtrn*rtrn*rtrn); /* Push the return */
  24. return 1; /* One return value */
  25. }
  26.  
  27.  
  28. /* Register this file's functions with the
  29. * luaopen_libraryname() function, where libraryname
  30. * is the name of the compiled .so output. In other words
  31. * it's the filename (but not extension) after the -o
  32. * in the cc command.
  33. *
  34. * So for instance, if your cc command has -o power.so then
  35. * this function would be called luaopen_power().
  36. *
  37. * This function should contain lua_register() commands for
  38. * each function you want available from Lua.
  39. *
  40. */
  41. int luaopen_power(lua_State *L){
  42. lua_register(
  43. L, /* Lua state variable */
  44. "square", /* func name as known in Lua */
  45. isquare /* func name in this file */
  46. );
  47. lua_register(L,"cube",icube);
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement