LuaFunctions.h
Go to the documentation of this file.00001
#ifndef LUA_FUNCTIONS_H
00002
#define LUA_FUNCTIONS_H
00003
00004
#include "LuaManager.h"
00005
#include "RageUtil.h"
00006
00007
extern "C"
00008 {
00009
#include <lua.h>
00010
#include <lualib.h>
00011 }
00012
00013
00014 #define LUA_ASSERT( expr, err ) if( !(expr) ) { LUA->Fail( err ); }
00015
00016
00017 #define REQ_ARGS(func, need) { \
00018
const int args = lua_gettop(L); \
00019
LUA_ASSERT( args == need, ssprintf( func " requires exactly %i argument%s, got %i", need, need == 1? "":"s", args) ); \
00020
}
00021
00022
00023 #define REQ_ARGS_BETWEEN(func, minargs, maxargs) { \
00024
const int args = lua_gettop(L); \
00025
LUA_ASSERT( args < minargs, ssprintf( func " requires between %i and %i argument%s, got %i", \
00026
minargs, maxargs, maxargs == 1? "":"s", args) ); \
00027
}
00028
00029
00030 #define REQ_ARG(func, n, type) { \
00031
LUA_ASSERT( lua_is##type(L, n), ssprintf("Argument %i to " func " must be %s", n, #type) ); }
00032
00033 #define REQ_ARG_NUMBER_RANGE(func, n, minimum, maximum) { \
00034
REQ_ARG(func, n, number); \
00035
const int val = (int) lua_tonumber( L, n ); \
00036
LUA_ASSERT( val >= minimum && val <= maximum, ssprintf("Argument %i to " func " must be an integer between %i and %i (got %i)", n, minimum, maximum, val) ); \
00037
}
00038 #define LUA_RETURN( expr ) { LUA->PushStack( expr ); return 1; }
00039
00040
00041
00042 #define LuaFunction_NoArgs( func, call ) \
00043
int LuaFunc_##func( lua_State *L ) { \
00044
REQ_ARGS( #func, 0 ); \
00045
LUA_RETURN( call ); \
00046
} \
00047
LuaFunction( func );
00048
00049 #define LuaFunction_Int( func, call ) \
00050
int LuaFunc_##func( lua_State *L ) { \
00051
REQ_ARGS( #func, 1 ); \
00052
REQ_ARG( #func, 1, number ); \
00053
const int a1 = int(lua_tonumber( L, 1 )); \
00054
LUA_RETURN( call ); \
00055
} \
00056
LuaFunction( func );
00057
00058 #define LuaFunction_IntInt( func, call ) \
00059
int LuaFunc_##func( lua_State *L ) { \
00060
REQ_ARGS( #func, 2 ); \
00061
REQ_ARG( #func, 1, number ); \
00062
REQ_ARG( #func, 2, number ); \
00063
const int a1 = int(lua_tonumber( L, 1 )); \
00064
const int a2 = int(lua_tonumber( L, 2 )); \
00065
LUA_RETURN( call ); \
00066
} \
00067
LuaFunction( func );
00068
00069 #define LuaFunction_Str( func, call ) \
00070
int LuaFunc_##func( lua_State *L ) { \
00071
REQ_ARGS( #func, 1 ); \
00072
REQ_ARG( #func, 1, string ); \
00073
CString str; \
00074
LUA->PopStack( str ); \
00075
LUA_RETURN( call ); \
00076
} \
00077
LuaFunction( func );
00078
00079 #define LuaFunction_StrStr( func, call ) \
00080
int LuaFunc_##func( lua_State *L ) { \
00081
REQ_ARGS( #func, 2 ); \
00082
REQ_ARG( #func, 1, string ); \
00083
REQ_ARG( #func, 2, string ); \
00084
CString str1; \
00085
CString str2; \
00086
LUA->PopStack( str2 ); \
00087
LUA->PopStack( str1 ); \
00088
LUA_RETURN( call ); \
00089
} \
00090
LuaFunction( func );
00091
00092
00093 #define LuaFunction_PlayerNumber( func, call ) \
00094
int LuaFunc_##func( lua_State *L ) { \
00095
REQ_ARGS( #func, 1 ); \
00096
REQ_ARG_NUMBER_RANGE( #func, 1, 1, NUM_PLAYERS ); \
00097
const PlayerNumber pn = (PlayerNumber) (int(lua_tonumber( L, -1 ))-1); \
00098
LUA_RETURN( call ); \
00099
} \
00100
LuaFunction( func );
00101
00102
00103 #define LuaFunction_PlayerNumber_OptInt( func, def, call ) \
00104
int LuaFunc_##func( lua_State *L ) { \
00105
REQ_ARGS_BETWEEN( #func, 1, 2 ); \
00106
REQ_ARG_NUMBER_RANGE( #func, 1, 1, NUM_PLAYERS ); \
00107
const PlayerNumber pn = (PlayerNumber) (int(lua_tonumber( L, 1 ))-1); \
00108
int a1 = def; \
00109
LUA->GetStack( L, 2, a1 ); \
00110
}
00111
00112
00113 struct LuaFunctionList
00114 {
00115
LuaFunctionList(
CString name, lua_CFunction
func );
00116 CString name;
00117 lua_CFunction func;
00118 LuaFunctionList *
next;
00119 };
00120 extern LuaFunctionList *
g_LuaFunctionList;
00121 #define LuaFunction( func ) static LuaFunctionList g_##func( #func, LuaFunc_##func )
00122
00123
#endif
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
Generated on Thu Jan 27 20:57:23 2005 for StepMania by
1.3.7