如何防止在 Luabind 类中创建新属性?
我正在使用 Luabind 将 C++ 类绑定到 Lua。从 Lua 脚本中,我可以将任意属性(键/值对)添加到代表我的类的表中,即使我从未告诉 Luabind 这些属性存在:
cheese = Cheese()
cheese.type = "gouda"
cheese.age = 2
cheese.hippopotamus = "monkey" -- Oops! This member was not defined!
河马行虽然无意义,但将执行而不会导致 Lua 错误。
我如何防止这些新属性?我知道当只使用 Lua C API 时,可以使用 __newindex 元方法来完成,但是有没有办法用 Luabind 来做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是根据经验说话,但这里有一些来自 luabind 用户邮件列表的电子邮件线程:首先, 一个人的独白尝试做你想做的事情。其次,来自 luabind 维护者的有点旧的(2009 年 11 月)电子邮件,指出 提供自定义 __newindex 元方法是目前不可能,但计划用于 1.0(当前版本是 0.9.something)。
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).