File: environments_on_functions.cpp

package info (click to toggle)
sol2 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,096 kB
  • sloc: cpp: 43,816; ansic: 1,018; python: 356; sh: 288; makefile: 202
file content (85 lines) | stat: -rw-r--r-- 2,481 bytes parent folder | download
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

#include <iostream>

int main(int, char**) {
	sol::state lua;
	lua.open_libraries(sol::lib::base);

	// Environments can set on functions (scripts), userdata and
	// threads let's look at functions

	lua.script("f = function() return test end");
	sol::function f = lua["f"];

	sol::environment env_f(lua, sol::create);
	env_f["test"] = 31;
	bool env_f_set_success = sol::set_environment(env_f, f);
	SOL_ASSERT(env_f_set_success);

	// the function returns the value from the environment table
	int result = f();
	SOL_ASSERT(result == 31);


	// You can also protect from variables
	// being set without the 'local' specifier
	lua.script("g = function() test = 5 end");
	sol::function g = lua["g"];
	sol::environment env_g(lua, sol::create);
	bool env_g_set_success
	     = env_g.set_on(g); // same as set_environment
	SOL_ASSERT(env_g_set_success);

	g();
	// the value can be retrieved from the env table
	int test = env_g["test"];
	SOL_ASSERT(test == 5);


	// the global environment
	// is not polluted at all, despite both functions being used
	// and set
	sol::object global_test = lua["test"];
	SOL_ASSERT(!global_test.valid());


	// You can retrieve environments in C++
	// and check the environment of functions
	// gotten from Lua

	// get the environment from any sol::reference-styled type,
	// including sol::object, sol::function, sol::table,
	// sol::userdata ...
	lua.set_function("check_f_env",
	     // capture necessary variable in C++ lambda
	     [&env_f](sol::object target) {
		     // pull out the environment from func using
		     // sol::env_key constructor
		     sol::environment target_env(sol::env_key, target);
		     int test_env_f = env_f["test"];
		     int test_target_env = target_env["test"];
		     // the environment for f the one gotten from
		     // `target` are the same
		     SOL_ASSERT(test_env_f == test_target_env);
		     SOL_ASSERT(test_env_f == 31);
		     SOL_ASSERT(env_f == target_env);
	     });
	lua.set_function(
	     "check_g_env", [&env_g](sol::function target) {
		     // equivalent:
		     sol::environment target_env
		          = sol::get_environment(target);
		     int test_env_g = env_g["test"];
		     int test_target_env = target_env["test"];
		     SOL_ASSERT(test_env_g == test_target_env);
		     SOL_ASSERT(test_env_g == 5);
		     SOL_ASSERT(env_g == target_env);
	     });

	lua.script("check_f_env(f)");
	lua.script("check_g_env(g)");

	return 0;
}