lua-utils.c - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/lua-utils.c
blob: 095cac89d03da925c03bc944113a68b4de3d2cec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

/* lua-utils.c
 *
 * Copyright (C) 2009 Francesco Abbate
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
#include <stdio.h>
#include "lua-utils.h"
#include "gs-types.h"
static char const * const CACHE_FIELD_NAME = "__cache";
const struct luaL_Reg *
mlua_find_method (const struct luaL_Reg *p, const char *key)
{
 for (/* */; p->name; p++)
 {
 if (strcmp (p->name, key) == 0)
	return p;
 }
 return NULL;
}
int
mlua_get_property (lua_State *L, const struct luaL_Reg *p, bool use_cache)
{
 int rval;
 bool cache_is_new = false;
 if (! use_cache)
 return p->func (L);
 lua_getfenv (L, 1);
 lua_getfield (L, -1, CACHE_FIELD_NAME);
 if (lua_isnil (L, -1))
 {
 lua_pop (L, 1);
 lua_newtable (L);
 lua_pushvalue (L, -1);
 lua_setfield (L, -3, CACHE_FIELD_NAME);
 cache_is_new = true;
 }
 if (! cache_is_new)
 {
 lua_getfield (L, -1, p->name);
 if (! lua_isnil (L, -1))
	return 1;
 lua_pop (L, 1);
 }
 rval = p->func (L);
 if (rval == 1)
 {
 lua_pushvalue (L, -1);
 lua_setfield (L, -3, p->name);
 return 1;
 }
 return rval;
}
int
mlua_index_with_properties (lua_State *L,
			 const struct luaL_Reg *properties,
			 const struct luaL_Reg *methods,
			 bool use_cache)
{
 char const * key;
 const struct luaL_Reg *reg;
 key = lua_tostring (L, 2);
 if (key == NULL)
 return 0;
 reg = mlua_find_method (methods, key);
 if (reg)
 {
 lua_pushcfunction (L, reg->func);
 return 1;
 }
 reg = mlua_find_method (properties, key);
 if (reg)
 {
 return mlua_get_property (L, reg, use_cache);
 }
 return 0;
}
int
mlua_newindex_with_properties (lua_State *L, const struct luaL_Reg *properties)
{
 char const * key;
 const struct luaL_Reg *reg;
 key = lua_tostring (L, 2);
 if (key == NULL)
 return 0;
 reg = mlua_find_method (properties, key);
 if (reg)
 {
 lua_remove (L, 2);
 return reg->func (L);
 }
 return luaL_error (L, "invalid property for %s object", full_type_name (L, 1));
}
lua_Number
mlua_named_optnumber (lua_State *L, int index, const char *key,
		 lua_Number default_value)
{
 lua_Number r;
 lua_getfield (L, index, key);
 r = luaL_optnumber (L, -1, default_value);
 lua_pop (L, 1);
 return r;
}
const char *
mlua_named_optstring (lua_State *L, int index, const char *key,
		 const char * default_value)
{
 const char * r;
 lua_getfield (L, index, key);
 r = luaL_optstring (L, -1, default_value);
 lua_pop (L, 1);
 return r;
}
lua_Number
mlua_named_number (lua_State *L, int index, const char *key)
{
 lua_Number r;
 lua_getfield (L, index, key);
 if (! lua_isnumber (L, -1))
 luaL_error (L, "number expected");
 r = lua_tonumber (L, -1);
 lua_pop (L, 1);
 return r;
}
const char *
mlua_named_string (lua_State *L, int index, const char *key)
{
 const char * r;
 lua_getfield (L, index, key);
 if (! lua_isstring (L, -1))
 luaL_error (L, "string expected");
 r = lua_tostring (L, -1);
 lua_pop (L, 1);
 return r;
}
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月20日 06:59:56 +0000

AltStyle によって変換されたページ (->オリジナル) /