Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua-utils.c
More file actions
Latest commit
167 lines (148 loc) · 3.8 KB
/
Copy pathlua-utils.c
File metadata and controls
167 lines (148 loc) · 3.8 KB
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
/* 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"
charconst*constCACHE_FIELD_NAME="__cache";
conststructluaL_Reg*
mlua_find_method (conststructluaL_Reg*p, constchar*key)
{
for (/* */; p->name; p++)
{
if (strcmp (p->name, key) ==0)
returnp;
}
returnNULL;
}
int
mlua_get_property (lua_State*L, conststructluaL_Reg*p, booluse_cache)
{
intrval;
boolcache_is_new= false;
if (! use_cache)
returnp->func (L);
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, 1, CACHE_FIELD_NAME);
cache_is_new= true;
}
if (! cache_is_new)
{
lua_getfield (L, -1, p->name);
if (! lua_isnil (L, -1))
return1;
lua_pop (L, 1);
}
rval=p->func (L);
if (rval==1)
{
lua_pushvalue (L, -1);
lua_setfield (L, -3, p->name);
return1;
}
returnrval;
}
void
mlua_null_cache (lua_State*L, intindex)
{
lua_pushnil (L);
lua_setfield (L, index, CACHE_FIELD_NAME);
}
int
mlua_index_with_properties (lua_State*L, conststructluaL_Reg*properties,
booluse_cache)
{
charconst*key;
conststructluaL_Reg*reg;
key=lua_tostring (L, 2);
if (key==NULL)
return0;
reg=mlua_find_method (properties, key);
if (reg)
{
returnmlua_get_property (L, reg, use_cache);
}
lua_getmetatable (L, 1);
lua_replace (L, 1);
lua_gettable (L, 1);
return1;
}
void
mlua_check_field_type (lua_State*L, intindex, constchar*key, inttype,
constchar*error_msg)
{
lua_getfield (L, index, key);
if (lua_type (L, -1) !=type)
{
if (error_msg)
luaL_error (L, "field \"%s\", ", key, error_msg);
else
luaL_error (L, "field \"%s\" should be an %s", key,
lua_typename (L, type));
}
lua_pop (L, 1);
}
lua_Number
mlua_named_optnumber (lua_State*L, intindex, constchar*key,
lua_Numberdefault_value)
{
lua_Numberr;
lua_getfield (L, index, key);
r=luaL_optnumber (L, -1, default_value);
lua_pop (L, 1);
returnr;
}
constchar*
mlua_named_optstring (lua_State*L, intindex, constchar*key,
constchar*default_value)
{
constchar*r;
lua_getfield (L, index, key);
r=luaL_optstring (L, -1, default_value);
lua_pop (L, 1);
returnr;
}
lua_Number
mlua_named_number (lua_State*L, intindex, constchar*key)
{
lua_Numberr;
lua_getfield (L, index, key);
if (! lua_isnumber (L, -1))
luaL_error (L, "number expected");
r=lua_tonumber (L, -1);
lua_pop (L, 1);
returnr;
}
constchar*
mlua_named_string (lua_State*L, intindex, constchar*key)
{
constchar*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);
returnr;
}