From cd6644ea4ddc78597934ab0ef5ba50e3c3daa927 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sat, 8 Jul 2023 23:25:41 +0200 Subject: Moved to a simpler SSG --- public/extend-lua-with-custom-c.html | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 public/extend-lua-with-custom-c.html (limited to 'public/extend-lua-with-custom-c.html') diff --git a/public/extend-lua-with-custom-c.html b/public/extend-lua-with-custom-c.html new file mode 100755 index 0000000..c27548a --- /dev/null +++ b/public/extend-lua-with-custom-c.html @@ -0,0 +1,39 @@ +Extend Lua with custom C functions using Clang

Extend Lua with custom C functions using Clang

May 23, 2023

Here is a boilerplate for extending Lua with custom C functions. This requires +Clang and Lua 5.1 to be installed. GCC can be used instead of Clang, but the +Makefile will need to be modified.

  • nativefunc.c

    #include <lua.h>
    +#include <lauxlib.h>
    +
    +static int l_mult50(lua_State *L) {
    +  double number = luaL_checknumber(L, 1);
    +  lua_pushnumber(L, number * 50);
    +  return 1;
    +}
    +
    +int luaopen_nativefunc(lua_State *L) {
    +  static const struct luaL_Reg nativeFuncLib[] = {{"mult50", l_mult50}, {NULL, NULL}};
    +
    +  luaL_register(L, "nativelib", nativeFuncLib);
    +  return 1;
    +}
    +
  • main.lua

    require "nativefunc"
    +print(nativelib.mult50(50))
    +
  • Makefile

    CC       = clang
    +CFLAGS   =
    +INCLUDES = `pkg-config lua5.1 --cflags-only-I`
    +
    +all:
    +  $(CC) -shared -o nativefunc.so -fPIC nativefunc.c $(CFLAGS) $(INCLUDES)
    +
    +clean:
    +  rm *.so
    +
\ No newline at end of file -- cgit v1.2.3