1 #define SOL_ALL_SAFETIES_ON 1
2 #include <sol/sol.hpp>
3 
4 #include <iostream>
5 
6 // Simple sol3 version of the below
simple(sol::this_state ts,sol::this_environment te)7 void simple(sol::this_state ts, sol::this_environment te) {
8 	sol::state_view lua = ts;
9 	if (te) {
10 		sol::environment& env = te;
11 		sol::environment freshenv = lua["freshenv"];
12 		bool is_same_env = freshenv == env;
13 		std::cout << "this_environment -- env == freshenv : " << is_same_env << std::endl;
14 	}
15 	std::cout << "this_environment -- no environment present" << std::endl;
16 }
17 
18 // NOTE:
19 // THIS IS A LOW-LEVEL EXAMPLE, using pieces of sol2
20 // to facilitate better usage
21 // It is recommended you just do the simple version, as it is basically this code
22 // but it is sometimes useful to show the hoops you need to jump through to use the Lua C API
complicated(sol::this_state ts)23 void complicated(sol::this_state ts) {
24 	lua_State* L = ts;
25 
26 	lua_Debug info;
27 	// Level 0 means current function (this C function, which is useless for our purposes)
28 	// Level 1 means next call frame up the stack. This is probably the environment we're looking for?
29 	int level = 1;
30 	int pre_stack_size = lua_gettop(L);
31 	if (lua_getstack(L, level, &info) != 1) {
32 		// failure: call it quits
33 		std::cout << "error: unable to traverse the stack" << std::endl;
34 		lua_settop(L, pre_stack_size);
35 		return;
36 	}
37 	// the "f" identifier is the most important here
38 	// it pushes the function running at `level` onto the stack:
39 	// we can get the environment from this
40 	// the rest is for printing / debugging purposes
41 	if (lua_getinfo(L, "fnluS", &info) == 0) {
42 		// failure?
43 		std::cout << "manually -- error: unable to get stack information" << std::endl;
44 		lua_settop(L, pre_stack_size);
45 		return;
46 	}
47 
48 	// Okay, so all the calls worked.
49 	// Print out some information about this "level"
50 	std::cout << "manually -- [" << level << "] " << info.short_src << ":" << info.currentline << " -- " << (info.name ? info.name : "<unknown>") << "["
51 	          << info.what << "]" << std::endl;
52 
53 	// Grab the function off the top of the stack
54 	// remember: -1 means top, -2 means 1 below the top, and so on...
55 	// 1 means the very bottom of the stack,
56 	// 2 means 1 more up, and so on to the top value...
57 	sol::function f(L, -1);
58 	// The environment can now be ripped out of the function
59 	sol::environment env(sol::env_key, f);
60 	if (!env.valid()) {
61 		std::cout << "manually -- error: no environment to get" << std::endl;
62 		lua_settop(L, pre_stack_size);
63 		return;
64 	}
65 	sol::state_view lua(L);
66 	sol::environment freshenv = lua["freshenv"];
67 	bool is_same_env = freshenv == env;
68 	std::cout << "manually -- env == freshenv : " << is_same_env << std::endl;
69 }
70 
main()71 int main() {
72 	std::cout << "=== environment snooping ===" << std::endl;
73 	sol::state lua;
74 
75 	sol::environment freshenv(lua, sol::create, lua.globals());
76 	lua["freshenv"] = freshenv;
77 	lua.set_function("f", simple);
78 	lua.set_function("g", complicated);
79 
80 	lua.script("f()", freshenv);
81 	lua.script("g()", freshenv);
82 
83 	std::cout << std::endl;
84 
85 	return 0;
86 }
87