protect¶
routine to mark a function / variable as requiring safety
template <typename T>
auto protect( T&& value );
sol::protect( my_func )
allows you to protect a function call or member variable call when it is being set to Lua. It can be used with usertypes or when just setting a function into sol. Below is an example that demonstrates that a call that would normally not error without Safety features turned on that instead errors and makes the Lua safety-call wrapper pcall
fail:
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 | #define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
#include "assert.hpp"
int main(int, char*[]) {
struct protect_me {
int gen(int x) {
return x;
}
};
sol::state lua;
lua.open_libraries(sol::lib::base);
lua.new_usertype<protect_me>("protect_me",
"gen", sol::protect( &protect_me::gen )
);
lua.script(R"__(
pm = protect_me.new()
value = pcall(pm.gen,"wrong argument")
)__");
bool value = lua["value"];
c_assert(!value);
return 0;
}
|