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-05-07 22:16:47 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2019-05-22 07:17:31 +08:00
|
|
|
// Simple sol3 version of the below
|
2017-05-10 01:24:56 +08:00
|
|
|
void simple(sol::this_state ts, sol::this_environment te) {
|
|
|
|
sol::state_view lua = ts;
|
|
|
|
if (te) {
|
|
|
|
sol::environment& env = te;
|
|
|
|
sol::environment freshenv = lua["freshenv"];
|
|
|
|
bool is_same_env = freshenv == env;
|
|
|
|
std::cout << "this_environment -- env == freshenv : " << is_same_env << std::endl;
|
|
|
|
}
|
|
|
|
std::cout << "this_environment -- no environment present" << std::endl;
|
|
|
|
}
|
|
|
|
|
2017-05-07 22:16:47 +08:00
|
|
|
// NOTE:
|
|
|
|
// THIS IS A LOW-LEVEL EXAMPLE, using pieces of sol2
|
|
|
|
// to facilitate better usage
|
2017-05-10 01:24:56 +08:00
|
|
|
// It is recommended you just do the simple version, as it is basically this code
|
|
|
|
// but it is sometimes useful to show the hoops you need to jump through to use the Lua C API
|
|
|
|
void complicated(sol::this_state ts) {
|
2017-05-07 22:16:47 +08:00
|
|
|
lua_State* L = ts;
|
|
|
|
|
|
|
|
lua_Debug info;
|
|
|
|
// Level 0 means current function (this C function, which is useless for our purposes)
|
|
|
|
// Level 1 means next call frame up the stack. This is probably the environment we're looking for?
|
|
|
|
int level = 1;
|
|
|
|
int pre_stack_size = lua_gettop(L);
|
|
|
|
if (lua_getstack(L, level, &info) != 1) {
|
|
|
|
// failure: call it quits
|
|
|
|
std::cout << "error: unable to traverse the stack" << std::endl;
|
|
|
|
lua_settop(L, pre_stack_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// the "f" identifier is the most important here
|
|
|
|
// it pushes the function running at `level` onto the stack:
|
|
|
|
// we can get the environment from this
|
|
|
|
// the rest is for printing / debugging purposes
|
|
|
|
if (lua_getinfo(L, "fnluS", &info) == 0) {
|
|
|
|
// failure?
|
2017-05-10 01:24:56 +08:00
|
|
|
std::cout << "manually -- error: unable to get stack information" << std::endl;
|
2017-05-07 22:16:47 +08:00
|
|
|
lua_settop(L, pre_stack_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, so all the calls worked.
|
|
|
|
// Print out some information about this "level"
|
2021-03-06 14:03:23 +08:00
|
|
|
std::cout << "manually -- [" << level << "] " << info.short_src << ":" << info.currentline << " -- " << (info.name ? info.name : "<unknown>") << "["
|
|
|
|
<< info.what << "]" << std::endl;
|
2017-05-07 22:16:47 +08:00
|
|
|
|
|
|
|
// Grab the function off the top of the stack
|
|
|
|
// remember: -1 means top, -2 means 1 below the top, and so on...
|
2021-03-06 14:03:23 +08:00
|
|
|
// 1 means the very bottom of the stack,
|
2017-05-07 22:16:47 +08:00
|
|
|
// 2 means 1 more up, and so on to the top value...
|
|
|
|
sol::function f(L, -1);
|
|
|
|
// The environment can now be ripped out of the function
|
|
|
|
sol::environment env(sol::env_key, f);
|
|
|
|
if (!env.valid()) {
|
2017-05-10 01:24:56 +08:00
|
|
|
std::cout << "manually -- error: no environment to get" << std::endl;
|
2017-05-07 22:16:47 +08:00
|
|
|
lua_settop(L, pre_stack_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sol::state_view lua(L);
|
|
|
|
sol::environment freshenv = lua["freshenv"];
|
|
|
|
bool is_same_env = freshenv == env;
|
2017-05-10 01:24:56 +08:00
|
|
|
std::cout << "manually -- env == freshenv : " << is_same_env << std::endl;
|
2017-05-07 22:16:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== environment snooping ===" << std::endl;
|
2017-05-07 22:16:47 +08:00
|
|
|
sol::state lua;
|
|
|
|
|
|
|
|
sol::environment freshenv(lua, sol::create, lua.globals());
|
|
|
|
lua["freshenv"] = freshenv;
|
2017-05-10 01:24:56 +08:00
|
|
|
lua.set_function("f", simple);
|
|
|
|
lua.set_function("g", complicated);
|
2017-05-07 22:16:47 +08:00
|
|
|
|
|
|
|
lua.script("f()", freshenv);
|
2017-05-10 01:24:56 +08:00
|
|
|
lua.script("g()", freshenv);
|
2017-05-07 22:16:47 +08:00
|
|
|
|
2017-08-07 14:07:21 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
|
2017-05-07 22:16:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|