Skip to content

Commit a715a01

Browse files
authored
Ldblib (#39)
1 parent 742adb6 commit a715a01

File tree

5 files changed

+416
-212
lines changed

5 files changed

+416
-212
lines changed

build/ldblib.c

+3-211
Original file line numberDiff line numberDiff line change
@@ -38,49 +38,6 @@ static void checkstack (lua_State *L, lua_State *L1, int n) {
3838
}
3939

4040

41-
static int db_getregistry (lua_State *L) {
42-
lua_pushvalue(L, LUA_REGISTRYINDEX);
43-
return 1;
44-
}
45-
46-
47-
static int db_getmetatable (lua_State *L) {
48-
luaL_checkany(L, 1);
49-
if (!lua_getmetatable(L, 1)) {
50-
lua_pushnil(L); /* no metatable */
51-
}
52-
return 1;
53-
}
54-
55-
56-
static int db_setmetatable (lua_State *L) {
57-
int t = lua_type(L, 2);
58-
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
59-
"nil or table expected");
60-
lua_settop(L, 2);
61-
lua_setmetatable(L, 1);
62-
return 1; /* return 1st argument */
63-
}
64-
65-
66-
static int db_getuservalue (lua_State *L) {
67-
if (lua_type(L, 1) != LUA_TUSERDATA)
68-
lua_pushnil(L);
69-
else
70-
lua_getuservalue(L, 1);
71-
return 1;
72-
}
73-
74-
75-
static int db_setuservalue (lua_State *L) {
76-
luaL_checktype(L, 1, LUA_TUSERDATA);
77-
luaL_checkany(L, 2);
78-
lua_settop(L, 2);
79-
lua_setuservalue(L, 1);
80-
return 1;
81-
}
82-
83-
8441
/*
8542
** Auxiliary function used by several library functions: check for
8643
** an optional thread as function's first argument and set 'arg' with
@@ -142,7 +99,7 @@ static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
14299
** two optional outputs (function and line table) from function
143100
** 'lua_getinfo'.
144101
*/
145-
static int db_getinfo (lua_State *L) {
102+
extern int db_getinfo (lua_State *L) {
146103
lua_Debug ar;
147104
int arg;
148105
lua_State *L1 = getthread(L, &arg);
@@ -190,114 +147,6 @@ static int db_getinfo (lua_State *L) {
190147
}
191148

192149

193-
static int db_getlocal (lua_State *L) {
194-
int arg;
195-
lua_State *L1 = getthread(L, &arg);
196-
lua_Debug ar;
197-
const char *name;
198-
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
199-
if (lua_isfunction(L, arg + 1)) { /* function argument? */
200-
lua_pushvalue(L, arg + 1); /* push function */
201-
lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
202-
return 1; /* return only name (there is no value) */
203-
}
204-
else { /* stack-level argument */
205-
int level = (int)luaL_checkinteger(L, arg + 1);
206-
if (!lua_getstack(L1, level, &ar)) /* out of range? */
207-
return luaL_argerror(L, arg+1, "level out of range");
208-
checkstack(L, L1, 1);
209-
name = lua_getlocal(L1, &ar, nvar);
210-
if (name) {
211-
lua_xmove(L1, L, 1); /* move local value */
212-
lua_pushstring(L, name); /* push name */
213-
lua_rotate(L, -2, 1); /* re-order */
214-
return 2;
215-
}
216-
else {
217-
lua_pushnil(L); /* no name (nor value) */
218-
return 1;
219-
}
220-
}
221-
}
222-
223-
224-
static int db_setlocal (lua_State *L) {
225-
int arg;
226-
const char *name;
227-
lua_State *L1 = getthread(L, &arg);
228-
lua_Debug ar;
229-
int level = (int)luaL_checkinteger(L, arg + 1);
230-
int nvar = (int)luaL_checkinteger(L, arg + 2);
231-
if (!lua_getstack(L1, level, &ar)) /* out of range? */
232-
return luaL_argerror(L, arg+1, "level out of range");
233-
luaL_checkany(L, arg+3);
234-
lua_settop(L, arg+3);
235-
checkstack(L, L1, 1);
236-
lua_xmove(L, L1, 1);
237-
name = lua_setlocal(L1, &ar, nvar);
238-
if (name == NULL)
239-
lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */
240-
lua_pushstring(L, name);
241-
return 1;
242-
}
243-
244-
245-
/*
246-
** get (if 'get' is true) or set an upvalue from a closure
247-
*/
248-
static int auxupvalue (lua_State *L, int get) {
249-
const char *name;
250-
int n = (int)luaL_checkinteger(L, 2); /* upvalue index */
251-
luaL_checktype(L, 1, LUA_TFUNCTION); /* closure */
252-
name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
253-
if (name == NULL) return 0;
254-
lua_pushstring(L, name);
255-
lua_insert(L, -(get+1)); /* no-op if get is false */
256-
return get + 1;
257-
}
258-
259-
260-
static int db_getupvalue (lua_State *L) {
261-
return auxupvalue(L, 1);
262-
}
263-
264-
265-
static int db_setupvalue (lua_State *L) {
266-
luaL_checkany(L, 3);
267-
return auxupvalue(L, 0);
268-
}
269-
270-
271-
/*
272-
** Check whether a given upvalue from a given closure exists and
273-
** returns its index
274-
*/
275-
static int checkupval (lua_State *L, int argf, int argnup) {
276-
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
277-
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
278-
luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
279-
"invalid upvalue index");
280-
return nup;
281-
}
282-
283-
284-
static int db_upvalueid (lua_State *L) {
285-
int n = checkupval(L, 1, 2);
286-
lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
287-
return 1;
288-
}
289-
290-
291-
static int db_upvaluejoin (lua_State *L) {
292-
int n1 = checkupval(L, 1, 2);
293-
int n2 = checkupval(L, 3, 4);
294-
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
295-
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
296-
lua_upvaluejoin(L, 1, n1, 3, n2);
297-
return 0;
298-
}
299-
300-
301150
/*
302151
** Call hook function registered at hook table for the current
303152
** thread (if there is one)
@@ -344,7 +193,7 @@ static char *unmakemask (int mask, char *smask) {
344193
}
345194

346195

347-
static int db_sethook (lua_State *L) {
196+
extern int db_sethook (lua_State *L) {
348197
int arg, mask, count;
349198
lua_Hook func;
350199
lua_State *L1 = getthread(L, &arg);
@@ -376,7 +225,7 @@ static int db_sethook (lua_State *L) {
376225
}
377226

378227

379-
static int db_gethook (lua_State *L) {
228+
extern int db_gethook (lua_State *L) {
380229
int arg;
381230
lua_State *L1 = getthread(L, &arg);
382231
char buff[5];
@@ -397,60 +246,3 @@ static int db_gethook (lua_State *L) {
397246
lua_pushinteger(L, lua_gethookcount(L1)); /* 3rd result = count */
398247
return 3;
399248
}
400-
401-
402-
static int db_debug (lua_State *L) {
403-
for (;;) {
404-
char buffer[250];
405-
lua_writestringerror("%s", "lua_debug> ");
406-
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
407-
strcmp(buffer, "cont\n") == 0)
408-
return 0;
409-
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
410-
lua_pcall(L, 0, 0, 0))
411-
lua_writestringerror("%s\n", lua_tostring(L, -1));
412-
lua_settop(L, 0); /* remove eventual returns */
413-
}
414-
}
415-
416-
417-
static int db_traceback (lua_State *L) {
418-
int arg;
419-
lua_State *L1 = getthread(L, &arg);
420-
const char *msg = lua_tostring(L, arg + 1);
421-
if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
422-
lua_pushvalue(L, arg + 1); /* return it untouched */
423-
else {
424-
int level = (int)luaL_optinteger(L, arg + 2, (L == L1) ? 1 : 0);
425-
luaL_traceback(L, L1, msg, level);
426-
}
427-
return 1;
428-
}
429-
430-
431-
static const luaL_Reg dblib[] = {
432-
{"debug", db_debug},
433-
{"getuservalue", db_getuservalue},
434-
{"gethook", db_gethook},
435-
{"getinfo", db_getinfo},
436-
{"getlocal", db_getlocal},
437-
{"getregistry", db_getregistry},
438-
{"getmetatable", db_getmetatable},
439-
{"getupvalue", db_getupvalue},
440-
{"upvaluejoin", db_upvaluejoin},
441-
{"upvalueid", db_upvalueid},
442-
{"setuservalue", db_setuservalue},
443-
{"sethook", db_sethook},
444-
{"setlocal", db_setlocal},
445-
{"setmetatable", db_setmetatable},
446-
{"setupvalue", db_setupvalue},
447-
{"traceback", db_traceback},
448-
{NULL, NULL}
449-
};
450-
451-
452-
LUAMOD_API int luaopen_debug (lua_State *L) {
453-
luaL_newlib(L, dblib);
454-
return 1;
455-
}
456-

src/lapi.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::lzio::{luaZ_init, ZIO};
3838
use crate::types::{
3939
lua_Alloc, lua_CFunction, lua_Integer, lua_KContext, lua_KFunction, lua_Number, lua_Reader,
4040
lua_Writer, LUA_MULTRET, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS, LUA_TFUNCTION, LUA_TNIL,
41-
LUA_TTABLE, LUA_VERSION_NUM,
41+
LUA_TTABLE, LUA_TTHREAD, LUA_VERSION_NUM,
4242
};
4343

4444
#[inline(always)]
@@ -636,6 +636,11 @@ pub unsafe extern "C" fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c
636636
};
637637
}
638638

639+
#[no_mangle]
640+
pub unsafe extern "C" fn lua_isthread(L: *mut lua_State, idx: c_int) -> bool {
641+
return lua_type(L, idx) == LUA_TTHREAD;
642+
}
643+
639644
#[no_mangle]
640645
pub unsafe extern "C" fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State {
641646
let o: StkId = index2addr(L, idx);

src/lauxlib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,16 @@ pub unsafe extern "C" fn luaL_loadstring(L: *mut lua_State, s: *const libc::c_ch
12481248
return luaL_loadbufferx(L, s, strlen(s), s, ptr::null_mut());
12491249
}
12501250

1251+
#[no_mangle]
1252+
pub unsafe fn luaL_loadbuffer(
1253+
L: *mut lua_State,
1254+
buff: *const c_char,
1255+
size: size_t,
1256+
name: *const c_char,
1257+
) -> c_int {
1258+
return luaL_loadbufferx(L, buff, size, name, 0 as *const i8);
1259+
}
1260+
12511261
/* }====================================================== */
12521262

12531263
#[no_mangle]

0 commit comments

Comments
 (0)