4

I try to use lua in a C++ project. For lua executing 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 the 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)

That didn't work and I've got error message:

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

But when I add

print(needReload)

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

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Elijah
  • 81
  • 1
  • 5
  • 3
    Can you print out the contents of `script.c_str()` and `script.size()` that's fed to `luaL_loadbuffer`? Show both versions, one with the `print(needReload)` and one without. – greatwolf Dec 19 '14 at 11:39

2 Answers2

1

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

I suspect that script.size() is wrong. But I can't explain why adding that line works.

lhf
  • 70,581
  • 9
  • 108
  • 149
0

Thank you all for your answers. Yes, that was trouble with script.size() coz when it was replaced to e = luaL_loadbuffer(luaEngine, script.c_str(), strlen(script.c_str()), NULL); that started work fine. Sorry for my stupid question.

Elijah
  • 81
  • 1
  • 5