Advertisement
Guest User

bug2.cpp

a guest
Nov 15th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4.  
  5. #include "lua.hpp"
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. lua_State *lstate1;
  14. lua_Integer size;
  15. int COMPONENTS_stack_index, second_stack_index;
  16.  
  17. lstate1 = luaL_newstate();
  18. luaL_openlibs(lstate1);
  19.  
  20. if (luaL_dofile(lstate1, "components.lua"))
  21. {
  22. cout<<"dofile() error"<<endl;
  23. return -1;
  24. }
  25.  
  26. cout<<"LUAI_MAXSTACK = "<<LUAI_MAXSTACK<<endl;
  27.  
  28. lua_getglobal(lstate1, "COMPONENTS");
  29.  
  30. COMPONENTS_stack_index = lua_absindex(lstate1, -1);
  31.  
  32. lua_len(lstate1, -1);
  33.  
  34. size = lua_tointeger(lstate1, -1);
  35.  
  36. lua_pop(lstate1, 1);
  37.  
  38. cout<<"COMPONENTS size is "<<size<<endl;
  39.  
  40. for (int i = 0; i < size; i++)
  41. {
  42. const char *str_ptr;
  43.  
  44. cout<<"lua_gettop() = "<< lua_gettop(lstate1)<<endl;
  45.  
  46.  
  47. if (lua_istable(lstate1, COMPONENTS_stack_index))
  48. {
  49. lua_pushinteger(lstate1, i + 1);
  50. lua_gettable(lstate1, COMPONENTS_stack_index); //COMPONENTS[i]
  51. second_stack_index = lua_absindex(lstate1, -1);
  52.  
  53. if (lua_istable(lstate1, -1))
  54. {
  55. lua_pushinteger(lstate1, 1);
  56. lua_gettable(lstate1, second_stack_index); //COMPONENTS[i][1]
  57.  
  58. str_ptr = lua_tolstring(lstate1, -1, NULL);
  59.  
  60. if (!str_ptr)
  61. {
  62. cout<<"lua_tolstring() returned NULL!"<<endl;
  63.  
  64. cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
  65.  
  66. return -1;
  67. }
  68.  
  69. cout<<"COMPONENTS["<<i<<"][1] = "<<str_ptr<<endl;
  70.  
  71. //lua_pop(lstate1, 1);
  72.  
  73. /////////////////////////////////
  74.  
  75. lua_pushinteger(lstate1,2);
  76. lua_gettable(lstate1, second_stack_index); //COMPONENTS[i][2]
  77.  
  78. str_ptr = lua_tolstring(lstate1, -1, NULL);
  79.  
  80. if (!str_ptr)
  81. {
  82. cout<<"lua_tolstring() returned NULL!"<<endl;
  83.  
  84. cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
  85.  
  86. return -1;
  87. }
  88.  
  89. cout<<"COMPONENTS["<<i<<"][2] = "<<str_ptr<<endl;
  90.  
  91. //lua_pop(lstate1, 1);
  92.  
  93. /////////////////////////////////
  94.  
  95. lua_pushinteger(lstate1,3);
  96. lua_gettable(lstate1, second_stack_index); //COMPONENTS[i][3]
  97.  
  98. str_ptr = lua_tolstring(lstate1, -1, NULL);
  99.  
  100. if (!str_ptr)
  101. {
  102. cout<<"lua_tolstring() returned NULL!"<<endl;
  103.  
  104. cout<<"type on stack top is "<<lua_typename(lstate1, lua_type(lstate1, -1))<<endl;
  105.  
  106. return -1;
  107. }
  108.  
  109. cout<<"COMPONENTS["<<i<<"][3] = "<<str_ptr<<endl;
  110.  
  111. //lua_pop(lstate1, 1);
  112.  
  113. }
  114. else
  115. {
  116. cout<<"COMPONENTS["<<i<<"] value is not a table!"<<endl;
  117. return -1;
  118. }
  119.  
  120. //lua_pop(lstate1, 1);
  121. }
  122. else
  123. {
  124. cout<<"error, not a table type"<<endl;
  125. return -1;
  126. }
  127. }
  128.  
  129. lua_close(lstate1);
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement