lua-users home
lua-l archive

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

> -----Original Message-----
> From: [] On
> Behalf Of Tim Hill
> Sent: vrijdag 14 februari 2014 19:53
> To: Lua mailing list
> Subject: Re: protect a set in c (non-existing elements)
> On Feb 14, 2014, at 7:40 AM, Thijs Schreijer <>
> wrote:
> >
> > Btw is it possible to pass parameters to lua code loaded with
> luaL_loadstring?
> >
> > Thijs
> lua_loadstring() leaves a Lua function on the stack (at the top) .. you can
> then call this function using lua_call() or lua_pcall() .. look at the docs
> for these functions to see how you pass parameters and get back return
> values (hint: you push them onto the stack).
> -Tim

As mentioned in one of my previous posts in this thread;
> So whether it accepts arguments depends on how the chunk is loaded, with what function signature
> If with 'function my_chunk(...)'  then I could use the vararg in the lua code
> If with 'function my_chunk()' then the Lua code will never receive any arguments

Tested it and this code (modified from the example by Dirk);

char *lua_init_code =
"local params={...} "
"for k in pairs(params) do print(k,v) end "

int testfunc (lua_State *L) {
  luaL_loadstring(L, lua_init_code);
  lua_pushstring(L, "parameter 1");
  lua_pushinteger(L, 2);
  lua_call(L, 2, 0);  // 2 args, 0 results  
  return 1;

1  nil
2  nil

So the function returned by luaL_loadstring has a signature 'function my_chunk()' and doesn't define the vararg. Too bad...
Any clues to why not? Would make sense to me that it would define the vararg.