Lines Matching refs:state
29 #include "state.ipp"
44 // A note about the lutok::state tests.
46 // The methods of lutok::state are, in general, thin wrappers around the
53 // Lastly, for every test case that stresses a single lutok::state method, we
54 // only call that method directly. All other Lua state manipulation operations
64 /// \param state The Lua state.
69 is_available(lutok::state& state, const char* symbol)
71 luaL_loadstring(raw(state), (std::string("return ") + symbol).c_str());
72 const bool ok = (lua_pcall(raw(state), 0, 1, 0) == 0 &&
73 !lua_isnil(raw(state), -1));
74 lua_pop(raw(state), 1);
85 /// \param state The Lua state.
88 check_modules(lutok::state& state, const std::string& expected)
92 ATF_REQUIRE(!((expected == "base") ^ (is_available(state, "assert"))));
94 (is_available(state, "string.byte"))));
96 (is_available(state, "table.concat"))));
105 /// \param raw_state The raw Lua state.
111 lutok::state state = lutok::state_c_gate::connect(raw_state);
112 const int i1 = lua_tointeger(raw_state, state.upvalue_index(1));
113 const int i2 = lua_tointeger(raw_state, state.upvalue_index(2));
125 /// \param state The Lua state.
129 cxx_multiply_closure(lutok::state& state)
131 const int f1 = lua_tointeger(raw(state), lua_upvalueindex(1));
132 const int f2 = lua_tointeger(raw(state), -1);
133 lua_pushinteger(raw(state), f1 * f2);
145 /// \param state The Lua state.
154 cxx_divide(lutok::state& state)
156 const int dividend = state.to_integer(-2);
157 const int divisor = state.to_integer(-1);
162 state.push_integer(dividend / divisor);
163 state.push_integer(dividend % divisor);
172 /// \param state The Lua state.
179 raise_long_error(lutok::state& state)
181 const int length = state.to_integer();
192 lutok::state state;
193 state.close();
194 // The destructor for state will run now. If it does a second close, we may
202 lutok::state state;
203 ATF_REQUIRE(luaL_dostring(raw(state), "test_variable = 3") == 0);
204 state.get_global("test_variable");
205 ATF_REQUIRE(lua_isnumber(raw(state), -1));
206 lua_pop(raw(state), 1);
213 lutok::state state;
214 state.get_global("test_variable");
215 ATF_REQUIRE(lua_isnil(raw(state), -1));
216 lua_pop(raw(state), 1);
223 lutok::state state;
224 ATF_REQUIRE(luaL_dostring(raw(state), "global_variable = 'hello'") == 0);
225 state.get_global_table();
226 lua_pushstring(raw(state), "global_variable");
227 lua_gettable(raw(state), -2);
228 ATF_REQUIRE(lua_isstring(raw(state), -1));
229 ATF_REQUIRE(std::strcmp("hello", lua_tostring(raw(state), -1)) == 0);
230 lua_pop(raw(state), 2);
237 lutok::state state;
238 luaL_openlibs(raw(state));
239 ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
241 lua_getglobal(raw(state), "t");
242 ATF_REQUIRE(state.get_metafield(-1, "foo"));
243 ATF_REQUIRE(lua_isnumber(raw(state), -1));
244 ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
245 lua_pop(raw(state), 2);
252 lutok::state state;
253 luaL_openlibs(raw(state));
254 ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
256 lua_getglobal(raw(state), "t");
257 ATF_REQUIRE(!state.get_metafield(-1, "bar"));
258 lua_pop(raw(state), 1);
265 lutok::state state;
266 luaL_openlibs(raw(state));
267 ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
269 lua_getglobal(raw(state), "t");
270 ATF_REQUIRE(state.get_metatable());
271 ATF_REQUIRE(lua_istable(raw(state), -1));
272 lua_pushstring(raw(state), "foo");
273 lua_gettable(raw(state), -2);
274 ATF_REQUIRE(lua_isnumber(raw(state), -1));
275 ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
276 lua_pop(raw(state), 3);
283 lutok::state state;
284 luaL_openlibs(raw(state));
285 ATF_REQUIRE(luaL_dostring(raw(state), "meta = { foo = 567 }; "
287 lua_getglobal(raw(state), "t");
288 lua_pushinteger(raw(state), 5555);
289 ATF_REQUIRE(state.get_metatable(-2));
290 ATF_REQUIRE(lua_istable(raw(state), -1));
291 lua_pushstring(raw(state), "foo");
292 lua_gettable(raw(state), -2);
293 ATF_REQUIRE(lua_isnumber(raw(state), -1));
294 ATF_REQUIRE_EQ(567, lua_tointeger(raw(state), -1));
295 lua_pop(raw(state), 4);
302 lutok::state state;
303 ATF_REQUIRE(luaL_dostring(raw(state), "t = {}") == 0);
304 lua_getglobal(raw(state), "t");
305 ATF_REQUIRE(!state.get_metatable(-1));
306 lua_pop(raw(state), 1);
313 lutok::state state;
314 ATF_REQUIRE(luaL_dostring(raw(state), "t = { a = 1, bar = 234 }") == 0);
315 lua_getglobal(raw(state), "t");
316 lua_pushstring(raw(state), "bar");
317 state.get_table();
318 ATF_REQUIRE(lua_isnumber(raw(state), -1));
319 ATF_REQUIRE_EQ(234, lua_tointeger(raw(state), -1));
320 lua_pop(raw(state), 2);
327 lutok::state state;
328 lua_pushnil(raw(state));
329 lua_pushinteger(raw(state), 1);
330 REQUIRE_API_ERROR("lua_gettable", state.get_table());
331 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
332 lua_pop(raw(state), 2);
339 lutok::state state;
340 ATF_REQUIRE(luaL_dostring(raw(state),
342 lua_getglobal(raw(state), "the_table");
343 lua_pushstring(raw(state), "baz");
344 state.get_table();
345 ATF_REQUIRE(lua_isnil(raw(state), -1));
346 lua_pop(raw(state), 2);
353 lutok::state state;
354 ATF_REQUIRE_EQ(0, state.get_top());
355 lua_pushinteger(raw(state), 3);
356 ATF_REQUIRE_EQ(1, state.get_top());
357 lua_pushinteger(raw(state), 3);
358 ATF_REQUIRE_EQ(2, state.get_top());
359 lua_pop(raw(state), 2);
366 lutok::state state;
367 lua_pushinteger(raw(state), 1);
368 lua_pushinteger(raw(state), 2);
369 lua_pushinteger(raw(state), 3);
370 lua_pushinteger(raw(state), 4);
371 state.insert(-3);
372 ATF_REQUIRE_EQ(3, lua_tointeger(raw(state), -1));
373 ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -2));
374 ATF_REQUIRE_EQ(4, lua_tointeger(raw(state), -3));
375 ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -4));
376 lua_pop(raw(state), 4);
383 lutok::state state;
384 ATF_REQUIRE(!state.is_boolean());
391 lutok::state state;
392 lua_pushnil(raw(state));
393 ATF_REQUIRE(!state.is_boolean());
394 lua_pushboolean(raw(state), 1);
395 ATF_REQUIRE(state.is_boolean());
396 lua_pop(raw(state), 2);
403 lutok::state state;
404 lua_pushboolean(raw(state), 1);
405 ATF_REQUIRE(state.is_boolean(-1));
406 lua_pushinteger(raw(state), 5);
407 ATF_REQUIRE(!state.is_boolean(-1));
408 ATF_REQUIRE(state.is_boolean(-2));
409 lua_pop(raw(state), 2);
416 lutok::state state;
417 ATF_REQUIRE(!state.is_function());
424 lutok::state state;
425 luaL_dostring(raw(state), "function my_func(a, b) return a + b; end");
427 lua_pushnil(raw(state));
428 ATF_REQUIRE(!state.is_function());
429 lua_getglobal(raw(state), "my_func");
430 ATF_REQUIRE(state.is_function());
431 lua_pop(raw(state), 2);
438 lutok::state state;
439 luaL_dostring(raw(state), "function my_func(a, b) return a + b; end");
441 lua_getglobal(raw(state), "my_func");
442 ATF_REQUIRE(state.is_function(-1));
443 lua_pushinteger(raw(state), 5);
444 ATF_REQUIRE(!state.is_function(-1));
445 ATF_REQUIRE(state.is_function(-2));
446 lua_pop(raw(state), 2);
453 lutok::state state;
454 ATF_REQUIRE(state.is_nil());
461 lutok::state state;
462 lua_pushnil(raw(state));
463 ATF_REQUIRE(state.is_nil());
464 lua_pushinteger(raw(state), 5);
465 ATF_REQUIRE(!state.is_nil());
466 lua_pop(raw(state), 2);
473 lutok::state state;
474 lua_pushnil(raw(state));
475 ATF_REQUIRE(state.is_nil(-1));
476 lua_pushinteger(raw(state), 5);
477 ATF_REQUIRE(!state.is_nil(-1));
478 ATF_REQUIRE(state.is_nil(-2));
479 lua_pop(raw(state), 2);
486 lutok::state state;
487 ATF_REQUIRE(!state.is_number());
494 lutok::state state;
495 lua_pushnil(raw(state));
496 ATF_REQUIRE(!state.is_number());
497 lua_pushinteger(raw(state), 5);
498 ATF_REQUIRE(state.is_number());
499 lua_pop(raw(state), 2);
506 lutok::state state;
507 lua_pushnil(raw(state));
508 ATF_REQUIRE(!state.is_number(-1));
509 lua_pushinteger(raw(state), 5);
510 ATF_REQUIRE(state.is_number(-1));
511 ATF_REQUIRE(!state.is_number(-2));
512 lua_pop(raw(state), 2);
519 lutok::state state;
520 ATF_REQUIRE(!state.is_string());
527 lutok::state state;
528 lua_pushnil(raw(state));
529 ATF_REQUIRE(!state.is_string());
530 lua_pushinteger(raw(state), 3);
531 ATF_REQUIRE(state.is_string());
532 lua_pushstring(raw(state), "foo");
533 ATF_REQUIRE(state.is_string());
534 lua_pop(raw(state), 3);
541 lutok::state state;
542 lua_pushinteger(raw(state), 3);
543 ATF_REQUIRE(state.is_string(-1));
544 lua_pushnil(raw(state));
545 ATF_REQUIRE(!state.is_string(-1));
546 ATF_REQUIRE(state.is_string(-2));
547 lua_pushstring(raw(state), "foo");
548 ATF_REQUIRE(state.is_string(-1));
549 ATF_REQUIRE(!state.is_string(-2));
550 ATF_REQUIRE(state.is_string(-3));
551 lua_pop(raw(state), 3);
558 lutok::state state;
559 ATF_REQUIRE(!state.is_table());
566 lutok::state state;
567 luaL_dostring(raw(state), "t = {3, 4, 5}");
569 lua_pushstring(raw(state), "foo");
570 ATF_REQUIRE(!state.is_table());
571 lua_getglobal(raw(state), "t");
572 ATF_REQUIRE(state.is_table());
573 lua_pop(raw(state), 2);
580 lutok::state state;
581 luaL_dostring(raw(state), "t = {3, 4, 5}");
583 lua_pushstring(raw(state), "foo");
584 ATF_REQUIRE(!state.is_table(-1));
585 lua_getglobal(raw(state), "t");
586 ATF_REQUIRE(state.is_table(-1));
587 ATF_REQUIRE(!state.is_table(-2));
588 lua_pop(raw(state), 2);
595 lutok::state state;
596 ATF_REQUIRE(!state.is_userdata());
603 lutok::state state;
605 lua_pushstring(raw(state), "foo");
606 ATF_REQUIRE(!state.is_userdata());
607 lua_newuserdata(raw(state), 1234);
608 ATF_REQUIRE(state.is_userdata());
609 lua_pop(raw(state), 2);
616 lutok::state state;
618 lua_pushstring(raw(state), "foo");
619 ATF_REQUIRE(!state.is_userdata(-1));
620 lua_newuserdata(raw(state), 543);
621 ATF_REQUIRE(state.is_userdata(-1));
622 ATF_REQUIRE(!state.is_userdata(-2));
623 lua_pop(raw(state), 2);
634 lutok::state state;
635 state.load_file("test.lua");
636 ATF_REQUIRE(lua_pcall(raw(state), 0, 0, 0) == 0);
637 lua_getglobal(raw(state), "in_the_file");
638 ATF_REQUIRE(std::strcmp("oh yes", lua_tostring(raw(state), -1)) == 0);
639 lua_pop(raw(state), 1);
650 lutok::state state;
651 REQUIRE_API_ERROR("luaL_loadfile", state.load_file("test.lua"));
658 lutok::state state;
660 state.load_file("missing.lua"));
667 lutok::state state;
668 state.load_string("return 2 + 3");
669 ATF_REQUIRE(lua_pcall(raw(state), 0, 1, 0) == 0);
670 ATF_REQUIRE_EQ(5, lua_tointeger(raw(state), -1));
671 lua_pop(raw(state), 1);
678 lutok::state state;
679 REQUIRE_API_ERROR("luaL_loadstring", state.load_string("-"));
686 lutok::state state;
687 state.new_table();
688 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
689 ATF_REQUIRE(lua_istable(raw(state), -1));
690 lua_pop(raw(state), 1);
697 lutok::state state;
698 int* pointer = state.new_userdata< int >();
700 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
701 ATF_REQUIRE(lua_isuserdata(raw(state), -1));
702 lua_pop(raw(state), 1);
709 lutok::state state;
710 luaL_dostring(raw(state), "t = {}");
712 lua_getglobal(raw(state), "t");
713 lua_pushstring(raw(state), "this is a dummy value");
714 lua_pushnil(raw(state));
715 ATF_REQUIRE(!state.next(-3));
716 lua_pop(raw(state), 2);
723 lutok::state state;
724 luaL_dostring(raw(state), "t = {}; t[1] = 100; t[2] = 200");
726 lua_getglobal(raw(state), "t");
727 lua_pushnil(raw(state));
729 ATF_REQUIRE(state.next());
730 ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
731 ATF_REQUIRE(lua_isnumber(raw(state), -2));
732 ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -2));
733 ATF_REQUIRE(lua_isnumber(raw(state), -1));
734 ATF_REQUIRE_EQ(100, lua_tointeger(raw(state), -1));
735 lua_pop(raw(state), 1);
737 ATF_REQUIRE(state.next());
738 ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
739 ATF_REQUIRE(lua_isnumber(raw(state), -2));
740 ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -2));
741 ATF_REQUIRE(lua_isnumber(raw(state), -1));
742 ATF_REQUIRE_EQ(200, lua_tointeger(raw(state), -1));
743 lua_pop(raw(state), 1);
745 ATF_REQUIRE(!state.next());
746 lua_pop(raw(state), 1);
753 lutok::state state;
754 check_modules(state, "");
755 state.open_base();
756 check_modules(state, "base");
763 lutok::state state;
764 check_modules(state, "");
765 state.open_string();
766 check_modules(state, "string");
773 lutok::state state;
774 check_modules(state, "");
775 state.open_table();
776 check_modules(state, "table");
783 lutok::state state;
784 luaL_loadstring(raw(state), "function mul(a, b) return a * b; end");
785 state.pcall(0, 0, 0);
786 state.get_global_table();
787 lua_pushstring(raw(state), "mul");
788 lua_gettable(raw(state), -2);
789 lua_pushinteger(raw(state), 3);
790 lua_pushinteger(raw(state), 5);
791 state.pcall(2, 1, 0);
792 ATF_REQUIRE_EQ(15, lua_tointeger(raw(state), -1));
793 lua_pop(raw(state), 2);
800 lutok::state state;
801 lua_pushnil(raw(state));
802 REQUIRE_API_ERROR("lua_pcall", state.pcall(0, 0, 0));
809 lutok::state state;
810 lua_pushinteger(raw(state), 10);
811 lua_pushinteger(raw(state), 20);
812 lua_pushinteger(raw(state), 30);
813 state.pop(1);
814 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
815 ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -1));
816 lua_pop(raw(state), 2);
823 lutok::state state;
824 lua_pushinteger(raw(state), 10);
825 lua_pushinteger(raw(state), 20);
826 lua_pushinteger(raw(state), 30);
827 state.pop(2);
828 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
829 ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -1));
830 lua_pop(raw(state), 1);
837 lutok::state state;
838 state.push_boolean(true);
839 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
840 ATF_REQUIRE(lua_toboolean(raw(state), -1));
841 state.push_boolean(false);
842 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
843 ATF_REQUIRE(!lua_toboolean(raw(state), -1));
844 ATF_REQUIRE(lua_toboolean(raw(state), -2));
845 lua_pop(raw(state), 2);
852 lutok::state state;
853 state.push_integer(15);
854 state.push_cxx_closure(cxx_multiply_closure, 1);
855 lua_setglobal(raw(state), "cxx_multiply_closure");
857 ATF_REQUIRE(luaL_dostring(raw(state),
859 ATF_REQUIRE_EQ(150, lua_tointeger(raw(state), -1));
860 lua_pop(raw(state), 1);
867 lutok::state state;
868 state.push_cxx_function(cxx_divide);
869 lua_setglobal(raw(state), "cxx_divide");
871 ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(17, 3)") == 0);
872 ATF_REQUIRE_EQ(5, lua_tointeger(raw(state), -2));
873 ATF_REQUIRE_EQ(2, lua_tointeger(raw(state), -1));
874 lua_pop(raw(state), 2);
881 lutok::state state;
882 state.push_cxx_function(cxx_divide);
883 lua_setglobal(raw(state), "cxx_divide");
885 ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(15, 0)") != 0);
886 ATF_REQUIRE_MATCH("Divisor is 0", lua_tostring(raw(state), -1));
887 lua_pop(raw(state), 1);
894 lutok::state state;
895 state.push_cxx_function(cxx_divide);
896 lua_setglobal(raw(state), "cxx_divide");
898 ATF_REQUIRE(luaL_dostring(raw(state), "return cxx_divide(-3, -1)") != 0);
899 ATF_REQUIRE_MATCH("Unhandled exception", lua_tostring(raw(state), -1));
900 lua_pop(raw(state), 1);
907 lutok::state state;
908 state.push_cxx_function(raise_long_error);
909 lua_setglobal(raw(state), "fail");
911 ATF_REQUIRE(luaL_dostring(raw(state), "return fail(900)") != 0);
912 ATF_REQUIRE_MATCH(std::string(900, 'A'), lua_tostring(raw(state), -1));
913 lua_pop(raw(state), 1);
915 ATF_REQUIRE(luaL_dostring(raw(state), "return fail(8192)") != 0);
916 ATF_REQUIRE_MATCH(std::string(900, 'A'), lua_tostring(raw(state), -1));
917 lua_pop(raw(state), 1);
924 lutok::state state;
925 state.push_integer(12);
926 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
927 ATF_REQUIRE_EQ(12, lua_tointeger(raw(state), -1));
928 state.push_integer(34);
929 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
930 ATF_REQUIRE_EQ(34, lua_tointeger(raw(state), -1));
931 ATF_REQUIRE_EQ(12, lua_tointeger(raw(state), -2));
932 lua_pop(raw(state), 2);
939 lutok::state state;
940 state.push_nil();
941 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
942 ATF_REQUIRE(lua_isnil(raw(state), -1));
943 state.push_integer(34);
944 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
945 ATF_REQUIRE(!lua_isnil(raw(state), -1));
946 ATF_REQUIRE(lua_isnil(raw(state), -2));
947 lua_pop(raw(state), 2);
954 lutok::state state;
958 state.push_string(str);
959 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
960 ATF_REQUIRE_EQ(std::string("first"), lua_tostring(raw(state), -1));
962 state.push_string(str);
964 ATF_REQUIRE_EQ(2, lua_gettop(raw(state)));
965 ATF_REQUIRE_EQ(std::string("second"), lua_tostring(raw(state), -1));
966 ATF_REQUIRE_EQ(std::string("first"), lua_tostring(raw(state), -2));
967 lua_pop(raw(state), 2);
974 lutok::state state;
976 lua_pushinteger(raw(state), 10);
977 lua_pushinteger(raw(state), 20);
978 state.push_value();
979 ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
980 ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -1));
981 ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -2));
982 ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -3));
983 lua_pop(raw(state), 3);
990 lutok::state state;
992 lua_pushinteger(raw(state), 10);
993 lua_pushinteger(raw(state), 20);
994 state.push_value(-2);
995 ATF_REQUIRE_EQ(3, lua_gettop(raw(state)));
996 ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -1));
997 ATF_REQUIRE_EQ(20, lua_tointeger(raw(state), -2));
998 ATF_REQUIRE_EQ(10, lua_tointeger(raw(state), -3));
999 lua_pop(raw(state), 3);
1006 lutok::state state;
1008 luaL_openlibs(raw(state));
1010 raw(state), "t = {foo=123} ; setmetatable(t, {__index=1})") == 0);
1011 lua_getglobal(raw(state), "t");
1012 lua_pushstring(raw(state), "foo");
1013 state.raw_get();
1014 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1015 ATF_REQUIRE_EQ(123, lua_tointeger(raw(state), -1));
1016 lua_pop(raw(state), 2);
1023 lutok::state state;
1025 luaL_openlibs(raw(state));
1027 raw(state), "t = {foo=123} ; setmetatable(t, {__index=1})") == 0);
1028 lua_getglobal(raw(state), "t");
1029 lua_pushinteger(raw(state), 9876);
1030 lua_pushstring(raw(state), "foo");
1031 state.raw_get(-3);
1032 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1033 ATF_REQUIRE_EQ(123, lua_tointeger(raw(state), -1));
1034 ATF_REQUIRE_EQ(9876, lua_tointeger(raw(state), -2));
1035 lua_pop(raw(state), 3);
1042 lutok::state state;
1044 luaL_openlibs(raw(state));
1046 raw(state), "t = {} ; setmetatable(t, {__newindex=1})") == 0);
1047 lua_getglobal(raw(state), "t");
1048 lua_pushstring(raw(state), "foo");
1049 lua_pushinteger(raw(state), 345);
1050 state.raw_set();
1051 ATF_REQUIRE(luaL_dostring(raw(state), "return t.foo") == 0);
1052 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1053 ATF_REQUIRE_EQ(345, lua_tointeger(raw(state), -1));
1054 lua_pop(raw(state), 2);
1061 lutok::state state;
1063 luaL_openlibs(raw(state));
1065 raw(state), "t = {} ; setmetatable(t, {__newindex=1})") == 0);
1066 lua_getglobal(raw(state), "t");
1067 lua_pushinteger(raw(state), 876);
1068 lua_pushstring(raw(state), "foo");
1069 lua_pushinteger(raw(state), 345);
1070 state.raw_set(-4);
1071 ATF_REQUIRE(luaL_dostring(raw(state), "return t.foo") == 0);
1072 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1073 ATF_REQUIRE_EQ(345, lua_tointeger(raw(state), -1));
1074 ATF_REQUIRE_EQ(876, lua_tointeger(raw(state), -2));
1075 lua_pop(raw(state), 3);
1082 lutok::state state;
1083 lua_pushvalue(raw(state), lutok::registry_index);
1084 lua_pushstring(raw(state), "custom_variable");
1085 lua_pushstring(raw(state), "custom value");
1086 lua_settable(raw(state), -3);
1087 lua_pop(raw(state), 1);
1088 ATF_REQUIRE(luaL_dostring(raw(state),
1090 ATF_REQUIRE(lua_isboolean(raw(state), -1));
1091 ATF_REQUIRE(lua_toboolean(raw(state), -1));
1092 lua_pop(raw(state), 1);
1099 lutok::state state;
1100 lua_pushinteger(raw(state), 3);
1101 state.set_global("test_variable");
1102 ATF_REQUIRE(luaL_dostring(raw(state), "return test_variable + 1") == 0);
1103 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1104 ATF_REQUIRE_EQ(4, lua_tointeger(raw(state), -1));
1105 lua_pop(raw(state), 1);
1112 lutok::state state;
1114 raw(state),
1120 lua_getglobal(raw(state), "numbers");
1121 lua_getglobal(raw(state), "mt");
1122 state.set_metatable();
1123 lua_pop(raw(state), 1);
1125 ATF_REQUIRE(luaL_dostring(raw(state), "return numbers + 2") == 0);
1126 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1127 ATF_REQUIRE_EQ(7, lua_tointeger(raw(state), -1));
1128 lua_pop(raw(state), 1);
1135 lutok::state state;
1137 raw(state),
1143 lua_getglobal(raw(state), "numbers");
1144 lua_pushinteger(raw(state), 1234);
1145 lua_getglobal(raw(state), "mt");
1146 state.set_metatable(-3);
1147 lua_pop(raw(state), 2);
1149 ATF_REQUIRE(luaL_dostring(raw(state), "return numbers + 2") == 0);
1150 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1151 ATF_REQUIRE_EQ(7, lua_tointeger(raw(state), -1));
1152 lua_pop(raw(state), 1);
1159 lutok::state state;
1160 ATF_REQUIRE(luaL_dostring(raw(state), "t = { a = 1, bar = 234 }") == 0);
1161 lua_getglobal(raw(state), "t");
1163 lua_pushstring(raw(state), "bar");
1164 lua_pushstring(raw(state), "baz");
1165 state.set_table();
1166 ATF_REQUIRE_EQ(1, lua_gettop(raw(state)));
1168 lua_pushstring(raw(state), "a");
1169 lua_gettable(raw(state), -2);
1170 ATF_REQUIRE(lua_isnumber(raw(state), -1));
1171 ATF_REQUIRE_EQ(1, lua_tointeger(raw(state), -1));
1172 lua_pop(raw(state), 1);
1174 lua_pushstring(raw(state), "bar");
1175 lua_gettable(raw(state), -2);
1176 ATF_REQUIRE(lua_isstring(raw(state), -1));
1177 ATF_REQUIRE_EQ(std::string("baz"), lua_tostring(raw(state), -1));
1178 lua_pop(raw(state), 1);
1180 lua_pop(raw(state), 1);
1187 lutok::state state;
1188 lua_pushnil(raw(state));
1189 lua_pushinteger(raw(state), 1);
1190 lua_pushinteger(raw(state), 2);
1191 REQUIRE_API_ERROR("lua_settable", state.set_table(-3));
1192 lua_pop(raw(state), 3);
1199 lutok::state state;
1200 lua_pushboolean(raw(state), 1);
1201 ATF_REQUIRE(state.to_boolean());
1202 lua_pushboolean(raw(state), 0);
1203 ATF_REQUIRE(!state.to_boolean());
1204 lua_pop(raw(state), 2);
1211 lutok::state state;
1212 lua_pushboolean(raw(state), 0);
1213 lua_pushboolean(raw(state), 1);
1214 ATF_REQUIRE(!state.to_boolean(-2));
1215 ATF_REQUIRE(state.to_boolean(-1));
1216 lua_pop(raw(state), 2);
1223 lutok::state state;
1224 lua_pushstring(raw(state), "34");
1225 ATF_REQUIRE_EQ(34, state.to_integer());
1226 lua_pushinteger(raw(state), 12);
1227 ATF_REQUIRE_EQ(12, state.to_integer());
1228 lua_pop(raw(state), 2);
1235 lutok::state state;
1236 lua_pushinteger(raw(state), 12);
1237 lua_pushstring(raw(state), "foobar");
1238 ATF_REQUIRE_EQ(12, state.to_integer(-2));
1239 lua_pop(raw(state), 2);
1246 lutok::state state;
1247 lua_pushstring(raw(state), "foobar");
1248 ATF_REQUIRE_EQ("foobar", state.to_string());
1249 lua_pushinteger(raw(state), 12);
1250 ATF_REQUIRE_EQ("12", state.to_string());
1251 lua_pop(raw(state), 2);
1258 lutok::state state;
1259 lua_pushstring(raw(state), "foobar");
1260 lua_pushinteger(raw(state), 12);
1261 ATF_REQUIRE_EQ("foobar", state.to_string(-2));
1262 ATF_REQUIRE_EQ("12", state.to_string(-1));
1263 lua_pop(raw(state), 2);
1270 lutok::state state;
1273 lua_newuserdata(raw(state), sizeof(int)));
1277 int* pointer = state.to_userdata< int >();
1279 lua_pop(raw(state), 1);
1286 lutok::state state;
1289 lua_newuserdata(raw(state), sizeof(int)));
1293 lua_pushinteger(raw(state), 3);
1294 int* pointer = state.to_userdata< int >(-2);
1296 lua_pop(raw(state), 2);
1303 lutok::state state;
1304 lua_pushinteger(raw(state), 25);
1305 lua_pushinteger(raw(state), 30);
1306 lua_pushcclosure(raw(state), c_get_upvalues, 2);
1307 lua_setglobal(raw(state), "c_get_upvalues");
1309 ATF_REQUIRE(luaL_dostring(raw(state),
1311 ATF_REQUIRE_EQ(25, lua_tointeger(raw(state), -2));
1312 ATF_REQUIRE_EQ(30, lua_tointeger(raw(state), -1));
1313 lua_pop(raw(state), 2);