4

How can I call from delphi a DWScript procedure which takes a class as param?

ex:

DWScript side:

procedure Proc1(AParam: TObject);
begin
  // do something useful
end;

Delphi side:

var
  Obj: TObject;


Exec.Invoke('Proc1', [obj]);
LU RD
  • 34,438
  • 5
  • 88
  • 296
theo pana
  • 41
  • 1

1 Answers1

1

You need to wrap your Delphi-side object into a Script-side object, and create one if needs be.

For exposure, you can use TdwsUnit and manually expose (which can allow to safeguard the Delphi-side class from script mis-manipulations), or use RTTI exposure (but in that case, bugs on the script side will be able to directly affect the Delphi, and thus could crash the host, so only use RTTI if you know your classes are already safe, or if you don't care about sand-boxing).

The simpler approach if all you need is a simple exposure is to use the RTTI Environment (cf. TRTTIExposeTests.EnvironmentTest in URTTIExposeTests), but keep in mind the above notes about RTTI and safety/sandboxing.

The more complex approach is to create a script-side object with code like

scriptObj := Info.Vars['TScriptSideClassName'].Method['Create'].Call([param1, param2]);

And then manually adjust its ExternalObject property.

You may also have to worry about creating the script-side object only once (if you want script-side object comparisons to work as usual), as well as cleanup (ie. what happens if the Delphi-side object gets freed while the script is still running, or who should be responsible for freeing the Delphi-side object when the script-side object is freed), as well as other subtlety (which will get simpler when/if Delphi gets some form of automated memory management).

Eric Grange
  • 5,931
  • 1
  • 39
  • 61