1/**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "mod_lua.h"
19#include "lua_apr.h"
20
21apr_table_t *ap_lua_check_apr_table(lua_State *L, int index)
22{
23    apr_table_t *t;
24    luaL_checkudata(L, index, "Apr.Table");
25    t = lua_unboxpointer(L, index);
26    return t;
27}
28
29
30void ap_lua_push_apr_table(lua_State *L, apr_table_t *t)
31{
32    lua_boxpointer(L, t);
33    luaL_getmetatable(L, "Apr.Table");
34    lua_setmetatable(L, -2);
35}
36
37static int lua_table_set(lua_State *L)
38{
39    apr_table_t    *t = ap_lua_check_apr_table(L, 1);
40    const char     *key = luaL_checkstring(L, 2);
41    const char     *val = luaL_checkstring(L, 3);
42
43    apr_table_set(t, key, val);
44    return 0;
45}
46
47static int lua_table_get(lua_State *L)
48{
49    apr_table_t    *t = ap_lua_check_apr_table(L, 1);
50    const char     *key = luaL_checkstring(L, 2);
51    const char     *val = apr_table_get(t, key);
52    lua_pushstring(L, val);
53    return 1;
54}
55
56static const luaL_Reg lua_table_methods[] = {
57    {"set", lua_table_set},
58    {"get", lua_table_get},
59    {0, 0}
60};
61
62
63int ap_lua_init(lua_State *L, apr_pool_t *p)
64{
65    luaL_newmetatable(L, "Apr.Table");
66    luaL_register(L, "apr_table", lua_table_methods);
67    lua_pushstring(L, "__index");
68    lua_pushstring(L, "get");
69    lua_gettable(L, 2);
70    lua_settable(L, 1);
71
72    lua_pushstring(L, "__newindex");
73    lua_pushstring(L, "set");
74    lua_gettable(L, 2);
75    lua_settable(L, 1);
76
77    return 0;
78}
79
80
81
82