Advertisement
Guest User

Untitled

a guest
May 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. //Returns the sin of a number using C code
  2. //as described in The Lua Programming Language, p242
  3. #include "lua.h"
  4. #include "lauxlib.h"
  5. #include "math.h"
  6.  
  7. //function body
  8. static int l_sin (lua_State *L) {
  9.         //double d = luaL_checknumber(L, 1); //get arg off the stack
  10.         double d = lua_tonumber(L, 1); //get arg off the stack
  11.         lua_pushnumber(L, sin(d));     //push result back onto the stack
  12.         return 1;                      //return the number of results
  13. }
  14.  
  15. /*
  16. static int dumbstuff (lua_State *L) {
  17.         char dumb [] = "so dumb\n";
  18.         lua_pushstring(L, dumb);
  19.         return 1;
  20. }
  21. */
  22.  
  23. //register the function with Lua
  24. static const struct luaL_Reg mylib [] = {
  25.         {"mysin",l_sin},
  26.         {NULL,NULL} /* this is a "sentinel" for the end of this list structure */
  27. };
  28.  
  29. //this didn't work because lua was looking for luaopen_sintest in sintest.so
  30. //int luaopen_mylib (lua_State *L) {
  31. int luaopen_sintest (lua_State *L) {
  32.         //luaL_register(L, "sintest",mylib);
  33.         luaL_register(L, "hobjob",mylib);  //type require "sintest" but use namespace "hobjob"
  34.         return 1;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement