On Apr 30, 2007, at 2:02 PM, Akins, Brian wrote:

Probably more changes than needs to be in one patch:

- use hooks for:
-- wombat_open - called by create_vm

+1 Perfect!

-- wombat_request - called instead of apw_request_push

I would like to maintain a function which is analogous to lua_pushstring() and lua_pushinteger() for pushing the request_rec into a function call or whatnot from the C side.

Will this work with the hook? (I am a hook newb).


-added apr_lua.c and .h - only handles tables for now. Can be extended to do
more in future.


Index: apr_lua.c
===================================================================
--- apr_lua.c   (revision 0)
+++ apr_lua.c   (revision 0)
@@ -0,0 +1,55 @@
+#include "apr.h"
+#include "apr_tables.h"
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+
+#define lua_unboxpointer(L,i)      (*(void **)(lua_touserdata(L, i)))
+
+static apr_table_t* check_apr_table(lua_State* L, int index) {
+    luaL_checkudata(L, index, "Apr.Table");
+    apr_table_t* t = (apr_table_t*)lua_unboxpointer(L, index);
+    return t;
+}
+
+static int lua_table_set(lua_State* L) {
+    apr_table_t *t = check_apr_table(L, 1);
+    const char* key = luaL_checkstring(L, 2);
+    const char* val = luaL_checkstring(L, 3);
+
+    apr_table_set(t, key, val);
+    return 0;
+}
+
+static int lua_table_get(lua_State* L) {
+    apr_table_t *t = check_apr_table(L, 1);
+    const char* key = luaL_checkstring(L, 2);
+    const char *val = apr_table_get(t, key);
+    lua_pushstring(L, val);
+    return 1;
+}
+
+static const luaL_reg lua_table_methods[] = {
+    {"set", lua_table_set},
+    {"get", lua_table_get},
+    {0, 0}
+};

Even though these are static, we might want to be careful in naming as these are reaching into lua's namespace (lua_* and luaL_*).

+
+
+int apr_lua_init(lua_State *L, apr_pool_t *p) {
+    luaL_newmetatable(L, "Apr.Table");
+    luaL_openlib(L, "apr_table", lua_table_methods, 0);
+    lua_pushstring(L, "__index");
+    lua_pushstring(L, "get");
+    lua_gettable(L, 2);
+    lua_settable(L, 1);
+
+    lua_pushstring(L, "__newindex");
+    lua_pushstring(L, "set");
+    lua_gettable(L, 2);
+    lua_settable(L, 1);
+
+    return 0;
+}

Why pass the pool in (other than matching the hook form, but this isn't invoked via ) and what is the general policy on borrowing from the apr namespace for an exported function?


-Brian


Reply via email to