File: luabind.c

package info (click to toggle)
lua-curl 0.3.0-9.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 272 kB
  • sloc: ansic: 1,106; makefile: 159
file content (143 lines) | stat: -rw-r--r-- 3,754 bytes parent folder | download | duplicates (6)
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
/******************************************************************************
 * $Id: luabind.c,v 1.4 2008/10/25 11:58:03 gareuselesinge Exp $
 * This file is part of FreePOPs (http://www.freepops.org)                    *
 * This file is distributed under the terms of GNU GPL license or, at your    *
 * option,  MIT license .                                                     *
 ******************************************************************************/

/******************************************************************************
 * File description:
 *	hacks useful to create bindings
 * Notes:
 *	
 * Authors:
 * 	Name <gareuselesinge@users.sourceforge.net>
 ******************************************************************************/
#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
#include <stdarg.h>

#include "luabind.h"

/******************************************************************************
 * DEBUG ONLY
 * 
 */ 
#define LINE_PREFIX "L: "	
static void L_printstack(lua_State* s)	
{
int i;

fprintf(stderr,"%slua stack image:\n",LINE_PREFIX);
for(i=lua_gettop(s) ; i > 0 ; i-- )
	{
	fprintf(stderr,"%sstack(%2d) : %s: ",LINE_PREFIX,i,
		lua_typename(s,lua_type(s,i)));
	switch(lua_type(s,i)){
		case LUA_TSTRING:
			fprintf(stderr," \"%s\"\n",lua_tostring(s,i));
		break;
		case LUA_TNUMBER:
			fprintf(stderr," %5.3f\n",lua_tonumber(s,i));
		break;
		case LUA_TBOOLEAN:
			fprintf(stderr," %s\n",
				lua_toboolean(s,i)==0?"true":"false");
		break;
		case LUA_TNIL:
			fprintf(stderr," nil\n");
		break;
		default:
			fprintf(stderr," ??\n");
		break;
	}
	}
fprintf(stderr,"%sstack( 0) : --bottom--\n\n",LINE_PREFIX);
}

/******************************************************************************
 * The error function
 * 
 */ 
void L_error(lua_State* L, char* msg, ...){
char buffer[1024];
va_list ap;
	
va_start(ap,msg);
vsnprintf(buffer,1024,msg,ap);
va_end(ap);

L_printstack(L);
luaL_error(L,buffer);
}

/******************************************************************************
 * calculates the size of a table
 *
 */ 
int L_tablesize(lua_State* L,int n){
int i = 0;

if ( !lua_istable(L,n))
	L_error(L,"expecting a table, "
		"not a %s",lua_typename(L,lua_type(L,-1)));

lua_pushnil(L);
while( lua_next(L,n) != 0 ){
	i++;
	lua_pop(L,1);
}

return i;
}
/******************************************************************************
 * check number of arguments
 *
 */ 
void L_checknarg(lua_State* L,int n,char* msg){
if( lua_gettop(L) != n )
	L_error(L,"Stack has %d values: '%s'",lua_gettop(L),msg);
}
/******************************************************************************
 * expects a table on top and adds all t fields to this table
 *
 */ 
void L_openconst(lua_State* L,const struct L_const* t) {
int i;
for ( i = 0 ; t[i].name != NULL ; i++){
	lua_pushstring(L,t[i].name);
	lua_pushnumber(L,(lua_Number)t[i].value);
	lua_settable(L,-3);
}

}
/******************************************************************************
 * expects a table on top and adds all t fields to this table
 * also the metatable typeshould exist
 */ 
void L_openTconst(lua_State* L,const struct L_Tuserdata* t,const char * type){
int i;
for ( i = 0 ; t[i].name != NULL ; i++){
	void * c;
	lua_pushstring(L,t[i].name);
	c = lua_newuserdata(L,sizeof(void*));
	*(void**)c = t[i].data;
	luaL_getmetatable(L,type);
	lua_setmetatable(L,-2);
	lua_settable(L,-3);
}

}
/*******************************************************************************
 * since there is no luaL_checklightuserdata
 *
 *
 */ 
void * L_checkludata(lua_State* L,int n){
if (! lua_islightuserdata(L,n))
	L_error(L,"Argument %d is %s, expected is lightuserdata",n,
		lua_typename(L,lua_type(L,n)));
return lua_touserdata(L,n);
}