2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2018-03-17 04:47:09 +08:00
|
|
|
|
|
|
|
#include "assert.hpp"
|
|
|
|
|
2020-01-29 03:01:34 +08:00
|
|
|
int main() {
|
2018-03-17 04:47:09 +08:00
|
|
|
|
|
|
|
const auto& code = R"(
|
|
|
|
bark = {
|
|
|
|
woof = {
|
|
|
|
[2] = "arf!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
lua.script(code);
|
|
|
|
|
2019-09-15 12:43:44 +08:00
|
|
|
// produces table_proxy, implicitly converts to std::string, quietly destroys table_proxy
|
2018-03-17 04:47:09 +08:00
|
|
|
std::string arf_string = lua["bark"]["woof"][2];
|
|
|
|
|
|
|
|
// lazy-evaluation of tables
|
|
|
|
auto x = lua["bark"];
|
|
|
|
auto y = x["woof"];
|
|
|
|
auto z = y[2];
|
|
|
|
|
|
|
|
// retrivies value inside of lua table above
|
|
|
|
std::string value = z;
|
|
|
|
c_assert(value == "arf!");
|
|
|
|
|
|
|
|
// Can change the value later...
|
|
|
|
z = 20;
|
|
|
|
|
|
|
|
// Yay, lazy-evaluation!
|
|
|
|
int changed_value = z; // now it's 20!
|
|
|
|
c_assert(changed_value == 20);
|
|
|
|
lua.script("assert(bark.woof[2] == 20)");
|
|
|
|
|
|
|
|
lua["a_new_value"] = 24;
|
2020-01-29 03:01:34 +08:00
|
|
|
lua["chase_tail"] = [](int chasing) {
|
2018-03-17 04:47:09 +08:00
|
|
|
int r = 2;
|
|
|
|
for (int i = 0; i < chasing; ++i) {
|
|
|
|
r *= r;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
lua.script("assert(a_new_value == 24)");
|
|
|
|
lua.script("assert(chase_tail(2) == 16)");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|