GlobalLuaFunctions.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "GlobalLuaFunctions.h"
  2. #include <RmlUi/Core/Log.h>
  3. #include <RmlUi/Core/Types.h>
  4. #include <RmlUi/Lua/IncludeLua.h>
  5. #include <RmlUi/Lua/LuaType.h>
  6. #include <RmlUi/Lua/Utilities.h>
  7. namespace Rml {
  8. namespace Lua {
  9. /*
  10. Below here are global functions and their helper functions that help overwrite the Lua global functions
  11. */
  12. // Based off of the luaB_print function from Lua's lbaselib.c
  13. int LuaPrint(lua_State* L)
  14. {
  15. int n = lua_gettop(L); /* number of arguments */
  16. int i;
  17. lua_getglobal(L, "tostring");
  18. StringList string_list = StringList();
  19. String output = "";
  20. for (i = 1; i <= n; i++)
  21. {
  22. const char* s;
  23. lua_pushvalue(L, -1); /* function to be called */
  24. lua_pushvalue(L, i); /* value to print */
  25. lua_call(L, 1, 1);
  26. s = lua_tostring(L, -1); /* get result */
  27. if (s == nullptr)
  28. return luaL_error(L, "'tostring' must return a string to 'print'");
  29. if (i > 1)
  30. output += "\t";
  31. output += String(s);
  32. lua_pop(L, 1); /* pop result */
  33. }
  34. output += "\n";
  35. Log::Message(Log::LT_INFO, "%s", output.c_str());
  36. return 0;
  37. }
  38. void OverrideLuaGlobalFunctions(lua_State* L)
  39. {
  40. lua_getglobal(L, "_G");
  41. lua_pushcfunction(L, LuaPrint);
  42. lua_setfield(L, -2, "print");
  43. lua_pop(L, 1); // pop _G
  44. }
  45. } // namespace Lua
  46. } // namespace Rml