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>
|
2014-01-19 11:59:59 +08:00
|
|
|
|
2016-08-11 08:39:30 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2014-01-19 11:59:59 +08:00
|
|
|
int main() {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== basic ===" << std::endl;
|
2017-04-01 05:38:04 +08:00
|
|
|
// create an empty lua state
|
2017-04-01 10:39:16 +08:00
|
|
|
sol::state lua;
|
2014-01-19 11:59:59 +08:00
|
|
|
|
2017-04-01 10:39:16 +08:00
|
|
|
// by default, libraries are not opened
|
|
|
|
// you can open libraries by using open_libraries
|
|
|
|
// the libraries reside in the sol::lib enum class
|
|
|
|
lua.open_libraries(sol::lib::base);
|
2017-04-01 05:38:04 +08:00
|
|
|
// you can open all libraries by passing no arguments
|
2021-03-06 14:03:23 +08:00
|
|
|
// lua.open_libraries();
|
2014-01-19 11:59:59 +08:00
|
|
|
|
2017-04-01 10:39:16 +08:00
|
|
|
// call lua code directly
|
2016-08-11 08:39:30 +08:00
|
|
|
lua.script("print('hello world')");
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
// call lua code, and check to make sure it has loaded and
|
|
|
|
// run properly:
|
2017-08-06 07:20:28 +08:00
|
|
|
auto handler = &sol::script_default_on_error;
|
2017-04-01 05:38:04 +08:00
|
|
|
lua.script("print('hello again, world')", handler);
|
|
|
|
|
|
|
|
// Use a custom error handler if you need it
|
|
|
|
// This gets called when the result is bad
|
2021-03-06 23:14:48 +08:00
|
|
|
auto simple_handler =
|
|
|
|
[](lua_State*, sol::protected_function_result result) {
|
|
|
|
// You can just pass it through to let the
|
|
|
|
// call-site handle it
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
// the above lambda is identical to sol::simple_on_error,
|
|
|
|
// but it's shown here to show you can write whatever you
|
|
|
|
// like
|
2017-04-01 10:39:16 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
//
|
2017-04-01 10:39:16 +08:00
|
|
|
{
|
2021-03-06 23:14:48 +08:00
|
|
|
auto result = lua.script(
|
|
|
|
"print('hello hello again, world') \n return 24",
|
|
|
|
simple_handler);
|
2017-04-01 10:39:16 +08:00
|
|
|
if (result.valid()) {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "the third script worked, and a "
|
|
|
|
"double-hello statement should "
|
|
|
|
"appear above this one!"
|
|
|
|
<< std::endl;
|
2017-04-01 10:39:16 +08:00
|
|
|
int value = result;
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(value == 24);
|
2017-04-01 10:39:16 +08:00
|
|
|
}
|
|
|
|
else {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "the third script failed, check the "
|
|
|
|
"result type for more information!"
|
|
|
|
<< std::endl;
|
2017-04-01 10:39:16 +08:00
|
|
|
}
|
2017-04-01 05:38:04 +08:00
|
|
|
}
|
2017-04-01 10:39:16 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
{
|
2021-03-06 23:14:48 +08:00
|
|
|
auto result
|
|
|
|
= lua.script("does.not.exist", simple_handler);
|
2017-04-01 10:39:16 +08:00
|
|
|
if (result.valid()) {
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "the fourth script worked, which it "
|
|
|
|
"wasn't supposed to! Panic!"
|
|
|
|
<< std::endl;
|
2017-04-01 10:39:16 +08:00
|
|
|
int value = result;
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(value == 24);
|
2017-04-01 10:39:16 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sol::error err = result;
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "the fourth script failed, which "
|
|
|
|
"was intentional!\t\nError: "
|
|
|
|
<< err.what() << std::endl;
|
2017-04-01 10:39:16 +08:00
|
|
|
}
|
2017-04-01 05:38:04 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 08:39:30 +08:00
|
|
|
std::cout << std::endl;
|
2021-03-06 14:03:23 +08:00
|
|
|
|
2017-04-03 04:10:00 +08:00
|
|
|
return 0;
|
2019-05-29 19:58:27 +08:00
|
|
|
}
|