lua-users home
lua-l archive

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


2010/1/12 Tony Finch <dot@dotat.at>:
> On Tue, 12 Jan 2010, Francesco Abbate wrote:
> (2) is invalid because setfenv and getfenv cannot manipulate userdata
> environments in 5.1 so there's no change in 5.2

I'm missing something because I'm using this feature with Lua 5.1.4.
Here a sample of my code:

struct fdfmultimin *
push_new_fdf_multimin (lua_State *L, int findex, size_t n,
		       const gsl_multimin_fdfminimizer_type *T)
{
  struct fdfmultimin *m = lua_newuserdata (L, sizeof (struct fdfmultimin));
  m->s = gsl_multimin_fdfminimizer_alloc (T, n);

  if (m->s == NULL)
    luaL_error (L, OUT_OF_MEMORY_MSG);

  luaL_getmetatable (L, fdfmultimin_mt_name);
  lua_setmetatable (L, -2);

  lua_newtable (L);

  lua_pushvalue (L, findex);
  lua_rawseti (L, -2, FENV_FUNCTION);

  matrix_push_raw (L, n, 1);
  lua_rawseti (L, -2, FENV_X);

  matrix_push_view (L, NULL);
  lua_rawseti (L, -2, FENV_GRAD);

  lua_setfenv (L, -2);

  return m;
}