c++luac

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. 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!" 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.

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. 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);. 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. Debug mode, x86.

Code (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;
}

Solution

  • Main problem

    std::cin >> inputo does not read a full line from the standard input. 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:

    And these string are evaluated independently by the Lua interpreter. None of these strings are valid Lua statements, so the interpreter doesn't execute them. lua_dostring will return an error code, and let an error message on the Lua stack.

    To work line by line on the standard input, you can use std::getline, which works well in a loop:

    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: