lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Answer to Issue One :
 
The problem with your example is that in your c code you are calling lua_tocfunction (look at the "c" between "to" and "function"), which explicitly return a pointer to a C function as Lua likes them (that is taking a lua_State* and returning an int). funk is a pure Lua function, so lua_tocfunction returns NULL.
 
In manual there : http://www.lua.org/manual/5.1/manual.html#lua_tocfunction


De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Turgut Hakki ÖZDEMIR
Envoyé : 10 mai 2006 09:19
À : lua@bazar2.conectiva.com.br
Objet : Calling a function defined in lua from C/C++

Greetings,

I saw lua used on a cpu-hunger project, loved it (speed issues (another nearly same project, using python is running several times slower...(needless to say, i'm involved))) and start to tinker with it to embed it my own projects but i've ended up with several problems.

Issue one :) :  When i try to call lua functions from C++ code it is complaining about "attempt to call a nil value" or something similar. I've checked reference manual and wiki several times but it did not changed anything. Finally i've noticed all functions defined in lua scripts has nil values but C++, functions registered to lua are callable. Also other global variables (non function ones) appearing as they have to be.

Following code represents the problem.

Code is compiled using msvc++ 2005 express edition and lua 5.1 (compiled as c++ and linked as a static library) (i've tried whole thing also in c but the result is same)

stringf class is provided for compilation reasons and does not related with our subject :)

--- test.lua ---------------------------------
function funk()

end

msg(funk) -- will be nil
msg(msg) -- displays functions address
-------------------------------------------------

and the code

--- blabla.cpp  ----------------------------
#include <stdio.h>
#include <string>

#include <windows.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

// --- printf like string formatter --------------------------
class stringf
{
  protected :
    std::string str;
  public :
    stringf(const char *fmt, ...) {
      va_list va;
      char *tmp;

      va_start(va, fmt);
      tmp = new char [vsnprintf(0, 0, fmt, va) + 1];
      vsnprintf(tmp, vsnprintf(0, 0, fmt, va) + 1, fmt, va);
      str = tmp;
      delete [] tmp;
      va_end(va);
    }

    operator const char * () {
      return str.c_str();
    }
    operator std::string () {
      return str;
    }
};

// --- Message box emulation ----------------------
int msg(lua_State *L)
{
int n = lua_gettop(L);
std::string msg;

  for (uint i = 1; i <= n; i++) {
    switch(lua_type(L, i)) {
      case LUA_TFUNCTION :
          msg += (const char *) stringf("(function) : 0x%x", lua_tocfunction(L, i));
        break;
    }

  }

  MessageBox(0, msg.c_str(), "Message", MB_OK);

return 0;
}

// --- Start crying here --------------------------
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    lua_State *L = lua_open();
    luaL_openlibs(L);
   
    luaL_loadfile(L, "test.lua");
  
    lua_register(L, "msg", msg);
   
    lua_call(L, 0, 0); // when i use lua_pcall instead of lua_call here, surrounded with error handling functionality it complains about nil values
  
    lua_close(L);
}
-----------------------------------


Issue two : How can i convert exceptions (derived from std::exception) to lua? Well, i can do it by altering luaconf.h (i think) but how are you doing this?

  C++ code starts a script
  script calls a C++ function
  C++ function gets frightened and raises an exception
  How the script can handle thrown exception?

Issue four : What about C++ interoperatability? Any wrappers? (Actually i saw one but ended up with 404 error), How can i represent my classes/objects in lua in a little bit reasonable way (saw wiki). Where is the gooooooood sample "let's involve c++" code? :)

Issue three: Unicode?