wrap_Channel.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Copyright (c) 2006-2010 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_Channel.h"
  21. namespace love
  22. {
  23. namespace thread
  24. {
  25. Channel *luax_checkchannel(lua_State *L, int idx)
  26. {
  27. return luax_checktype<Channel>(L, idx);
  28. }
  29. int w_Channel_push(lua_State *L)
  30. {
  31. Channel *c = luax_checkchannel(L, 1);
  32. Variant var = Variant::fromLua(L, 2);
  33. if (var.getType() == Variant::UNKNOWN)
  34. return luaL_argerror(L, 2, "boolean, number, string, love type, or flat table expected");
  35. uint64 id = c->push(var);
  36. lua_pushnumber(L, (lua_Number) id);
  37. return 1;
  38. }
  39. int w_Channel_supply(lua_State *L)
  40. {
  41. Channel *c = luax_checkchannel(L, 1);
  42. Variant var = Variant::fromLua(L, 2);
  43. if (var.getType() == Variant::UNKNOWN)
  44. return luaL_argerror(L, 2, "boolean, number, string, love type, or flat table expected");
  45. c->supply(var);
  46. return 0;
  47. }
  48. int w_Channel_pop(lua_State *L)
  49. {
  50. Channel *c = luax_checkchannel(L, 1);
  51. Variant var;
  52. if (c->pop(&var))
  53. var.toLua(L);
  54. else
  55. lua_pushnil(L);
  56. return 1;
  57. }
  58. int w_Channel_demand(lua_State *L)
  59. {
  60. Channel *c = luax_checkchannel(L, 1);
  61. Variant var;
  62. c->demand(&var);
  63. var.toLua(L);
  64. return 1;
  65. }
  66. int w_Channel_peek(lua_State *L)
  67. {
  68. Channel *c = luax_checkchannel(L, 1);
  69. Variant var;
  70. if (c->peek(&var))
  71. var.toLua(L);
  72. else
  73. lua_pushnil(L);
  74. return 1;
  75. }
  76. int w_Channel_getCount(lua_State *L)
  77. {
  78. Channel *c = luax_checkchannel(L, 1);
  79. lua_pushnumber(L, c->getCount());
  80. return 1;
  81. }
  82. int w_Channel_hasRead(lua_State *L)
  83. {
  84. Channel *c = luax_checkchannel(L, 1);
  85. uint64 id = (uint64) luaL_checknumber(L, 2);
  86. luax_pushboolean(L, c->hasRead(id));
  87. return 1;
  88. }
  89. int w_Channel_clear(lua_State *L)
  90. {
  91. Channel *c = luax_checkchannel(L, 1);
  92. c->clear();
  93. return 0;
  94. }
  95. int w_Channel_performAtomic(lua_State *L)
  96. {
  97. Channel *c = luax_checkchannel(L, 1);
  98. luaL_checktype(L, 2, LUA_TFUNCTION);
  99. // Pass this channel as an argument to the function.
  100. lua_pushvalue(L, 1);
  101. lua_insert(L, 3);
  102. c->lockMutex();
  103. // call the function, passing the channel as the first argument and any
  104. // user-specified arguments after.
  105. int numargs = lua_gettop(L) - 2;
  106. int err = lua_pcall(L, numargs, LUA_MULTRET, 0);
  107. c->unlockMutex();
  108. // Unfortunately, this eats the stack trace, too bad.
  109. if (err != 0)
  110. return lua_error(L);
  111. // The function and everything after it in the stack are eaten by the pcall,
  112. // leaving only the Channel argument. Everything else is a return value.
  113. return lua_gettop(L) - 1;
  114. }
  115. static const luaL_Reg w_Channel_functions[] =
  116. {
  117. { "push", w_Channel_push },
  118. { "supply", w_Channel_supply },
  119. { "pop", w_Channel_pop },
  120. { "demand", w_Channel_demand },
  121. { "peek", w_Channel_peek },
  122. { "getCount", w_Channel_getCount },
  123. { "hasRead", w_Channel_hasRead },
  124. { "clear", w_Channel_clear },
  125. { "performAtomic", w_Channel_performAtomic },
  126. { 0, 0 }
  127. };
  128. extern "C" int luaopen_channel(lua_State *L)
  129. {
  130. return luax_register_type(L, &Channel::type, w_Channel_functions, nullptr);
  131. }
  132. } // thread
  133. } // love