gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:46:30 25 4
gpt4 key购买 nike

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

TSampleRecord = record
name: string;
end;


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

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


必须可从脚本访问:

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

最佳答案

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


注册记录类型
注册该类型的动态数组


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

关于delphi - 如何在DWScript中公开记录的动态数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com