Interpreter.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/Lua/Utilities.h>
  33. #include <RmlUi/Core/Core.h>
  34. #include <RmlUi/Core/Log.h>
  35. #include <RmlUi/Core/FileInterface.h>
  36. #include <RmlUi/Lua/LuaType.h>
  37. namespace Rml {
  38. namespace Lua {
  39. lua_State* Interpreter::GetLuaState()
  40. {
  41. return LuaPlugin::GetLuaState();
  42. }
  43. void Interpreter::LoadFile(const String& file)
  44. {
  45. lua_State* L = GetLuaState();
  46. //use the file interface to get the contents of the script
  47. FileInterface* file_interface = GetFileInterface();
  48. FileHandle handle = file_interface->Open(file);
  49. if (handle == 0) {
  50. lua_pushfstring(L, "LoadFile: Unable to open file: %s", file.c_str());
  51. Report(L);
  52. return;
  53. }
  54. size_t size = file_interface->Length(handle);
  55. if (size == 0) {
  56. lua_pushfstring(L, "LoadFile: File is 0 bytes in size: %s", file.c_str());
  57. Report(L);
  58. return;
  59. }
  60. char* file_contents = new char[size];
  61. file_interface->Read(file_contents, size, handle);
  62. file_interface->Close(handle);
  63. if (luaL_loadbuffer(L, file_contents, size, ("@" + file).c_str()) != 0)
  64. Report(L);
  65. else //if there were no errors loading, then the compiled function is on the top of the stack
  66. {
  67. if (lua_pcall(L, 0, 0, 0) != 0)
  68. Report(L);
  69. }
  70. delete[] file_contents;
  71. }
  72. void Interpreter::DoString(const String& code, const String& name)
  73. {
  74. lua_State* L = GetLuaState();
  75. if (luaL_loadbuffer(L, code.c_str(), code.length(), name.c_str()) != 0)
  76. Report(L);
  77. else
  78. {
  79. if (lua_pcall(L, 0, 0, 0) != 0)
  80. Report(L);
  81. }
  82. }
  83. void Interpreter::LoadString(const String& code, const String& name)
  84. {
  85. lua_State* L = GetLuaState();
  86. if (luaL_loadbuffer(L, code.c_str(), code.length(), name.c_str()) != 0)
  87. Report(L);
  88. }
  89. void Interpreter::BeginCall(int funRef)
  90. {
  91. lua_State* L = GetLuaState();
  92. lua_settop(L, 0); //empty stack
  93. //lua_getref(g_L,funRef);
  94. lua_rawgeti(L, LUA_REGISTRYINDEX, (int)funRef);
  95. }
  96. bool Interpreter::ExecuteCall(int params, int res)
  97. {
  98. lua_State* L = GetLuaState();
  99. bool ret = true;
  100. int top = lua_gettop(L);
  101. if (lua_type(L, top - params) != LUA_TFUNCTION)
  102. {
  103. ret = false;
  104. //stack cleanup
  105. if (params > 0)
  106. {
  107. for (int i = top; i >= (top - params); i--)
  108. {
  109. if (!lua_isnone(L, i))
  110. lua_remove(L, i);
  111. }
  112. }
  113. }
  114. else
  115. {
  116. if (lua_pcall(L, params, res, 0) != 0)
  117. {
  118. Report(L);
  119. ret = false;
  120. }
  121. }
  122. return ret;
  123. }
  124. void Interpreter::EndCall(int res)
  125. {
  126. lua_State* L = GetLuaState();
  127. //stack cleanup
  128. for (int i = res; i > 0; i--)
  129. {
  130. if (!lua_isnone(L, res))
  131. lua_remove(L, res);
  132. }
  133. }
  134. } // namespace Lua
  135. } // namespace Rml