-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotable.c
224 lines (197 loc) · 5.85 KB
/
rotable.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <lua.h>
#include "rotable.h"
/* The lookup code uses binary search on sorted `rotable_Reg` arrays
* to find functions/methods. For a small number of elements a linear
* search might be faster. */
#ifndef ROTABLE_BINSEARCH_MIN
# define ROTABLE_BINSEARCH_MIN 5
#endif
typedef struct {
#if LUA_VERSION_NUM < 503
/* on Lua 5.3 we use a lightuserdata in the uservalue of the
* userdata to hold the pointer to the luaL_Reg structure. Older
* Lua versions have to store it in the userdata payload. */
rotable_Reg const* p;
#endif
/* number of elements in the luaL_Reg array *if* it is sorted by
* name. We can use binary search in this case. Otherwise we need
* linear searches anyway and we can scan for the final `{NULL,0}`
* entry. `n` is 0 in this case. */
int n;
} rotable;
static char const unique_address[ 1 ] = { 0 };
static int reg_compare(void const* a, void const* b) {
return strcmp( (char const*)a, ((rotable_Reg const*)b)->name );
}
static rotable* check_rotable( lua_State* L, int idx, char const* func ) {
rotable* t = (rotable*)lua_touserdata( L, idx );
if( t ) {
if( lua_getmetatable( L, idx ) ) {
lua_pushlightuserdata( L, (void*)unique_address );
lua_rawget( L, LUA_REGISTRYINDEX );
if( !lua_rawequal( L, -1, -2 ) )
t = 0;
lua_pop( L, 2 );
}
}
if( !t ) {
char const* type = lua_typename( L, lua_type( L, idx ) );
if( lua_type( L, idx ) == LUA_TLIGHTUSERDATA ) {
type = "light userdata";
} else if( lua_getmetatable( L, idx ) ) {
lua_getfield( L, -1, "__name" );
lua_replace( L, -2 ); /* we don't need the metatable anymore */
if( lua_type( L, -1 ) == LUA_TSTRING )
type = lua_tostring( L, -1 );
}
lua_pushfstring( L, "bad argument #%d to '%s' "
"(rotable expected, got %s)", idx, func, type );
lua_error( L );
}
return t;
}
static rotable_Reg const* find_key( rotable_Reg const* p, int n,
char const* s ) {
if( s ) {
if( n >= ROTABLE_BINSEARCH_MIN ) { /* binary search */
return (rotable_Reg const*)bsearch( s, p, n, sizeof( *p ), reg_compare );
} else { /* use linear scan */
for( ; p->func; ++p ) {
if( 0 == reg_compare( s, p ) )
return p;
}
}
}
return 0;
}
static int rotable_func_index( lua_State* L ) {
char const* s = lua_tostring( L, 2 );
rotable_Reg const* p = (rotable_Reg const*)lua_touserdata( L, lua_upvalueindex( 1 ) );
int n = lua_tointeger( L, lua_upvalueindex( 2 ) );
p = find_key( p, n, s );
if( p )
lua_pushcfunction( L, p->func );
else
lua_pushnil( L );
return 1;
}
static int rotable_udata_index( lua_State* L ) {
rotable* t = (rotable*)lua_touserdata( L, 1 );
char const* s = lua_tostring( L, 2 );
#if LUA_VERSION_NUM < 503
rotable_Reg const* p = t->p;
#else
rotable_Reg const* p = 0;
lua_getuservalue( L, 1 );
p = (rotable_Reg const*)lua_touserdata( L, -1 );
#endif
p = find_key( p, t->n, s );
if( p )
lua_pushcfunction( L, p->func );
else
lua_pushnil( L );
return 1;
}
static int rotable_udata_len( lua_State* L ) {
lua_pushinteger( L, 0 );
return 1;
}
static int rotable_iter( lua_State* L ) {
rotable* t = check_rotable( L, 1, "__pairs iterator" );
char const* s = lua_tostring( L, 2 );
rotable_Reg const* q = 0;
#if LUA_VERSION_NUM < 503
rotable_Reg const* p = t->p;
#else
rotable_Reg const* p = 0;
lua_getuservalue( L, 1 );
p = (rotable_Reg const*)lua_touserdata( L, -1 );
#endif
if( s ) {
if( t->n >= ROTABLE_BINSEARCH_MIN ) { /* binary search */
q = (rotable_Reg const*)bsearch( s, p, t->n, sizeof( *p ), reg_compare );
if( q )
++q;
else
q = p + t->n;
} else { /* use linear scan */
for( q = p; q->func; ++q ) {
if( 0 == reg_compare( s, q ) ) {
++q;
break;
}
}
}
} else
q = p;
if( q->func ) {
lua_pushstring( L, q->name );
lua_pushcfunction( L, q->func );
return 2;
}
return 0;
}
static int rotable_udata_pairs( lua_State* L ) {
lua_pushcfunction( L, rotable_iter );
lua_pushvalue( L, 1 );
lua_pushnil( L );
return 3;
}
ROTABLE_EXPORT void rotable_newlib( lua_State* L, void const* v ) {
rotable_Reg const* reg = (rotable_Reg const*)v;
rotable* t = (rotable*)lua_newuserdata( L, sizeof( *t ) );
lua_pushlightuserdata( L, (void*)unique_address );
lua_rawget( L, LUA_REGISTRYINDEX );
if( !lua_istable( L, -1 ) ) {
lua_pop( L, 1 );
lua_createtable( L, 0, 5 );
lua_pushcfunction( L, rotable_udata_index );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, rotable_udata_len );
lua_setfield( L, -2, "__len" );
lua_pushcfunction( L, rotable_udata_pairs );
lua_setfield( L, -2, "__pairs" );
lua_pushboolean( L, 0 );
lua_setfield( L, -2, "__metatable" );
lua_pushliteral( L, "rotable" );
lua_setfield( L, -2, "__name" );
lua_pushlightuserdata( L, (void*)unique_address );
lua_pushvalue( L, -2 );
lua_rawset( L, LUA_REGISTRYINDEX );
}
lua_setmetatable( L, -2 );
#if LUA_VERSION_NUM < 503
t->p = reg;
#else
lua_pushlightuserdata( L, (void*)reg );
lua_setuservalue( L, -2 );
#endif
t->n = 0;
if( reg->func ) {
int i = 1;
for( ; reg[ i ].func; ++i ) {
if( strcmp( reg[ i-1 ].name, reg[ i ].name ) >= 0 )
return;
}
t->n = i;
}
}
ROTABLE_EXPORT void rotable_newidx( lua_State* L, void const* v ) {
rotable_Reg const* reg = (rotable_Reg const*)v;
int i = 0;
lua_pushlightuserdata( L, (void*)v);
for( ; reg[ i ].func; ++i ) {
if( strcmp( reg[ i-1 ].name, reg[ i ].name ) >= 0 ) {
i = 0;
break;
}
}
if( i >= ROTABLE_BINSEARCH_MIN ) {
lua_pushinteger( L, i );
lua_pushcclosure( L, rotable_func_index, 2 );
} else
lua_pushcclosure( L, rotable_func_index, 1 );
}