lua-users home
lua-l archive

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


you can see my repo: https://github.com/starwing/lbind/tree/newinterface/runtime

this is a C module that has many useful snippet, one useful for this issue is lbind_setmetafield.

A small C snippet can solve this issue (untested):

static int protect_index(lua_State *L) {
  lua_rawget(L, 1);
  if (lua_isnil(L, -1)) luaL_error(L, "....", luaL_tolstring(L, 2, NULL));
  return 1;
}

static int protect(lua_State *L) {
    lua_pushcfunction(L, protect_index);
    lbind_setmetafield(L, 1, "__index");
    lua_settop(L, 1);
    return 1;
}


2014-02-15 0:32 GMT+08:00 Thijs Schreijer <thijs@thijsschreijer.nl>:


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Dirk Laurie
> Sent: vrijdag 14 februari 2014 17:05
> To: Lua mailing list
> Subject: Re: protect a set in c (non-existing elements)
>
> 2014-02-14 17:40 GMT+02:00 Thijs Schreijer <thijs@thijsschreijer.nl>:
>
> > Btw is it possible to pass parameters to lua code loaded with
> luaL_loadstring?
>
> >From the manual:
>
> int luaL_dostring (lua_State *L, const char *str);
>
> Loads and runs the given string. It is defined as the following macro:
>
>      (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
>
> You can use that code, modified to pass parameters to lua_pcall.

From the same manual;
Also as lua_load, this function only loads the chunk; it does not run it.

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

I guess I'll have to go and try that.
Thijs




--
regards,
Xavier Wang.