卢阿 + SWIG 猴子修补
我使用 SWIG 将一组类绑定到 lua。我知道 C++ 本身不支持猴子修补,而且我并没有尝试修改我的 C++ 对象,只是修改它们的 lua 表示。如果我想开始对 SWIG 导出的 lua 表和对象进行猴子修补,这样我就可以修改 lua 端提供的 API,那么问题就来了。
例如以下 lua 代码:
game.GetEnemies1 = game.GetEnemies2
无法按预期工作。该行之后的行为仍然与原始 GetEnemies1 而不是 GetEnemies2 一致。
我该如何解决这个问题?
I have used SWIG to bind a set of classes to lua. I know C++ itself doesn't support monkey patching, and I'm not trying to modify my C++ objects, merely their lua representations. The problem comes if I want to start monkey patching the lua tables and objects exported by SWIG, so that I can modify the API presented on the lua side.
e.g. the following lua code:
game.GetEnemies1 = game.GetEnemies2
does not work as expected. The behaviour after that line is still consistent with the original GetEnemies1 not GetEnemies2.
how do I combat this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过添加和替换现有方法成功对 lua 用户数据进行了猴子修补。它涉及修改他们的元表。
这是我必须做的示例向现有的用户数据对象添加几个方法。
正如您所看到的,我必须修改其元表,而不是修改对象本身。
仅当您的用户数据对象设置为使其元表“指向自身”时,此解决方案才有效:
mt.__index = mt
。问候!
I've successfully monkeypatched lua userdata by adding and replacing existing methods. It involved modifying their metatables.
Here's a sample of what I had to do in order to add a couple methods to an existing userdata object.
As you can see, instead of modifying the object iself, I had to modify its metatable.
This solution will only work if your userdata objects are set up so their metatables "point to themselves":
mt.__index = mt
.Regards!
Swig 从 C++ 函数生成 lua 包装器,它不会将 lua 函数注入到 C++ 中。如果 GetEnemies1 是一个 C++ 函数,从其他 C++ 函数调用,猴子修补将不起作用。
您必须重写 C++ 代码,以便执行 GetEnemies1 的代码查找某种可以用 swig 包装的回调。
Swig generates lua wrappers from c++ functions, it does not inject lua functions into c++. If GetEnemies1 is a c++ function, called from other c++ functions, monkey patching wont work.
You will have to rewrite your c++ code so that the code which executes GetEnemies1 looks for some sort of callback which you can wrap with swig.