I'm using Luabind to bind C++ classes to Lua. From the Lua script, I can add arbitrary properties (key/value pairs) to the tables that represent my classes, even if I never told Luabind these properties exist:
cheese = Cheese()
cheese.type = "gouda"
cheese.age = 2
cheese.hippopotamus = "monkey" -- Oops! This member was not defined!
The hippopotamus line, though nonsensical, will execute without causing a Lua error.
How do I prevent these new properties? I know that when using just the Lua C API this can be accomplished using the __newindex metamethod, but is there a way to do this with Luabind?
I don't speak from experience, but here are some email threads from the luabind-user mailing list: first, a monologue by a guy trying to do exactly what you want. Second, somewhat old (Nov 2009) email from the luabind maintainer, stating that providing custom __newindex metamethod is not currently possible, but planned for 1.0 (current version is 0.9.something).