The expected argument to the lua function is close to <eof>

I am trying to use lua in a C ++ project. To execute lua, I write this:

#include <lua.hpp>
...
luaEngine = luaL_newstate();
luaL_openlibs(luaEngine);

register_results(luaEngine); // For register c++ object in the LUA script as metatable

lua_pushstring(luaEngine, resultsId.c_str());
lua_setglobal(luaEngine, "resultsId");

lua_pushboolean(luaEngine, needReloadModel);
lua_setglobal(luaEngine, "needReload");
...
e = luaL_loadbuffer(luaEngine, script.c_str(), script.size(), NULL);
if(e != 0)
    // error message
e = lua_pcall(luaEngine, 0, 1, 0);
if(e != 0)
    // error message
...
lua_close(luaEngine);

And lua script:

local Res = ResUpdateLUA(resultsId)
if current_result == "Normal" or current_result=='-'  then
    status = 'E'
else
    status = 'O'
end
needReload = Res:setShowAnalyte('2320', status)

This did not work, and I got an error message:

[string "?"]:7: function arguments expected near <eof>

But when I add

print(needReload)

at the end of the lua script it works well. What am I doing wrong?

+4
source share
2 answers

The error message means that Lua has reached the end of the source after viewing Res:s, but before seeing (.

I suspect it script.size()is wrong. But I can’t explain why adding this line works.

+1
source

. , script.size() coz, e = luaL_loadbuffer(luaEngine, script.c_str(), strlen(script.c_str()), NULL);, . .

0

Source: https://habr.com/ru/post/1568302/


All Articles