2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
2019-05-22 09:28:18 +08:00
|
|
|
#include <iostream>
|
2019-05-22 07:17:31 +08:00
|
|
|
|
|
|
|
void create_namespace_sf(sol::state& lua) {
|
|
|
|
// this would explode
|
|
|
|
// lua["sf"]["value"] = 256;
|
|
|
|
lua[sol::create_if_nil]["sf"]["value"] = 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, char*[]) {
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cout << "=== sol::lua_value/sol::array_value ==="
|
|
|
|
<< std::endl;
|
2019-05-22 07:17:31 +08:00
|
|
|
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
|
|
|
const auto& code = R"(
|
|
|
|
print(sf)
|
|
|
|
print(sf.value)
|
|
|
|
assert(sf.value == 256)
|
|
|
|
)";
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
auto result
|
|
|
|
= lua.safe_script(code, sol::script_pass_on_error);
|
2019-05-22 07:17:31 +08:00
|
|
|
// did not work
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(!result.valid());
|
2019-05-22 07:17:31 +08:00
|
|
|
|
|
|
|
// create values
|
|
|
|
create_namespace_sf(lua);
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
auto result2
|
|
|
|
= lua.safe_script(code, sol::script_pass_on_error);
|
2019-05-22 07:17:31 +08:00
|
|
|
// it worked properly
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(result2.valid());
|
2019-05-22 07:17:31 +08:00
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|