This commit is contained in:
hladu357 2024-06-04 14:16:36 +02:00
parent 392635af86
commit b61a0e1e62
7 changed files with 38 additions and 8 deletions

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
so/testlib.so: src/module.c
gcc -O2 -shared -fpic -I /usr/include/luajit-2.1 -o so/clib.so src/module.c
PHONY: run
run:
/usr/local/openresty/bin/openresty -p .

View File

@ -9,15 +9,19 @@ events {
http {
access_log /dev/stdout;
lua_package_path "$prefix/lua/?.lua;;";
lua_package_cpath '$prefix/so/?.so;;';
server {
listen 80;
location / {
default_type text/plain;
content_by_lua_block {
local key_header = require "key_header"
key_header.greet("a Lua module")
local clib = require("clib")
local llib = require("llib")
ngx.say( clib.func("Hello from") )
ngx.say( llib.func("Hello from") )
}
}
}

0
logs/error.log Normal file
View File

View File

@ -1,5 +0,0 @@
local _M = {}
function _M.greet(name)
ngx.say("Greetings from ", name)
end
return _M

6
lua/llib.lua Normal file
View File

@ -0,0 +1,6 @@
local _M = {}
function _M.func(str)
ngx.say(str, " lua function")
end
return _M

BIN
so/clib.so Executable file

Binary file not shown.

19
src/module.c Normal file
View File

@ -0,0 +1,19 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
static int func(lua_State *L) {
const char *arg = luaL_checkstring(L, 1); // pop argument
lua_pushfstring(L, "%s C func!", arg); // push result
return 1; // number of results
}
static const struct luaL_Reg funcs[2] = {
{"func", func},
{NULL, NULL}
};
int luaopen_clib(lua_State *L) {
luaL_newlib(L, funcs);
return 1;
}