ToluaUtils.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../IO/VectorBuffer.h"
  24. #include <toluapp/tolua++.h>
  25. #include "../LuaScript/ToluaUtils.h"
  26. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str)
  27. {
  28. return tolua_tostring(L, narg, str);
  29. }
  30. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str)
  31. {
  32. return tolua_tourho3dstring(L, narg, str.CString());
  33. }
  34. void SetContext(lua_State* L, Context* context)
  35. {
  36. assert(L && context);
  37. tolua_pushusertype(L, static_cast<void*>(context), "Context");
  38. lua_setglobal(L, ".context"); // This property is internal, the exposed 'context' property is obtained via GetContext() call
  39. }
  40. Context* GetContext(lua_State* L)
  41. {
  42. lua_getglobal(L, ".context");
  43. if (lua_isnil(L, -1))
  44. {
  45. lua_State* L1 = lua_getmainthread(L);
  46. return (L == L1) ? 0 : GetContext(L1);
  47. }
  48. tolua_Error error; // Ensure we are indeed getting a Context object before casting
  49. return tolua_isusertype(L, -1, "Context", 0, &error) ? static_cast<Context*>(tolua_tousertype(L, -1, 0)) : 0;
  50. }
  51. // Explicit template specialization only required for StringVector
  52. template <> int ToluaIsVector<String>(lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  53. {
  54. return tolua_isstringarray(L, lo, -1, def, err);
  55. }
  56. template <> void* ToluaToVector<String>(lua_State* L, int narg, void* /*def*/)
  57. {
  58. if (!lua_istable(L, narg))
  59. return 0;
  60. static Vector<String> result;
  61. result.Clear();
  62. result.Resize((unsigned)lua_objlen(L, narg));
  63. for (unsigned i = 0; i < result.Size(); ++i)
  64. {
  65. lua_rawgeti(L, narg, i + 1);
  66. result[i] = tolua_tourho3dstring(L, -1, "");
  67. lua_pop(L, 1);
  68. }
  69. return &result;
  70. }
  71. template <> int ToluaPushVector<String>(lua_State* L, void* data, const char* /*type*/)
  72. {
  73. lua_newtable(L);
  74. const Vector<String>& vector = *static_cast<const Vector<String>*>(data);
  75. for (unsigned i = 0; i < vector.Size(); ++i)
  76. {
  77. tolua_pushurho3dstring(L, vector[i]);
  78. lua_rawseti(L, -2, i + 1);
  79. }
  80. return 1;
  81. }
  82. // Explicit template specialization only required for boolean
  83. template <> int ToluaIsPODVector<bool>(double /*overload*/, lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  84. {
  85. return tolua_isbooleanarray(L, lo, -1, def, err);
  86. }
  87. template <> void* ToluaToPODVector<bool>(double /*overload*/, lua_State* L, int narg, void* /*def*/)
  88. {
  89. if (!lua_istable(L, narg))
  90. return 0;
  91. static PODVector<bool> result;
  92. result.Clear();
  93. result.Resize((unsigned)lua_objlen(L, narg));
  94. for (unsigned i = 0; i < result.Size(); ++i)
  95. {
  96. lua_rawgeti(L, narg, i + 1);
  97. result[i] = (bool)tolua_toboolean(L, -1, 0);
  98. lua_pop(L, 1);
  99. }
  100. return &result;
  101. }
  102. template <> int ToluaPushPODVector<bool>(double /*overload*/, lua_State* L, void* data, const char* /*type*/)
  103. {
  104. lua_newtable(L);
  105. const PODVector<bool>& vector = *static_cast<const PODVector<bool>*>(data);
  106. for (unsigned i = 0; i < vector.Size(); ++i)
  107. {
  108. lua_pushboolean(L, vector[i]);
  109. lua_rawseti(L, -2, i + 1);
  110. }
  111. return 1;
  112. }
  113. namespace Urho3D
  114. {
  115. template <> const VariantValue* Variant::Get<const VariantValue*>() const
  116. {
  117. return &value_;
  118. }
  119. }
  120. void ToluaToVariant(lua_State* L, int narg, void* def, Variant& variant)
  121. {
  122. switch (lua_type(L, narg)) // Use the type of lua object to determine the final variant type
  123. {
  124. case LUA_TNIL:
  125. variant = Variant::EMPTY;
  126. break;
  127. case LUA_TBOOLEAN:
  128. variant = (bool)lua_toboolean(L, narg); // Still need to cast to bool as Lua/LuaJIT return it as int
  129. break;
  130. case LUA_TNUMBER:
  131. {
  132. // Use the original variant type to further determine the final variant type
  133. // CAVEAT: if lhs has integral data type and double is desired then lhs needs to be reset first before assigning
  134. double value = lua_tonumber(L, narg);
  135. switch (variant.GetType())
  136. {
  137. case VAR_INT:
  138. variant = (int)value;
  139. break;
  140. case VAR_BOOL:
  141. variant = value != 0.0f;
  142. break;
  143. case VAR_FLOAT:
  144. variant = (float)value;
  145. break;
  146. default:
  147. variant = value;
  148. }
  149. }
  150. break;
  151. case LUA_TSTRING:
  152. variant = lua_tostring(L, narg);
  153. break;
  154. case LUA_TUSERDATA:
  155. {
  156. if (lua_getmetatable(L, narg))
  157. {
  158. lua_rawget(L, LUA_REGISTRYINDEX); // registry[mt]
  159. const char* typeName = lua_tostring(L, -1);
  160. lua_pop(L, 1);
  161. void* value = tolua_tousertype(L, narg, def);
  162. switch (Variant::GetTypeFromName(typeName))
  163. {
  164. case VAR_NONE:
  165. // Handle special cases
  166. if (typeName)
  167. {
  168. tolua_Error error;
  169. if (strcmp(typeName, "Variant") == 0)
  170. variant = *static_cast<Variant*>(value);
  171. else if (strcmp(typeName, "VectorBuffer") == 0)
  172. variant = *static_cast<VectorBuffer*>(value);
  173. else if (tolua_isusertype(L, narg, "RefCounted", 0, &error))
  174. variant = static_cast<RefCounted*>(value);
  175. else
  176. variant = value; // void*
  177. }
  178. break;
  179. case VAR_VECTOR2:
  180. variant = *static_cast<Vector2*>(value);
  181. break;
  182. case VAR_VECTOR3:
  183. variant = *static_cast<Vector3*>(value);
  184. break;
  185. case VAR_VECTOR4:
  186. variant = *static_cast<Vector4*>(value);
  187. break;
  188. case VAR_QUATERNION:
  189. variant = *static_cast<Quaternion*>(value);
  190. break;
  191. case VAR_COLOR:
  192. variant = *static_cast<Color*>(value);
  193. break;
  194. case VAR_INTRECT:
  195. variant = *static_cast<IntRect*>(value);
  196. break;
  197. case VAR_INTVECTOR2:
  198. variant = *static_cast<IntVector2*>(value);
  199. break;
  200. case VAR_MATRIX3:
  201. variant = *static_cast<Matrix3*>(value);
  202. break;
  203. case VAR_MATRIX3X4:
  204. variant = *static_cast<Matrix3x4*>(value);
  205. break;
  206. case VAR_MATRIX4:
  207. variant = *static_cast<Matrix4*>(value);
  208. break;
  209. case VAR_RESOURCEREF:
  210. variant = *static_cast<ResourceRef*>(value);
  211. break;
  212. case VAR_RESOURCEREFLIST:
  213. variant = *static_cast<ResourceRefList*>(value);
  214. break;
  215. case VAR_VARIANTMAP:
  216. variant = *static_cast<VariantMap*>(value);
  217. break;
  218. default: break;
  219. }
  220. };
  221. }
  222. break;
  223. case LUA_TTABLE:
  224. {
  225. tolua_Error error;
  226. if (ToluaIsPODVector<unsigned char>(0.f, L, narg, "unsigned char", 0, &error))
  227. variant = *static_cast<PODVector<unsigned char>*>(ToluaToPODVector<unsigned char>(0.f, L, narg, def));
  228. else if (ToluaIsVector<Variant>(L, narg, "Variant", 0, &error))
  229. variant = *static_cast<VariantVector*>(ToluaToVector<Variant>(L, narg, def));
  230. else if (ToluaIsVector<String>(L, narg, "String", 0, &error))
  231. variant = *static_cast<StringVector*>(ToluaToVector<String>(L, narg, def));
  232. }
  233. break;
  234. default: break;
  235. }
  236. }
  237. void ToluaPushVariant(lua_State* L, const Variant* variant, const char* type)
  238. {
  239. String typeName(type);
  240. switch (variant->GetType())
  241. {
  242. case VAR_INT:
  243. if (typeName == "unsigned" || typeName == "unsigned int" || typeName == "UInt" || typeName == "uint")
  244. tolua_pushnumber(L, (lua_Number)variant->GetUInt());
  245. else if (typeName == "StringHash")
  246. {
  247. // Make a new local copy
  248. tolua_pushusertype(L, Mtolua_new(StringHash(variant->GetStringHash())), "StringHash");
  249. tolua_register_gc(L, lua_gettop(L));
  250. }
  251. else
  252. tolua_pushnumber(L, (lua_Number)variant->GetInt());
  253. break;
  254. case VAR_BOOL:
  255. tolua_pushboolean(L, (int)variant->GetBool());
  256. break;
  257. case VAR_FLOAT:
  258. tolua_pushnumber(L, (lua_Number)variant->GetFloat());
  259. break;
  260. case VAR_DOUBLE:
  261. tolua_pushnumber(L, (lua_Number)variant->GetDouble());
  262. break;
  263. case VAR_VECTOR2:
  264. case VAR_VECTOR3:
  265. case VAR_VECTOR4:
  266. case VAR_QUATERNION:
  267. case VAR_COLOR:
  268. case VAR_RESOURCEREF:
  269. case VAR_RESOURCEREFLIST:
  270. case VAR_VARIANTMAP:
  271. case VAR_INTRECT:
  272. case VAR_INTVECTOR2:
  273. tolua_pushusertype(L, (void*)variant->Get<const VariantValue*>(), variant->GetTypeName().CString());
  274. break;
  275. case VAR_STRING:
  276. tolua_pushurho3dstring(L, variant->GetString());
  277. break;
  278. case VAR_BUFFER:
  279. if (typeName == "VectorBufer")
  280. {
  281. tolua_pushusertype(L, Mtolua_new(VectorBuffer(variant->GetVectorBuffer())), "VectorBuffer");
  282. tolua_register_gc(L, lua_gettop(L));
  283. }
  284. else
  285. ToluaPushPODVector<unsigned char>(0.f, L, (void*)&variant->GetBuffer(), "unsigned char");
  286. break;
  287. case VAR_VOIDPTR:
  288. ToluaPushRegisteredUserType(L, static_cast<void*>(variant->GetVoidPtr()), type);
  289. break;
  290. case VAR_PTR:
  291. ToluaPushRegisteredUserType(L, static_cast<void*>(variant->GetPtr()), type);
  292. break;
  293. case VAR_VARIANTVECTOR:
  294. ToluaPushVector<Variant>(L, (void*)&variant->GetVariantVector(), "Variant");
  295. break;
  296. case VAR_STRINGVECTOR:
  297. ToluaPushVector<String>(L, (void*)&variant->GetStringVector(), "String");
  298. break;
  299. case VAR_MATRIX3:
  300. case VAR_MATRIX3X4:
  301. case VAR_MATRIX4:
  302. tolua_pushusertype(L, variant->Get<const VariantValue*>()->ptr_, variant->GetTypeName().CString());
  303. break;
  304. default:
  305. lua_pushnil(L);
  306. break;
  307. }
  308. }
  309. void ToluaPushRegisteredUserType(lua_State* L, void* data, const char* type)
  310. {
  311. if (type)
  312. {
  313. luaL_getmetatable(L, type);
  314. if (!lua_isnil(L, -1))
  315. {
  316. lua_pop(L, 1);
  317. tolua_pushusertype(L, data, type);
  318. }
  319. }
  320. else
  321. lua_pushnil(L);
  322. }
  323. void ToluaPushObject(lua_State* L, void* data, const char* type)
  324. {
  325. tolua_pushusertype(L, data, data ? static_cast<Object*>(data)->GetTypeName().CString() : type);
  326. }