delphi DWScript:对象的属性getter/setter

yacmzcpb  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(145)

tdwsUnit声明了一个TObject类型的属性,以及一个getter和setter。一个示例化的对象应该存储在 Delphi 端的“对象存储”中。目标是将对象从脚本A传输到脚本B,其中脚本A在运行时在 Delphi 端创建并存储对象,脚本B在运行时(相同的 Delphi 进程)将从 Delphi 端读取对象并使用它。

Var ObjectStore : TObject;

procedure TDWSLibTools.TApp_GetObject(Info: TProgramInfo; ExtObject: TObject);
begin
 // How to return object in ObjectStore?
 <ResultOfThisGetter> := ObjectStore;
end;

procedure TDWSLibTools.TApp_SetObject(Info: TProgramInfo; ExtObject: TObject);
begin
 // How to access the object in parameter named "Value"?
 ObjectStore := Info.Vars['Value'].Get?????
end;

并且:即使操作正确,对象在脚本运行之间会“存活”吗?

jk9hmnmh

jk9hmnmh1#

您可以将任何 Delphi 对象示例公开给任何脚本,因此在一个脚本中创建 Delphi 对象并从另一个脚本访问该对象应该不会有问题。
这听起来像你的对象最初是从脚本创建的。我假设这个对象是在 Delphi 端声明的,然后类类型以某种方式暴露给脚本-例如。通过TdwsUnit组件。
例如,在 Delphi 端,声明了一个TDelphiClass,并有一个对应的脚本类TScriptClass。我进一步假设你已经声明了一个TApp脚本类(例如:在TdwsUnit中)使用Object属性,因此脚本端API看起来像这样:

type
  TApp = class
  protected
    function GetObject: TScriptClass;
    procedure SetObject(Value: TScriptClass);
  public
    property Object: TScriptClass read GetObject write SetObject;
  end;

现在,据我所知,你只是在问如何在 Delphi 端实现GetObjectSetObjectTdwsUnit事件处理程序。在下面的例子中,我假设你的ObjectStore变量的类型是TDelphiClass(为了在引用它的地方更明显):

var
  ObjectStore: TDelphiClass;

// TScriptClass.Create
procedure TDataModuleScript.dwsUnitStuffClassesTScriptClassConstructorsCreateEval(
  Info: TProgramInfo; var ExtObject: TObject);
begin
  if (ExtObject = nil) then
    ExtObject := TDelphiClass.Create;
end;

// TApp.GetObject: TScriptObject
procedure TDataModuleScript.dwsUnitStuffClassesTAppMethodsGetObjectEval(
  Info: TProgramInfo; ExtObject: TObject);
var
  ClassSym: TClassSymbol;
  ScriptObj: IScriptObj;
begin
  // Get the script side class type
  ClassSym := TClassSymbol(Info.FindSymbolInUnits('TScriptClass'));

  if (ClassSym <> nil) then
  begin
    // We're not using the TApp Delphi side object here, but if 
    // there is one then ExtObject would point to it.

    // Create a script side object to represent the Delphi side object
    ScriptObj := TScriptObjInstance.Create(ClassSym, Info.Execution);

    // Store a reference to the Delphi side object inside the 
    // script side object
    ScriptObj.ExternalObject := ObjectStore;

    // Return the script side object
    Info.ResultAsVariant := ScriptObj;
  end;
end;

// TApp.SetObject(Value: TScriptObject)
procedure TDataModuleScript.dwsUnitStuffClassesTAppMethodsSetObjectEval(
  Info: TProgramInfo; ExtObject: TObject);
var
  ParamInfo: IInfo;
  ExtObj: TDelphiClass;
begin
  // Get the parameter value
  ParamInfo := Info.Vars['Value'];
  if (ParamInfo.ValueIsEmpty) then
    // parameter is nil
    ExtObj := nil 
  else
    // Parameter is object.
    // Get the Delphi side reference and case to correct 
    // type (with type check)
    ExtObj := ParamInfo.ExternalObject as TDelphiClass;

  // Stash the reference to the Delphi side object
  ObjectStore := TDelphiClass(ExtObj);
end;

相关问题