lua-users home
lua-l archive

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


Hi all,

I am writing some lua glue to keep the values in a (minimal) structure in the C core in sync with the corresponding values in a (much bigger) table in the lua environment. At first I had thought I could use the __index (via a proxy table) element of the metatable to trigger callbacks in the C code whenever the values I monitor are accessed.

However, the __index function gets called before the values are actually updated (makes sense), so at that stage I don't know if I need to call the C funcs (has the value changed?), and I don't know the new value, as it only gets affected after __index runs through.

Looking through the manual and book, I can't find any metatable entry that would act as a postnotification, something like:

function mt.__postmodify (key, oldval, newval)
  update_C_struct (key, newval)
end

As I'm still fairly new to lua, I can't seem to think of a way to bend lua to my needs, other than using lua functions to modify the values and do the check from there. Does anyone have an idea of how I could implement this kind of synchronisation transparently?

Thanks in advance,
David Anderson