1

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();
4

1 回答 1

2

在脚本中注册一个返回动态记录数组的函数之前,您需要:

  1. 登记记录类型
  2. 注册该类型的动态数组

TdwsUnit具有帮助方法ExposeRTTIDynamicArray来公开动态数组以进行脚本编写。TdwsRTTIExposer该方法由单元中的辅助类引入dwsRTTIExposer。不幸的是,这只适用于某些基本类型的动态数组,而不适用于记录或对象。TdwsUnit这是一个简单的类,可帮助您在实例的生命周期内注册记录类型和动态数组:

uses
  System.SysUtils, System.Classes, System.Rtti, dwsComp, dwsExprs, dwsInfo,
  dwsErrors, dwsRTTIExposer;

type
  TDwsDynamicArrayExposer<T: record> = class(TComponent)
  strict private
    FRttiType: TRttiType;
    FDwsSymbol: TdwsSymbol;
    FDwsArray: TdwsArray;
    function GetDwsUnit: TdwsUnit;
  strict protected
    class var RTTIContext: TRttiContext;
    property DwsUnit: TdwsUnit read GetDwsUnit;
    property RttiType: TRttiType read FRttiType;
    property DwsSymbol: TdwsSymbol read FDwsSymbol;
    property DwsArray: TdwsArray read FDwsArray;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure SetInfo(const Info: IInfo; const Values: TArray<T>);
  end;

constructor TdwsDynamicArrayExposer<T>.Create(AOwner: TComponent);
begin
  if not (AOwner is TdwsUnit) then
    raise EArgumentException.Create('Owner must be instance of TdwsUnit.');
  inherited;
  FRttiType := RTTIContext.GetType(TypeInfo(T));
  FDwsSymbol := DwsUnit.ExposeRTTI(FRttiType.Handle);
  FDwsArray := DwsUnit.Arrays.Add;
  FDwsArray.DataType := FDwsSymbol.Name;
  FDwsArray.Name := FDwsSymbol.Name + 'Array';
  FDwsArray.IsDynamic := True;
end;

destructor TdwsDynamicArrayExposer<T>.Destroy;
begin
  if Assigned(DwsUnit) and (not (csDestroying in DwsUnit.ComponentState)) then
  begin
    // in case something went wrong in constructor
    FDwsArray.Free;
    FDwsSymbol.Free;
  end;
  inherited;
end;

function TdwsDynamicArrayExposer<T>.GetDwsUnit: TdwsUnit;
begin
  Result := TdwsUnit(Owner);
end;

procedure TdwsDynamicArrayExposer<T>.SetInfo(const Info: IInfo; const Values: TArray<T>);
var
  Index: Integer;
begin
  Info.Member['Length'].ValueAsInteger := Length(Values);
  for Index := 0 to Length(Values) - 1 do
    TdwsRTTIInvoker.AssignRecordFromValue(Info.Element([Index]),
      TValue.From<T>(Values[Index]), RttiType);
end;

该类还提供了从动态数组SetInfo初始化IInfo实例(参数、变量、结果变量...)的便捷方法。

现在您可以在 DWS 单元中为您的TSampleRecord和注册函数定义专门的暴露器:GetSampleRecordArray

type
  TSampleRecord = record
    Name: string;
  end;

  TArrayOfSampleRecordExposer = class(TdwsDynamicArrayExposer<TSampleRecord>)
  strict private
    FGetSampleRecordArrayFunction: TdwsFunction;
    procedure OnGetSampleRecordArrayEval(Info: TProgramInfo);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

function GetSampleRecordArray: TArray<TSampleRecord>;
begin
  SetLength(Result, 3);
  Result[0].Name := 'Name 0';
  Result[1].Name := 'Name 1';
  Result[2].Name := 'Name 2';
end;

constructor TArrayOfSampleRecordExposer.Create(AOwner: TComponent);
begin
  inherited;
  FGetSampleRecordArrayFunction := DwsUnit.Functions.Add;
  FGetSampleRecordArrayFunction.Name := 'GetSampleRecordArray';
  FGetSampleRecordArrayFunction.ResultType := DwsArray.Name;
  FGetSampleRecordArrayFunction.OnEval := OnGetSampleRecordArrayEval;
end;

destructor TArrayOfSampleRecordExposer.Destroy;
begin
  if Assigned(DwsUnit) and (not (csDestroying in DwsUnit.ComponentState)) then
    FGetSampleRecordArrayFunction.Free;
  inherited;
end;

procedure TArrayOfSampleRecordExposer.OnGetSampleRecordArrayEval(Info: TProgramInfo);
begin
  SetInfo(Info.ResultVars, GetSampleRecordArray);
end;

最后通过实例化注册 Delphi 函数TArrayOfSampleRecordExposer

Dws := TDelphiWebScript.Create(nil);
DwsUnit := TdwsUnit.Create(Dws);
DwsUnit.UnitName := 'Unit1';
DwsUnit.Script := Dws;
// one-time registration
TArrayOfSampleRecordExposer.Create(DwsUnit);

// ...

DwsProgram := Dws.Compile(
  'var SampleRecords := GetSampleRecordArray;'#13#10 +
  'for var SampleRecord in SampleRecords do'#13#10 +
  '  Println(SampleRecord.Name);');
if DwsProgram.Msgs.Count > 0 then
  raise Exception.Create(DwsProgram.Msgs.AsInfo);
DwsProgramExecution := DwsProgram.Execute;

这应该产生输出(DwsProgramExecution.Result.ToString):

名称 0
名称 1
名称 2

于 2019-06-27T11:04:32.267 回答