简体   繁体   English

Lua C API自定义打印函数,在字符串中传递空格时不调用

[英]Lua C API Custom Print function, not called when a space is passed in the string

Problem Description: I have created a custom C++ function print() that is supposed to be pushed as a global onto a table to so the user can use the print() function to print to the debug console. 问题描述:我创建了一个自定义C ++函数print() ,该函数应该作为全局函数推到表上,以便用户可以使用print()函数打印到调试控制台。 This function works to some extent, however, when you try to print a string with a space in it (over one word) the function is not called at all... This has greatly confused me, as I don't know why. 此函数在某种程度上有效,但是,当您尝试打印一个带有空格(超过一个单词)的字符串时,根本不会调用该函数……这使我非常困惑,因为我不知道为什么。 If I were to try and call something such as print("Hello!") the console will have "Hello!" 如果我尝试调用诸如print("Hello!")之类的控制台,则控制台将显示“ Hello!”。 printed to it, but if I were to try and print something such as print("Hello world!") the function will not be called at all, I know this because I have used a message box to alert when the function is called. 打印到它,但是如果我尝试打印诸如print("Hello world!")之类的函数,则根本不会调用该函数,我知道这一点,因为我使用了一个消息框来提醒何时调用该函数。

Additional Information: So, the closest thing to this I could find was a question asking how to make a custom print function in C++ with the Lua C API then push it onto the global table. 附加信息:因此,我能找到的最接近的问题是一个问题,询问如何使用Lua C API在C ++中创建自定义打印函数,然后将其推送到全局表中。 I can already do this, and my function works to some extent. 我已经可以做到这一点,并且我的功能在一定程度上可以正常工作。 My function isn't being pushed onto the Lua C API's global table, instead to a table that is created by me with lua_newtable(L, s); 我的函数没有被推送到Lua C API的全局表上,而是被推送到由我使用lua_newtable(L, s);创建的表上lua_newtable(L, s); . However, I've tried it both ways and it makes no difference. 但是,我已经尝试过两种方法,并且没有区别。 This print function does not support tables nor function as of now, I'm just focused on finding out and fixing why the function can't print strings over one word. 到目前为止,该打印功能不支持表格或功能,我只是专注于找出并修复为什么该功能无法在一个单词上打印字符串。 Just in case you were wondering, Lua v5.1.5 and Microsoft Visual Studio 2017 are used for this. 以防万一您想知道,为此使用了Lua v5.1.5和Microsoft Visual Studio 2017。 Debug mode, x86. 调试模式,x86。

Code (C++): 代码(C ++):

If anyone could help me fix this, that would be great! 如果有人可以帮助我解决此问题,那就太好了!

#include <iostream>
#include <string>
#include <Windows.h>
#pragma comment(lib, "Lua.lib")
#include "lua.hpp"
#include "luaconf.h"

static int print(lua_State* LUASTATE)
{
    MessageBoxA(NULL, "Custom print called.", "FUNCTION!", NULL);
    int nargs = lua_gettop(LUASTATE);
    std::string string = "";
    for (int i = 1; i <= nargs; i++)
    {
        if (i > 1) string += " ";
        switch (lua_type(LUASTATE, i))
        {
        case LUA_TSTRING:
            string += (std::string)lua_tostring(LUASTATE, i);
        case LUA_TNUMBER:
            string += (int)lua_tonumber(LUASTATE, i);
        case LUA_TBOOLEAN:
            string += (bool)lua_toboolean(LUASTATE, i);
        }
    }
    std::cout << string << "\n";
    return 0;
}
int pushs(lua_State* LuaState)
{
    luaL_openlibs(LuaState);
    lua_newtable(LuaState);
    lua_pushcfunction(LuaState, print);
    lua_setglobal(LuaState, "print");
    lua_settop(LuaState, 0);
    return 0;
}
int main()
{
    lua_State* ls = luaL_newstate();
    lua_State* LS = lua_newthread(ls);
    pushs(LS);
    while (true)
    {
        std::cout << " ";
        std::string inputo;
        std::cin >> inputo;
        luaL_dostring(LS, inputo.c_str());
        lua_settop(LS, 0);
    }
    lua_close(LS);
    return 0;
}

Main problem 主要问题

std::cin >> inputo does not read a full line from the standard input. std::cin >> inputo无法从标准输入中读取整行。 It just reads a single word. 它只读取一个单词。 So when you type the following input line in your shell: 因此,当您在外壳中键入以下输入行时:

print("Hello world")

Your main loop breaks it into two separate strings: 您的主循环将其分为两个单独的字符串:

  • print("Hello
  • world")

And these string are evaluated independently by the Lua interpreter. 这些字符串由Lua解释器独立评估。 None of these strings are valid Lua statements, so the interpreter doesn't execute them. 这些字符串都不是有效的Lua语句,因此解释器不会执行它们。 lua_dostring will return an error code, and let an error message on the Lua stack. lua_dostring将返回错误代码,并在Lua堆栈上显示错误消息。

To work line by line on the standard input, you can use std::getline , which works well in a loop: 要在标准输入上逐行处理,可以使用std :: getline ,它在循环中效果很好:

std::string line;
while (std::getline(std::cin, line)) {
    // do something with line.
}

Side notes 旁注

What follows is not directly related to your bug, but look suspicious: 以下内容与您的错误没有直接关系,但看起来可疑:

  • std::string += int (or bool) interprets the int as a single char , and append this single character to the string. std::string += int (或bool)将int解释为单个char ,并将此单个字符附加到字符串。
  • Your switch/case seems to be missing break statements. 您的switch/case似乎缺少break语句。
  • lua_State* ls is never closed. lua_State* ls永远不会关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM