如何在DWScript中公开记录的动态数组?

时间:2019-06-17 13:30:48

标签: delphi dwscript

我已经在DWScript单元中声明了一种简单的记录类型:

TSampleRecord = record
  name: string;
end;

如何将这样的数组从Delphi应用程序公开给脚本? 例如,Delphi应用程序中的以下方法:

// Delphi side
function GetSampleRecordArray(): array of TSampleRecord;

必须可以通过脚本访问:

// Script side
var myArray: array of TSampleRecord;
myArray := GetSampleRecordArray();

1 个答案:

答案 0 :(得分: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实例(参数,变量,结果变量...)。

现在,您可以为TSampleRecord定义专用的暴露对象,并在DWS单元中注册函数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;

最后,您通过实例化TArrayOfSampleRecordExposer来注册Delphi函数:

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