Interpreter.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 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 <RmlUi/Lua/Interpreter.h>
  29. #include "LuaPlugin.h"
  30. #include "LuaDocumentElementInstancer.h"
  31. #include "LuaEventListenerInstancer.h"
  32. #include <RmlUi/Core/Core.h>
  33. #include <RmlUi/Core/Log.h>
  34. #include <RmlUi/Core/FileInterface.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. Log::Message(Log::LT_WARNING, "LoadFile: Unable to open file: %s", file.c_str());
  77. return false;
  78. }
  79. size_t size = file_interface->Length(handle);
  80. if (size == 0) {
  81. Log::Message(Log::LT_WARNING, "LoadFile: File is 0 bytes in size: %s", file.c_str());
  82. return false;
  83. }
  84. UniquePtr<char[]> file_contents(new char[size]);
  85. file_interface->Read(file_contents.get(), size, handle);
  86. file_interface->Close(handle);
  87. if (luaL_loadbuffer(L, file_contents.get(), size, ("@" + file).c_str()) != 0)
  88. {
  89. Log::Message(Log::LT_WARNING, "%s", lua_tostring(L, -1));
  90. lua_pop(L, 1);
  91. return false;
  92. }
  93. return LuaCall(L, 0, 0);;
  94. }
  95. bool Interpreter::DoString(const String& code, const String& name)
  96. {
  97. lua_State* L = GetLuaState();
  98. return LoadString(code, name) && LuaCall(L, 0, 0);
  99. }
  100. bool Interpreter::LoadString(const String& code, const String& name)
  101. {
  102. lua_State* L = GetLuaState();
  103. if (luaL_loadbuffer(L, code.c_str(), code.length(), name.c_str()) != 0)
  104. {
  105. Log::Message(Log::LT_WARNING, "%s", lua_tostring(L, -1));
  106. lua_pop(L, 1);
  107. return false;
  108. }
  109. return true;
  110. }
  111. void Interpreter::BeginCall(int funRef)
  112. {
  113. lua_State* L = GetLuaState();
  114. lua_settop(L, 0); //empty stack
  115. //lua_getref(g_L,funRef);
  116. lua_rawgeti(L, LUA_REGISTRYINDEX, (int)funRef);
  117. }
  118. bool Interpreter::ExecuteCall(int params, int res)
  119. {
  120. lua_State* L = GetLuaState();
  121. return LuaCall(L, params, res);
  122. }
  123. void Interpreter::EndCall(int res)
  124. {
  125. lua_State* L = GetLuaState();
  126. lua_pop(L, res);
  127. }
  128. } // namespace Lua
  129. } // namespace Rml