Interpreter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LuaDocumentElementInstancer.h"
  29. #include "LuaEventListenerInstancer.h"
  30. #include "LuaPlugin.h"
  31. #include <RmlUi/Core/Core.h>
  32. #include <RmlUi/Core/FileInterface.h>
  33. #include <RmlUi/Core/Log.h>
  34. #include <RmlUi/Lua/Interpreter.h>
  35. #include <RmlUi/Lua/LuaType.h>
  36. namespace Rml {
  37. namespace Lua {
  38. static int ErrorHandler(lua_State* L)
  39. {
  40. const char* msg = lua_tostring(L, 1);
  41. if (msg == NULL)
  42. {
  43. if (luaL_callmeta(L, 1, "__tostring") && lua_type(L, -1) == LUA_TSTRING)
  44. return 1;
  45. else
  46. msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1));
  47. }
  48. luaL_traceback(L, L, msg, 1);
  49. return 1;
  50. }
  51. static bool LuaCall(lua_State* L, int nargs, int nresults)
  52. {
  53. int errfunc = -2 - nargs;
  54. lua_pushcfunction(L, ErrorHandler);
  55. lua_insert(L, errfunc);
  56. if (lua_pcall(L, nargs, nresults, errfunc) != LUA_OK)
  57. {
  58. Log::Message(Log::LT_WARNING, "%s", lua_tostring(L, -1));
  59. lua_pop(L, 2);
  60. return false;
  61. }
  62. lua_remove(L, -1 - nresults);
  63. return true;
  64. }
  65. lua_State* Interpreter::GetLuaState()
  66. {
  67. return LuaPlugin::GetLuaState();
  68. }
  69. bool Interpreter::LoadFile(const String& file)
  70. {
  71. lua_State* L = GetLuaState();
  72. // use the file interface to get the contents of the script
  73. FileInterface* file_interface = GetFileInterface();
  74. FileHandle handle = file_interface->Open(file);
  75. if (handle == 0)
  76. {
  77. Log::Message(Log::LT_WARNING, "LoadFile: Unable to open file: %s", file.c_str());
  78. return false;
  79. }
  80. size_t size = file_interface->Length(handle);
  81. if (size == 0)
  82. {
  83. Log::Message(Log::LT_WARNING, "LoadFile: File is 0 bytes in size: %s", file.c_str());
  84. return false;
  85. }
  86. UniquePtr<char[]> file_contents(new char[size]);
  87. file_interface->Read(file_contents.get(), size, handle);
  88. file_interface->Close(handle);
  89. if (luaL_loadbuffer(L, file_contents.get(), size, ("@" + file).c_str()) != 0)
  90. {
  91. Log::Message(Log::LT_WARNING, "%s", lua_tostring(L, -1));
  92. lua_pop(L, 1);
  93. return false;
  94. }
  95. return LuaCall(L, 0, 0);
  96. ;
  97. }
  98. bool Interpreter::DoString(const String& code, const String& name)
  99. {
  100. lua_State* L = GetLuaState();
  101. return LoadString(code, name) && LuaCall(L, 0, 0);
  102. }
  103. bool Interpreter::LoadString(const String& code, const String& name)
  104. {
  105. lua_State* L = GetLuaState();
  106. if (luaL_loadbuffer(L, code.c_str(), code.length(), name.c_str()) != 0)
  107. {
  108. Log::Message(Log::LT_WARNING, "%s", lua_tostring(L, -1));
  109. lua_pop(L, 1);
  110. return false;
  111. }
  112. return true;
  113. }
  114. void Interpreter::BeginCall(int funRef)
  115. {
  116. lua_State* L = GetLuaState();
  117. lua_settop(L, 0); // empty stack
  118. // lua_getref(g_L,funRef);
  119. lua_rawgeti(L, LUA_REGISTRYINDEX, (int)funRef);
  120. }
  121. bool Interpreter::ExecuteCall(int params, int res)
  122. {
  123. lua_State* L = GetLuaState();
  124. return LuaCall(L, params, res);
  125. }
  126. void Interpreter::EndCall(int res)
  127. {
  128. lua_State* L = GetLuaState();
  129. lua_pop(L, res);
  130. }
  131. } // namespace Lua
  132. } // namespace Rml