add a new example, add a note, adjust for LuaJIT Beta3 and beyond (hopefully it doesn't flip its crap: will have to warn suers....)

This commit is contained in:
ThePhD 2017-05-07 10:16:47 -04:00
parent 5bc5def14d
commit c7237806ef
5 changed files with 98 additions and 7 deletions

10
.gitignore vendored
View File

@ -48,7 +48,7 @@ lua-5.2.4/
lua-5.2.2/ lua-5.2.2/
lua-5.2.3/ lua-5.2.3/
lua-5.1.5/ lua-5.1.5/
luajit-2.0.4/ luajit-2.0.5/
luajit-2.0.3/ luajit-2.0.3/
include/ include/
liblua.a liblua.a
@ -65,13 +65,13 @@ single/sol.hpp
lua53.dll lua53.dll
main.exe main.exe
main.o main.o
lua-5.3.3/ lua-5.3.4/
main.lua main.lua
LuaJIT-2.1.0/ LuaJIT-2.1.0/
lua-5.3.3-cxx/ lua-5.3.4-cxx/
lua-5.3.3.vcxproj-cxx.filters lua-5.3.4.vcxproj-cxx.filters
sol.pyproj sol.pyproj
lua-5.3.3.dll lua-5.3.4.dll
main.ilk main.ilk
main.pdb main.pdb
lua-5.3.4-cxx/ lua-5.3.4-cxx/

View File

@ -0,0 +1,72 @@
#define SOL_CHECK_ARGUMENTS
#include <sol.hpp>
#include <cassert>
#include <iostream>
// NOTE:
// THIS IS A LOW-LEVEL EXAMPLE, using pieces of sol2
// to facilitate better usage
// If you need to do this often, you can copy this code and paste it in a utility function for yourself
// so you can grab the environment whenever you need to
void some_function_called_by_sol2(sol::this_state ts) {
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?
std::cout << "error: unable to get stack information" << std::endl;
lua_settop(L, pre_stack_size);
return;
}
// Okay, so all the calls worked.
// Print out some information about this "level"
std::cout << "[" << level << "] " << info.short_src << ":" << info.currentline
<< " -- " << (info.name ? info.name : "<unknown>") << "[" << info.what << "]" << std::endl;
// Grab the function off the top of the stack
// remember: -1 means top, -2 means 1 below the top, and so on...
// 1 means the very bottom of the stack,
// 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()) {
std::cout << "error: no environment to get" << std::endl;
lua_settop(L, pre_stack_size);
return;
}
sol::state_view lua(L);
sol::environment freshenv = lua["freshenv"];
bool is_same_env = freshenv == env;
std::cout << "env == freshenv : " << is_same_env << std::endl;
}
int main() {
std::cout << "=== environment snooping example ===" << std::endl;
sol::state lua;
sol::environment freshenv(lua, sol::create, lua.globals());
lua["freshenv"] = freshenv;
lua.set_function("f", some_function_called_by_sol2);
lua.script("f()", freshenv);
return 0;
}

View File

@ -3,6 +3,11 @@
#include <cassert> #include <cassert>
// NOTE:
// There are TWO ways to retrieve the "this"
// object from calls, when it comes to constructors and regular member functions
// please pay attention to both: this is a low-level operation!
int main() { int main() {
struct thing { struct thing {

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script. // This file was generated with a script.
// Generated 2017-05-04 07:17:19.632331 UTC // Generated 2017-05-07 14:16:38.081474 UTC
// This header was generated with sol v2.17.1 (revision 7b78558) // This header was generated with sol v2.17.1 (revision 5bc5def)
// https://github.com/ThePhD/sol2 // https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP #ifndef SOL_SINGLE_INCLUDE_HPP
@ -1111,8 +1111,15 @@ inline int luaL_loadbufferx(lua_State* L, const char* buff, size_t size, const c
#define lua_pushglobaltable(L) \ #define lua_pushglobaltable(L) \
lua_pushvalue(L, LUA_GLOBALSINDEX) lua_pushvalue(L, LUA_GLOBALSINDEX)
#ifndef SOL_LUAJIT
#define luaL_newlib(L, l) \ #define luaL_newlib(L, l) \
(lua_newtable((L)),luaL_setfuncs((L), (l), 0)) (lua_newtable((L)),luaL_setfuncs((L), (l), 0))
#else
#if SOL_LUAJIT_VERSION < 20100
#define luaL_newlib(L, l) \
(lua_newtable((L)),luaL_setfuncs((L), (l), 0))
#endif // LuaJIT-2.1.0-beta3 added this in itself
#endif // LuaJIT Compatibility
void luaL_checkversion(lua_State *L); void luaL_checkversion(lua_State *L);

View File

@ -33,8 +33,15 @@
#define lua_pushglobaltable(L) \ #define lua_pushglobaltable(L) \
lua_pushvalue(L, LUA_GLOBALSINDEX) lua_pushvalue(L, LUA_GLOBALSINDEX)
#ifndef SOL_LUAJIT
#define luaL_newlib(L, l) \ #define luaL_newlib(L, l) \
(lua_newtable((L)),luaL_setfuncs((L), (l), 0)) (lua_newtable((L)),luaL_setfuncs((L), (l), 0))
#else
#if SOL_LUAJIT_VERSION < 20100
#define luaL_newlib(L, l) \
(lua_newtable((L)),luaL_setfuncs((L), (l), 0))
#endif // LuaJIT-2.1.0-beta3 added this in itself
#endif // LuaJIT Compatibility
void luaL_checkversion(lua_State *L); void luaL_checkversion(lua_State *L);