LuaEnvironment.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdarg.h>
  24. #include "LuaEnvironment.h"
  25. #include "Assert.h"
  26. #include "StringUtils.h"
  27. #include "LuaStack.h"
  28. #include "Device.h"
  29. #include "LuaResource.h"
  30. #include "ResourceManager.h"
  31. #include "Config.h"
  32. namespace crown
  33. {
  34. //-----------------------------------------------------------------------------
  35. LuaEnvironment::LuaEnvironment(lua_State* L)
  36. : m_state(L)
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. bool LuaEnvironment::load_and_execute(const char* res_name)
  41. {
  42. CE_ASSERT_NOT_NULL(res_name);
  43. ResourceManager* resman = device()->resource_manager();
  44. // Load the resource
  45. ResourceId res_id = resman->load("lua", res_name);
  46. resman->flush();
  47. LuaResource* lr = (LuaResource*) resman->data(res_id);
  48. lua_getglobal(m_state, "debug");
  49. lua_getfield(m_state, -1, "traceback");
  50. if (luaL_loadbuffer(m_state, (const char*) lr->program(), lr->size(), res_name) == 0)
  51. {
  52. if (lua_pcall(m_state, 0, 0, -2) == 0)
  53. {
  54. // Unloading is OK since the script data has been copied to Lua
  55. resman->unload(res_id);
  56. lua_pop(m_state, 2); // Pop debug.traceback
  57. return true;
  58. }
  59. }
  60. error();
  61. lua_pop(m_state, 2); // Pop debug.traceback
  62. return false;
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool LuaEnvironment::execute_string(const char* s)
  66. {
  67. CE_ASSERT_NOT_NULL(s);
  68. lua_getglobal(m_state, "debug");
  69. lua_getfield(m_state, -1, "traceback");
  70. if (luaL_loadstring(m_state, s) == 0)
  71. {
  72. if (lua_pcall(m_state, 0, 0, -2) == 0)
  73. {
  74. // Unloading is OK since the script data has been copied to Lua
  75. lua_pop(m_state, 2); // Pop debug.traceback
  76. return true;
  77. }
  78. }
  79. error();
  80. lua_pop(m_state, 2); // Pop debug.traceback
  81. return false;
  82. }
  83. //-----------------------------------------------------------------------------
  84. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  85. {
  86. luaL_Reg entry[2];
  87. entry[0].name = name;
  88. entry[0].func = func;
  89. entry[1].name = NULL;
  90. entry[1].func = NULL;
  91. luaL_register(m_state, module, entry);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void LuaEnvironment::load_module_enum(const char* module, const char* name, uint32_t value)
  95. {
  96. // Checks table existance
  97. lua_pushstring(m_state, module);
  98. lua_rawget(m_state, LUA_GLOBALSINDEX);
  99. if (!lua_istable(m_state, -1)) // If not exixts
  100. {
  101. // Creates table
  102. lua_newtable(m_state);
  103. lua_setglobal(m_state, module);
  104. }
  105. // Adds field to table
  106. lua_getglobal(m_state, module);
  107. lua_pushinteger(m_state, value);
  108. lua_setfield(m_state, -2, name);
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  112. {
  113. CE_ASSERT_NOT_NULL(func);
  114. LuaStack stack(m_state);
  115. va_list vl;
  116. va_start(vl, argc);
  117. lua_getglobal(m_state, "debug");
  118. lua_getfield(m_state, -1, "traceback");
  119. lua_getglobal(m_state, func);
  120. for (uint8_t i = 0; i < argc; i++)
  121. {
  122. const int type = va_arg(vl, int);
  123. switch (type)
  124. {
  125. case ARGUMENT_FLOAT:
  126. {
  127. stack.push_float(va_arg(vl, double));
  128. break;
  129. }
  130. default:
  131. {
  132. CE_ASSERT(false, "Oops, lua argument unknown");
  133. break;
  134. }
  135. }
  136. }
  137. va_end(vl);
  138. if (lua_pcall(m_state, argc, 0, -argc - 2) != 0)
  139. {
  140. error();
  141. lua_pop(m_state, 2); // Pop debug.traceback
  142. return false;
  143. }
  144. lua_pop(m_state, 2); // Pop debug.traceback
  145. return true;
  146. }
  147. //-----------------------------------------------------------------------------
  148. void LuaEnvironment::error()
  149. {
  150. const char* msg = lua_tostring(m_state, -1);
  151. Log::e(msg);
  152. lua_pop(m_state, 1);
  153. }
  154. } // namespace crown