18

I exposed a Delphi class to the scripts using TdwsUnit.ExposeRTTI method. It works very well with basic data types.

However it doesn't work when I add a TNotifyEvent. My Delphi class has an OnChange : TNotifyEvent property and when assigning this OnChage in a script, I get an error

"Syntax Error: More arguments expected!"

It looks like the script compiler understand I want to call the event Handler instead of assigning the OnChange property. Add an "@" sign doesn't help.

How can my Delphi object have an event Handler having his handler code in a script ?

Regards.

Zissouu
  • 924
  • 2
  • 10
  • 17
fpiette
  • 11,983
  • 1
  • 24
  • 46
  • 1
    I don't think this is possible in a "clean" way, mainly because "native" object will call "OnChange" by passing itself as Sender without thinking that the "target" is in the code side, now, in order to achieve this, you need to call the "OnChange" event in dws using special calls, you may be able to do this with global variables and the like, but it will be a lot of overhead, at least that's how I see it... –  May 13 '13 at 17:10
  • Does this link help, [`Delphi Web Script (DWScript) link a script method to an external control event`](http://stackoverflow.com/a/12692140/576719)? – LU RD May 13 '13 at 17:16
  • Try skipping using RTTI to expose your class and use a TdwsUnit to manually add your class definition at design time being careful to add your event procedure to the class definition together with its Sender parameter. Does this behave the same way? – Brian Frost May 13 '13 at 21:16
  • @ComputerSaysNo: I don't really care about the sender argument. – fpiette May 14 '13 at 06:21
  • @LURD: I already saw that before asking, but I don't think I can do with that. – fpiette May 14 '13 at 06:23
  • @BrianFrost: RTTI is not the issue. Exposing the object using the RTTI helper, or thru the object inspector or by use adding items to the collections is finally all the same. The issue is in the script side where I cannot assign a "procedure of object" to an object field. – fpiette May 14 '13 at 06:26
  • 2
    RTTI exposer & TdwsUnit don't support functions pointers/delegates types, you have to declare those in script code. If I understood correctly what you're after, you need to implement a an even stub on the Delphi side that will call into the script, and assign it to your event. Since TMethod (and events) don't have any form of automatic memory management, this will involve some case-specific code to handle cleaning up the event and making sure the script program & execution aren't released early. – Eric Grange May 14 '13 at 08:34
  • I worked around the problem my using Windows messages. My script is now using a message pump and the exposed instances post some custom messages to signal the script that an event has occured. Then the script call back Delphi code to get détails. Of course I had to implement GetMessage / PeekMessage / TranslateMessage and DispatchMessage. – fpiette May 27 '13 at 14:48

1 Answers1

1

I worked around the problem by using Windows messages. My script is now using a message pump and the exposed instances post some custom messages to signal the script that an event has occured. Then the script call back Delphi code to get details. Of course I had to implement GetMessage / PeekMessage / TranslateMessage and DispatchMessage.

fpiette
  • 11,983
  • 1
  • 24
  • 46