test(lua): add example showing how to access/update an environment object using multiple threads
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
0af8f17834
commit
eb9d0f0552
2 changed files with 34 additions and 0 deletions
|
@ -7,6 +7,7 @@ Author: Leonardo de Moura
|
|||
#include <sstream>
|
||||
#include <lua.hpp>
|
||||
#include "kernel/environment.h"
|
||||
#include "kernel/formatter.h"
|
||||
#include "bindings/lua/util.h"
|
||||
#include "bindings/lua/name.h"
|
||||
#include "bindings/lua/level.h"
|
||||
|
@ -128,8 +129,19 @@ static int environment_pred(lua_State * L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int environment_tostring(lua_State * L) {
|
||||
ro_environment env(L, 1);
|
||||
std::ostringstream out;
|
||||
// TODO(Leo): get formatter from registry
|
||||
formatter fmt = mk_simple_formatter();
|
||||
out << fmt(env);
|
||||
lua_pushfstring(L, out.str().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const struct luaL_Reg environment_m[] = {
|
||||
{"__gc", environment_gc}, // never throws
|
||||
{"__tostring", safe_function<environment_tostring>},
|
||||
{"add_uvar", safe_function<environment_add_uvar>},
|
||||
{"is_ge", safe_function<environment_is_ge>},
|
||||
{"get_uvar", safe_function<environment_get_uvar>},
|
||||
|
|
22
tests/lua/threads/th2.lua
Normal file
22
tests/lua/threads/th2.lua
Normal file
|
@ -0,0 +1,22 @@
|
|||
S1 = State()
|
||||
S2 = State()
|
||||
code = [[
|
||||
function f(env, prefix, num, type)
|
||||
for i = 1, num do
|
||||
env:add_var(prefix .. "_" .. i, type)
|
||||
end
|
||||
end
|
||||
]]
|
||||
S1:dostring(code)
|
||||
S2:dostring(code)
|
||||
e = environment()
|
||||
e:add_var("N", Type())
|
||||
code2 = [[
|
||||
e, prefix = ...
|
||||
f(e, prefix, 1000, Const("N"))
|
||||
]]
|
||||
T1 = thread(S1, code2, e, "x")
|
||||
T2 = thread(S2, code2, e, "y")
|
||||
T1:wait()
|
||||
T2:wait()
|
||||
print(e)
|
Loading…
Reference in a new issue