Saving the output from Lua in C++ with SOL3 to an std::string

huangapple go评论93阅读模式
英文:

Saving the output from Lua in C++ with SOL3 to an std::string

问题

我正在尝试将Lua解释器实现到我的C++代码中。我已经使用ImGui为我的项目实现了一个小型编辑器,并将编辑器的输出保存到一个std::vector<char>中。

我的Lua解释器的尝试实现如下:

  1. // 头文件
  2. std::string ExecuteLua();
  3. std::vector<char> m_luaEditorData;
  4. // cpp 文件
  5. std::string Proxy::ExecuteLua()
  6. {
  7. // 从字符串加载Lua代码
  8. std::string luaCode(m_luaEditorData.data());
  9. // 创建Lua状态
  10. sol::state lua;
  11. // 加载标准Lua库
  12. lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table);
  13. // 执行Lua代码并存储结果
  14. sol::protected_function_result result = lua.script(luaCode);
  15. // 检查错误
  16. if (!result.valid())
  17. {
  18. sol::error error = result;
  19. std::string errorMsg = error.what();
  20. return "Lua错误: " + errorMsg;
  21. }
  22. // 将结果作为字符串获取
  23. std::string output = lua["tostring"](result.get<sol::object>());
  24. // 返回输出
  25. return output;
  26. }
  27. ...
  28. if (m_luaEditorData.empty())
  29. m_luaEditorData.push_back('
    // 头文件
  30. std::string ExecuteLua();
  31. std::vector<char> m_luaEditorData;
  32. // cpp 文件
  33. std::string Proxy::ExecuteLua()
  34. {
  35.     // 从字符串加载Lua代码
  36.     std::string luaCode(m_luaEditorData.data());
  37.     // 创建Lua状态
  38.     sol::state lua;
  39.     // 加载标准Lua库
  40.     lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table);
  41.     // 执行Lua代码并存储结果
  42.     sol::protected_function_result result = lua.script(luaCode);
  43.     // 检查错误
  44.     if (!result.valid())
  45.     {
  46.         sol::error error = result;
  47.         std::string errorMsg = error.what();
  48.         return "Lua错误: " + errorMsg;
  49.     }
  50.     // 将结果作为字符串获取
  51.     std::string output = lua["tostring"](result.get<sol::object>());
  52.     // 返回输出
  53.     return output;
  54. }
  55. ...
  56. if (m_luaEditorData.empty())
  57.     m_luaEditorData.push_back('\0');
  58. auto luaEditorFlags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackResize;
  59. ImGui::InputTextMultiline("##LuaEditor", m_luaEditorData.data(), m_luaEditorData.size(), ImVec2(ImGui::GetWindowContentRegionWidth(), ImGui::GetWindowHeight() - (ImGui::GetTextLineHeight() * 16), luaEditorFlags, ResizeInputTextCallback, &m_luaEditorData);
  60. ...
  61. ');
  62. auto luaEditorFlags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackResize;
  63. ImGui::InputTextMultiline("##LuaEditor", m_luaEditorData.data(), m_luaEditorData.size(), ImVec2(ImGui::GetWindowContentRegionWidth(), ImGui::GetWindowHeight() - (ImGui::GetTextLineHeight() * 16), luaEditorFlags, ResizeInputTextCallback, &m_luaEditorData);
  64. ...

当我运行这段代码时,我的输出中只会得到nil,正确的输出应该是标准输出(我不希望它输出在这里,而是输出到我的std::string中),当我输入错误的代码时,它会在sol.hpp中抛出异常。我没有找到如何执行此操作的示例,因此我正在尝试自己解决这个问题。

英文:

I'm trying to implement a lua interpreter to my C++ code. I have implemented a small editor for my project using ImGui and I'm saving the output from the editor to an std::vector<char>.

My attempted implementation of my lua interpeter looks like so;

  1. // header
  2. std::string ExecuteLua();
  3. std::vector&lt;char&gt; m_luaEditorData;
  4. // cpp
  5. std::string Proxy::ExecuteLua()
  6. {
  7. // Load the Lua code from the string
  8. std::string luaCode(m_luaEditorData.data());
  9. // Create a Lua state
  10. sol::state lua;
  11. // Load standard Lua libraries
  12. lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table);
  13. // Execute the Lua code and store the result
  14. sol::protected_function_result result = lua.script(luaCode);
  15. // Check for errors
  16. if (!result.valid())
  17. {
  18. sol::error error = result;
  19. std::string errorMsg = error.what();
  20. return &quot;Lua error: &quot; + errorMsg;
  21. }
  22. // Get the result as a string
  23. std::string output = lua[&quot;tostring&quot;](result.get&lt;sol::object&gt;());
  24. // Return the output
  25. return output;
  26. }
  27. ...
  28. if (m_luaEditorData.empty())
  29. m_luaEditorData.push_back(&#39;\0&#39;);
  30. auto luaEditorFlags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackResize;
  31. ImGui::InputTextMultiline(&quot;##LuaEditor&quot;, m_luaEditorData.data(), m_luaEditorData.size(), ImVec2(ImGui::GetWindowContentRegionWidth(), ImGui::GetWindowHeight() - (ImGui::GetTextLineHeight() * 16)), luaEditorFlags, ResizeInputTextCallback, &amp;m_luaEditorData);
  32. ...

When I run this code, I only get nil in my output, the correct output to stdout (don't really want it to output here, but to my std::string and when I put in bad code, it throws an exception in sol.hpp. I didn't really find any examples on how I can do this and I'm therefore am trying to figure this out on my own.

答案1

得分: 1

我通过用自己的print函数替换原来的print函数来解决了这个问题,如下所示:

  1. int print(lua_State* L)
  2. {
  3. std::string printString = lua_tostring(L, 1);
  4. g_luaOutput += printString + "\n";
  5. return 1;
  6. }
  7. std::string Proxy::ExecuteLua()
  8. {
  9. std::string luaCode(m_luaEditorData.data());
  10. sol::state lua;
  11. lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table, sol::lib::jit);
  12. lua.set_function("print", print);
  13. sol::protected_function_result result = lua.script(luaCode);
  14. ...
  15. }

现在,当我在我的Lua编辑器中调用print函数时,它将不再打印到标准输出(stdout),而是打印到我的自定义控制台上。

英文:

I was able to solve the problem myself by replacing the print function with my own like so;

  1. int print(lua_State* L)
  2. {
  3. std::string printString = lua_tostring(L, 1);
  4. g_luaOutput += printString + &quot;\n&quot;;
  5. return 1;
  6. }
  7. std::string Proxy::ExecuteLua()
  8. {
  9. std::string luaCode(m_luaEditorData.data());
  10. sol::state lua;
  11. lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table, sol::lib::jit);
  12. lua.set_function(&quot;print&quot;, print);
  13. sol::protected_function_result result = lua.script(luaCode);
  14. ...

When I now call print in my lua editor I will not print to my own console instead of stdout.

huangapple
  • 本文由 发表于 2023年2月19日 06:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定