like rawToCString, but will check if the global Lua function tostring() has been replaced with a custom function, and call that instead if so
| 1849 | // like rawToCString, but will check if the global Lua function tostring() |
| 1850 | // has been replaced with a custom function, and call that instead if so |
| 1851 | static const char* toCString(lua_State* L, int idx) |
| 1852 | { |
| 1853 | int a = idx>0 ? idx : 1; |
| 1854 | int n = idx>0 ? idx : lua_gettop(L); |
| 1855 | lua_getglobal(L, "tostring"); |
| 1856 | lua_CFunction cf = lua_tocfunction(L,-1); |
| 1857 | if(cf == tostring) // optimization: if using our own C tostring function, we can bypass the call through Lua and all the string object allocation that would entail |
| 1858 | { |
| 1859 | lua_pop(L,1); |
| 1860 | return rawToCString(L, idx); |
| 1861 | } |
| 1862 | else // if the user overrided the tostring function, we have to actually call it and store the temporarily allocated string it returns |
| 1863 | { |
| 1864 | lua_pushstring(L, ""); |
| 1865 | for (int i=a; i<=n; i++) { |
| 1866 | lua_pushvalue(L, -2); // function to be called |
| 1867 | lua_pushvalue(L, i); // value to print |
| 1868 | lua_call(L, 1, 1); |
| 1869 | if(lua_tostring(L, -1) == NULL) |
| 1870 | luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print")); |
| 1871 | lua_pushstring(L, (i<n) ? " " : "\r\n"); |
| 1872 | lua_concat(L, 3); |
| 1873 | } |
| 1874 | const char* str = lua_tostring(L, -1); |
| 1875 | strncpy(s_tempStr, str, s_tempStrMaxLen); |
| 1876 | s_tempStr[s_tempStrMaxLen-1] = 0; |
| 1877 | lua_pop(L, 2); |
| 1878 | return s_tempStr; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | // replacement for luaB_print() that goes to the appropriate textbox instead of stdout |
| 1883 | static int print(lua_State *L) |
no test coverage detected