Call dwscript procedure with class as param from delphi

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

Example:

DWScript Side:

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

Delphi side:

 var Obj: TObject; Exec.Invoke('Proc1', [obj]); 
+4
source share
1 answer

You need to wrap the object on the Delphi side in a Script -side object and create it if necessary.

For exposure, you can use TdwsUnit and manually set it (which allows you to protect the Delphi class from a script of incorrect manipulations) or use the RTTI effect (but in this case, errors on the side of the script will have the ability to directly affect Delphi and, thus, may cause a crash in the host, so use RTTI only if you know that your classes are already safe or you don’t like sandboxing).

A simpler approach if all you need to do is simply expose your use of the RTTI environment (see TRTTIExposeTests.EnvironmentTest in URTTIExposeTests), but keep in mind the above notes about RTTI and security / sandboxing.

A 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 the comparison of Script -side objects to work as usual), as well as cleaning up (that is, what happens if Delphi- if the script object is still running, or who should be responsible for freeing the object on the Delphi side when releasing the Script object), as well as another subtlety (which will become easier if / if Delphi gets some form of automatic memory management).

+1
source

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


All Articles