apr_lua.c revision fa21815c2fce84c8a6f56bccd3ad4c0537c6f1fd
842ae4bd224140319ae7feec1872b93dfd491143fielding#include "apr.h"
842ae4bd224140319ae7feec1872b93dfd491143fielding#include "apr_tables.h"
842ae4bd224140319ae7feec1872b93dfd491143fielding
842ae4bd224140319ae7feec1872b93dfd491143fielding#include "lua.h"
842ae4bd224140319ae7feec1872b93dfd491143fielding#include "lauxlib.h"
842ae4bd224140319ae7feec1872b93dfd491143fielding#include "lualib.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "apr_lua.h"
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/**
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * make a userdata out of a C pointer, and vice versa
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * instead of using lightuserdata
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd */
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd#ifndef lua_boxpointer
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd#define lua_boxpointer(L,u) (*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#define lua_unboxpointer(L,i) (*(void **)(lua_touserdata(L, i)))
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
2261031aa94be82d7e6b1b8c367afc1b282317f5ianhapr_table_t* check_apr_table(lua_State* L, int index) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding luaL_checkudata(L, index, "Apr.Table");
2261031aa94be82d7e6b1b8c367afc1b282317f5ianh apr_table_t* t = (apr_table_t*)lua_unboxpointer(L, index);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return t;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
cccd31fa4a72fe23cc3249c06db181b274a55a69gsteinvoid apl_push_apr_table(lua_State* L, const char *name, apr_table_t *t) {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein lua_boxpointer(L, t);
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein luaL_getmetatable(L, "Apr.Table");
1b21d7b3d97def358b2e923655edeb16613a1c31gstein lua_setmetatable(L, -2);
be3223a6a18d9a3a3cf7155d5430a5d92bcddceegstein lua_setfield(L, -2, name);
573394373c777e1624a481160f3d02f8fb09f3ffrjung}
e302f38fd646764ce1a1e1c578d794aef514a9e5sf
1b21d7b3d97def358b2e923655edeb16613a1c31gsteinstatic int lua_table_set(lua_State* L) {
1b21d7b3d97def358b2e923655edeb16613a1c31gstein apr_table_t *t = check_apr_table(L, 1);
1b21d7b3d97def358b2e923655edeb16613a1c31gstein const char* key = luaL_checkstring(L, 2);
1b21d7b3d97def358b2e923655edeb16613a1c31gstein const char* val = luaL_checkstring(L, 3);
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein apr_table_set(t, key, val);
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein return 0;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein}
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gsteinstatic int lua_table_get(lua_State* L) {
1b21d7b3d97def358b2e923655edeb16613a1c31gstein apr_table_t *t = check_apr_table(L, 1);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char* key = luaL_checkstring(L, 2);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb const char *val = apr_table_get(t, key);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lua_pushstring(L, val);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic const luaL_reg lua_table_methods[] = {
cf8fe49d9d89cfa4b62cb2b1376ca6f25b81b362trawick {"set", lua_table_set},
874fa3c6bbef1b4ab4bed0a2ff9852b21ea1b187trawick {"get", lua_table_get},
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding {0, 0}
d37a236a4b64d0aeb4a8bbfd3978503af8c82765sf};
d37a236a4b64d0aeb4a8bbfd3978503af8c82765sf
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
963f8b44ac95132458ea3b6aaa8ebc135188e473takashiint apr_lua_init(lua_State *L, apr_pool_t *p) {
963f8b44ac95132458ea3b6aaa8ebc135188e473takashi luaL_newmetatable(L, "Apr.Table");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding luaL_openlib(L, "apr_table", lua_table_methods, 0);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lua_pushstring(L, "__index");
a7318397b355119d990d1f311b951dea2aafc53arbb lua_pushstring(L, "get");
a7318397b355119d990d1f311b951dea2aafc53arbb lua_gettable(L, 2);
a7318397b355119d990d1f311b951dea2aafc53arbb lua_settable(L, 1);
a7318397b355119d990d1f311b951dea2aafc53arbb
cb9e6e5c78f5a1690214e9548250fc6af1fc73b5wrowe lua_pushstring(L, "__newindex");
cb9e6e5c78f5a1690214e9548250fc6af1fc73b5wrowe lua_pushstring(L, "set");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lua_gettable(L, 2);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding lua_settable(L, 1);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a3228198adb03e6996f7738c361a612777ecab6aaron return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding