forked from cleoold/lua_interpreter_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_test.cxx
160 lines (147 loc) · 6.51 KB
/
demo_test.cxx
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
#include <stdexcept>
#include <vector>
#include "lua_interpreter.hxx"
#define ASSERT(condition) \
do { \
if(!(condition)) \
throw std::runtime_error(std::string( __FILE__ ) \
+ std::string( ":" ) \
+ std::to_string( __LINE__ ) \
); \
} while (0)
#define SHOULD_THROW(expr) \
do { \
bool thrown {false}; \
try { \
expr; \
} catch (luai::luastate_error &) { \
thrown = true; \
} \
ASSERT(thrown); \
} while (0)
using namespace luai;
// my helper function to get field recursively
// len of names must >= 1
template<types Type>
auto get_field_recur(lua_interpreter &state, const std::vector<std::string> &names) {
if (names.size() == 1)
return state.get_global<Type>(names[0].c_str());
auto staq = std::vector<table_handle>{};
staq.emplace_back(state.get_global<types::TABLE>(names[0].c_str()));
for (size_t i = 1; i < names.size()-1; ++i)
staq.emplace_back(staq[i-1].get_field<types::TABLE>(names[i].c_str()));
return staq.back().get_field<Type>(names.back().c_str());
// table handles destructed
}
int main() {
auto state = lua_interpreter{};
state.openlibs();
auto ret = state.run_chunk(
"print('this is a test script')\n"
"x = 15\n"
"y = x + 16.6\n"
"s = (function() return 'hahaha' end)()\n"
"b = true\n"
);
ASSERT(std::get<0>(ret) == true);
ASSERT(state.get_global<types::INT>("x") == 15);
ASSERT(state.get_global<types::NUM>("y") == 31.6);
ASSERT(state.get_global<types::STR>("s") == std::string{"hahaha"});
ASSERT(state.get_global<types::BOOL>("b") == true);
SHOULD_THROW(state.get_global<types::INT>("xx"));
// int <-> string, num <-> string are convertible, but int <-> num is not
SHOULD_THROW(state.get_global<types::INT>("y"));
ASSERT(state.get_global<types::LTYPE>("x") == types::INT);
ASSERT(state.get_global<types::LTYPE>("y") == types::NUM);
ASSERT(state.get_global<types::LTYPE>("s") == types::STR);
ASSERT(state.get_global<types::LTYPE>("b") == types::BOOL);
ASSERT(state.get_global<types::LTYPE>("xx") == types::NIL);
state.run_chunk(
"a = { 1, 6.6, 'haha', false, {} }\n"
);
ASSERT(state.get_global<types::LTYPE>("a") == types::TABLE);
// must open up new scope for tables
{
auto a = state.get_global<types::TABLE>("a");
ASSERT(a.get_index<types::INT>(1) == 1);
ASSERT(a.get_index<types::NUM>(2) == 6.6);
ASSERT(a.get_index<types::STR>(3) == std::string{"haha"});
ASSERT(a.get_index<types::BOOL>(4) == false);
a.get_index<types::TABLE>(5);
ASSERT(a.len() == 5);
ASSERT(a.get_index<types::LTYPE>(1) == types::INT);
}
state.run_chunk(
"t = { ['wow'] = 7, ['nest'] = { ['ehh'] = 8, ['more'] = { ['oh'] = 9 } } }\n"
);
{
auto t = state.get_global<types::TABLE>("t");
ASSERT(t.get_field<types::INT>("wow") == 7);
ASSERT(t.get_field<types::LTYPE>("nest") == types::TABLE);
{
auto nest = t.get_field<types::TABLE>("nest");
ASSERT(nest.get_field<types::INT>("ehh") == 8);
ASSERT(state.get_global<types::INT>("x") == 15);
ASSERT(t.get_field<types::INT>("wow") == 7);
ASSERT(nest.get_field<types::LTYPE>("ehh") == types::INT);
}
ASSERT(t.get_field<types::INT>("wow") == 7);
}
ASSERT(get_field_recur<types::INT>(state, {"t", "nest", "more", "oh"}) == 9);
state.run_chunk(
"k = { haha = 8, spam = 8.8, hehe = { wow = 9, kk = { cc = 10, [16] = true } } }\n"
);
{
// test table handle RAII for manipulating stack indices
// define new table handles only in the scope
auto k = state.get_global<types::TABLE>("k");
ASSERT(k.get_field<types::INT>("haha") == 8);
{
auto hehe = k.get_field<types::TABLE>("hehe");
{
auto kk = hehe.get_field<types::TABLE>("kk");
ASSERT(kk.get_field<types::INT>("cc") == 10);
ASSERT(k.get_field<types::INT>("haha") == 8);
// don't try to assign values to k:
// k = state.get_global<types::TABLE>("t");
// do not move table handles to containers, threads, etc that
// lives longer than current scope:
// std::thread([kk = std::move(kk)] {...}).detach();
// they break RAII, so behaviour is undefined
ASSERT(hehe.get_field<types::INT>("wow") == 9);
SHOULD_THROW(kk.get_field<types::NUM>("spam"));
}
ASSERT(hehe.get_field<types::INT>("wow") == 9);
}
ASSERT(k.get_field<types::NUM>("spam") == 8.8);
}
// a scope contains tables with common ancestor ("k")
{
auto k = state.get_global<types::TABLE>("k");
auto hehe = k.get_field<types::TABLE>("hehe");
auto kk = hehe.get_field<types::TABLE>("kk");
ASSERT(k.get_field<types::INT>("haha") == 8);
ASSERT(kk.get_field<types::INT>("cc") == 10);
ASSERT(k.get_field<types::INT>("haha") == 8);
ASSERT(hehe.get_field<types::INT>("wow") == 9);
SHOULD_THROW(kk.get_field<types::NUM>("spam"));
ASSERT(hehe.get_field<types::INT>("wow") == 9);
ASSERT(k.get_field<types::NUM>("spam") == 8.8);
}
// short hand
ASSERT(state.get_global<types::TABLE>("k")
.get_field<types::TABLE>("hehe")
.get_field<types::TABLE>("kk")
.get_index<types::BOOL>(16) == true);
// move
auto state2 = std::move(state);
ASSERT(state2.get_global<types::INT>("x") == 15);
{
auto k = state2.get_global<types::TABLE>("k");
auto k2 = std::move(k);
ASSERT(k2.get_field<types::NUM>("spam") == 8.8);
}
state2.run_chunk(
"print('bye!')\n"
);
}