What is the best way to open a class instance in DWScript

I am compiling a built-in script function using the excellent Pascal DWScript . I also add my own Delphi class definition (TDemo) in DWScript using:

dwsUnit.ExposeRTTI( TDemo.ClassInfo ) 

It just works and is a great way to quickly add properties and methods.

I also want to add an existing instance in a similar way, so I created my FDemo instance of type TDemo and then executed:

  dwsUnit.ExposeInstanceToUnit( 'Demo', 'TDemo', FDemo ); 

This looks like a promising routine, but I get AV from an uninitialized unit table. I also looked at the unit test code of the SVN source to see the use of this function, but to no avail. Can someone tell me what should I add / change?

+6
source share
1 answer

ExposeInstanceToUnit should be used from the initialization of the TdwsUnit table, see RTTIExposeTests / ExposeInstancesAfterInitTable for some code example. It allows you to directly expose dynamic instances.

Another approach is to use a collection of instances of the TdwsUnit component, you get development-time support and more controls over your instances and their lifetime.

Also keep in mind that the instances that you expose will behave correctly, even if the script is misbehaves, fi when the user tries to manually destroy the instance that you exposed, and this instance should not be destroyed. By default, ExposeRTTI displays destructors, so you can limit this by specifying eoNoFreeOnCleanup.

edit: The last recently added approach is to use the TdwsRttiConnector, which basically allows you to expose and connect to anything accessible via RTTI. It is very easy in terms of customization code, but the disadvantage is that you do not get any compile time checking.

+4
source

Source: https://habr.com/ru/post/895843/


All Articles