2 * LuCI Core - Utility library
3 * Copyright (C) 2008 Steven Barth <steven@midlink.org>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #define LUCI_MODNAME "luci.cutil"
21 #define LUCI_MODDESC "LuCI Core Utility Library"
22 #define LUCI_MODCOPY "2008 Steven Barth"
25 /* Pythonic overloaded MOD operator */
26 static int luci__string_mod(lua_State *L) {
29 luaL_checkstring(L, 1);
32 /* Discard further arguments */
35 /* Get format and push it to the bottom of the stack */
36 lua_getfield(L, 1, "format");
39 /* If second argument is a table, unpack it */
40 if (lua_istable(L, 3)) {
43 luaL_checkstack(L, n, "too many results to unpack");
44 for (i=1; i<=n; i++) {
57 /* Instantiate a class */
58 static int luci__instantiate(lua_State *L) {
59 luaL_checktype(L, 1, LUA_TTABLE);
61 /* Create the object */
64 /* Create the metatable */
65 lua_createtable(L, 0, 1);
67 lua_setfield(L, -2, "__index");
68 lua_setmetatable(L, -2);
70 /* Move instance at the bottom of the stack */
73 /* Invoke constructor if it exists */
74 lua_getfield(L, 1, "__init__");
75 if (lua_isfunction(L, -1)) {
76 /* Put instance at the bottom for the 2nd time */
80 /* Call constructor */
82 lua_call(L, lua_gettop(L)-2, 0);
90 /* luci.cutil.class(baseclass) */
91 static int luci_class(lua_State *L) {
95 /* Create metatable and register parent class if any */
96 if (lua_gettop(L) > 1 && lua_istable(L, 1)) {
97 lua_createtable(L, 0, 2);
99 lua_setfield(L, -2, "__index");
101 lua_createtable(L, 0, 1);
104 /* Set instantiator */
105 lua_pushcfunction(L, luci__instantiate);
106 lua_setfield(L, -2, "__call");
108 lua_setmetatable(L, -2);
112 /* luci.cutil.instanceof(object, class) */
113 static int luci_instanceof(lua_State *L) {
119 if (lua_getmetatable(L, 1)) {
120 /* get parent class */
121 lua_getfield(L, -1, "__index");
122 while (lua_istable(L, -1)) {
123 /* parent class == class */
124 if (lua_equal(L, -1, 2)) {
129 /* remove last metatable */
132 /* get metatable of parent class */
133 if (lua_getmetatable(L, -1)) {
134 /* remove last parent class */
137 /* get next parent class */
138 lua_getfield(L, -1, "__index");
145 lua_pushboolean(L, stat);
150 /* luci.cutil.pcdata(obj) */
151 static int luci_pcdata(lua_State *L) {
152 if (lua_isnone(L, 1)) {
157 /* Discard anything else */
161 lua_pushvalue(L, lua_upvalueindex(1));
166 lua_pushvalue(L, lua_upvalueindex(2));
169 lua_pushvalue(L, lua_upvalueindex(3));
171 /* get gsub function */
172 lua_getfield(L, -3, "gsub");
175 /* tostring(obj):gsub(pattern, repl) */
181 /* Registration helper for luci.cutil.pcdata */
182 static void luci__register_pcdata(lua_State *L) {
184 lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
187 lua_pushliteral(L, "[&\"'<>]");
190 lua_createtable(L, 0, 5);
192 lua_pushliteral(L, "&");
193 lua_setfield(L, -2, "&");
194 lua_pushliteral(L, """);
195 lua_setfield(L, -2, "\"");
196 lua_pushliteral(L, "'");
197 lua_setfield(L, -2, "'");
198 lua_pushliteral(L, "<");
199 lua_setfield(L, -2, "<");
200 lua_pushliteral(L, ">");
201 lua_setfield(L, -2, ">");
203 /* register function */
204 lua_pushcclosure(L, luci_pcdata, 3);
205 lua_setfield(L, -2, "pcdata");
209 static const luaL_Reg registry[] = {
210 {"class", luci_class},
211 {"instanceof", luci_instanceof},
216 LUALIB_API int luaopen_luci_cutil(lua_State *L) {
217 luaL_register(L, LUCI_MODNAME, registry);
219 lua_pushliteral(L, LUCI_MODDESC);
220 lua_setfield(L, -2, "_DESCRIPTION");
222 lua_pushliteral(L, LUCI_MODCOPY);
223 lua_setfield(L, -2, "_COPYRIGHT");
225 /* Additional registrations */
226 luci__register_pcdata(L);
229 /* Register pythonic printf string operator */
230 lua_pushliteral(L, "");
231 lua_getmetatable(L, -1);
232 lua_pushcfunction(L, luci__string_mod);
233 lua_setfield(L, -2, "__mod");