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

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

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

问题

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

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

// 头文件
std::string ExecuteLua();
std::vector<char> m_luaEditorData;

// cpp 文件
std::string Proxy::ExecuteLua()
{
    // 从字符串加载Lua代码
    std::string luaCode(m_luaEditorData.data());

    // 创建Lua状态
    sol::state lua;

    // 加载标准Lua库
    lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table);

    // 执行Lua代码并存储结果
    sol::protected_function_result result = lua.script(luaCode);

    // 检查错误
    if (!result.valid())
    {
        sol::error error = result;
        std::string errorMsg = error.what();
        return "Lua错误: " + errorMsg;
    }

    // 将结果作为字符串获取
    std::string output = lua["tostring"](result.get<sol::object>());

    // 返回输出
    return output;
}

...

if (m_luaEditorData.empty())
    m_luaEditorData.push_back('
// 头文件
std::string ExecuteLua();
std::vector<char> m_luaEditorData;

// cpp 文件
std::string Proxy::ExecuteLua()
{
    // 从字符串加载Lua代码
    std::string luaCode(m_luaEditorData.data());

    // 创建Lua状态
    sol::state lua;

    // 加载标准Lua库
    lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table);

    // 执行Lua代码并存储结果
    sol::protected_function_result result = lua.script(luaCode);

    // 检查错误
    if (!result.valid())
    {
        sol::error error = result;
        std::string errorMsg = error.what();
        return "Lua错误: " + errorMsg;
    }

    // 将结果作为字符串获取
    std::string output = lua["tostring"](result.get<sol::object>());

    // 返回输出
    return output;
}

...

if (m_luaEditorData.empty())
    m_luaEditorData.push_back('\0');

auto luaEditorFlags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackResize;
ImGui::InputTextMultiline("##LuaEditor", m_luaEditorData.data(), m_luaEditorData.size(), ImVec2(ImGui::GetWindowContentRegionWidth(), ImGui::GetWindowHeight() - (ImGui::GetTextLineHeight() * 16), luaEditorFlags, ResizeInputTextCallback, &m_luaEditorData);

...
'
);
auto luaEditorFlags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackResize; ImGui::InputTextMultiline("##LuaEditor", m_luaEditorData.data(), m_luaEditorData.size(), ImVec2(ImGui::GetWindowContentRegionWidth(), ImGui::GetWindowHeight() - (ImGui::GetTextLineHeight() * 16), luaEditorFlags, ResizeInputTextCallback, &m_luaEditorData); ...

当我运行这段代码时,我的输出中只会得到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;

// header
std::string ExecuteLua();
std::vector&lt;char&gt; m_luaEditorData;


// cpp
std::string Proxy::ExecuteLua()
{
	// Load the Lua code from the string
	std::string luaCode(m_luaEditorData.data());

	// Create a Lua state
	sol::state lua;

	// Load standard Lua libraries
	lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table);

	// Execute the Lua code and store the result
	sol::protected_function_result result = lua.script(luaCode);

	// Check for errors
	if (!result.valid())
	{
		sol::error error = result;
		std::string errorMsg = error.what();
		return &quot;Lua error: &quot; + errorMsg;
	}

	// Get the result as a string
	std::string output = lua[&quot;tostring&quot;](result.get&lt;sol::object&gt;());

	// Return the output
	return output;
}

...

		if (m_luaEditorData.empty())
			m_luaEditorData.push_back(&#39;\0&#39;);

			auto luaEditorFlags = ImGuiInputTextFlags_AllowTabInput | ImGuiInputTextFlags_CallbackResize;
			ImGui::InputTextMultiline(&quot;##LuaEditor&quot;, m_luaEditorData.data(), m_luaEditorData.size(), ImVec2(ImGui::GetWindowContentRegionWidth(), ImGui::GetWindowHeight() - (ImGui::GetTextLineHeight() * 16)), luaEditorFlags, ResizeInputTextCallback, &amp;m_luaEditorData);

...

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函数来解决了这个问题,如下所示:

int print(lua_State* L)
{
    std::string printString = lua_tostring(L, 1);
    g_luaOutput += printString + "\n";
    return 1;
}

std::string Proxy::ExecuteLua()
{
    std::string luaCode(m_luaEditorData.data());

    sol::state lua;

    lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table, sol::lib::jit);

    lua.set_function("print", print);

    sol::protected_function_result result = lua.script(luaCode);
    ...
}

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

英文:

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

int print(lua_State* L)
{
	std::string printString = lua_tostring(L, 1);
	g_luaOutput += printString + &quot;\n&quot;;
	return 1;
}

std::string Proxy::ExecuteLua()
{
	std::string luaCode(m_luaEditorData.data());

	sol::state lua;

	lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string, sol::lib::table, sol::lib::jit);

	lua.set_function(&quot;print&quot;, print);

	sol::protected_function_result result = lua.script(luaCode);
	...

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:

确定