0

Within my Lua-application I have some own functions defined that are registered with lua_register("lua_fct_name","my_fct_name") so that they are known to the Lua script.

Now I have some custom/user data that need to be accessible within my_fct_name(). It is just a pointer to a memory area I manage for my own so I use lua_pushlightuserdata (L,data) to add it to Lua-context.

Now it seems I don't have the correct position to add these data. When done right after L was created I can't access the data in my_fct_name(), here lua_touserdata(L,1) does return NULL, so it is not available on the stack. When done right before lua_pcall() executes the script, I get an error message about unexpected data.

So where/when do I have to set my user data so that they are available within my_fct_name()?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Elmi
  • 5,899
  • 15
  • 72
  • 143
  • A few things that come to my mind: **1.** Are you passing the light user data variable to `my_fct_name()` as an argument (sorry, if that's a silly question)? **2.** Are you fiddling with setting different environment for the script (eg sandboxing)? **3. Can you show some code?** – W.B. May 23 '13 at 12:53
  • No, my_fct_name() is called only from LUA where these (global) user data do not exist. The User data are available on initialisation and it _seems_ like they have to be pushed to LUA-context L before the script is started. – Elmi May 23 '13 at 13:01
  • 1
    **Please post your code**. `lua_touserdata(L, 1)` converts your function's first argument to user data. It does not work on any global. Suppose your global luserdata name is `ud`, if you want that to work, you need to invoke your function `my_fct_name(ud)`. Or on C side just get your global. – W.B. May 23 '13 at 13:10
  • May be my approach is completely wrong, so lets ask this question more generic. I have a pointer to some user data available during initialisation, means before the LUA-script is started. Now when a C-function is called out of a running LUA-script I need to access these user data. Since the C-function is always defined as int my_lua_called_c_fct(lua_State *L) there is only L where I could get these user data from. But how can I put the data to L during initialisation and how can I retrieve them within that function? – Elmi May 23 '13 at 13:21
  • Read my edit for a read on Lua C API. – W.B. May 23 '13 at 13:40

1 Answers1

3

Since you're refusing to provide your code, which is not helping at all, let me provide an example.

Setup of Lua state (C side):

lua_State *L = luaL_newstate();

//Set your userdata as a global
lua_pushlightuserdata(L, mypointer);
lua_setglobal(L, "mypointer");

//Setup my function
lua_pushcfunction(L, my_fct_name);
lua_setglobal(L, "my_fct_name");

//Load your script - luaScript is a null terminated const char* buffer with my script
luaL_loadstring(L, luaScript);

//Call the script (no error handling)
lua_pcall(L, 0, 0, 0);

Lua code V1:

my_fct_name(mypointer)

Lua code V2:

my_fct_name()

In the V1 you would get your pointer like this, since you provide it as an argument:

int my_fct_name(lua_State *L)
{
    void *myPtr = lua_touserdata(L, 1);
    //Do some stuff
    return 0;
}

In the V2, you would have to get it from the globals table (which would work for V1 as well)

int my_fct_name(lua_State *L)
{
    lua_getglobal(L, "mypointer");
    void *myPtr = lua_touserdata(L, -1);  //Get it from the top of the stack
    //Do some stuff
    return 0;
}

Have a look at the Lua Reference Manual and Programming in Lua. Mind you that the book that is available online is based on Lua 5.0, so it's not completely up to date, but should be sufficient for learning basics of interacting between C and Lua.

W.B.
  • 5,445
  • 19
  • 29