diff options
Diffstat (limited to 'c-embed-lua/main.c')
| -rw-r--r-- | c-embed-lua/main.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/c-embed-lua/main.c b/c-embed-lua/main.c index 7ab678a..662ea70 100644 --- a/c-embed-lua/main.c +++ b/c-embed-lua/main.c | |||
| @@ -4,10 +4,21 @@ | |||
| 4 | 4 | ||
| 5 | #include "hi.h" | 5 | #include "hi.h" |
| 6 | 6 | ||
| 7 | static int l_sum(lua_State *L) { | ||
| 8 | double a = luaL_checknumber(L, 1); | ||
| 9 | double b = luaL_checknumber(L, 2); | ||
| 10 | lua_pushnumber(L, a + b); | ||
| 11 | return 1; | ||
| 12 | } | ||
| 13 | |||
| 7 | int main(void) { | 14 | int main(void) { |
| 8 | lua_State *L = luaL_newstate(); | 15 | lua_State *L = luaL_newstate(); |
| 9 | luaL_openlibs(L); | 16 | luaL_openlibs(L); |
| 10 | 17 | ||
| 18 | // You do not need to prefix name of the function with `c_`. I did it to | ||
| 19 | // make it clear in the lua script code that this is a custom function. | ||
| 20 | lua_register(L, "c_sum", l_sum); | ||
| 21 | |||
| 11 | // Not wise since this is not a null-terminated string. | 22 | // Not wise since this is not a null-terminated string. |
| 12 | /* luaL_dostring(L, (const char*)hi); */ | 23 | /* luaL_dostring(L, (const char*)hi); */ |
| 13 | 24 | ||
