aboutsummaryrefslogtreecommitdiff
path: root/content
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-05-24 08:43:08 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-05-24 08:43:08 +0200
commitda066db1d2cb9496528c5a6b064b9bea17570ba9 (patch)
treee9a0e4a4d28398632ed4a866fa71d20bc930f7b0 /content
parentdcf5ee0982f7dbf22a8dcb05063976299216d246 (diff)
downloadmitjafelicijan.com-da066db1d2cb9496528c5a6b064b9bea17570ba9.tar.gz
Note: Extending Lua with C
Diffstat (limited to 'content')
-rw-r--r--content/notes/extend-lua-with-custom-c.md53
-rw-r--r--content/notes/parse-rss-with-lua.md2
2 files changed, 54 insertions, 1 deletions
diff --git a/content/notes/extend-lua-with-custom-c.md b/content/notes/extend-lua-with-custom-c.md
new file mode 100644
index 0000000..ba0a046
--- /dev/null
+++ b/content/notes/extend-lua-with-custom-c.md
@@ -0,0 +1,53 @@
1---
2title: Extend Lua with custom C functions using Clang
3url: extend-lua-with-custom-c.html
4date: 2023-05-24
5type: notes
6draft: false
7tags: [lua, clang, c]
8---
9
10Here is a boilerplate for extending Lua with custom C functions. This requires
11Clang and Lua 5.1 to be installed. GCC can be used instead of Clang, but the
12Makefile will need to be modified.
13
14- nativefunc.c
15
16 ```c
17 #include <lua.h>
18 #include <lauxlib.h>
19
20 static int l_mult50(lua_State *L) {
21 double number = luaL_checknumber(L, 1);
22 lua_pushnumber(L, number * 50);
23 return 1;
24 }
25
26 int luaopen_nativefunc(lua_State *L) {
27 static const struct luaL_Reg nativeFuncLib[] = {{"mult50", l_mult50}, {NULL, NULL}};
28
29 luaL_register(L, "nativelib", nativeFuncLib);
30 return 1;
31 }
32 ```
33
34- main.lua
35
36 ```lua
37 require "nativefunc"
38 print(nativelib.mult50(50))
39 ```
40
41- Makefile
42
43 ```Makefile
44 CC = clang
45 CFLAGS =
46 INCLUDES = `pkg-config lua5.1 --cflags-only-I`
47
48 all:
49 $(CC) -shared -o nativefunc.so -fPIC nativefunc.c $(CFLAGS) $(INCLUDES)
50
51 clean:
52 rm *.so
53 ```
diff --git a/content/notes/parse-rss-with-lua.md b/content/notes/parse-rss-with-lua.md
index 9c7ca84..1757d45 100644
--- a/content/notes/parse-rss-with-lua.md
+++ b/content/notes/parse-rss-with-lua.md
@@ -1,7 +1,7 @@
1--- 1---
2title: Parse RSS feeds with Lua 2title: Parse RSS feeds with Lua
3url: parse-rss-with-lua.html 3url: parse-rss-with-lua.html
4date: 2023-05-24 4date: 2023-05-23
5type: notes 5type: notes
6draft: false 6draft: false
7tags: [lua, rss] 7tags: [lua, rss]