-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaVM.hpp
78 lines (65 loc) · 1.57 KB
/
LuaVM.hpp
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
#pragma once
#include <map>
extern "C"
{
#include "lua.h"
}
class LuaVM
{
public:
LuaVM ( ) = delete;
LuaVM ( lua_State * state );
~LuaVM ( ) = default;
void RegisterFunction ( const std::string& name, lua_CFunction func );
/* Pushing of Lua Values to the stack */
template<typename T>
void Push ( const T& value );
template<typename T, typename... S>
void Push ( const T& value, S... args )
{
Push ( value );
Push ( args... );
}
void Push ( ) {};
/* Popping T from the stack */
template<typename T>
void Pop ( T& value );
/* Attempts to read a T from the stack*/
template<typename T>
void GetParam ( T& e )
{
if ( hasError )
return;
if ( DoesTypeAtIndexMatch<T> ( currentIndex ) )
{
Pop ( e );
currentIndex++;
return;
}
hasError = true;
error = "Expected T at argument " + std::to_string ( currentIndex );
}
template<typename T>
bool DoesTypeAtIndexMatch ( unsigned int index );
template<typename T>
void GetParam ( T& e, const T& def );
bool Good ( ) { return !hasError; }
public:
template <void ( *T )( LuaVM & )>
static int LuaFunctionDispatcher ( lua_State* L )
{
auto vm = LuaVM ( L );
vm.numReturns = 0;
vm.currentIndex = 1;
vm.hasError = false;
T ( vm );
return vm.numReturns;
}
private:
int numReturns;
int currentIndex;
std::string error;
bool hasError;
private:
lua_State * L;
};