On 5/3/07 12:10 PM, "Brian McCallister" <[EMAIL PROTECTED]> wrote:
> We should probably figure out how to avoid pushing the various values
> into the Apache2.Request metatable -- we are going to need a general
> purpose solution sooner rather than later, I think.
I think I mostly figured this out:
static int server_index(lua_State* L) {
server_rec* s = lua_unboxpointer(L, 1);
const char* key = luaL_checkstring(L, 2);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "server_index: %s", key);
if (0 == apr_strnatcmp("server_hostname", key)) {
lua_pushstring(L, s->server_hostname);
return 1;
}
return 0;
}
static const struct luaL_Reg server_methods[] = {
{"__index", server_index},
{NULL, NULL}
};
void apw_load_request_lmodule(lua_State *L) {
luaL_newmetatable(L, "Apache2.Request"); // [metatable]
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, request_methods); // [metatable]
lua_pop(L, 2);
luaL_newmetatable(L, "Apache2.Connection"); // [metatable]
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, connection_methods); // [metatable]
lua_pop(L, 2);
luaL_newmetatable(L, "Apache2.Server");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* pushes the metatable */
lua_settable(L, -3); /* metatable.__index = metatable */
luaL_openlib(L, NULL, server_methods, 0);
lua_pop(L, 2);
}
And this works in lua:
s = r.server
r:puts("Server name: " .. s.server_hostname .. "\n");
We could probably wrap the server_index type funtion into something so we
don't have to write huge if/else/ statements everytime. Mayme just copy the
way Lua does it with LuaL_Reg. Could wrap the whole metatable creation and
population thing, I suppose. Maybe do that in apr_lua?
static const struct apr_lua_reg server_methods[] = {
"blah", get_blah,
...
{NULL, NULL}
}
apr_lua_register("Apache2.Server", server_methods);
Would take care of all the details. Would need to be able to get set, maybe
pass that in as to whetehr it is __index or __newindex???
(Just thinking out load on the apr_lua stuff...)
--
Brian Akins
Chief Operations Engineer
Turner Digital Media Technologies