如何最好地在 DWScript 中公开类实例
我正在使用出色的 Pascal DWScript 构建内置脚本功能。我还使用以下方法将我自己的 Delphi 端类定义 (TDemo) 添加到 DWScript:
dwsUnit.ExposeRTTI( TDemo.ClassInfo )
这很有效,并且是快速添加属性和方法的好方法。
我还希望以类似的方式添加现有实例,因此我创建了 TDemo 类型的实例 FDemo,然后执行:
dwsUnit.ExposeInstanceToUnit( 'Demo', 'TDemo', FDemo );
这看起来是一个有前途的调用例程,但我从未初始化的单元表中获取了 AV。我也查看了SVN源码的单元测试代码来查看这个函数的使用,但没有结果。谁能指出我应该添加/更改什么?
I am putting together a built-in script capability using the excellent Pascal DWScript. I have also add my own Delphi-side class definition (TDemo) to DWScript using:
dwsUnit.ExposeRTTI( TDemo.ClassInfo )
This just works and is a great way of quickly adding properties and methods.
I also wish to add an existing instance in a similar way, so I have created my instance FDemo of type TDemo and then performed:
dwsUnit.ExposeInstanceToUnit( 'Demo', 'TDemo', FDemo );
This looks a promising routine to call but I get an AV from an uninitialised unit table. I've also looked in the unit test code of the SVN source to see the use of this function but to no avail. Can anyone point me at what I should add / change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
必须在 TdwsUnit 表初始化中使用 ExposeInstanceToUnit,有关一些示例代码,请参阅 RTTIExposeTests/ExposeInstancesAfterInitTable。它允许直接公开动态实例。
另一种方法是使用 TdwsUnit 组件的Instances集合,您可以获得设计时支持,并对实例及其生命周期进行更多控制。
另请记住,即使脚本行为不当,您也必须确保您公开的实例能够正常运行,例如当用户尝试手动销毁您公开的实例时,并且该实例不应被销毁。默认情况下,ExposeRTTI 将映射析构函数,因此您可能希望通过指定 eoNoFreeOnCleanup 来限制它。
编辑:最近添加的最后一种方法是使用 TdwsRttiConnector,它基本上允许公开和连接到通过 RTTI 可访问的任何内容。就设置代码而言,这是非常轻量级的,但缺点是您无法获得任何形式的编译时检查。
ExposeInstanceToUnit has to be used from within the TdwsUnit table initialization, see RTTIExposeTests/ExposeInstancesAfterInitTable for some sample code. It allows directly exposing dynamic instances.
The other approach is to use the Instances collection of a TdwsUnit component, you get design-time support, and more controls over your instances and their lifetime.
Also keep in mind you have to make sure the instances you expose will properly behave even if the script misbehaves, f.i. when the user attempts to manually destroys an instance you exposed, and that instance shouldn't be destroyed. By default ExposeRTTI will map the destructors, so you may want to restrict that by specifying eoNoFreeOnCleanup.
edit: a last approach recently added is to use the TdwsRttiConnector, which basically allows exposing and connection to anything that's reachable through RTTI. That's very lightweight in terms of code to setup, but the downside is you don't get any form of compile-time checks.