pcall.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #define LUABIND_BUILDING
  20. #include <luabind/detail/pcall.hpp>
  21. #include <luabind/error.hpp>
  22. #include <luabind/lua_include.hpp>
  23. namespace luabind { namespace detail
  24. {
  25. int pcall(lua_State *L, int nargs, int nresults)
  26. {
  27. pcall_callback_fun e = get_pcall_callback();
  28. int en = 0;
  29. if ( e )
  30. {
  31. int base = lua_gettop(L) - nargs;
  32. lua_pushcfunction(L, e);
  33. lua_insert(L, base); // push pcall_callback under chunk and args
  34. en = base;
  35. }
  36. int result = lua_pcall(L, nargs, nresults, en);
  37. if ( en )
  38. lua_remove(L, en); // remove pcall_callback
  39. return result;
  40. }
  41. int resume_impl(lua_State *L, int nargs, int)
  42. {
  43. #if LUA_VERSION_NUM >= 501
  44. // Lua 5.1 added LUA_YIELD as a possible return value,
  45. // this was causing crashes, because the caller expects 0 on success.
  46. int res = lua_resume(L, nargs);
  47. return (res == LUA_YIELD) ? 0 : res;
  48. #else
  49. return lua_resume(L, nargs);
  50. #endif
  51. }
  52. }}