lua_environment.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "error.h"
  7. #include "lua_environment.h"
  8. #include "lua_stack.h"
  9. #include "lua_resource.h"
  10. #include "device.h"
  11. #include "lua_assert.h"
  12. #include "resource_manager.h"
  13. #include "log.h"
  14. #include <stdarg.h>
  15. namespace crown
  16. {
  17. // Lua modules
  18. extern void load_debug_line(LuaEnvironment& env);
  19. extern void load_device(LuaEnvironment& env);
  20. extern void load_float_setting(LuaEnvironment& env);
  21. extern void load_gui(LuaEnvironment& env);
  22. extern void load_int_setting(LuaEnvironment& env);
  23. extern void load_keyboard(LuaEnvironment& env);
  24. extern void load_math(LuaEnvironment& env);
  25. extern void load_matrix4x4(LuaEnvironment& env);
  26. extern void load_mouse(LuaEnvironment& env);
  27. extern void load_physics_world(LuaEnvironment& env);
  28. extern void load_quaternion(LuaEnvironment& env);
  29. extern void load_resource_package(LuaEnvironment& env);
  30. extern void load_sound_world(LuaEnvironment& env);
  31. extern void load_string_setting(LuaEnvironment& env);
  32. extern void load_touch(LuaEnvironment& env);
  33. extern void load_vector3(LuaEnvironment& env);
  34. extern void load_window(LuaEnvironment& env);
  35. extern void load_world(LuaEnvironment& env);
  36. extern void load_color4(LuaEnvironment& env);
  37. extern void load_material(LuaEnvironment& env);
  38. // When an error occurs, logs the error message and pauses the engine.
  39. static int error_handler(lua_State* L)
  40. {
  41. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  42. if (!lua_istable(L, -1))
  43. {
  44. lua_pop(L, 1);
  45. return 0;
  46. }
  47. lua_getfield(L, -1, "traceback");
  48. if (!lua_isfunction(L, -1))
  49. {
  50. lua_pop(L, 2);
  51. return 0;
  52. }
  53. lua_pushvalue(L, 1); // Pass error message
  54. lua_pushinteger(L, 2);
  55. lua_call(L, 2, 1); // Call debug.traceback
  56. CE_LOGE(lua_tostring(L, -1)); // Print error message
  57. lua_pop(L, 1); // Remove error message from stack
  58. lua_pop(L, 1); // Remove debug.traceback from stack
  59. device()->pause();
  60. return 0;
  61. }
  62. // Redirects require to the resource manager.
  63. static int require(lua_State* L)
  64. {
  65. using namespace lua_resource;
  66. LuaStack stack(L);
  67. const LuaResource* lr = (LuaResource*)device()->resource_manager()->get(LUA_TYPE, stack.get_resource_id(1));
  68. luaL_loadbuffer(L, program(lr), size(lr), "");
  69. return 1;
  70. }
  71. static int lightuserdata_add(lua_State* L)
  72. {
  73. LuaStack stack(L);
  74. const Vector3& a = stack.get_vector3(1);
  75. const Vector3& b = stack.get_vector3(2);
  76. stack.push_vector3(a + b);
  77. return 1;
  78. }
  79. static int lightuserdata_sub(lua_State* L)
  80. {
  81. LuaStack stack(L);
  82. const Vector3& a = stack.get_vector3(1);
  83. const Vector3& b = stack.get_vector3(2);
  84. stack.push_vector3(a - b);
  85. return 1;
  86. }
  87. static int lightuserdata_mul(lua_State* L)
  88. {
  89. LuaStack stack(L);
  90. const Vector3& a = stack.get_vector3(1);
  91. const float b = stack.get_float(2);
  92. stack.push_vector3(a * b);
  93. return 1;
  94. }
  95. static int lightuserdata_div(lua_State* L)
  96. {
  97. LuaStack stack(L);
  98. const Vector3& a = stack.get_vector3(1);
  99. const float b = stack.get_float(2);
  100. stack.push_vector3(a / b);
  101. return 1;
  102. }
  103. static int lightuserdata_unm(lua_State* L)
  104. {
  105. LuaStack stack(L);
  106. stack.push_vector3(-stack.get_vector3(1));
  107. return 1;
  108. }
  109. static int lightuserdata_index(lua_State* L)
  110. {
  111. LuaStack stack(L);
  112. Vector3& v = stack.get_vector3(1);
  113. const char* s = stack.get_string(2);
  114. switch (s[0])
  115. {
  116. case 'x': stack.push_float(v.x); return 1;
  117. case 'y': stack.push_float(v.y); return 1;
  118. case 'z': stack.push_float(v.z); return 1;
  119. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  120. }
  121. return 0;
  122. }
  123. static int lightuserdata_newindex(lua_State* L)
  124. {
  125. LuaStack stack(L);
  126. Vector3& v = stack.get_vector3(1);
  127. const char* s = stack.get_string(2);
  128. const float value = stack.get_float(3);
  129. switch (s[0])
  130. {
  131. case 'x': v.x = value; break;
  132. case 'y': v.y = value; break;
  133. case 'z': v.z = value; break;
  134. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  135. }
  136. return 0;
  137. }
  138. LuaEnvironment::LuaEnvironment()
  139. : L(NULL)
  140. , _vec3_used(0)
  141. , _quat_used(0)
  142. , _mat4_used(0)
  143. {
  144. L = luaL_newstate();
  145. CE_ASSERT(L, "Unable to create lua state");
  146. }
  147. LuaEnvironment::~LuaEnvironment()
  148. {
  149. lua_close(L);
  150. }
  151. void LuaEnvironment::load_libs()
  152. {
  153. // Open default libraries
  154. luaL_openlibs(L);
  155. // Register crown libraries
  156. load_debug_line(*this);
  157. load_device(*this);
  158. load_float_setting(*this);
  159. load_gui(*this);
  160. load_int_setting(*this);
  161. load_keyboard(*this);
  162. load_math(*this);
  163. load_matrix4x4(*this);
  164. load_mouse(*this);
  165. load_physics_world(*this);
  166. load_quaternion(*this);
  167. load_resource_package(*this);
  168. load_sound_world(*this);
  169. load_string_setting(*this);
  170. load_touch(*this);
  171. load_vector3(*this);
  172. load_window(*this);
  173. load_world(*this);
  174. load_color4(*this);
  175. load_material(*this);
  176. // Register custom loader
  177. lua_getfield(L, LUA_GLOBALSINDEX, "package");
  178. lua_getfield(L, -1, "loaders");
  179. lua_remove(L, -2);
  180. int num_loaders = 0;
  181. lua_pushnil(L);
  182. while (lua_next(L, -2) != 0)
  183. {
  184. lua_pop(L, 1);
  185. num_loaders++;
  186. }
  187. lua_pushinteger(L, num_loaders + 1);
  188. lua_pushcfunction(L, require);
  189. lua_rawset(L, -3);
  190. lua_pop(L, 1);
  191. // Create metatable for lightuserdata
  192. luaL_newmetatable(L, "Lightuserdata_mt");
  193. lua_pushstring(L, "__add");
  194. lua_pushcfunction(L, lightuserdata_add);
  195. lua_settable(L, 1);
  196. lua_pushstring(L, "__sub");
  197. lua_pushcfunction(L, lightuserdata_sub);
  198. lua_settable(L, 1);
  199. lua_pushstring(L, "__mul");
  200. lua_pushcfunction(L, lightuserdata_mul);
  201. lua_settable(L, 1);
  202. lua_pushstring(L, "__div");
  203. lua_pushcfunction(L, lightuserdata_div);
  204. lua_settable(L, 1);
  205. lua_pushstring(L, "__unm");
  206. lua_pushcfunction(L, lightuserdata_unm);
  207. lua_settable(L, 1);
  208. lua_pushstring(L, "__index");
  209. lua_pushcfunction(L, lightuserdata_index);
  210. lua_settable(L, 1);
  211. lua_pushstring(L, "__newindex");
  212. lua_pushcfunction(L, lightuserdata_newindex);
  213. lua_settable(L, 1);
  214. lua_pop(L, 1); // Pop Lightuserdata_mt
  215. // Ensure stack is clean
  216. CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
  217. }
  218. void LuaEnvironment::execute(const LuaResource* lr)
  219. {
  220. using namespace lua_resource;
  221. lua_pushcfunction(L, error_handler);
  222. luaL_loadbuffer(L, program(lr), size(lr), "<unknown>");
  223. lua_pcall(L, 0, 0, -2);
  224. lua_pop(L, 1);
  225. }
  226. void LuaEnvironment::execute_string(const char* s)
  227. {
  228. lua_pushcfunction(L, error_handler);
  229. luaL_loadstring(L, s);
  230. lua_pcall(L, 0, 0, -2);
  231. lua_pop(L, 1);
  232. }
  233. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  234. {
  235. luaL_newmetatable(L, module);
  236. luaL_Reg entry[2];
  237. entry[0].name = name;
  238. entry[0].func = func;
  239. entry[1].name = NULL;
  240. entry[1].func = NULL;
  241. luaL_register(L, NULL, entry);
  242. lua_setglobal(L, module);
  243. lua_pop(L, -1);
  244. }
  245. void LuaEnvironment::load_module_function(const char* module, const char* name, const char* value)
  246. {
  247. luaL_newmetatable(L, module);
  248. lua_getglobal(L, value);
  249. lua_setfield(L, -2, name);
  250. lua_setglobal(L, module);
  251. }
  252. void LuaEnvironment::load_module_constructor(const char* module, const lua_CFunction func)
  253. {
  254. // Create dummy tables to be used as module's metatable
  255. lua_createtable(L, 0, 1);
  256. lua_pushstring(L, "__call");
  257. lua_pushcfunction(L, func);
  258. lua_settable(L, 1); // dummy.__call = func
  259. lua_getglobal(L, module);
  260. lua_pushvalue(L, -2); // Duplicate dummy metatable
  261. lua_setmetatable(L, -2); // setmetatable(module, dummy)
  262. lua_pop(L, -1);
  263. }
  264. void LuaEnvironment::load_module_enum(const char* module, const char* name, uint32_t value)
  265. {
  266. // Checks table existance
  267. lua_pushstring(L, module);
  268. lua_rawget(L, LUA_GLOBALSINDEX);
  269. if (!lua_istable(L, -1)) // If not exixts
  270. {
  271. // Creates table
  272. lua_newtable(L);
  273. lua_setglobal(L, module);
  274. }
  275. // Adds field to table
  276. lua_getglobal(L, module);
  277. lua_pushinteger(L, value);
  278. lua_setfield(L, -2, name);
  279. lua_pop(L, 2);
  280. }
  281. void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  282. {
  283. CE_ASSERT_NOT_NULL(func);
  284. LuaStack stack(L);
  285. va_list vl;
  286. va_start(vl, argc);
  287. lua_pushcfunction(L, error_handler);
  288. lua_getglobal(L, func);
  289. for (uint8_t i = 0; i < argc; i++)
  290. {
  291. const int type = va_arg(vl, int);
  292. switch (type)
  293. {
  294. case ARGUMENT_FLOAT:
  295. {
  296. stack.push_float(va_arg(vl, double));
  297. break;
  298. }
  299. default:
  300. {
  301. CE_ASSERT(false, "Oops, lua argument unknown");
  302. break;
  303. }
  304. }
  305. }
  306. va_end(vl);
  307. lua_pcall(L, argc, 0, -argc - 2);
  308. lua_pop(L, -1);
  309. }
  310. Vector3* LuaEnvironment::next_vector3(const Vector3& v)
  311. {
  312. CE_ASSERT(_vec3_used < CROWN_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
  313. return &(_vec3_buffer[_vec3_used++] = v);
  314. }
  315. Matrix4x4* LuaEnvironment::next_matrix4x4(const Matrix4x4& m)
  316. {
  317. CE_ASSERT(_mat4_used < CROWN_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  318. return &(s_mat4_buffer[_mat4_used++] = m);
  319. }
  320. Quaternion* LuaEnvironment::next_quaternion(const Quaternion& q)
  321. {
  322. CE_ASSERT(_quat_used < CROWN_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
  323. return &(_quat_buffer[_quat_used++] = q);
  324. }
  325. bool LuaEnvironment::is_vector3(int index)
  326. {
  327. void* type = lua_touserdata(L, index);
  328. return (type >= &_vec3_buffer[0] && type <= &_vec3_buffer[CROWN_MAX_LUA_VECTOR3 - 1]);
  329. }
  330. bool LuaEnvironment::is_matrix4x4(int index)
  331. {
  332. void* type = lua_touserdata(L, index);
  333. return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4 - 1]);
  334. }
  335. bool LuaEnvironment::is_quaternion(int index)
  336. {
  337. void* type = lua_touserdata(L, index);
  338. return (type >= &_quat_buffer[0] && type <= &_quat_buffer[CROWN_MAX_LUA_QUATERNION - 1]);
  339. }
  340. void LuaEnvironment::clear_temporaries()
  341. {
  342. _vec3_used = 0;
  343. _mat4_used = 0;
  344. _quat_used = 0;
  345. }
  346. } // namespace crown