MCPcopy Create free account
hub / github.com/WilliamLCobb/iNDS / toCString

Function toCString

desmume/src/lua-engine.cpp:1078–1107  ·  view source on GitHub ↗

like rawToCString, but will check if the global Lua function tostring() has been replaced with a custom function, and call that instead if so

Source from the content-addressed store, hash-verified

1076// like rawToCString, but will check if the global Lua function tostring()
1077// has been replaced with a custom function, and call that instead if so
1078static const char* toCString(lua_State* L, int idx)
1079{
1080 int a = idx>0 ? idx : 1;
1081 int n = idx>0 ? idx : lua_gettop(L);
1082 lua_getglobal(L, "tostring");
1083 lua_CFunction cf = lua_tocfunction(L,-1);
1084 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
1085 {
1086 lua_pop(L,1);
1087 return rawToCString(L, idx);
1088 }
1089 else // if the user overrided the tostring function, we have to actually call it and store the temporarily allocated string it returns
1090 {
1091 lua_pushstring(L, "");
1092 for (int i=a; i<=n; i++) {
1093 lua_pushvalue(L, -2); // function to be called
1094 lua_pushvalue(L, i); // value to print
1095 lua_call(L, 1, 1);
1096 if(lua_tostring(L, -1) == NULL)
1097 luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
1098 lua_pushstring(L, (i<n) ? " " : "\r\n");
1099 lua_concat(L, 3);
1100 }
1101 const char* str = lua_tostring(L, -1);
1102 strncpy(s_tempStr, str, s_tempStrMaxLen);
1103 s_tempStr[s_tempStrMaxLen-1] = 0;
1104 lua_pop(L, 2);
1105 return s_tempStr;
1106 }
1107}
1108
1109// replacement for luaB_print() that goes to the appropriate textbox instead of stdout
1110DEFINE_LUA_FUNCTION(print, "...")

Callers 4

doPopupFunction · 0.85
toCStringConverterFunction · 0.85
lua-engine.cppFile · 0.85
gui_osdtextFunction · 0.85

Calls 1

rawToCStringFunction · 0.85

Tested by

no test coverage detected