wrap_ByteData.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2006-2023 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "wrap_ByteData.h"
  21. #include "wrap_Data.h"
  22. #include "common/config.h"
  23. #include <algorithm>
  24. namespace love
  25. {
  26. namespace data
  27. {
  28. ByteData *luax_checkbytedata(lua_State *L, int idx)
  29. {
  30. return luax_checktype<ByteData>(L, idx);
  31. }
  32. int w_ByteData_clone(lua_State *L)
  33. {
  34. ByteData *t = luax_checkbytedata(L, 1);
  35. ByteData *c = nullptr;
  36. luax_catchexcept(L, [&](){ c = t->clone(); });
  37. luax_pushtype(L, c);
  38. c->release();
  39. return 1;
  40. }
  41. int w_ByteData_setString(lua_State *L)
  42. {
  43. Data *t = luax_checkdata(L, 1);
  44. size_t size = 0;
  45. const char *str = luaL_checklstring(L, 2, &size);
  46. int64 offset = (int64)luaL_optnumber(L, 3, 0);
  47. size = std::min(size, t->getSize());
  48. if (size == 0)
  49. return 0;
  50. if (offset < 0 || offset + size > (int64) t->getSize())
  51. return luaL_error(L, "The given string offset and size don't fit within the Data's size.");
  52. memcpy((char *) t->getData() + (size_t) offset, str, size);
  53. return 0;
  54. }
  55. template <typename T>
  56. static int w_ByteData_setT(lua_State *L)
  57. {
  58. ByteData *t = luax_checkbytedata(L, 1);
  59. int64 offset = (int64) luaL_checknumber(L, 2);
  60. bool istable = lua_type(L, 3) == LUA_TTABLE;
  61. int nargs = std::max(1, istable ? (int) luax_objlen(L, 3) : lua_gettop(L) - 2);
  62. if (offset < 0 || offset + sizeof(T) * nargs > t->getSize())
  63. return luaL_error(L, "");
  64. auto data = (T *)((uint8 *) t->getData() + offset);
  65. if (istable)
  66. {
  67. for (int i = 0; i < nargs; i++)
  68. {
  69. lua_rawgeti(L, 3, i + 1);
  70. data[i] = (T) luaL_checknumber(L, -1);
  71. lua_pop(L, 1);
  72. }
  73. }
  74. else
  75. {
  76. for (int i = 0; i < nargs; i++)
  77. data[i] = (T) luaL_checknumber(L, 3 + i);
  78. }
  79. return 0;
  80. }
  81. int w_ByteData_setFloat(lua_State *L)
  82. {
  83. return w_ByteData_setT<float>(L);
  84. }
  85. int w_ByteData_setDouble(lua_State *L)
  86. {
  87. return w_ByteData_setT<double>(L);
  88. }
  89. int w_ByteData_setInt8(lua_State *L)
  90. {
  91. return w_ByteData_setT<int8>(L);
  92. }
  93. int w_ByteData_setUInt8(lua_State *L)
  94. {
  95. return w_ByteData_setT<uint8>(L);
  96. }
  97. int w_ByteData_setInt16(lua_State *L)
  98. {
  99. return w_ByteData_setT<int16>(L);
  100. }
  101. int w_ByteData_setUInt16(lua_State *L)
  102. {
  103. return w_ByteData_setT<uint16>(L);
  104. }
  105. int w_ByteData_setInt32(lua_State *L)
  106. {
  107. return w_ByteData_setT<int32>(L);
  108. }
  109. int w_ByteData_setUInt32(lua_State *L)
  110. {
  111. return w_ByteData_setT<uint32>(L);
  112. }
  113. int w_ByteData_setInt64(lua_State *L)
  114. {
  115. return w_ByteData_setT<int64>(L);
  116. }
  117. int w_ByteData_setUInt64(lua_State *L)
  118. {
  119. return w_ByteData_setT<uint64>(L);
  120. }
  121. static const luaL_Reg w_ByteData_functions[] =
  122. {
  123. { "clone", w_ByteData_clone },
  124. { "setString", w_ByteData_setString },
  125. { "setFloat", w_ByteData_setFloat },
  126. { "setDouble", w_ByteData_setDouble },
  127. { "setInt8", w_ByteData_setInt8 },
  128. { "setUInt8", w_ByteData_setUInt8 },
  129. { "setInt16", w_ByteData_setInt16 },
  130. { "setUInt16", w_ByteData_setUInt16 },
  131. { "setInt32", w_ByteData_setInt32 },
  132. { "setUInt32", w_ByteData_setUInt32 },
  133. { "setInt64", w_ByteData_setInt64 },
  134. { "setUInt64", w_ByteData_setUInt64 },
  135. { 0, 0 }
  136. };
  137. int luaopen_bytedata(lua_State *L)
  138. {
  139. luax_register_type(L, &ByteData::type, w_Data_functions, w_ByteData_functions, nullptr);
  140. love::data::luax_rundatawrapper(L, ByteData::type);
  141. return 0;
  142. }
  143. } // data
  144. } // love