Added exposed C functions to Lua embed
| Author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-07-17 17:13:45 +0200 |
| Committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-07-17 17:13:45 +0200 |
| Commit | c83e5d2713334d39bf0718f626cab387f7331a98 (patch) |
|
-rw-r--r-- |
c-embed-lua/hi.h | 13 | |
-rw-r--r-- |
c-embed-lua/hi.lua | 7 | |
-rw-r--r-- |
c-embed-lua/main.c | 11 |
3 files changed, 29 insertions, 2 deletions
| diff --git a/c-embed-lua/hi.h b/c-embed-lua/hi.h | |||
| ... | |||
| 169 | 0x28, 0x29, 0x20, 0x64, 0x6f, 0x0a, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, |
169 | 0x28, 0x29, 0x20, 0x64, 0x6f, 0x0a, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, |
| 170 | 0x28, 0x6c, 0x69, 0x6e, 0x65, 0x29, 0x0a, 0x65, 0x6e, 0x64, 0x0a, 0x0a, |
170 | 0x28, 0x6c, 0x69, 0x6e, 0x65, 0x29, 0x0a, 0x65, 0x6e, 0x64, 0x0a, 0x0a, |
| 171 | 0x66, 0x69, 0x6c, 0x65, 0x3a, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x28, 0x29, |
171 | 0x66, 0x69, 0x6c, 0x65, 0x3a, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x28, 0x29, |
| 172 | 0x0a |
172 | 0x0a, 0x0a, 0x2d, 0x2d, 0x20, 0x45, 0x78, 0x70, 0x6f, 0x73, 0x69, 0x6e, |
| 173 | 0x67, 0x20, 0x43, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, |
||
| 174 | 0x73, 0x2e, 0x0a, 0x0a, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x28, 0x22, |
||
| 175 | 0x45, 0x78, 0x70, 0x6f, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x43, 0x20, 0x66, |
||
| 176 | 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x29, 0x0a, 0x0a, |
||
| 177 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, |
||
| 178 | 0x20, 0x3d, 0x20, 0x63, 0x5f, 0x73, 0x75, 0x6d, 0x28, 0x33, 0x2c, 0x20, |
||
| 179 | 0x35, 0x29, 0x0a, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x28, 0x22, 0x53, 0x75, |
||
| 180 | 0x6d, 0x20, 0x69, 0x73, 0x22, 0x2c, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, |
||
| 181 | 0x74, 0x29, 0x0a |
||
| 173 | }; |
182 | }; |
| 174 | unsigned int hi_len = 2041; |
183 | unsigned int hi_len = 2151; |
| diff --git a/c-embed-lua/hi.lua b/c-embed-lua/hi.lua | |||
| ... | |||
| 104 | end |
104 | end |
| 105 | 105 | ||
| 106 | file:close() |
106 | file:close() |
| 107 | |||
| 108 | -- Exposing C functions. |
||
| 109 | |||
| 110 | header("Exposing C functions") |
||
| 111 | |||
| 112 | local result = c_sum(3, 5) |
||
| 113 | print("Sum is", result) |
||
| diff --git a/c-embed-lua/main.c b/c-embed-lua/main.c | |||
| ... | |||
| 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); |
| 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); |
||
| 10 | 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); */ |
| ... | |||