I've declared a simple record type in a DWScript unit:
TSampleRecord = record
name: string;
end;
How can I expose such an array from the Delphi application to the script ? For example, the following method in the Delphi application:
// Delphi side
function GetSampleRecordArray(): array of TSampleRecord;
Must be accessible from a script:
// Script side
var myArray: array of TSampleRecord;
myArray := GetSampleRecordArray();
Before registering a function in script that returns dynamic array of records you need to:
TdwsUnithas helper methodExposeRTTIDynamicArrayto expose dynamic arrays for scripting. The method is introduced by helper classTdwsRTTIExposerin unitdwsRTTIExposer. Unfortunately this works only with dynamic arrays of some basic types and not records or objects. Here's a simple class that helps you registering a record type and a dynamic array for the lifetime ofTdwsUnitinstance:The class also provides conveniece method
SetInfofor initializingIInfoinstance (parameter, variable, result variable, ...) from a dynamic array.Now you can define specialized exposer for your
TSampleRecordand register functionGetSampleRecordArraywithin the DWS unit:Finally you register the Delphi function by instantiating
TArrayOfSampleRecordExposer:This should produce the output (
DwsProgramExecution.Result.ToString):