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