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>
|
2017-04-03 10:37:01 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
int main(int, char**) {
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
|
|
|
// Environments can set on functions (scripts), userdata and threads
|
|
|
|
// let's look at functions
|
|
|
|
|
|
|
|
lua.script("f = function() return test end");
|
|
|
|
sol::function f = lua["f"];
|
2021-03-06 14:03:23 +08:00
|
|
|
|
2017-04-19 08:23:20 +08:00
|
|
|
sol::environment env_f(lua, sol::create);
|
|
|
|
env_f["test"] = 31;
|
|
|
|
sol::set_environment(env_f, f);
|
2017-04-03 10:37:01 +08:00
|
|
|
|
|
|
|
// the function returns the value from the environment table
|
|
|
|
int result = f();
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(result == 31);
|
|
|
|
|
2017-04-03 10:37:01 +08:00
|
|
|
|
|
|
|
// You can also protect from variables
|
|
|
|
// being set without the 'local' specifier
|
|
|
|
lua.script("g = function() test = 5 end");
|
|
|
|
sol::function g = lua["g"];
|
|
|
|
sol::environment env_g(lua, sol::create);
|
|
|
|
env_g.set_on(g); // same as set_environment
|
|
|
|
|
|
|
|
g();
|
|
|
|
// the value can be retrieved from the env table
|
|
|
|
int test = env_g["test"];
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(test == 5);
|
2017-04-03 10:37:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
// the global environment
|
|
|
|
// is not polluted at all, despite both functions being used and set
|
|
|
|
sol::object global_test = lua["test"];
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(!global_test.valid());
|
2017-04-03 10:37:01 +08:00
|
|
|
|
2017-04-19 08:23:20 +08:00
|
|
|
|
|
|
|
// You can retrieve environments in C++
|
|
|
|
// and check the environment of functions
|
|
|
|
// gotten from Lua
|
|
|
|
|
|
|
|
// get the environment from any sol::reference-styled type,
|
|
|
|
// including sol::object, sol::function, sol::table, sol::userdata ...
|
|
|
|
lua.set_function("check_f_env",
|
2021-03-06 14:03:23 +08:00
|
|
|
// capture necessary variable in C++ lambda
|
|
|
|
[&env_f](sol::object target) {
|
|
|
|
// pull out the environment from func using
|
|
|
|
// sol::env_key constructor
|
|
|
|
sol::environment target_env(sol::env_key, target);
|
|
|
|
int test_env_f = env_f["test"];
|
|
|
|
int test_target_env = target_env["test"];
|
|
|
|
// the environment for f the one gotten from `target`
|
|
|
|
// are the same
|
|
|
|
sol_c_assert(test_env_f == test_target_env);
|
|
|
|
sol_c_assert(test_env_f == 31);
|
|
|
|
sol_c_assert(env_f == target_env);
|
|
|
|
});
|
|
|
|
lua.set_function("check_g_env", [&env_g](sol::function target) {
|
|
|
|
// equivalent:
|
|
|
|
sol::environment target_env = sol::get_environment(target);
|
|
|
|
int test_env_g = env_g["test"];
|
|
|
|
int test_target_env = target_env["test"];
|
|
|
|
sol_c_assert(test_env_g == test_target_env);
|
|
|
|
sol_c_assert(test_env_g == 5);
|
|
|
|
sol_c_assert(env_g == target_env);
|
|
|
|
});
|
2017-04-19 08:23:20 +08:00
|
|
|
|
|
|
|
lua.script("check_f_env(f)");
|
|
|
|
lua.script("check_g_env(g)");
|
2018-03-04 23:56:05 +08:00
|
|
|
|
2017-04-03 10:37:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|