Advertisement
Snusmumriken

Munimal C-Lua library

Dec 16th, 2018
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include "lua/lua.h"
  2. #include "lua/lauxlib.h"
  3.  
  4. int lua_summ(lua_State *L){
  5.     double a = luaL_checknumber(L, 1); /* get first arg */
  6.     double b = luaL_checknumber(L, 2); /* get second arg */
  7.     lua_pushnumber(L, a + b);          /* return function value */
  8.     return 1;                          /* count of args returned to lua */
  9. }
  10.  
  11. #if defined(_WIN32) || defined(_WIN64) /* windows compat */
  12. __declspec(dllexport)
  13. #endif
  14. int luaopen_mylib(lua_State *L) {       /* compile it to mylib.dll or mylib.so */
  15.     lua_newtable(L);                    /* create table */
  16.         lua_pushstring(L, "summ");      /* push "summ" as key*/
  17.         lua_pushcfunction(L, lua_summ); /* push lua_summ as value*/
  18.         lua_rawset(L, -3);              /* assign key and value to this table (it gone from stack, but not table) */
  19.  
  20.     return 1; /* return table to lua */
  21. }
  22.  
  23. /*
  24.     -- compile command like this:
  25.     -- tcc -shared -L./path_to_lua.lib -I./path_to_lua.h -llua51 main.c -o ./mylib.dll
  26.  
  27.     -- Usage on lua-side:
  28.     local lib = require'mylib'
  29.     print(lib.summ(10, 20)) --> 30
  30. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement