Home c++ Call a function in Lua and get the return value in C++

Call a function in Lua and get the return value in C++

Author

Date

Category

How do I get data from a Lua script in C++ code? As I understand, the exchange of data from the script to the pluses goes through a special LUA-Stack . Help me figure it out, let’s say there is such a very useful function in Lua :

function add (x, y)
  return x + y
end

In C++, this is the code:

# include "stdafx.h"
extern "C" {
#include "Lua \ lua.h"
#include "Lua \ lauxlib.h"
#include "Lua \ lualib.h"
}
lua_State * L = NULL;
int from_lua_add (int x, int y)
{
  int sum;
  lua_getglobal (L, "add");
  lua_pushnumber (L, x);
  lua_pushnumber (L, y);
  lua_call (L, 2, 1); /* Error */
  sum = (int) lua_tointeger (L, -1);
  lua_pop (L, 1);
  return sum;
}
int main ()
{
  L = lua_open ();
  luaL_openlibs (L);
  luaL_dofile (L, "script.lua");
  int summ;
  summ = from_lua_add (1, 1);
  printf ("the summ from lua is% d \ n", summ);
  lua_close (L);
  getchar ();
  return 0;
}

a crash occurs at the call to lua_call , an error with the following content:

unprotected error in call to lua api (attempt to call a nil value)

What did I do wrong?


Answer 1, authority 100%

Code examples can be viewed here: http://lua-users.org/wiki/SampleCode
There is a long example of working with Lua from C++ here: http://lua-users.org/wiki/CallingLuaFromCpp

I will give a short example of getting data from Lua in C. The difference from C++ is small.

http://lua-users.org/wiki/GettingValuesFromLua

Example code for Lua 5.1.1:

returnone.c:

# include & lt; lua.h & gt;
#include & lt; lualib.h & gt;
#include & lt; lauxlib.h & gt;
int main ()
{
  lua_State * L = luaL_newstate ();
  char buff [] = "return 1, 'a'";
  int error;
  printf ("% d \ n", lua_gettop (L));
  error = luaL_loadbuffer (L, buff, strlen (buff), "my test") || lua_pcall (L, 0, LUA_MULTRET, 0);
  if (error) {
   fprintf (stderr, "% s", lua_tostring (L, -1));
   lua_pop (L, 1); / * pop error message from the stack * /
  }
  printf ("% d \ n", lua_gettop (L));
  printf ("% s \ n", lua_tostring (L, -2));
  printf ("% s \ n", lua_tostring (L, -1));
  return 0;
}

Answer 2, authority 91%

What did I do wrong?

You have completely ignored error handling in your code.

The luaL_dofile function returns a response code that needs to be parsed (surprise!), not just ignored. The same goes for lua_getglobal and so on.

If you add the following processing to compile and run the script:

int err = luaL_dofile (L, "script.lua");
if (err) {
  printf ("Cannot dofile:% s", lua_tostring (L, -1));
  lua_pop (L, 1);
  return 1;
}

then if the script file is suddenly not found, the following message will be displayed:

Cannot dofile: cannot open script.lua: No such file or directory

Also, any syntax errors in the script itself will be handled if it is suddenly written with errors.

If you remove this processing, then if the script.lua file is not available, a message will be displayed as in your question.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions