this_environment

retrieving the environment of the calling function

Sometimes in C++ it’s useful to know where a Lua call is coming from and what environment it is from. The former is covered by Lua’s Debug API, which is extensive and is not fully wrapped up by sol3. But, sol3 covers the latter in letting you get the environment of the calling script / function, if it has one. sol::this_environment is a transparent argument and does not need to be passed in Lua scripts or provided when using sol::function, similar to sol::this_state:

 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
29
30
31
#define SOL_ALL_SAFETIES_ON
#include <sol/sol.hpp>

#include <iostream>


void env_check(sol::this_state ts, int x, sol::this_environment te) {
        std::cout << "In C++, 'int x' is in the second position, and its value is: " << x << std::endl;
        if (!te) {
                std::cout << "function does not have an environment: exiting function early" << std::endl;
                return;
        }
        sol::environment& env = te;
        sol::state_view lua = ts;
        sol::environment freshenv = lua["freshenv"];
        bool is_same_env = freshenv == env;
        std::cout << "env == freshenv : " << is_same_env << std::endl;
}

int main() {
        sol::state lua;
        sol::environment freshenv(lua, sol::create, lua.globals());
        lua["freshenv"] = freshenv;

        lua.set_function("f", env_check);

        // note that "f" only takes 1 argument and is okay here
        lua.script("f(25)", freshenv);

        return 0;
}

Also see this example for more details.